curve_fit即使在正弦波上也失败 [英] curve_fit failing on even a sine wave

查看:99
本文介绍了curve_fit即使在正弦波上也失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试处理更复杂的问题之前,我尝试使用 curve_fit 拟合简单的正弦波(甚至没有任何噪声)作为测试。不幸的是,它甚至没有给出正确的答案。这是我的语法:

I'm trying to use curve_fit to fit a simple sine wave (not even with any noise) as a test before I move on to more complex problems. Unfortunately it's not giving even remotely the right answer. Here's my syntax:

x = linspace(0,100,300)
y = sin(1.759*x)
def mysine(x, a):
    return sin(a*x)

popt, pcov = curve_fit(mysine, x, y)
popt
array([ 0.98679056])

然后,如果我尝试初步猜测(例如1.5):

And then if I try an initial guess (say 1.5):

popt, pcov = curve_fit(mysine, x, y, p0=1.5)
popt
array([ 1.49153365])

...这仍然离正确答案不远。

... which is still nowhere near the right answer.

我想我很惊讶,考虑到该函数的采样效果如何,拟合度不能很好地工作。

I guess I'm surprised that, given how well the function is sampled, the fit doesn't work well.

推荐答案

如果您知道正尝试拟合的正弦波频率,则可以使用线性回归拟合正弦波。任何正弦波都可以用正弦和余弦函数的线性组合表示。您可以使用线性回归找到正弦和余弦的系数。这种方法的好处在于,无需进行初步猜测,只有一个满足回归公式的答案(例如,您不会得到错误的答案)。

If you know the frequency of the sine wave you are trying fit, you can use linear regression to fit the sine wave. Any sine wave can be represented by a linear combination of a sine and cosine function. You can find the coefficients for the sine and cosine using linear regression. The nice thing about this approach is that no initial guessing is required and there only there is only one answer that satisfies the regression formula (e.g. you will not get answers that are 'wrong').

http://exnumerus.blogspot.com/2010/04/how-to-fit-sine-wave-example-in-python.html 中有一个简短的示例代码教程

http://exnumerus.blogspot.com/2010/04/how-to-fit-sine-wave-example-in-python.html has a short tutorial with example code.

这篇关于curve_fit即使在正弦波上也失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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