我目前正在编写一个Python脚本来执行四连杆机构的运动学分析
四杆机构可以数学上表示为矢量回路方程:
a\*e^(j\*theta1) + b\*e^(j\*theta2) + c\*e^(j\*theta3) + d\*e^(j\*theta4) = 0
其中:
a
是驱动连杆的长度,theta1
是驱动连杆的角位移b
是连杆的长度,theta2
是连杆的角位移c
是输出链接的长度,theta3
是输出链接的角位移d
是基本连杆的长度,theta4
是基本连杆的角位移这里,a
、b
、c
、d
和theta4
的值是已知的,而theta1
充当输入变量。因此,theta2
和theta3
是theta1
的函数
对于脚本的第一部分,我希望sympy执行以下操作:
我目前的代码如下:
from sympy import *
a, b, c, d, theta1, theta4 = symbols("a b c d theta1 theta4", real=True)
theta2 = Function("theta2")(theta1)
theta3 = Function("theta3")(theta1)
vector_loop = a*exp(I*theta1) + b*exp(I*theta2) + c*exp(I*theta3) + d*exp(I*theta4)
vector_loop_trig = vector_loop.rewrite(cos).expand()
vector_loop_real = re(vector_loop_trig)
vector_loop_im = im(vector_loop_trig)
我从代码中得到的输出是:
真实部分:
a⋅cos(θ₁) - b⋅cos(re(θ₂(θ₁)))⋅sinh(im(θ₂(θ₁))) + b⋅cos(re(θ₂(θ₁)))⋅cosh(im(θ₂(θ₁))) - c⋅cos(re(θ₃(θ₁)))⋅sinh(im(θ₃(θ₁))) + c⋅cos(re(θ₃(θ₁)))⋅cosh(im(θ₃(θ₁))) + d⋅cos(θ₄)
虚部:
a⋅sin(θ₁) - b⋅sin(re(θ₂(θ₁)))⋅sinh(im(θ₂(θ₁))) + b⋅sin(re(θ₂(θ₁)))⋅cosh(im(θ₂(θ₁))) - c⋅sin(re(θ₃(θ₁)))⋅sinh(im(θ₃(θ₁))) + c⋅sin(re(θ₃(θ₁)))⋅cosh(im(θ₃(θ₁))) + d⋅sin(θ₄)
但是,我应该得到的输出是:
真实部分:
a⋅cos(θ₁) + b⋅cos(θ₂) + c⋅cos(θ₃) + d⋅cos(θ₄)
虚部:
a⋅sin(θ₁) + b⋅sin(θ₂) + c⋅sin(θ₃) + d⋅sin(θ₄)
如何修复代码以获得正确的输出
您需要将θ2和θ3声明为真实值:
相关问题 更多 >
编程相关推荐