How to solve an exponential equation in Python

10.9k views Asked by At

Now I have an equation to solve:

exp(x * a)-exp(x * b) = c, where a,b and c are known constants.

I tried sympy and scipy.optimize.fsolve, even brenth and newton. Nothing good. I 'm new to python, like 2 weeks. So pls help me out of this. Thanks!

1

There are 1 answers

1
sascha On BEST ANSWER

It's still unclear what you really want. Symbolic- vs. Numerical-optimization and exact solution vs. least-squares solution.

Ignoring this and just presenting the least-squares approach:

from scipy.optimize import minimize_scalar
import math

a = 3
b = 2
c = 1

def func(x):
    return (math.exp(x * a) - math.exp(x * b) - c)**2

res = minimize_scalar(func)
print(res.x)
print(res.fun)

Output:

0.382245085908
1.2143546318937163e-19

Alternative example:

a = 5
b = 2
c = -1

Output:

-0.305430244172
0.4546398791780655

That's just a demo in regards to scipy.optimize. This might not be what you want after all.