Nickpick
Nickpick

Reputation: 6587

Fitting exponential function through two data points with scipy curve_fit

I want to fit an exponential function y=x ** pw with a constant pw to fit through two datapoints. The scipy curve_fit function should optimise adj1 and adj2. I have tried with the code below but couldn't get it to work. The curve does not go through the datapoints. How can I fix it?

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

def func(x, adj1,adj2):
    return np.round(((x+adj1) ** pw) * adj2, 2)

x = [0.5,0.85] # two given datapoints to which the exponential function with power pw should fit
y = [0.02,4]

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

xf=np.linspace(0,1,50)

plt.figure()
plt.plot(x, y, 'ko', label="Original Data")
plt.plot(xf, func(xf, *popt), 'r-', label="Fitted Curve")
plt.show()

Upvotes: 5

Views: 5275

Answers (3)

joshua
joshua

Reputation: 2519

It's just because the round method is killing curve_fit's ability to search the space. Small perturbations of p0 will always give an equal result, so it stops searching immediatly and will always return whatever you give it as a starting point (by default p0 = [1.,1.]). The solution is to simply remove np.round from your function.

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

def func(x, adj1,adj2):
    return ((x+adj1) ** pw) * adj2

x = [0.5,0.85] # two given datapoints to which the exponential function with power pw should fit
y = [0.02,4]

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

xf=np.linspace(0,1,50)

plt.figure()
plt.plot(x, y, 'ko', label="Original Data")
plt.plot(xf, func(xf, *popt), 'r-', label="Fitted Curve")
plt.show()

enter image description here

Upvotes: 3

xnx
xnx

Reputation: 25478

If you want to find the two parameters in your objective function from only two data points, this isn't necessarily a problem for least-squares fitting: just solve the simultaneous equations y1 = b(x1+a)^p and y2 = b(x2+a)^p for the parameters a and b:

import numpy as np
import matplotlib.pyplot as plt

def func(x, adj1,adj2):
    return ((x+adj1) ** pw) * adj2

x = [0.5,0.85] # two given datapoints to which the exponential function with power pw should fit
y = [0.02,4]

pw = 15
A = np.exp(np.log(y[0]/y[1])/pw)
a = (x[0] - x[1]*A)/(A-1)
b = y[0]/(x[0]+a)**pw

xf=np.linspace(0,1,50)
plt.figure()
plt.plot(x, y, 'ko', label="Original Data")
plt.plot(xf, func(xf, a, b), 'r-', label="Fitted Curve")
plt.show()

The resulting function will pass through both points exactly, of course.

enter image description here

Upvotes: 5

Nickpick
Nickpick

Reputation: 6587

Here the solution. I think for curve fitting lmfit is a good alternative to scipy.

from lmfit import minimize, Parameters, Parameter, report_fit
import numpy as np

# create data to be fitted
xf = [0.5,0.85] # two given datapoints to which the exponential function with power pw should fit
yf = [0.02,4]

# define objective function: returns the array to be minimized
def fcn2min(params, x, data):
    pw = params['pw'].value
    adj1 = params['adj1'].value
    adj2 = params['adj2'].value

    model = adj1 * np.power(x + adj2, pw)
    return model - data

pw=2

# create a set of Parameters
params = Parameters()
params.add('pw',   value= pw, vary=False)
params.add('adj1', value= 1)
params.add('adj2', value= 1)


# do fit, here with leastsq model
result = minimize(fcn2min, params, args=(xf, yf))

# calculate final result
final = yf + result.residual

# write error report
report_fit(result.params)
adj1=result.params['adj1']
adj2=result.params['adj2']

# try to plot results
x = np.linspace(0, 1, 100)
y = adj1 * np.power(x + adj2, pw)

import pylab
pylab.plot(xf, yf, 'ko')
pylab.plot(x, y, 'r')
pylab.show()

Upvotes: 1

Related Questions