Exponential curve fitting in SciPy

drastega

I have two NumPy arrays x and y. When I try to fit my data using exponential function and curve_fit (SciPy) with this simple code

#!/usr/bin/env python
from pylab import *
from scipy.optimize import curve_fit

x = np.array([399.75, 989.25, 1578.75, 2168.25, 2757.75, 3347.25, 3936.75, 4526.25, 5115.75, 5705.25])
y = np.array([109,62,39,13,10,4,2,0,1,2])

def func(x, a, b, c, d):
    return a*np.exp(b-c*x)+d

popt, pcov = curve_fit(func, x, y)

I get wrong coefficients popt

[a,b,c,d] = [1., 1., 1., 24.19999988]

What is the problem?

Warren Weckesser

First comment: since a*exp(b - c*x) = (a*exp(b))*exp(-c*x) = A*exp(-c*x), a or b is redundant. I'll drop b and use:

def func(x, a, c, d):
    return a*np.exp(-c*x)+d

That isn't the main issue. The problem is simply that curve_fit fails to converge to a solution to this problem when you use the default initial guess (which is all 1s). Check pcov; you'll see that it is inf. This is not surprising, because if c is 1, most of the values of exp(-c*x) underflow to 0:

In [32]: np.exp(-x)
Out[32]: 
array([  2.45912644e-174,   0.00000000e+000,   0.00000000e+000,
         0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
         0.00000000e+000,   0.00000000e+000,   0.00000000e+000,
         0.00000000e+000])

This suggests that c should be small. A better initial guess is, say, p0 = (1, 1e-6, 1). Then I get:

In [36]: popt, pcov = curve_fit(func, x, y, p0=(1, 1e-6, 1))

In [37]: popt
Out[37]: array([  1.63561656e+02,   9.71142196e-04,  -1.16854450e+00])

This looks reasonable:

In [42]: xx = np.linspace(300, 6000, 1000)

In [43]: yy = func(xx, *popt)

In [44]: plot(x, y, 'ko')
Out[44]: [<matplotlib.lines.Line2D at 0x41c5ad0>]

In [45]: plot(xx, yy)
Out[45]: [<matplotlib.lines.Line2D at 0x41c5c10>]

fit plot

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exponential Fitting with Scipy.Optimise Curve_fit not working

From Dev

Curve Fitting to Exponential

From Dev

Scipy sigmoid curve fitting

From Dev

Fitting exponential function through two data points with scipy curve_fit

From Dev

Fitting an exponential modified gaussian curve to data with Python

From Dev

Fitting an exponential curve to numerical data in python

From Dev

MATLAB - Exponential Curve Fitting without Toolbox

From Dev

R fitting a double exponential growth curve

From Dev

python numpy/scipy curve fitting

From Dev

Curve fitting in Python using scipy

From Dev

scipy curve fitting negative value

From Dev

How to calculate the likelihood of curve-fitting in scipy?

From Dev

scipy splrep() with weights not fitting the given curve

From Dev

curve fitting optimisation scipy python numpy

From Dev

How to fit the exponential function using Matlab curve fitting tool?

From Dev

Create scipy curve fitting definitions for fourier series dynamically

From Dev

Correct fitting with scipy curve_fit including errors in x?

From Dev

Exponential fitting with R

From Dev

How do I tell how good my exponential curve fit is in SciPy?

From Dev

How do I tell how good my exponential curve fit is in SciPy?

From Dev

JS exponential curve fit

From Dev

R nls exponential curve

From Dev

Exponential curve fit matlab

From Dev

Fitted Exponential Curve Error

From Dev

Fitting a 2D Gaussian function using scipy.optimize.curve_fit - ValueError and minpack.error

From Dev

Simple curve fitting

From Dev

weighted curve fitting with lsqcurvefit

From Dev

Bayesian curve fitting model

From Dev

Iteratively fitting polynomial curve

Related Related

HotTag

Archive