gpt4 book ai didi

matlab - 如何拟合余弦函数?

转载 作者:行者123 更新时间:2023-12-03 17:05:14 27 4
gpt4 key购买 nike

我写了一个python函数来获取以下余弦函数的参数: enter image description here

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中如何得到余弦函数的幅值、偏移量和偏移量?

最佳答案

我的经验告诉我,尽可能少地依赖工具箱是总是好的。对于您的特定情况,该模型很简单,手动操作非常简单。

假设您有以下模型:

y = B + A*cos(w*x + phi)

并且你的数据是等间距的,那么:

%// Create some bogus data

A = 8;
B = -4;
w = 0.2;
phi = 1.8;

x = 0 : 0.1 : 8.4*pi;
y = B + A*cos(w*x + phi) + 0.5*randn(size(x));

%// Find kick-ass initial estimates
L = length(y);
N = 2^nextpow2(L);

B0 = (max(y(:))+min(y(:)))/2;

Y = fft(y-B0, N)/L;
f = 5/(x(2)-x(1)) * linspace(0,1,N/2+1);

[A0,I] = max( 2*abs(Y(1:N/2+1)) );
w0 = f(I);
phi0 = 2*imag(Y(I));

%// Refine the fit
sol = fminsearch(@(t) sum( (y(:)-t(1)-t(2)*cos(t(3)*x(:)+t(4))).^2 ), [B0 A0 w0 phi0])

结果:

sol = %// B was -4      A was 8       w was 0.2     phi was 1.8                
-4.0097e+000 7.9913e+000 1.9998e-001 1.7961e+000

关于matlab - 如何拟合余弦函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19514315/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com