In: Advanced Math
Find polynomial of degree at most 1 best approximates function f(x) = ex in the sense of uniform approximation on [0,1]. give the error of approximation of the function f with this polynomial.
%%Matlab code for integration
clear all
close all
ff=@(x) exp(x);
%displaying the function
fprintf('For the function \n')
disp(ff)
%4th order polynomial
xx=linspace(0,1,10);
yy=ff(xx);
%matrix for 4th order polynomial
for i=1:length(xx)
A(i,1)=1;
A(i,2)=xx(i);
A(i,3)=(xx(i)).^2;
A(i,4)=(xx(i)).^3;
A(i,5)=(xx(i)).^4;
b(i,1)=yy(i);
end
fprintf('The coefficient Matrix is\n')
disp(vpa(A,3))
fprintf('b matrix is\n')
disp(b)
c=A\b;
fprintf('The 4th oder polynomial is
f(x)=%f+(%f)*x+(%f)*x^2+(%f)*x^3+(%f)*x^4\n',c(1),c(2),c(3),c(4),c(5))
fun=@(x) c(1)+c(2).*x+c(3).*x.^2+c(4).*x.^3+c(5).*x.^4;
xx1=linspace(0,1,101);
yy1=fun(xx1);
fprintf('Error in polynomial is %e\n',norm(yy1-ff(xx1)))
hold on
plot(xx,yy,'r*')
plot(xx1,yy1)
xlabel('x')
ylabel('f(x)=exp(x)')
title('x vs. exp(x) plot')
legend('Actual Data','polynomial fit','location','best')
box on
%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%