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

function [y] = NewtonPoly(xd,yd,x) % function [y] = NewtonPoly(xd,yd,x) % This f

ID: 2966742 • Letter: F

Question

function [y] = NewtonPoly(xd,yd,x)

% function [y] = NewtonPoly(xd,yd,x)

% This function returns the divided differences

% This function returns y=P(x), where P is the polynomial whose

% graph passes through (x_0,y_0),...,(x_n,y_n).

% Input: xd -- a vector that stores [x_0,x_1,...,x_n].

% yd -- a vector that stores [y_0,y_1,...,y_n].

% Newton's divided difference formula is used.

% Check if the input is good.

if length(xd)~=length(yd)

disp(['Input Invalid']);

y=NaN;

return;

end

m = length(xd); %Length of the vector xd. It is n+1.

% First we compute the divided differences, which is done by the subroutine

% DivDiff (need to be finished by you) in the bottom of this function.

D = DivDiff(xd,yd);

% Find y by using Newton's formula.

% Insert your code here.

end

function [D]=DivDiff(xd,yd)

% This subroutine returns the divided differences

% f[x_0], f[x_0,x_1], ..., f[x_0,x_1,...,x_n]

% from data points (x_0,y_0),...,(x_n,y_n).

% Input: xd -- a vector that stores [x_0,x_1,...,x_n].

% yd -- a vector that stores [y_0,y_1,...,y_n].

% Output: D -- a vector that stores f[x_0], f[x_0,x_1], ..., f[x_0,x_1,...,x_n]

% Insert your code that finds the divided differences.

end

5. Write a Matlab program to compute y = Pn(x) for a given x, where Pn is the polynomial of degree at most n that goes through n + 1 points (x0, y0). (x1, y1). Newton's method should be used in your program. An incomplete code for this purpose is given in NewtonPoly.m. Provide your completed code. Use your program to find P2(2), where P2(x) is the polynomial that goes through (0,-1), (1, -1), and (-1, 7).

Explanation / Answer

function [y] = NewtonPoly(xd,yd,x)

% function [y] = NewtonPoly(xd,yd,x)
% This function returns the divided differences
% This function returns y=P(x), where P is the polynomial whose
% graph passes through (x_0,y_0),...,(x_n,y_n).
% Input: xd -- a vector that stores [x_0,x_1,...,x_n].
% yd -- a vector that stores [y_0,y_1,...,y_n].
% Newton's divided difference formula is used.

% Check if the input is good.
if length(xd)~=length(yd)
disp(['Input Invalid']);
y=NaN;
return;
end

m = length(xd); %Length of the vector xd. It is n+1.

% First we compute the divided differences, which is done by the subroutine
% DivDiff (need to be finished by you) in the bottom of this function.

D = DivDiff(xd,yd);

% Find y by using Newton's formula.
% Insert your code here.

end

function [D]=DivDiff(xd,yd)
% This subroutine returns the divided differences
% f[x_0], f[x_0,x_1], ..., f[x_0,x_1,...,x_n]
% from data points (x_0,y_0),...,(x_n,y_n).
% Input: xd -- a vector that stores [x_0,x_1,...,x_n].
% yd -- a vector that stores [y_0,y_1,...,y_n].
% Output: D -- a vector that stores f[x_0], f[x_0,x_1], ..., f[x_0,x_1,...,x_n]

% Insert your code that finds the divided differences.
len = length(xd);
D = yd(1);
i = 1;
j = 2;
prevec = xd;
nuvec = yd;
D(1) = yd(1)
while i < len
while
place(i) = nuvec(i+1) - nuvec(i) / prevec(i+1) - prevec(i)

prevec = nuvec;
nuvec = place;
D(i+1) = nuvec(1);
i = i + 1;
  
end