简体   繁体   中英

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

I would like to substitute a variable(A) value into a 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 with K_P.subs({"A":A}) but the A value is not updated into the matrix. I am performing a task to compare the unknown symbolic cell value in (K_P) matrix with known cell value in matrix (K),to solve for unknown parameters listed in the sympy symbols list. If i found one value, say, A then the value should get updated to the K_P matrix in order for me to solve other unknown symbols. How can i do this in fast and efficient way?

To use a dictionary as the argument of the sympy subs method , the keys must be sympy symbols, not strings.

I made a few changes to your code to make it work:

  • solve returns a list containing a single element, so pull out that element by indexing the return value using [0] .
  • Don't redefine the variable A . Instead, assign the numerical value to a new variable, Aval .
  • Call the subs method with two arguments, A and Aval .

So the relevant lines now look like:

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

That last line could also be written using a dictionary with A as the key and Aval as the corresponding value:

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

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