首页 > 解决方案 > Sympy——AttributeError:'tuple'对象没有属性'right'

问题描述

我正在尝试在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)是 type 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

标签: pythonsympy

解决方案


推荐阅读