So im trying to add two matrixes together in sympy from a 2x2 matrix to a 3x3 matrix and im not really sure how to do it! What i want to achieve is this: Wanted Result
By adding together: K1 and K2. Beacuse they have different places in the total matrix. What i have done:
import sympy as sy
E,t = sy.symbols("E,t")
K1 = sy.Matrix([[17.5*E*t,-17.5*E*t],[-17.5*E*t,17.5*E*t]])
K2 = sy.Matrix([[12.5*E*t,-12.5*E*t],[-12.5*E*t,12.5*E*t]])
K_Total_Rad_1 = []
K_Total_Rad_2_1 = []
K_Total_Rad_2_2 = []
K_Total_Rad_3 = []
for i in range(len(K1)-2):
K_Total_Rad_1.append(K1[i])
for i in range(len(K2)-2):
K_Total_Rad_2_1.append(K1[i 2])
for i in range(len(K2)-2):
K_Total_Rad_2_2.append(K2[i])
for i in range(len(K2)-2):
K_Total_Rad_3.append(K2[i-2])
K_Total = sy.Matrix([K_Total_Rad_1, K_Total_Rad_2_2, K_Total_Rad_3])
K_Total
I Obviosly know that the error here is with defining the Matrix dimentions but i dont know how to do this here in python. Hope someone can help
CodePudding user response:
If I'm not mistaken, this is "standard" notation for FEM-like stiffness matrices.
This is how I would do it:
import sympy as sy
E, t = sy.symbols("E, t")
K1 = sy.Matrix([[17.5*E*t,-17.5*E*t],[-17.5*E*t,17.5*E*t]])
K2 = sy.Matrix([[12.5*E*t,-12.5*E*t],[-12.5*E*t,12.5*E*t]])
K = sy.zeros(3)
K[:2, :2] = K1
K[1:, 1:] = K2
K
CodePudding user response:
Maybe a bit less pythonic but more mathematical:
import sympy as sy
E, t = sy.symbols( "E, t" )
K1 = sy.Matrix(
[
[ 17.5 * E * t, -17.5 * E * t ],
[ -17.5 * E * t, 17.5 * E * t ]
]
)
K2 = sy.Matrix(
[
[ 12.5 * E * t, -12.5 * E * t ],
[ -12.5 * E * t, 12.5 * E * t ]
]
)
A = sy.diag( 1, sy.Matrix( [ 1, 0 ] ) )
B = sy.diag( sy.Matrix( [ 0, 1 ] ), 1 )
K = A * K1 * A.T B * K2 * B.T