Reputation: 319
I have the two following equations:
40 = Vmax*5.041667 + (Vmax**2/Amax)*sympy.exp((-Amax/Vmax)*5.041667) - (Vmax**2/Amax)
20 = Vmax*2.897628 + (Vmax**2/Amax)*sympy.exp((-Amax/Vmax)*2.897628) - (Vmax**2/Amax)
I wrote the following codes in sympy to solve this simultaneous equation to solve for Vmax and Amax
import sympy as sp
Vmax, Amax = symbols('Vmax, Amax')
eq1 = Vmax*5.041667 + (Vmax**2/Amax)*sympy.exp((-Amax/Vmax)*5.041667) - (Vmax**2/Amax)
eq2 = Vmax*2.897628 + (Vmax**2/Amax)*sympy.exp((-Amax/Vmax)*2.897628) - (Vmax**2/Amax)
print(nsolve((eq1, eq2), (Vmax, Amax), (40,20)))
This code gives the wrong answer. And the following code which I think is right takes too long to compute.
from sympy import *
Vmax, Amax = symbols('Vmax, Amax')
eq1 = Vmax*5.041667 + (Vmax**2/Amax)*sympy.exp((-Amax/Vmax)*5.041667) - (Vmax**2/Amax)
eq2 = Vmax*2.897628 + (Vmax**2/Amax)*sympy.exp((-Amax/Vmax)*2.897628) - (Vmax**2/Amax)
solve([eq1-40, eq2-20], (Vmax, Amax))
Any idea what I can do to fix my code so I can get the right answer?
Upvotes: 1
Views: 514
Reputation: 14565
You've misunderstood how nsolve
works. The third argument is an initial guess that is used to start the root-finding algorithm. Here [1, 1]
will work fine as the guess:
In [26]: from sympy import symbols, Eq, nsolve, exp
In [27]: Vmax, Amax = symbols('Vmax, Amax')
In [28]: eq1 = Eq(Vmax*5.041667 + (Vmax**2/Amax)*exp((-Amax/Vmax)*5.041667) - (Vmax**2/Amax), 40)
...: eq2 = Eq(Vmax*2.897628 + (Vmax**2/Amax)*exp((-Amax/Vmax)*2.897628) - (Vmax**2/Amax), 20)
In [29]: nsolve([eq1, eq2], [Amax, Vmax], [1, 1])
Out[29]:
⎡11.8641453843429⎤
⎢ ⎥
⎣9.41244210257784⎦
Note that this only finds one solution starting from any particular initial guess. It is possible that that a system like this has more than one solution.
Upvotes: 3