Reputation: 35
I have 12 non-linear equations and 7 variables to solve in Python. I tried to run the code below, but it seems that it has a error, I'm not sure if it is because the system results in multiple solutions. Is there another way to proceed? I'd like to know the value of each variable and the result of each equation. Thanks.
from scipy.optimize import fsolve
from math import exp
def equations(vars):
a, b, c, d, e, f, g = vars
eq1=a*c*f-0.17142857
eq2=a*c*g-0.296922996
eq3=a*d*f-0.514285714
eq4=a*d*g-0.890768987
eq5=a*e*f-1.542857143
eq6=a*e*g-2.67230696
eq7=b*c*f-4.628571429
eq8=b*c*g-8.016920881
eq9=b*d*f-13.88571429
eq10=b*d*g-24.05076264
eq11=b*e*f-41.65714286
eq12=b*e*g-72.15228793
return [eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9, eq10, eq11, eq12]
a, b, c, d, e, f, g = fsolve(equations, (1, 1, 1, 1, 1, 1, 1))
print(a, b, c, d, e, f, g)
print(eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9, eq10, eq11, eq12)
Upvotes: 0
Views: 162
Reputation: 3472
I think this (changing to 12 variables and 12 equations) solves your error:
from scipy.optimize import fsolve
from math import exp
def equations(vars):
a, b, c, d, e, f, g, h, i, j, k, l = vars
eq1=a*c*f-0.17142857
eq2=a*c*g-0.296922996
eq3=a*d*f-0.514285714
eq4=a*d*g-0.890768987
eq5=a*e*f-1.542857143
eq6=a*e*g-2.67230696
eq7=b*c*f-4.628571429
eq8=b*c*g-8.016920881
eq9=b*d*f-13.88571429
eq10=b*d*g-24.05076264
eq11=b*e*f-41.65714286
eq12=b*e*g-72.15228793
return [eq1, eq2, eq3, eq4, eq5, eq6, eq7, eq8, eq9, eq10, eq11, eq12]
a, b, c, d, e, f, g, h, i, j, k, l = fsolve(equations, (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1))
print(a, b, c, d, e, f, g, h, i, j, k, l)
print(equations([a, b, c, d, e, f, g, h, i, j, k, l]))
Output
0.09788651653637173 2.64293594672212 -0.44472893008448783 -1.334186790203708 -4.002560370786989 -3.9379024240750766 -6.820647073325204 10473726.450237518 -9311686.344387261 -2218712.0864749462 2072840.8309073711 -2892329.2634591246
[1.4286000160623757e-09, -4.3506231950374286e-10, 2.6662094754215104e-10, -3.384060809352718e-10, -1.3234724427491074e-10, 1.0219736168437521e-10, -7.342570995660935e-12, -1.84297022087776e-11, -3.5398635134242795e-09, 2.0477912698879663e-09, 1.210743505453138e-09, -6.863842827442568e-10]
Just ignore the output for the last 5 variables
Upvotes: 1