问题描述
我正在尝试拟合以下x数据:[0.4,0.165,0.165,0.585,0.585],此y数据:[.45,.22,.63,.22,.63]和此z数据: [1,0.99,0.98,0.97,0.96]变成抛物面.我正在使用scipy的curve_fit工具.这是我的代码:
I am trying to fit this x data: [0.4,0.165,0.165,0.585,0.585], this y data: [.45, .22, .63, .22, .63], and this z data: [1, 0.99, 0.98,0.97,0.96] to a paraboloid. I am using scipy's curve_fit tool. Here is my code:
doex = [0.4,0.165,0.165,0.585,0.585]
doey = [.45, .22, .63, .22, .63]
doez = np.array([1, .99, .98,.97,.96])
def paraBolEqn(data,a,b,c,d):
if b < .16 or b > .58 or c < .22 or c >.63:
return 1e6
else:
return ((data[0,:]-b)**2/(a**2)+(data[1,:]-c)**2/(a**2))
data = np.vstack((doex,doey))
zdata = doez
opt.curve_fit(paraBolEqn,data,zdata)
我正在尝试将抛物面居中在.16和.58(x轴)之间以及.22和.63(y轴)之间.如果b或c不在此范围内,我将通过返回一个较大的值来实现此目的.
I am trying to center the paraboloid between .16 and .58 (x axis) and between .22 and .63 (y axis). I am doing this by returning a large value if b or c are outside of this range.
不幸的是,拟合度很差,我的popt值都为1,而我的pcov是inf.
Unfortunately the fit is wayyy off and my popt values are all 1, and my pcov is inf.
任何帮助都会很棒.
谢谢
推荐答案
您不必提供超出范围区域的高返回值,而需要提供一个很好的初始猜测.此外,该模式缺少偏移参数,并且抛物面符号错误.将模型更改为:
Rather than forcing high return values for out-of range regions you need to provide a good initial guess. In addition, the mode lacks an offset parameter and the paraboloid has the wrong sign. Change the model to:
def paraBolEqn(data,a,b,c,d):
x,y = data
return -(((x-b)/a)**2+((y-d)/c)**2)+1.0
我将偏移量固定为1.0,因为如果将其添加为拟合参数,系统将无法确定(数据点数比拟合参数少或相等).像这样的初步猜测呼叫curve_fit
:
I fixed the offset to 1.0 because if it were added as fit parameter the system would be underdetermined (fewer or equal number of data points than fit parameters).Call curve_fit
with an initial guess like this:
popt,pcov=opt.curve_fit(paraBolEqn,np.vstack((doex,doey)),doez,p0=[1.5,0.4,1.5,0.4])
这将产生:
[ 1.68293045 0.31074135 2.38822062 0.36205424]
与数据的很好的匹配:
这篇关于抛物面(3D抛物线)表面贴合python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!