1

我有一个函数: f(theta) = a+b*cos(theta - c) 以及采样数据。我想找到最小化均方误差的系数 a、b 和 c。知道在 python 中是否有一种有效的方法可以做到这一点?

编辑:

import numpy as np
from scipy.optimize import curve_fit

#definition of the function 
def myfunc(x, a, b, c):
    return a + b * np.cos(x - c)

#sample data
x_data = [0, 60, 120, 180, 240, 300]
y_data = [25, 40, 70, 30, 10, 15]

#the actual curve fitting procedure, a, b, c are stored in popt
popt, _pcov = curve_fit(myfunc, x_data, y_data)
print(popt)
print(np.degrees(popt[2]))

#the rest is just a graphic representation of the data points and the fitted curve
from matplotlib import pyplot as plt

#x_fit = np.linspace(-1, 6, 1000)
y_fit = myfunc(x_data, *popt)

plt.plot(x_data, y_data, "ro")
plt.plot(x_data, y_fit, "b")
plt.xlabel(r'$\theta$ (degrees)');
plt.ylabel(r'$f(\theta)$');

plt.legend()
plt.show()

这是一张显示曲线如何不真正适合点的图片。似乎幅度应该更高。当地的最小值和最大值似乎在正确的位置。

在此处输入图像描述

4

1 回答 1

1

scipy.optimize.curve_fit使得将数据点拟合到您的自定义函数变得非常容易:

import numpy as np
from scipy.optimize import curve_fit

#definition of the function 
def myfunc(x, a, b, c):
    return a + b * np.cos(x - c)

#sample data
x_data = np.arange(5)
y_data = 2.34 + 1.23 * np.cos(x_data + .23)

#the actual curve fitting procedure, a, b, c are stored in popt
popt, _pcov = curve_fit(myfunc, x_data, y_data)
print(popt)

#the rest is just a graphic representation of the data points and the fitted curve
from matplotlib import pyplot as plt

x_fit = np.linspace(-1, 6, 1000)
y_fit = myfunc(x_fit, *popt)

plt.plot(x_data, y_data, "ro", label = "data points")
plt.plot(x_fit, y_fit, "b", label = "fitted curve\na = {}\nb = {}\nc = {}".format(*popt))

plt.legend()
plt.show()

输出:

[ 2.34  1.23 -0.23]

编辑:

您的问题更新引入了几个问题。首先,您的 x 值以度为单位,而np.cos期望值以弧度为单位。因此,我们最好用 转换值np.deg2rad。反向功能将是np.rad2deg
其次,适合不同频率也是一个好主意,让我们为此引入一个附加参数。
第三,拟合通常对初始猜测非常敏感。您可以为此提供一个参数p0。 第四,您将拟合曲线的分辨率更改为数据点的低分辨率,因此它看起来如此欠采样。如果我们解决所有这些问题:scipy

import numpy as np
from scipy.optimize import curve_fit

#sample data
x_data = [0, 60, 120, 180, 240, 300]
y_data = [25, 40, 70, 30, 10, 15]

#definition of the function with additional frequency value d
def myfunc(x, a, b, c, d):
    return a + b * np.cos(d * np.deg2rad(x) - c)
#initial guess of parameters a, b, c, d
p_initial = [np.average(y_data), np.average(y_data), 0, 1]

#the actual curve fitting procedure, a, b, c, d are stored in popt
popt, _pcov = curve_fit(myfunc, x_data, y_data, p0 = p_initial)
print(popt)
#we have to convert the phase shift back into degrees
print(np.rad2deg(popt[2]))

#graphic representation of the data points and the fitted curve
from matplotlib import pyplot as plt
#define x_values for a smooth curve representation
x_fit = np.linspace(np.min(x_data), np.max(x_data), 1000)
y_fit = myfunc(x_fit, *popt)

plt.plot(x_data, y_data, "ro", label = "data")
plt.plot(x_fit, y_fit, "b", label = "fit")
plt.xlabel(r'$\theta$ (degrees)');
plt.ylabel(r'$f(\theta)$');

plt.legend()
plt.show()

我们得到这个输出:

[34.31293761 26.92479369  2.20852009  1.18144319]
126.53888003953764

在此处输入图像描述

于 2018-04-28T17:53:13.010 回答