In: Advanced Math
let p3x be the third order polynomiol interpolatign function f(x)=sin(x) at 0,1/3,2/3,1 show that for any x in [0,1] we have f(x)-p3(x), or equal to 1/54
%%Matlab code for 4th degree polynomial
clear all
close all
%all data points
x=[0 1/3 2/3 1];
y=sin(x);
fprintf('x vector is\n')
disp(x')
fprintf('y vector is\n')
disp(y')
%Matrix for 4th degree polynomial
for i=1:length(x)
A(i,1)=1;
A(i,2)=x(i);
A(i,3)=(x(i)).^2;
A(i,4)=(x(i)).^3;
end
fprintf('Coefficient matrix is \n')
disp(vpa(A,3))
%all coefficients are
p=A\y';
fprintf('The 3rd degree polynomial is\n')
fprintf('\tp(x)=(%f)+(%f)*x+(%f)*x^2+(%f)*x^3\n',p(1),p(2),p(3),p(4))
%plotting actual and fitted data
figure(1)
hold on
plot(x,y,'r*')
pp=@(t) p(1)+p(2).*t+p(3).*t.^2+p(4).*t.^3;
plot(x,pp(x))
xlabel('x')
ylabel('sin(x)')
title('x vs. y(x) plot')
legend('Actual data','polynomial fit','location','best')
box on; grid on;
xx=linspace(0,1);
yy=pp(xx);
fprintf('Error norm for 3rd order polynomial fit is
%f\n',norm(yy-sin(xx)))
%%%%%%%%%%%%%%%%%%%% End of Code
%%%%%%%%%%%%%%%%%%%