python拟合曲线,用python拟合指数曲线

I am trying to convert some Matlab code I have for curve fitting my data into python code but am having trouble getting similar answers. The data is:

x = array([ 0. , 12.5 , 24.5 , 37.75, 54. , 70.25, 87.5 ,

108.5 , 129.5 , 150.5 , 171.5 , 193.75, 233.75, 273.75])

y = array([-8.79182857, -5.56347794, -5.45683824, -4.30737662, -1.4394612 ,

-1.58047016, -0.93225927, -0.6719836 , -0.45977157, -0.37622436,

-0.56115757, -0.3038559 , -0.26594558, -0.26496367])

The Matlab code is:

function [estimates, model] = curvefit(xdata, ydata)

% fits data to the curve y(x)=A-B*e(-lambda*x)

start_point = rand(1,3);

model =@efun;

options = optimset('Display','off','TolFun',1e-16,'TolX',1e-16);

estimates = fminsearch(model, start_point,options);

% expfun accepts curve parameters as inputs, and outputs sse,

% the sum of squares error for A -B* exp(-lambda * xdata) - ydata,

% and the FittedCurve.

function [sse,FittedCurve] = efun(v)

A=v(1);

B=v(2);

lambda=v(3);

FittedCurve =A - B*exp(-lambda*xdata);

ErrorVector=FittedCurve-ydata;

sse = sum(ErrorVector .^2);

end

end

err = Inf;

numattempts = 100;

for k=1:numattempts

[intermed,model]=curvefit(x, y));

[thiserr,thismodel]=model(intermed);

if thiserr

err = thiserr;

coeffs = intermed;

ymodel = thismodel;

end

and so far in Python I have:

import numpy as np

from pandas import Series, DataFrame

import pandas as pd

import matplotlib.pyplot as plt

from scipy import stats

from scipy.optimize import curve_fit

import pickle

def fitFunc(A, B, k, t):

return A - B*np.exp(-k*t)

init_vals = np.random.rand(1,3)

fitParams, fitCovariances = curve_fit(fitFunc, y, x], p0=init_vals)

I think I have to do something with running 100 attempts on the p0, but the curve only converges about 1/10 times and it converges to a straight line, way off from the value I get in Matlab. Also most questions regarding curve fitting that I have seen use Bnp.exp(-kt) + A, but the exponential formula I have above is the one I have to use for this data. Any thoughts? Thank you for your time!

解决方案

curve_fit(fitFunc, y, x], p0=init_vals) should be curve_fit(fitFunc, x,y, p0=init_vals) ie, x goes before y . fitFunc(A, B, k, t) should be fitFunc(t,A, B, k). The independent variable goes first. See the code below:

import numpy as np

import matplotlib.pyplot as plt

from scipy.optimize import curve_fit

x = np.array([ 0. , 12.5 , 24.5 , 37.75, 54. , 70.25, 87.5 ,

108.5 , 129.5 , 150.5 , 171.5 , 193.75, 233.75, 273.75])

y = np.array([-8.79182857, -5.56347794, -5.45683824, -4.30737662, -1.4394612 ,

-1.58047016, -0.93225927, -0.6719836 , -0.45977157, -0.37622436,

-0.56115757, -0.3038559 , -0.26594558, -0.26496367])

def fitFunc(t, A, B, k):

return A - B*np.exp(-k*t)

init_vals = np.random.rand(1,3)

fitParams, fitCovariances = curve_fit(fitFunc, x, y, p0=init_vals)

print fitParams

plt.plot(x,y)

plt.plot(x,fitFunc(x,*fitParams))

plt.show()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值