Julian
Julian

Reputation: 965

fsolve for function with exp

I'm trying to solve a nonlinear equation with Python and Scipy, heres the simple input:

from numpy import exp
from scipy.optimize import fsolve

def func(x):
    return 5*x*(2*x-1+exp(2*x))-5


x0 = fsolve(func,0)

print(x0)

However executing the function leads to RuntimeWarning: overflow encountered in exp message. Using Matlab and fzero with the same function works fine and returns 0.4385 for the root.

How can I solve this?

Upvotes: 0

Views: 375

Answers (1)

Aly Hosny
Aly Hosny

Reputation: 827

using 0 as a starting estimate causes some problems you can use any arbitary value and if you want to start from zero use something like 1e-6

from numpy import exp
from scipy.optimize import fsolve

def func(x):
    return 5*x*(2*x-1+exp(2*x))-5


x0 = fsolve(func,1e-6)

print(x0)

yields

[0.43848533]

Upvotes: 1

Related Questions