How to substitute a variable value into a symbolic matrix in sympy python?

I would like to substitute the value of the variable (A) into the symbolic matrix (K_P). The full code is shown below.

import numpy
from sympy import symbols, Matrix,zeros,Transpose,solve

#Symbolic matrix K_P

Zc,Yc,Zg,Yg=symbols("Zc Yc Zg Yg",real=True)
A,Iz,Iy,J,kz,ky,E,G,L=symbols("A Iz Iy J kz ky E G L",real=True,positive=True)
E=10400000
G=3909800
L=5
def phi_z():
    phi_z=(12*E*Iy)/(kz*A*G*L**2)
    return phi_z
def phi_y():
    phi_y=(12*E*Iz)/(ky*A*G*L**2)
    return phi_y
K_P=zeros(6,6)
K1=Matrix(([E*A/L,0,0],[0,(12*E*Iz)/((1+phi_y())*L**3),0],[0,0,(12*E*Iy)/((1+phi_z())*L**3)]))
K2=Matrix(([G*J/L,0,0],[0,E*Iy/L,0],[0,0,E*Iz/L]))
Q1=Matrix(([0,Zg,-Yg],[-Zc,0,L/2],[Yc,-L/2,0]))
Q1_T=Transpose(Q1)
K11=K1; K12=K1*Q1; K22=Q1_T*K1*Q1+K2
K_P[0:3,0:3]=K11; K_P[0:3,3:6]=K12; K_P[3:6,3:6]=K22

#Converting Upper triangular stiffness matrix to Symmetric stiffness matrix           
for i in range(0,6):           
    for j in range(0,6):
        K_P[j,i]=K_P[i,j]
#Known K matrix using which all the unknown variables in K_P matrix need to be found out
K=numpy.matrix([[15704000, 0,0,0,0,2605293.6], [0, 321226.4,0,0,0,803066.2],[0, 0,321226.4,0,-803066.2,0],[0, 0,0,15482808,0,0],[0, 0,-803066.2,0,64407665.5,0],[2605293.6, 803066.2,0,0,0,64839883.7]])

#Solving for A
A=solve(K_P[0,0]-K[0,0],A); print("Value of A: ",A)
#Solving for Iz
Iz=solve(K_P[1,1]-K[1,1],Iz); print("Value of Iz: ",Iz)
Value of Iz:  [1817.83770032051*A*ky/(5650.0*A*ky - 2321.0)]

#A value found out is not updated into the matrix K_P. How to substitute A into matrix K_P?

K_P=K_P.subs({"A":A})
print("Value of Iz: ",Iz) 
Value of Iz:  [1817.83770032051*A*ky/(5650.0*A*ky - 2321.0)]#again A value is not assigned.

I tried to use K_P.subs ({"A": A}), but the value of A is not updated in the matrix. I perform the task of comparing the unknown value of the character cell in the (K_P) matrix with the known value of the cell in the matrix (K) to solve the unknown parameters listed in the character list of simplex characters. If I find one value, say A, then the value must be updated to the K_P matrix so that I can resolve other unknown characters. How can I do this in a quick and efficient way?

+4
source share
1 answer

sympy subs, , .

, :

  • solve , , , [0].
  • A. Aval.
  • subs A Aval.

, :

Aval = solve(K_P[0,0] - K[0,0], A)[0]
K_P = K_P.subs(A, Aval)

A Aval :

K_P = K_P.subs({A: Aval})
+4

Source: https://habr.com/ru/post/1691532/


All Articles