-4

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!

Kyle
  • 1
  • 1
  • 1
  • 3
  • You could try the `numpy` package. – GreenSaber Sep 13 '17 at 13:33
  • 1
    ```Nothing good``` does not tell us anything. Besides that, sympy and scipy.optimize are as different approaches as it gets which makes we wonder what you really want and if you know what you want. – sascha Sep 13 '17 at 13:33
  • Depending on a, b, and c there may be no solution which might be why your methods cannot find a solution. – wrkyle Sep 13 '17 at 13:36
  • @wrkyle there is only one solution a=b and c=0 – Dadep Sep 13 '17 at 13:39
  • @sascha indeed I should make it clear. First time asking question here,sry for the reluctance – Kyle Sep 13 '17 at 16:44
  • @wrkyle yes it depends on the parameters, I figured this out just then. Thanks for your illustration,really helpful! – Kyle Sep 13 '17 at 16:46

1 Answers1

3

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.

sascha
  • 32,238
  • 6
  • 68
  • 110
  • Actually I want the numerical solution, so least-squaresolution should do, thanks. For some parameters in my data set the equation has no real root so I have to change my model LOL. Thanks again for your help and illustration! – Kyle Sep 13 '17 at 16:51