Home > Back-end >  how to use python to calculate resistance value bases on linear system?
how to use python to calculate resistance value bases on linear system?

Time:11-11

I have searched many guidance on how to compute my simular situation, I choose to use sympy, but I failed to get correct result.

Bellow is my formula system to calculate my resistances R3 and R4:

1/r3   1/r4 = 1/r_ref
Vneg * r3/(r3 r4) = Vbias

I further convert them to be:

r4 = r3 * r_ref / (r3 - r_ref)
r4 = (Vneg / Vbias) * r3 - r3

r_ref is a configurable known resistance Vneg, and Vbias is known voltages.

I searched many pages and result bellow code, but I couldn't generate right result

from sympy import *

Vbias = -717.39
Vneg = -5000
r_ref = 43

y1 = (Vneg/Vbias) * x - x
y2 = x*r_ref / (x - r)
solve((y1,y2))

I get result like:

{x: 0.0}

CodePudding user response:

Here I wrote your two initial equations. Note that it is always better to tell solve what to solve for.

from sympy import *

r3, r4 = symbols("r3, r4")
Vbias = -717.39
Vneg = -5000
r_ref = 43

eq1 = Eq(1/r3   1/r4, 1/r_ref)
eq2 = Eq(Vneg * r3/(r3 r4), Vbias)

sol = solve([eq1, eq2], [r3, r4], dict=True)[0]
print("r3 =", sol[r3])
# out: r3 = 50.2030303950161
print("r4 =", sol[r4])
# out: r4 = 299.697514601542

If you don't know in advance the numerical values, you can define symbols for Vbias, Vneg, r_ref:

r3, r4, r_ref, Vneg, Vbias = symbols("r3, r4, r_ref, V_neg, V_bias", real=True)
eq1 = Eq(1/r3   1/r4, 1/r_ref)
eq2 = Eq(Vneg * r3/(r3 r4), Vbias)
sol = solve([eq1, eq2], [r3, r4], dict=True)[0]
print("r3 =", sol[r3])
# out: r3 = -V_neg*r_ref/(V_bias - V_neg)
print("r4 =", sol[r4])
# out: r4 = V_neg*r_ref/V_bias

Then, when you you are ready to try numerical values:

d = {Vbias: -717.39, Vneg: -5000, r_ref: 43}
print("r3 =", sol[r3].subs(d))
# out: r3 = 50.2030303950161
print("r4=", sol[r4].subs(d))
# out: r4= 299.697514601542
  • Related