如何将函数与数据拟合以获取拟合参数? [英] How do I fit my function with data to get fit parameters?

查看:56
本文介绍了如何将函数与数据拟合以获取拟合参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据,其中x和y是函数中的已知参数,它们以x = x和y = x1的形式写在函数中,我需要拟合数据,以便可以获取未知参数(E,B0,S0).到目前为止,我已经有了这个,但是当我尝试运行它时,出现错误:

I have a set of data where x and y are the known parameters in my function, they are written in the function as x=x and y=x1, and I need to fit the data so I can get values for the unknown parameters (E, B0, S0). I have this so far but when I try to run this I get the error:

ValueError: x and y must have same first dimension, but have shapes (4L,) and (1L,)

当我尝试绘制拟合曲线时会发生此错误.我也收到了关于我设置的边界的错误:

This error happens when I try to plot the against the fit curve. Also I get this error in regards to the bounds I have setup:

 lb, ub = [np.asarray(b, dtype=float) for b in bounds]
ValueError: too many values to unpack

:

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

def func (x, x1, E, B0, S0):
    # function to optimize where x and x1 are known
    # E, B0, S0 need to be fitted
    return sum((x-np.power((E*B0*(1+((x1-S0)/(B0)))),(1/2)))**2)

#define the data to be fit
xdata = [0.00, 3.42, 4.56, 5.31] #distance
ydata = [335.4, 149.1, 167.1, 292.2] # beam size
plt.plot(xdata, ydata, 'b-', label='data')
pl.show()

# fit for parameters E, B0, and S0
popt, pcov = curve_fit(func, xdata, ydata)
plt.plot(xdata, func(xdata, *popt), 'r-', label='fit')

#put bounds on the optimization: 0.5<E<5, 1<S0<10, 0.1<B0,10
bnds= [(0.5,5.0),(0.1,10.0),(1,10)]
popt, pcov = curve_fit(func, xdata, ydata, bounds = [(0.5,5.0),(0.1,10.0),
(1.0,10.0)])
plt.plot(xdata,func(xdata, *popt),'g--', label='fit-with-bounds')
plt.xlabel('distance')
plt.ylabel('beam size')
plt.legend()
plt.show()

推荐答案

不清楚 func 函数中的 sum 应该做什么.您可以省略它以消除第一个错误.

It's not clear what the sum in the func function is supposed to do. You may leave it out to get rid of the first error.

第二,curve_fit方法的边界是独立变量的边界,而不是参数的边界.离开界限,您将摆脱第二个错误.

Second, the bounds in the curve_fit method are the bounds for the independent variable, not for the parameters. Leave the bounds out and you'll get rid of the second error.

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

def func (x, x1, E, B0, S0):
    # function to optimize where x and x1 are known
    # E, B0, S0 need to be fitted
    return (x-np.power((E*B0*(1.+((x1-S0)/(B0)))),(1/2.)))**2

#define the data to be fit
xdata = [0.00, 3.42, 4.56, 5.31] #distance
ydata = [335.4, 149.1, 167.1, 292.2] # beam size
plt.plot(xdata, ydata, 'b-', label='data')


# fit for parameters E, B0, and S0
popt, pcov = curve_fit(func, xdata, ydata)
plt.plot(xdata, func(xdata, *popt), 'r-', label='fit')

popt, pcov = curve_fit(func, xdata, ydata) 
plt.plot(xdata,func(xdata, *popt),'g--', label='fit-with-bounds')
plt.xlabel('distance')
plt.ylabel('beam size')
plt.legend()
plt.show()

现在显然fit"和fit-with-bounds"是一样的.

Now obviously "fit" and "fit-with-bounds" are the same.

<小时>仅适合 E,B0,S0 ,fit函数应仅将这些值用作参数.


To fit for E, B0, S0 only, the fit function should only take those values as arguments.

funcwithx1 = lambda x,x1, E, B0, S0: (x-np.power((E*B0*(1.+((x1-S0)/(B0)))),(1/2.)))**2
x1 = 4.6
func = lambda x, E, B0, S0: funcwithx1(x, x1, E, B0, S0)

这篇关于如何将函数与数据拟合以获取拟合参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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