The MATLAB procedure for polynomial least-squares is polyfit. Study the following example;
x =(0:0.1:5)'; % x from 0 to 5 in steps of 0.1
y = sin(x); % get y values
p = polyfit(x,y,3); % fit a cubic to the data
f = polyval(p,x); % evaluate the cubic on the x data
plot(x,y,'o',x,f,'-') % plot y and its approximation f
For the given data points;
to which we will fit
Hint: First, we should compute a new table with
Construct the normal equations
Hints: and
Solve these normal equations to find and
Convert back to the original variables
Plot vs and vs then compare them.
Soln:
Apply the procedure given in the first item by using the data set in the previous item.
Hints:
fit a cubic to the data
evaluate the cubic on the x data
Plot by
Compare this least-square polynomial with the function used in the previous item.