使用scipy curve_fit通过两个数据点拟合指数函数 [英] Fitting exponential function through two data points with scipy curve_fit

查看:224
本文介绍了使用scipy curve_fit通过两个数据点拟合指数函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用常数 pw 拟合指数函数 y = x ** pw 数据点。 scipy curve_fit 函数应优化 adj1 adj2 。我已经尝试使用以下代码,但无法正常工作。曲线不经过数据点。我该如何解决?

  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]#幂pw的指数函数应适合的两个给定数据点
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 =原始数据)
plt.plot(xf,func(xf,* popt),'r-',label =拟合曲线)
plt.show()


解决方案

在这里。我认为对于lmfit,曲线拟合是scipy的一个很好的替代方法。 b import numpy as np

#创建要拟合的数据
xf = [0.5,0.85]#两个幂为pw的指数函数应适合的两个给定数据点
yf = [0.02,4]

#定义目标函数:返回要最小化的数组
def fcn2min(params,x,data):
pw = params ['pw'] .value
adj1 = params ['adj1']。value
adj2 = params ['adj2']。value

model = adj1 * np.power(x + adj2, pw)
返回模型-数据

pw = 2

#创建一组参数
params = Parameters()
params。 add('pw',value = pw,variable = False)
params.add('adj1',value = 1)
params.add('adj2',value = 1)


#合适,此处使用最小二乘模型
结果=最小化(fcn2min,params,args =(xf,yf))

#计算最终结果$ b final = yf +结果。剩余

#写入错误报告
report_fit(result.params)
adj1 = result.params ['adj1']
adj2 = result.params ['adj2' ]

#尝试绘制结果
x = np.linspace(0,1,100)
y = adj1 * np.power(x + adj2,pw)

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


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()

解决方案

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()

这篇关于使用scipy curve_fit通过两个数据点拟合指数函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆