b. Repeat the experiment using the Chebyshev nodes given by x₁ = cos[(i-1)π/20] (1 ≤ i ≤21) c. With 21 equally spaced knots, repeat the experiment using a cubic interpolating spline.
The following MATLAB code is for part a for the question. Can you help me code part b and c?
% Define the function f(x)
f = @(x) 1 ./ (1 + 6*x.^2);
% Define the interpolation nodes (21 equally spaced nodes including endpoints)
nodes = linspace(-1, 1, 21);
% Compute the values of f(x) and p(x) at the interpolation nodes
f_values = f(nodes);
% Fit a polynomial of degree 20 through the interpolation nodes
p = polyfit(nodes, f_values, 20);
% Define the points where you want to evaluate f(x), p(x), and f(x) - p(x)
eval_points = linspace(-1, 1, 41);
f_eval = f(eval_points);
p_eval = polyval(p, eval_points);
difference = f_eval - p_eval;
% Create a table to display the results
T = table(eval_points', f_eval', p_eval', difference', 'VariableNames', {'x', 'f(x)', 'p(x)', 'f(x) - p(x)'});
disp(T);
Step by step
Solved in 4 steps with 3 images