Tom Spargo
Tom Spargo

Reputation: 59

Simplifying equations in Sympy using inequalities

I am trying in Sympy to use a standard engineering method of simplifying an equation, where you know one variable is much larger or smaller than another. For instance, given the equation

C1*R2*s+C1*R2+R1+R2

and knowing that

R1 >> R2

the equation can be simplified to

C1*R2*s+C1*R2+R1.

What you'd normally do by hand is divide by R1, giving

C1*R2*s/R1+C1*R2/R1+1+R2/R1

then anyplace you see R2/R1 by itself you can set it to zero and then multiply by R1. I've not been able to figure how this would be done in Sympy. Obviously it's easy to do the division step, but I haven't been able to figure out how to do the search and replace step- just using subs gives you

R1

which isn't the right answer. factor, expand, collect, don't seem to get me anywhere.

Upvotes: 3

Views: 560

Answers (1)

Stelios
Stelios

Reputation: 5521

Using replace instead of subs works here.

C1, C2, R1, R2 = sp.symbols('C1, C2, R1, R2', real = True)
s = sp.symbols('s')

expr = C1*R2*s+C1*R2+R1+R2
print('original expression:', expr)

expr_approx = (R1 * ((expr/R1).expand().replace(R2/R1,0))).simplify()
print('approximate expression:', expr_approx)
original expression: C1*R2*s + C1*R2 + R1 + R2
approximate expression: C1*R2*s + C1*R2 + R1

Upvotes: 2

Related Questions