We have modified the the given code in the required form.
OUTPUT
-------------------------------------------MATLAB code-------------------------------------------
clc;
clear all
format short;
fprintf('\n\n')
x=input('Enter the values of x as a vector: ');
f_x=cos(x)-x;
n=numel(x)-1;
% xp=input('Enter a value xp between 0 and 100: ');
xp=x(2);
P_x=0;
for i=1:n+1
L_k=1;
for j=1:n+1
if j~=i
L_k=L_k*(xp-x(j))/(x(i)-x(j));
end
end
P_x=P_x+f_x(i)*L_k;
end
fprintf('\n\n')
disp (['The polynomial P_xp at x1=',num2str(x(2)), ' is: ',
num2str(P_x)]);
fprintf('\n\n')
disp (['Absolute error at x1=',num2str(x(2)), ' is: ',
num2str(abs(P_x-f_x(2)))]);
fprintf('\n\n')
Get Answers For Free
Most questions answered within 1 hours.