简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM