matlab 命令获得最适合的形式 $y(t)=a_1+a_2*t+a_3*sin(t) $

matlab command obtain best fit of form $y(t)=a_1+a_2*t+a_3*sin(t) $

有没有matlab命令获得最适合形式y(t)=a_1+a_2*t+a_3*sin(t) ???

由于正弦函数,找不到命令

谢谢!!

在这种情况下,您可以使用 fittype 命令。

以下代码可能有帮助:

%sample data
x=[1;2;3;4;5];y=3+4*x+5*sin(x)+rand(5,1)*0.01;

%curve-fitting
p=fittype('a_1+a_2*t+a_3*sin(t)','independent','t')  
f=fit(x,y,p)  

%plot
plot(f,x,y);  

它将 return f 像这样:(由于随机数据可能会有一些不同,但答案是正确的 [3,4,5])

f =

 General model:
 f(t) = a_1+a_2*t+a_3*sin(t)
 Coefficients (with 95% confidence bounds):
   a_1 =       2.996  (2.95, 3.043)
   a_2 =       4.002  (3.987, 4.017)
   a_3 =       5.005  (4.978, 5.032)
fh = @(a,t) a(1) + a(2) * t + a(3) * sin(t);
a = [2 -1 3];
x = ...;
y = ...;

fit = lsqcurvefit(fh,a,x,y);

figure();
plot(x,y,'ko',x,fh(a,x),'b-');
title('Result');
legend('Data','Fitted');

参考官方文档和示例:

https://mathworks.com/help/optim/ug/lsqcurvefit.html