Ask Your Question

# 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]

edit retag close merge delete

## 1 answer

Sort by ยป oldest newest most voted

Here is a one line solution using Python list comprehension

matrix(3, [dVdC.lhs().coefficient(C) for dVdC in [dVdC4, dVdC5, dVdC6] for C in [C4,C5,C6]])


From which you obtain

[     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]

more

## Comments

Thank you very much! Python is on my to-do list; it seems to be very useful for Sage.

( 2019-06-09 09:07:59 -0500 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2019-06-09 06:06:40 -0500

Seen: 56 times

Last updated: Jun 09 '19