Write a function that implements Lagrange interpolation, with the following inpu
ID: 3793439 • Letter: W
Question
Write a function that implements Lagrange interpolation, with the following input/output. function y = Lagrange(data ptX, data ptY, x) end In the input bracket, where data X and data Y are two arrays that storing values of the data points for interpolating ({xi, yi}), and x is the value of x in the function for calculating y. if you have n+1 points (meaning n + 1 elements in data X, data Y), you should fit nth order Lagrange. Use all the values from table 1, and calculate x=9.5 through the program and give the answer.Explanation / Answer
%function y=lagrange(x,pointx,pointy) function y=Lagrange(dataptX,dataptY,x) % %LAGRANGE approx a point-defined function using the Lagrange polynomial interpolation % % Lagrange(dataptX,dataptY,x) approx the function definited by the points: % P1=(dataptX(1),dataptY(1)), P2=(dataptX(2),dataptY(2)), ..., PN(dataptX(N),dataptY(N)) % and calculate it in each elements of X % % If dataptX and dataptY have different number of elements the function will return the NaN value % n=size(dataptX,2); L=ones(n,size(x,2)); if (size(dataptX,2)~=size(dataptY,2)) fprintf(1,' ERROR! dataptX and dataptY must have the same number of elements '); y=NaN; else for i=1:n for j=1:n if (i~=j) L(i,:)=L(i,:).*(x-dataptX(j))/(dataptY(i)-dataptX(j)); end end end y=0; for i=1:n y=y+dataptY(i)*L(i,:); end end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.