我有一些我知道可以很好地近似为三角函数的数据,我可以将其拟合scipy.optimize.curve_fit如下:
from __future__import division
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#Load the data
data = np.load('example_data.npy')
x = data[:,0]
y = data[:,1]
#Define the parametric trig function:
def trig_function(t,A,B,omega,offset):
return A*np.sin(omega*t) + B*np.cos(omega*t) + offset
#Define fitting procedure
def fit(x,y):
sigma = np.ones(len(t))
sigma[[0, -1]] = 1e-1 #weight the end points
func = trig_function #define the fitting function
#Some guesses for initial parameter values
dA = (y.max() + y.min()) / 2
y_shifted = y - offset
omega = np.pi * np.sum(y_shifted[:-1] * y_shifted[1:] < 0) / (t.max() - t.min())
p0 = (dA,dA, omega,dA)
#Do the fit
popt, pcov = curve_fit(func, x,y,p0=p0,sigma=sigma)
#return fitted data
return func(x, *popt)
#Define plotting environment
fig = plt.figure(figsize=(24,10))
ax1 = plt.subplot2grid((2,1), (0,0))
ax2 = plt.subplot2grid((2,1), (1,0),sharex=ax1)
#get fit data
y_approx = fit(x,y)
#Plot both solutions and relative error
ax1.plot(x,y)
ax1.plot(x,y_approx)
dy = (y_approx - y)/y
ax2.plot(x,dy)
然而,显然仍有一些特征没有被模型解释,特别是接近最小值的地方,如相对误差的峰值所证明的那样。
谁能建议如何进一步开发模型(即参数触发函数)以正确解释行为?
谢谢
