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

Polynomial Interpolation Your task is to write a Matlab function function p = In

ID: 2262287 • Letter: P

Question

Polynomial Interpolation Your task is to write a Matlab function function p = Interpolate (f,a,b,n,' spacing') that returns an n-degree polynomial p that approximates the function f on the interval [a, b using polyfit. If the input argument spacing is omitted, then space the (n + 1) inter- polation nodes xk evenly (the function linspace may be useful). If spacing is equal to 'chebyshev', then use the Chebyshev nodes given by 2k-1 Test your function on the polynomial f(x) = R1on the interval [-1,1] with n = 1,2,..., 15. Compare the values of your interpolating functions pn(x) and qn(x) against f(x) for z =-1 : 0.001: 1, and print out a table with the information +252 n | max Ipn(x) f(x)maxn(x) f(x) TEl-1,1 rE-1,1 10 1. The output polynomial p should be a function handle, not a vector representing a 2. When using Chebyshev nodes, try to define the nodes xk entirely using vector notation. 3. As a bonus, consider looking up the extra features of polyfit to make your function polynomial. Consider combining anonymous functions with polyval to define p You should be able to do it without using a for loop work well in cases where [a, b is badly scaled or not centered around zero.

Explanation / Answer

function 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 to plot the results:

f = @(x)(1./(1+25*x.^2));
p = Interpolate(f,-1,1,10,'');
q = Interpolate(f,-1,1,10,'chebyshev');
fplot(f,[-1 1]);
hold on;
fplot(p,[-1 1]);
hold on;
fplot(q,[-1 1]);
legend('f(x)','even spacing','chebyshev');