Resolution of multiple equations (with exponential)

Viewed 72

I would like to automatise the resolution of this kind of equations :

a*exp(-b*90)=33

a*exp(-b*92)=66

I first tried this with one equation and it worked:

import sympy as sym
from sympy import solveset, S
from sympy.abc import x
from sympy import Symbol

a = symbols('a')
eq=sym.Eq(sym.exp(a*90),33)
solveset(eq,a,domain=S.Reals)

Then I tried this but it didn’t work :

a,b = symbols('a,b')
eq1 = sym.Eq(a*sym.exp(-b*90),33)
eq2 = sym.Eq(a*sym.exp(-b*92),66)
result=sym.solveset([eq1,eq2],(a,b),domain=S.Reals)

I get this error :

ValueError                                Traceback (most recent call last)
Input In [59], in <cell line: 9>()
      7 eq1 = sym.Eq(a*sym.exp(-b*90),33)
      8 eq2 = sym.Eq(a*sym.exp(-b*92),66)
----> 9 result=sym.solveset([eq1,eq2],(a,b),domain=S.Reals)

File C:\ProgramData\Anaconda\lib\site-packages\sympy\solvers\solveset.py:2178, in solveset(f, symbol, domain)
   2175     return S.EmptySet
   2177 if not isinstance(f, (Expr, Relational, Number)):
-> 2178     raise ValueError("%s is not a valid SymPy expression" % f)
   2180 if not isinstance(symbol, (Expr, Relational)) and  symbol is not None:
   2181     raise ValueError("%s is not a valid SymPy symbol" % (symbol,))

ValueError: [Eq(a*exp(-90*b), 33), Eq(a*exp(-92*b), 66)] is not a valid SymPy expression

Does anyone have an idea of how I could proceed? Maybe solveset is not the adapted function?

Also I tried :

import sympy as sym
from sympy import symbols, nonlinsolve

a, b = symbols('a, b', real=True)
eq1 = sym.Eq(a*sym.exp(-b*90),33)
eq2 = sym.Eq(a*sym.exp(-b*92),66)

nonlinsolve([eq1,eq2],[a,b])

I get

{(NaN, NaN),(3390(2+)35184372088832, {(2+)+log(2⎯⎯√2)|||||∈ℤ}),(3318035184372088832, {2+log(2⎯⎯√2)|||||∈ℤ})}

However I want real solutions and if I solve if by hand, i get a= 33*2**(-45) and b= -ln(2)/2. I don't understand why I don't get this solution.

Thanks for reading

1 Answers

It looks like nonlinsolve gives you all solutions and you are having difficulty extracting the real solutions. Someone else can suggestion how to handle the Set solutions. I would recommend converting your equation to linear form and solving those equations with solve:

>>> var('a:d')
>>> eq = Eq(a*exp(-b*c), d)
>>> leq = (log(eq.lhs) - log(eq.rhs)).expand(force=True)
>>> e1 = leq.subs([(c,90),(d,33)]);e2 = leq.subs([(c,92),(d,66)])
>>> solve((e1,e2),(a,b))
[(33/35184372088832, -log(2)/2)]
Related