我怎样才能用symphy把方程的实部和虚部分开呢?

2024-10-01 07:51:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在编写一个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是基本连杆的角位移

这里,abcdtheta4的值是已知的,而theta1充当输入变量。因此,theta2theta3theta1的函数

对于脚本的第一部分,我希望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(θ₄)

如何修复代码以获得正确的输出


Tags: reloopsincos方程vectorim连杆