Franz Forstmayr
Franz Forstmayr

Reputation: 1300

Iterative calculation with SymPy, solving the same equation for different values of parameters

I want to use sympy for electronic design calculations. I have a equation, which I solve for certain resistor values. I can only use standard resistor values, so I set the choosen value again in the equation, and get a final result.

'''Example calculations for LMZ22010 switching regulator'''
from IPython import get_ipython
get_ipython().magic('reset -sf')

from sympy.solvers import solve
from sympy import Symbol, Eq, symbols, var

syms = ['Rent, Renb, Vuvlo']

var(','.join(syms))

Eq_uvlo = Eq(Rent/Renb, (Vuvlo/1.274) -1).subs({Rent:47e3, Vuvlo:8})
Renb = solve(Eq_uvlo, Renb)[0]
print(Renb)
>>> 8902.46

Now i want to try 9100 for Renb. But i cannot calculate the final Vuvlo value, because it's already substituted.

Vuvlo = solve(Eq_uvlo.subs({Renb:9.1}), Vuvlo)

Is there any better way, to make a calculation like this?

Upvotes: 1

Views: 779

Answers (1)

user6655984
user6655984

Reputation:

You had Renb as a symbol, but then assigned a value to it. This means you lost that symbol, you don't have a handle on it anymore. Use a different Python variable to hold any numerical value related to that symbol, like Renb_sol below.

Also, substitution of numerical values can be done later, after the solution is obtained. This allows you to use the same equation solving for different variables in it.

Eq_uvlo = Eq(Rent/Renb, (Vuvlo/1.274) -1)
Renb_sol = solve(Eq_uvlo, Renb)[0].subs({Rent: 47e3, Vuvlo: 8})
print(Renb_sol)
Vuvlo_sol = solve(Eq_uvlo, Vuvlo)[0].subs({Rent: 47e3, Renb: 9100})
print(Vuvlo_sol)

prints

8902.46803449301
7.85400000000000

Upvotes: 2

Related Questions