% Find a polynomial P\left(x\right) that agrees with a given functionf\left(x\right) at the points a=x_0 <x_1 <\ldotp \ldotp \ldotp <x_n =b. The gap between two consecutive points is h. Also find the error, which is the maximum difference of f\left(x\right) and P\left(x\right) for x\in \left\lbrack a,b\right\rbrack.

a = -3; b= 3; %interval [a,b]
h = 2; %step size
x = a:h:b; %grid points
y = f(x); %corresponding value of y
P = polyfit(x,y,length(x)-1); %interpolation polynomial
x_fine = a:h/10:b; %a finer grid on the x-axis
y_exact = f(x_fine); %list of values of the exact function
y_appr = polyval(P,x_fine); %list of values of interpolation polynomial
err = max(abs(y_exact-y_appr))
plot(x_fine,y_exact,'r'), hold on, %plot the exact function in red
plot(x_fine,y_appr,'b'), %plot the polynomial in blue
plot(x,y,'k*') %draw the points in black

% Function f:

function y = f(x)
    y = sin(x);
end