Python同情方程到矩阵

时间:2018-05-15 21:10:26

标签: python sympy

我试图将4个方程式转换为矩阵形式,但输出中的第4行不正确。任何帮助将不胜感激:

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)

我得到以下输出:

enter image description here

预期输出:

enter image description here

1 个答案:

答案 0 :(得分:2)

似乎如果您在将系统转换为矩阵形式之前使用以下代码行展开bc4,则会得到正确的结果:

bc4 = sympy.expand(Eq(0, fnc.diff(z,2).subs(z,L)))

如果其余代码保持不变,则会产生以下输出:

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)⎦