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 Inte

ID: 2261187 • 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 x 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(z-R on the interval [-1,1] with n- 1,2,...,15. Compare the values of your interpolating functions p(x) and gn(x) against f(x) for 1:0.001: 1, and print out a table with the information el-l rel-1,1 10 1. The output polynomial p should be a function handle, not a vector representing a polynomial. Consider combining anonymous functions with polyval to define p 2. When using Chebyshev nodes, try to define the nodes z entirely using vector notation. You should be able to do it without using a for loop. 3. As a bonus, consider looking up the extra features of polyfit to make your function work well in cases where la, 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');