[英]Python sympy equations to matrix
I am trying to convert 4 equations into matrix form with the following, but the 4th row in the output is incorrect . 我试图将4个方程转换成矩阵形式,但输出中的第4行是不正确的 。 Any help would be appreciated: 任何帮助,将不胜感激:
from sympy import linear_eq_to_matrix, symbols, simplify, sin, cos, Eq, pprint
A, B, C, D, z, L, k = symbols('A, B, C, D, z, L, k')
fnc = A + B*z + C*sin(k*z) + D*cos(k*z)
bc1 = Eq(0, fnc.subs(z,0))
bc2 = Eq(0, fnc.subs(z,L))
bc3 = Eq(0, fnc.diff(z,2).subs(z,0))
bc4 = Eq(0, fnc.diff(z,2).subs(z,L))
a, b = linear_eq_to_matrix([bc1, bc2, bc3, bc4], [A, B, C, D])
pprint(bc1)
pprint(bc2)
pprint(bc3)
pprint(bc4)
pprint(a)
I get the following output: 我得到以下输出:
Expected output: 预期产量:
It seems that if you simply expand bc4
using the following line of code before converting the system to matrix form, you get the correct result: 看来,如果您在将系统转换为矩阵形式之前使用以下代码行扩展bc4
,则会得到正确的结果:
bc4 = sympy.expand(Eq(0, fnc.diff(z,2).subs(z,L)))
With rest of the code unchanged, this produces the following output: 如果其余代码保持不变,则会产生以下输出:
0 = A + D
0 = A + B⋅L + C⋅sin(L⋅k) + D⋅cos(L⋅k)
2
0 = -D⋅k
2 2
0 = - C⋅k ⋅sin(L⋅k) - D⋅k ⋅cos(L⋅k)
⎡-1 0 0 -1 ⎤
⎢ ⎥
⎢-1 -L -sin(L⋅k) -cos(L⋅k) ⎥
⎢ ⎥
⎢ 2 ⎥
⎢0 0 0 k ⎥
⎢ ⎥
⎢ 2 2 ⎥
⎣0 0 k ⋅sin(L⋅k) k ⋅cos(L⋅k)⎦
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.