Numerical Analysis, Matlab computer programming (Matlab) (a) Write a Matlab func
ID: 3607732 • Letter: N
Question
Numerical Analysis, Matlab computer programming
(Matlab) (a) Write a Matlab function that inputs , a vector of z-coordinates of data points (nodes); y, an array of y-coordinates of data points; n, the total number of data points; and z, one location on the x-axis. Have it output the value of the in terpolating polynomial for the data points, computed using Lagrange form, at z Write out or print out your function and turn it in (b) Use your program to approximate f(2) for the table of data 01 4 9 16 f(x)0123 4 Write out or print out your results and turn them inExplanation / Answer
CODE:
The below function will Calculate the Lagrange polynomial interpolation.
function y = lagrange(x, pointx, pointy)
% x = vector of points where you want to interpolate
% pointx = vector of data values for x - axis
% pointy = vector of data values for y - axis
%
% If length of pointx and pointy are different, the function will return the NaN value
L = ones(n,size(x,2));
n = size(pointx,2);
if (size(pointx,2)~=size(pointy,2))
fprintf(1,'pointx and pointy dimensions are not same ');
y=NaN;
else
for i=1:n
for j=1:n
if (i~=j)
L(i,:)=L(i,:).*(x-pointx(j))/(pointx(i)-pointx(j));
end
end
end
y=0;
for i=1:n
y=y+pointy(i)*L(i,:);
end
end
It will return a vector y as the output. From the output y, we can get the y[2] value as desired for
second part of the problem.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.