Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write a general function, in MATLAB, to fit a sinusoidal curve of the type y = a

ID: 3672128 • Letter: W

Question

write a general function, in MATLAB, to fit a sinusoidal curve of the type y = a0 + a1 sin(t) + a2 cos(t) to a data set (t,y) with the following format: function [A,r] = fourier(t,y,) where A is a vector that contains the coefficients a0, a1, and a2, and r is the correlation coefficient, and is the angular frequency of the oscillations (if you are not familiar with this parameter, please look it up, and, in particular, see how it relates to the oscillations period). This function should implement the general linear least-squares theory studied in class and therefore use matrix operations (only) to compute the coefficients; a loop should NOT be used. The function should plot the data as well as the sinusoidal fit on the same plot.

Explanation / Answer

Comments added

% declaring arrays
t = linspace(0,2,20)';
y = mod(2*t,1);
w = 10;
% uainf fourier series .. Fseries to get fit
[a,b,yfitval] = Fseries(t,y,10);
% Grid evaluation
xgrid = linspace(0,2)';
A = Fseriesval(a,b,xgrid);
% plotting resutls obtained
plot(t,y,'x',t,yfitval,'o',xfine,A)
disp(A)