Updating variables with sympy
Asked Answered
Y

1

-1

I have a problem with sympy where it won't update my values. So I'm trying to do an iterative method for solving $\sigma_x$ and when I try to put numerical values into my expression it won't update.

I have imported sympy as sy
So I first have this code:

q,b,E,t,nu,L,z,x,y = sy.symbols("q,b,E,t,nu,L,z,x,y") 
D = (E*t**3)/(12*(1-nu**2)) 
q_0 = 4*q/(sy.pi*b) * sy.sin(sy.pi/2)*(1-sy.cos(sy.pi)) 
D2 = (sy.pi**2 / L**2) + (sy.pi**2 / b**2) 
w = q_0/(D* D2**2) * sy.sin(sy.pi*x/L) * sy.sin(sy.pi * y / b) 
M = 4 
N = 4 
w_iterert = 0 
for m in range(1,M+1):
     for n in range(N+1):
         q_iterert = 4*q/(sy.pi*b*m)*sy.sin(sy.pi*n/2)*(1-sy.cos(sy.pi*m))
         w_mn = q_iterert/(D*((sy.pi**2*m**2 / L**2) + (sy.pi**2 * n**2 / b**2))**2)
         w_iterert += w_mn*sy.sin(m*pi*x/L)*sy.sin(n*pi*y/b)

Then I plot the analytical expression:

w_iterert

And now I use formulas to find my sigma_x:

w_xx_iter = sy.diff(w_iterert,x,2) 
w_yy_iter = sy.diff(w_iterert,y,2) 
sigma_x_iter = - z*E/(1-nu**2)*(w_xx_iter+nu*w_yy_iter)

Here is where I get it wrong. now I do this:

E = 210000 
pi = sy.pi 
q = 20 
nu = 0.3 
L = 4000 
b = 1000  
t = 10 
x = 2 
z = t/2 
y = b/2 
sigma_x_iter

And I would expect this to update the values and give me the numerical value. Instead I just get the same analytical expression. How can I update my values?

I tried everything, I just wrote and tried to copy the text into another notebook which obviously worked. But then I can't change the M N values and get a different result unless I do it automatically.

Yahairayahata answered 23/3, 2022 at 13:9 Comment(2)
I think you need to study the sympy tutorial more. Specifically docs.sympy.org/latest/tutorial/gotchas.html#symbols. You created symbols and assigned them to Python variables. Reassigning those variables looses the connection between the variables and symbols. You have to use subs to assign values to the symbols.Kingbolt
If you try this with my problem you find out this wont work beacuse it treats the numerics as symbols and tries to derivate w.r.t 2000Yahairayahata
K
1

I had to edit your code several times to get something that I could copy-n-paste and run.

What I meant by subs is:

In [38]: sigma_x_iter.subs({E:210000, q:20,nu:0.3,L:4000,b:1000,t:10,x:2,z:10/2,
    ...:  y:1000/2})
Out[38]: 
        ⎛                      ⎛ π  ⎞                       ⎛3⋅π ⎞⎞           
        ⎜654766080000000000⋅sin⎜────⎟   9844326400000000⋅sin⎜────⎟⎟           
        ⎜                      ⎝2000⎠                       ⎝2000⎠⎟           
2.0e-10⋅⎜──────────────────────────── + ──────────────────────────⎟   9.6e-10⋅
        ⎝           243049                         2601           ⎠           
─────────────────────────────────────────────────────────────────── + ────────
                                  3                                           
                                 π                                            

⎛                    ⎛3⋅π ⎞                         ⎛ π  ⎞⎞
⎜1321369600000000⋅sin⎜────⎟   725790720000000000⋅sin⎜────⎟⎟
⎜                    ⎝2000⎠                         ⎝2000⎠⎟
⎜────────────────────────── + ────────────────────────────⎟
⎝           2601                         243049           ⎠
───────────────────────────────────────────────────────────
                          3                                
                         π                                 

In [39]: _38.n()
Out[39]: 0.361692509661739

In sympy you need to keep a clear distinction between a symbol, and the variable that references it. sympy works within the Python environment, without changing syntax. You may need to study the documented gotachas some more.

E as defined is a symbol:

In [42]: type(E)
Out[42]: sympy.core.symbol.Symbol

This assignment assigns an int to E, breaking any connection it had with the symbol. The symbol still exists in the various expressions, but you can no longer reference it with the variable E. This assignment does not touch any of the expressions.

In [43]: E = 210000

In [44]: type(E)
Out[44]: int
 
Kingbolt answered 23/3, 2022 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.