python多自变量拟合函数,在python中拟合变量Sinc函数

I would like to fit a sinc function to a bunch of datalines.

Using a gauss the fit itself does work but the data does not seem to be sufficiently gaussian, so I figured I could just switch to sinc..

I just tried to put together a short piece of self running code but realized, that I probably do not fully understand, how arrays are handled if handed over to a function, which could be part of the reason, why I get error messages calling my program

So my code currently looks as follows:

from numpy import exp

from scipy.optimize import curve_fit

from math import sin, pi

def gauss(x,*p):

print(p)

A, mu, sigma = p

return A*exp(-1*(x[:]-mu)*(x[:]-mu)/sigma/sigma)

def sincSquare_mod(x,*p):

A, mu, sigma = p

return A * (sin(pi*(x[:]-mu)*sigma) / (pi*(x[:]-mu)*sigma))**2

p0 = [1., 30., 5.]

xpos = range(100)

fitdata = gauss(xpos,p0)

p1, var_matrix = curve_fit(sincSquare_mod, xpos, fitdata, p0)

What I get is:

Traceback (most recent call last):

File "orthogonal_fit_test.py", line 18, in

fitdata = gauss(xpos,p0)

File "orthogonal_fit_test.py", line 7, in gauss

A, mu, sigma = p

ValueError: need more than 1 value to unpack

From my understanding p is not handed over correctly, which is odd, because it is in my actual code. I then get a similar message from the sincSquare function, when fitted, which could probably be the same type of error. I am fairly new to the star operator, so there might be a glitch hidden...

Anybody some ideas? :)

Thanks!

解决方案

You need to make three changes,

def gauss(x, A, mu, sigma):

return A*exp(-1*(x[:]-mu)*(x[:]-mu)/sigma/sigma)

def sincSquare_mod(x, A, mu, sigma):

x=np.array(x)

return A * (np.sin(pi*(x[:]-mu)*sigma) / (pi*(x[:]-mu)*sigma))**2

fitdata = gauss(xpos,*p0)

2, replace sin by the numpy version for array broadcasting

3, straight forward right? :P

Note, i think you are looking for p1, var_matrix = curve_fit(gauss,... rather than the one in the OP, which appears do not have a solution.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值