construct matrix from equations
I have derived 3 linear equations symbolically with variables C4, C5 and C6, and I would like to convert these into matrix-vector format [A][x]=[0]
. I have found another question on this forum that relates to this: Turning system of linear equations into a matrix, but I don't know how to apply that in this case.
E, Iz, A, l, kt, aL, dT, N0 ,C4, C5, C6 = var('E, Iz, A, l, kt, aL, dT, N0, C4, C5, C6')
w(x) = C4*(x^3 -l*x^2) + C5*(x^4 - l^2*x^2) + C6*(x^5 - l^3*x^2)
dw(x) = diff(w(x), x, 1); dw(x)
d2w(x) = diff(w(x), x, 2); d2w(x)
V1 = 1/2*E*Iz*((d2w(x))^2); V1
V2 = 1/2*N0*((dw(x))^2); V2
V = integral(V1, x, 0, l) + integral(V2, x, 0, l); show(V.simplify())
The equations to put in matrix-vector format:
dVdC4 = diff(V, C4).collect(C4).collect(C5).collect(C6) == 0; dVdC4
2/15*(N0*l^5 + 30*E*Iz*l^3)*C4 + 1/30*(7*N0*l^6 + 240*E*Iz*l^4)*C5 +
1/42*(13*N0*l^7 + 504*E*Iz*l^5)*C6 == 0
dVdC5 = diff(V, C5).collect(C4).collect(C5).collect(C6) == 0; dVdC5
1/30*(7*N0*l^6 + 240*E*Iz*l^4)*C4 + 4/105*(11*N0*l^7 + 441*E*Iz*l^5)*C5 +
1/30*(17*N0*l^8 + 780*E*Iz*l^6)*C6 == 0
dVdC6 = diff(V, C6).collect(C4).collect(C5).collect(C6) == 0; dVdC6
1/42*(13*N0*l^7 + 504*E*Iz*l^5)*C4 + 1/30*(17*N0*l^8 + 780*E*Iz*l^6)*C5 +
1/63*(49*N0*l^9 + 2592*E*Iz*l^7)*C6 == 0
Then I created the next matrix by hand; but I assume there is a smarter way to construct a matrix:
matrix1 = matrix([
[2/15*(N0*l^5 + 30*E*Iz*l^3), 1/30*(7*N0*l^6 + 240*E*Iz*l^4), 1/42*(13*N0*l^7 + 504*E*Iz*l^5)],
[1/30*(7*N0*l^6 + 240*E*Iz*l^4), 4/105*(11*N0*l^7 + 441*E*Iz*l^5), 1/30*(17*N0*l^8 + 780*E*Iz*l^6)],
[1/42*(13*N0*l^7 + 504*E*Iz*l^5), 1/30*(17*N0*l^8 + 780*E*Iz*l^6), 1/63*(49*N0*l^9 + 2592*E*Iz*l^7)]
])
matrix1
[ 2/15*N0*l^5 + 4*E*Iz*l^3 7/30*N0*l^6 + 8*E*Iz*l^4 13/42*N0*l^7 + 12*E*Iz*l^5]
[ 7/30*N0*l^6 + 8*E*Iz*l^4 44/105*N0*l^7 + 84/5*E*Iz*l^5 17/30*N0*l^8 + 26*E*Iz*l^6]
[ 13/42*N0*l^7 + 12*E*Iz*l^5 17/30*N0*l^8 + 26*E*Iz*l^6 7/9*N0*l^9 + 288/7*E*Iz*l^7]