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

Must be in Matlab code please . Computer Problem: Write a program that takes tak

ID: 3283191 • Letter: M

Question

Must be in Matlab code please . Computer Problem: Write a program that takes takes data points (xi , y-1), 1-0, ,n, and outputs the Newton form of the interpolating polynomial (suffices to compute the coefficients). Write another program for the Lagrange form. Using the code you wrote, interpolate the function f (x)-sin(2nx) at ten equi-spaced nodes in [0,1, including 0 and 1. Overlay the value of the interpolating polynomial at twenty equi-spaced points iin [0,1] (including 0 and 1) Verify that both your programs give the same results.

Explanation / Answer

%%% Newton ploynomial program code %%%%%%%

function [f] = newpoly(x,y,p)
%---------------------------------------------------------------------------
%
%
% Inputs
%   X   vector of abscissas
%   Y   vector of ordinates
% p is point x0
% Return
%   C   coefficient list for the Newton polynomial
%
%
%
%---------------------------------------------------------------------------

n = length(x);
d(:,1)=y';
for j=2:n
    for i=j:n
        d(i,j)= ( d(i-1,j-1)-d(i,j-1)) / (x(i-j+1)-x(i));
    end
end
a = diag(d)';

Df(1,:) = repmat(1, size(p));
c(1,:) = repmat(a(1), size(p));
for j = 2 : n
   Df(j,:)=(p - x(j-1)) .* Df(j-1,:);
   c(j,:) = a(j) .* Df(j,:);
end
f=sum(c)
end

%%%%%%% Lagrange code %%%%%%%%

function y0 = lagrange_interp(x, y, z)
% x is the vector of abscissas.
% y is the matching vector of ordinates
% x0 represents the target to be interpolated
% y0 represents the solution from the Lagrange interpolation

y0 = 0;
n=length(x);
for j = 1 : n
t = 1;
for i = 1 : n
if i~=j
t = t * (z-x(i))/(x(j)-x(i));
end
end
y0 = y0 + t*y(j);
end

end

%%%%%%%%% calling functions %%%%%%%

clear all;
clc;

a=0;
b=1;
n=20; % number of nodes
h=(b-a)/n; % step length
x=a:h:b;
for i=1:n+1
    y(i)=sin(2*pi*x(i));
end
p=0.42;
[f] = newpoly(x,y,p);
y0 = lagrange_interp(x, y, p)

%%%% solution %%%%


f =

    0.4818


y0 =

    0.4818

>>