2.5 6.5 4 f(x) 0 a) (20points) Develop a code in MATLAB that finds the Newton\'s
ID: 3282741 • Letter: 2
Question
2.5 6.5 4 f(x) 0 a) (20points) Develop a code in MATLAB that finds the Newton's interpolating polynomial of order (n) for a set of (n+1) data points. Pseudocode can be found in your textbook on page 473-474. Calculate f(3.4) using Newton's interpolation polynomial of order 1 through 3 b) (20points) Develop a code in MATLAB that finds the Lagrange's interpolating polynomial of order (n) for a set of (n+1) data points. Calculate f(3.4) using Lagrange's interpolation polynomial of order 1 through 3.Explanation / Answer
(a) x=[ 1 2 2.5 3 4 5 ]; y=[ 0 5 6.5 7 3 1]; a = newton_interpolation(x, y, 3.4); n = length(x); a(1) = y(1); for k = 1 : n - 1 d(k, 1) = (y(k+1) - y(k))/(x(k+1) - x(k)); end for j = 2 : n - 1 for k = 1 : n - j d(k, j) = (d(k+1, j - 1) - d(k, j - 1))/(x(k+j) - x(k)); end end d for j = 2 : n a(j) = d(1, j-1); end Df(1) = 1; c(1) = a(1); for j = 2 : n Df(j)=(p - x(j-1)) .* Df(j-1); c(j) = a(j) .* Df(j); end fp=sum(c); (b)x=[ 1 2 2.5 3 4 5 ]; y=[ 0 5 6.5 7 3 1]; sum = 0; for i = 1:length(x) p=1; for j=1:length(x) if j~=i c = poly((x-x(j)))/(x(i)-x(j)) ; p = conv(c, p); end end sum = sum + y(i)*p; end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.