如何拟合余弦函数? [英] How can I fit a cosine function?
问题描述
我写了一个python函数来获取以下余弦函数的参数:
I wrote a python function to get the parameters of the following cosine function:
param = Parameters()
param.add( 'amp', value = amp_guess, min = 0.1 * amp_guess, max = amp_guess )
param.add( 'off', value = off_guess, min = -10, max = 10 )
param.add( 'shift', value = shift_guess[0], min = 0, max = 2 * np.pi, )
fit_values = minimize( self.residual, param, args = ( azi_unique, los_unique ) )
def residual( self, param, azi, data ):
"""
Parameters
----------
Returns
-------
"""
amp = param['amp'].value
off = param['off'].value
shift = param['shift'].value
model = off + amp * np.cos( azi - shift )
return model - data
在Matlab中如何获取余弦函数的幅度,偏移和位移?
In Matlab how can get the amplitude, offset and shift of the cosine function?
推荐答案
MATLAB在优化工具箱中具有一个名为lsqcurvefit
的函数:
MATLAB has a function called lsqcurvefit
in the optimisation toolbox:
lsqcurvefit(fun,X0,xdata,ydata,lbound,ubound);
其中,fun
是要拟合的函数,x0
是初始参数猜测值,xdata和ydata是不言自明的,lbound和ubound是参数的上下限.因此,例如,您可能具有以下功能:
where fun
is the function to fit, x0
is the initial parameter guess, xdata and ydata are self-explanatory, and lbound and ubound are the lower and upper bounds to the parameters. So, for instance, you might have a function:
% x(1) = amp
% x(2) = shift
% x(3) = offset
% note cosd instead of cos, because your data appears to be in degrees
cosfit = @(x,xdata) x(1) .* cosd(xdata - x(2)) + x(3);
然后您将调用lsqcurvefit函数,如下所示:
You would then call the lsqcurvefit function as follows:
guess = [7,150,0.5];
lbound = [-10,0,-10]
ubound = [10,360,10]
fit_values = lsqcurvefit(cosfit,guess,azi_unique,los_unique,lbound,ubound);
这篇关于如何拟合余弦函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!