In: Advanced Math
Part c). is needed
On the interval [-1,1], consider interpolating Runge’s function f(x) = 1/ (1 + 25x^2) By Pn(x), use computer to graph:
(a) Take the nodes xi to be –1, 0, 1 and obtain P2(x). In the same graph, plot the two functions f(x) and P2(x) over the interval [-1,1]. Use different line-styles, so that f(x) and P2(x) look distinct.
(b) Take five nodes xi to be -1, -0.5, 0, 0.5, 1 and obtain P4(x). In the same graph, plot the two functions f(x) & P4(x) over the interval [-1,1] . Use different line-styles, so that f(x) and P4(x) look distinct.
(c) Take 11 equally spaced nodes in [-1,1], starting at –1, ending at 1, and obtain the interpolating polynomial P10(x). Also, use 11 Chebyshev nodes in [-1,1] and obtain Pc(x), the corresponding interpolating polynomial. In the same graph, plot the three functions f(x), P10(x) and Pc(x) over the interval [-1,1] . Use different line-styles, so that f(x), P10(x) and Pc(x) look distinct.
We have developed a MATLAB code for the given problem. Here, input of the function is the degree of the polynomial.
---------------------MATLAB code---------------------
function Runge_function(n)
% n=degree of the polynomial;
% Therefore, number of data popints is n+1;
fx=@(x) 1./(1+25*x.^2);
a=-1;
b=1;
x_exact=a:0.01:b;
y_exact=fx(x_exact);
x=(a:(b-a)/n:b)';
y=fx(x);
A=x.^(0:n);
coeff=(A'*A)\(A'*y);
fprintf('\nRequired %g th degree polynomial is: \n\n',n)
fprintf(' y= %g + (%g) x',coeff(1),coeff(2))
for i=3:n+1
fprintf(' + (%g) x^%g',coeff(i),i-1);
end
fprintf('\n\n')
xx=a:0.01:b;
yy=0*xx;
y2=0*x;
for j=0:n
yy=yy+coeff(j+1)*xx.^j;
y2=y2+coeff(j+1)*x.^j;
end
VarNames = {'x','exact_y','approx_y','Absolute_error'};
TT=table(x,y,y2,abs(y-y2), 'VariableNames',VarNames);
disp(TT);
figure(1)
plot(xx,yy,'-',x_exact,y_exact,'--',x,y,'*k','LineWidth',1.25);
xlabel('x','fontsize',14)
ylabel('y(x)','fontsize',14)
legend('Polynomial','Actual curve','Node points')
title('Regression line vs Actual data','fontsize',14);
end
(a)
(b)
(c)