我正在尝试在sympy
中建立一个简单的摆模型。
有人知道我为什么会收到此错误吗?
教程在这里:
http://for.rest/articles/The-Spring-Pendulum-with-Sympy/
这是代码。请注意,由于本教程选择了非常不直观的坐标系(+ y指向下方),因此我对代码进行了一些修改。
import sympy
from sympy import symbols, init_printing
import sympy.physics.mechanics as me
init_printing()
import matplotlib.pyplot as plt
import numpy as np
from pydy.system import System
from numpy import linspace
%matplotlib inline
# Create the variables
L, theta = me.dynamicsymbols('L theta')
# Create the velocities
L_dot, theta_dot = me.dynamicsymbols('L_dot theta_dot')
# Create the constants
m, g, t, L_0 = sympy.symbols('m g t L_0')
# Create the world frame
A = me.ReferenceFrame('A')
# Create the pendulum frame
B = A.orientnew('B', 'axis', [theta, A.z])
# Set the rotation of the pendulum frame
B.set_ang_vel(A, theta_dot * A.z)
# Create the Origin
O = me.Point('O')
# Create the mass point
P = O.locatenew('P', L * -B.y)
# Display the mass point location in the A frame
P.pos_from(O).express(A)
# Set origin velocity to zero
O.set_vel(A, 0)
# Create the velocity of the mass point
P.set_vel(B, L_dot * -B.y)
P.v1pt_theory(O, A, B)
P.vel(A).express(A)
问题出现在该代码的最后一行。注意,对象P.vel(A)
的类型为sympy.physics.vector.vector.Vector
,其类型为express
。
这是错误
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
691 type_pprinters=self.type_printers,
692 deferred_pprinters=self.deferred_printers)
--> 693 printer.pretty(obj)
694 printer.flush()
695 return stream.getvalue()
C:\ProgramData\Anaconda3\lib\site-packages\IPython\lib\pretty.py in pretty(self, obj)
363 if cls in self.type_pprinters:
364 # printer registered in self.type_pprinters
--> 365 return self.type_pprinters[cls](obj, self, cycle)
366 else:
367 # deferred printer
C:\ProgramData\Anaconda3\lib\site-packages\sympy\interactive\printing.py in _print_plain(arg, p, cycle)
66 """caller for pretty, for use in IPython 0.11"""
67 if _can_print_latex(arg):
---> 68 p.text(stringify_func(arg))
69 else:
70 p.text(IPython.lib.pretty.pretty(arg))
C:\ProgramData\Anaconda3\lib\site-packages\sympy\printing\pretty\pretty.py in pretty(expr, **settings)
2162
2163 try:
-> 2164 return pp.doprint(expr)
2165 finally:
2166 pretty_use_unicode(uflag)
C:\ProgramData\Anaconda3\lib\site-packages\sympy\printing\pretty\pretty.py in doprint(self, expr)
60
61 def doprint(self, expr):
---> 62 return self._print(expr).render(**self._settings)
63
64 # empty op so _print(stringPict) returns the same
C:\ProgramData\Anaconda3\lib\site-packages\sympy\physics\vector\vector.py in render(self, *args, **kwargs)
283 pform = vp._print(
284 ar[i][0][j])
--> 285 pform = prettyForm(*pform.right(" ",
286 ar[i][1].pretty_vecs[j]))
287 else:
AttributeError: 'tuple' object has no attribute 'right'
这造成了一个单独的问题,因为如果不表达这一点,我以后将无法运行计算。
pendulum = me.Particle('pend', P, m)
gravity = m * g * A.y
forces = gravity
kane = me.KanesMethod(A,
q_ind=[L, theta],
u_ind=[L_dot, theta_dot],
kd_eqs=[L_dot - L.diff(t),
theta_dot - theta.diff(t)])
哪个会产生此错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-222-aeb1b3bb40e8> in <module>()
----> 1 fr, frstar = kane.kanes_equations([(P, forces)], [pendulum])
C:\ProgramData\Anaconda3\lib\site-packages\sympy\physics\mechanics\kane.py in kanes_equations(self, bodies, loads)
537 'kinematic differential equations to use this method.')
538 fr = self._form_fr(loads)
--> 539 frstar = self._form_frstar(bodies)
540 if self._uaux:
541 if not self._udep:
C:\ProgramData\Anaconda3\lib\site-packages\sympy\physics\mechanics\kane.py in _form_frstar(self, bl)
332 v = [msubs(vel, self._qdot_u_map) for vel in vlist]
333 return partial_velocity(v, self.u, N)
--> 334 partials = [get_partial_velocity(body) for body in bl]
335
336 # Compute fr_star in two components:
C:\ProgramData\Anaconda3\lib\site-packages\sympy\physics\mechanics\kane.py in <listcomp>(.0)
332 v = [msubs(vel, self._qdot_u_map) for vel in vlist]
333 return partial_velocity(v, self.u, N)
--> 334 partials = [get_partial_velocity(body) for body in bl]
335
336 # Compute fr_star in two components:
C:\ProgramData\Anaconda3\lib\site-packages\sympy\physics\mechanics\kane.py in get_partial_velocity(body)
326 vlist = [body.masscenter.vel(N), body.frame.ang_vel_in(N)]
327 elif isinstance(body, Particle):
--> 328 vlist = [body.point.vel(N),]
329 else:
330 raise TypeError('The body list may only contain either '
C:\ProgramData\Anaconda3\lib\site-packages\sympy\physics\vector\point.py in vel(self, frame)
453 if not (frame in self._vel_dict):
454 raise ValueError('Velocity of point ' + self.name + ' has not been'
--> 455 ' defined in ReferenceFrame ' + frame.name)
456 return self._vel_dict[frame]
457
ValueError: Velocity of point P has not been defined in ReferenceFrame A