piojo
piojo

Reputation: 6723

Sympy: how to solve in terms of X

I'm simulating some voltages in sympy, and I've had problems getting the system to simplify the output. Can I ask the system not to include an unneeded variable in the output? I hoped to eliminate V. I ran:

from sympy import *
var('V1 V2 V Vout R1 R2 Rf')

solve([
    -V + V1*Rf/(Rf+R1),
    -V + (V2-Vout)*Rf/(Rf+R2),
], Vout)
# output: {Vout: -R2*V/Rf - V + V2}

Can I tell it to give me a result in terms of V1, V2, R1, R2, and Rf, but not V?

Note that manually removing V is not ideal, because not all equations are as simple as these. When I manually removed it (and decided to set R1=R2), I found a similar problem--

solve([
    Eq(V1*Rf/(Rf+R1), (V2-Vout)*Rf/(Rf+R2)),
    Eq(R1, R2)
], Vout)
# output: {Vout: (R1*V2 - R2*V1 - Rf*V1 + Rf*V2)/(R1 + Rf)}

Both R1 and R2 are included in the output. How can I make the system eliminate one? (Incidentally, eliminating R2 will cause R1 to cancel out.) The output should be:

{Vout: -V1 + V2}

(Note to the electronics aficionados: you may notice is a differential amplifier. I'm aware something is wrong with the initial equations. Figuring out Jupyter/sympy would really help me see what's wrong.)

Upvotes: 2

Views: 180

Answers (1)

user6655984
user6655984

Reputation:

If V is added to the variables to solve for, it will not appear on the right:

solve([
    -V + V1*Rf/(Rf+R1),
    -V + (V2-Vout)*Rf/(Rf+R2),
], (Vout, V))

returns {V: Rf*V1/(R1 + Rf), Vout: (-V1*(R2 + Rf) + V2*(R1 + Rf))/(R1 + Rf)}. Access the solution for Vout with solve(..., (Vout, V))[Vout]

Similarly,

solve([
    Eq(V1*Rf/(Rf+R1), (V2-Vout)*Rf/(Rf+R2)),
    Eq(R1, R2)
], (Vout, R2))[Vout]

returns -V1 + V2.

Upvotes: 1

Related Questions