Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HW 4 is used for reference since HW 5 says \"your function Interpolate from HW4

ID: 2263706 • Letter: H

Question

HW 4 is used for reference since HW 5 says "your function Interpolate from HW4 should give you all the data you need for the second plot."

Math 98, Spring 2018 Homework 5 Plotting Your task is to reproduce the following figure as accurately as you are able. A Visualization of the Runge Phenomenon 0.01 0.005 3 Even Spacing Chebyshev This is bad -0.005 -0.01 -1 0.8 0 -0.4 -02 0 02 04 06 8 1 Polynomial interpolation with 10 nodes 1.5 Even spacing Chebyshev 1 + 25z2 0.5 -0.5 1 0.8 0.6 04 -02 00.2 0.4 0.6 0.8 1 Notes on the functions shown in the plots 1. Given a set of (interpolation) points Zo,Ti,..,Zn, the function w is defined as and is related to the error bounds for polynomial interpolation. This means that the w(x) defined using evenly spaced nodes will not be the same as the w(x) defined using Chebvshev nodes. 2. The main function in the second subplot is f(x) = 1/(1 + 25x2), and the functions graphed in blue and red are the polynomial interpolations of f(x) using evenly spaced nodes and Chebyshev nodes, respectively. Your function Interpolate from HW4 should give you all the data you need for the second plot.

Explanation / Answer

Following is the MATLAB function for calculating interpolation nodes and interpolation polynomial:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [xk, p] = Interpolate(f,a,b,n,spacing)
if strcmp(spacing,'')
xk = linspace(a,b,n+1);
elseif strcmp(spacing,'chebyshev')
k = 1:n+1;
xk = 0.5*(a+b)+0.5*(b-a)*cos((2*k-1)*pi./(2*(n+1)));
end
y=f(linspace(a,b,n+1));
polyCoeffs = polyfit(xk,y,n);
p = @(x)(sum(polyCoeffs.*[x.^(n:-1:0)]));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Following is the MATLAB code for plotting w(x)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

n = 10;
f = @(x)(1./(1+25*x.^2));
[xk, p]= Interpolate(f,-1,1,n,'');
[x1k, q] = Interpolate(f,-1,1,n,'chebyshev');

wx = @(x)((x-xk(1))*(x-xk(2))*(x-xk(3))*(x-xk(4))*(x-xk(5))*(x-xk(6))*...
(x-xk(7))*(x-xk(8))*(x-xk(9))*(x-xk(10))*(x-xk(11))); % w(x) defined by evenly spaced nodes
w1x = @(x)((x-x1k(1))*(x-x1k(2))*(x-x1k(3))*(x-x1k(4))*(x-x1k(5))*(x-x1k(6))*...
(x-x1k(7))*(x-x1k(8))*(x-x1k(9))*(x-x1k(10))*(x-x1k(11))); % w(x) defined by chebyshev nodes
  
% MATLAB code for plotting w(x)
fplot(wx,[-1 1]);
ylim([-0.01 0.01]);
xlabel('x');
ylabel('w(x)');
hold on; grid on;
fplot(w1x,[-1 1]);
legend('Even Spacing', 'Chebyshev');

% MATLAB code for plotting interpolation polynomial
figure;
fplot(f,[-1 1]);
xlabel('x');
ylabel('f(x)');
hold on;grid on;
fplot(p,[-1 1]);
hold on;
fplot(q,[-1 1]);
legend('1/1+25x^2','even spacing','chebyshev');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%