Home matlab command obtain best fit of form $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)$

Tiger Blood
1#
Tiger Blood Published in 2017-12-07 01:01:01Z
 Is there a matlab command obtain best fit of form y(t)=a_1+a_2*t+a_3*sin(t) ??? Cannot find a command because of the sine function thank you!!
Hunter Jiang
2#
Hunter Jiang Reply to 2017-12-22 01:22:41Z
 In this case, you could use fittype command. Here is the code might help: %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);  It will return f like this: (there could be little different because of random data, but the answer is correct [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) 
Tommaso Belluzzo
3#
Tommaso Belluzzo Reply to 2017-12-07 01:41:00Z
 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');  Reference to official documentation with examples: https://mathworks.com/help/optim/ug/lsqcurvefit.html
 You need to login account before you can post.
Processed in 0.313544 second(s) , Gzip On .