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

问题描述 投票:0回答:2

是否有matlab命令获得最佳拟合形式y(t)= a_1 + a_2 * t + a_3 * sin(t)???

由于正弦函数无法找到命令

谢谢!!

matlab curve-fitting
2个回答
1
投票

在这种情况下,您可以使用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);  

它将返回如下:(由于随机数据可能会有一些不同,但答案是正确的[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)

1
投票
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

© www.soinside.com 2019 - 2024. All rights reserved.