STEM 隨筆︰古典力學︰向量【四上】

如果平鋪直敘,此時應該以  PyDy 的

Beginner’s Tutorial

Once you have SymPy installed on your machine it is time to explore the how it operates. The first step is to open up your python interpreter. This varies depending on your work style, but the simplest way is to type python in a command prompt:

………

 

,來認識『參考系』以及『向量運算』也。

之所以用 ipython3 ,假借 PyDy 概觀︰

Commands

What follows is a short rundown of the commands used in PyDy. All commands that you would type in an interactive session are preceded by ‘’>>> ‘’.

 

一行一行演練矣︰

SymPy Commands

In [1]: from sympy import *

This imports all of the classes and functions in the ‘’SymPy’’ package. Now we can do some symbolic math:

 

In [2]: x, y, z = symbols('x y z')

In [3]: e = x**2 + y**2 + z**2

In [4]: print(e)
x**2 + y**2 + z**2

In [5]: diff(e, x)
Out[5]: 2*x

This creates 3 symbols, x, y, and z, and illustrates how to form a SymPy expression and take its derivative.

 

Mechanics commands

What follows is a list of objects you can create, and functions you can run. The first step is to import the functions and classes related to mechanics:

In [6]: from sympy.physics.mechanics import *

Classes

Here is a list of classes:
  • ReferenceFrame
  • Point
  • Vector – not created directly
  • Dyadic – not created directly
  • Particle
  • RigidBody
  • KanesMethod

You can call ‘’help(class)’’ to see the help entry for ‘’class’‘. For example, ‘’help(ReferenceFrame)’’ to see the help entry for ‘’ReferenceFrame’‘.

Code Snippets

Here are some brief usage examples of common functions and classes.

In [7]: q1, q2 = dynamicsymbols('q1 q2')

Creates two time varying symbols, ‘’q1’’ and ‘’q2’‘

 

In [8]: N = ReferenceFrame('N')

This creates a new reference frame named N.

 

In [9]: N.x
Out[9]: N.x

Access to the ‘’x’’ basis vector in the ‘’N’’ reference frame

 

In [10]: a = N.x + N.y

Creates a new vector, ‘’a’‘, which is the sum of the ‘’x’’ and ‘’y’’ basis vectors in the ‘’N’’ reference frame.

 

In [11]: P = Point('P')

Creates a point named P.

 

In [12]: K = KanesMethod(N)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-358545fb2b9d> in <module>()
----> 1 K = KanesMethod(N)

TypeError: __init__() missing 2 required positional arguments: 'q_ind' and 'u_ind'

FIXME Creates a new ‘’KanesMethod’’ object, used to generate Fr+FrFr+Fr∗, with ‘’N’’ as the inertial reference frame.

 

In [13]: D = RigidBody('BodyD', masscenter=P, frame=N, mass=2, inertia=I)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-8cc508203a17> in <module>()
----> 1 D = RigidBody('BodyD', masscenter=P, frame=N, mass=2, inertia=I)

/usr/lib/python3/dist-packages/sympy/physics/mechanics/rigidbody.py in __init__(self, name, masscenter, frame, mass, inertia)
     57         self.mass = mass
     58         self.frame = frame
---> 59         self.inertia = inertia
     60         self.potential_energy = 0
     61 

/usr/lib/python3/dist-packages/sympy/physics/mechanics/rigidbody.py in inertia(self, I)
    151     @inertia.setter
    152     def inertia(self, I):
--> 153         if not isinstance(I[0], Dyadic):
    154             raise TypeError("RigidBody inertia must be a Dyadic object.")
    155         if not isinstance(I[1], Point):

TypeError: 'ImaginaryUnit' object does not support indexing

Creates a rigid body container and defines the center of mass location, the body fixed frame, the mass and the inertia.

 

In [14]: Par = Particle()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-0e3ee5456180> in <module>()
----> 1 Par = Particle()

TypeError: __init__() missing 3 required positional arguments: 'name', 'point', and 'mass'

Creates a new particle container.

Functions

Here is a list of functions:
  • mprint
  • inertia
  • mprint
  • mpprint
  • mlatex
  • cross
  • dot
  • outer
  • kinematic equations

On each of them, you can call ‘’help(function)’’ to see the help entry for ‘’function’‘. For example, ‘’help(inertia)’’ will describe what the ‘’inertia’’ function is and how to use it.

Code Snippets

In [15]: mechanics_printing()

Sets the default printing to use the mechanics printing.

 

In [16]: mprint(q1)
q1

Prints ‘’q1’’ using the mechanics printer; use if mechanics_printing is not on.

 

In [17]: I = inertia(N, 1, 2, 3)

Creates an inertia dyadic in the frame N with principle measure numbers of 1, 2, and 3.

 

In [18]: mprint(kinematic_equations([u1,u2,u3], [q1,q2,q3], 'body', '313'))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-18-9e27c73cfb8e> in <module>()
----> 1 mprint(kinematic_equations([u1,u2,u3], [q1,q2,q3], 'body', '313'))

NameError: name 'u1' is not defined

Prints out kinematic differential equations which relate the body fixed angular velocity measure numbers ‘’u1, u2, u3’’ to the time derivatives of the coordinates ‘’q1, q2, q3’‘, assuming a 313 (ZXZ) body fixed rotations.

 

希望讀者先知

‧ 軟件版本自有前後

‧ 文件時期恐或不同

‧ 派生環境各安其好

‧ ………

 

所謂學習乙事,實不宜囫圇吞棗乎◎