generating polynomial using newton divided difference

123 views (last 30 days)
Hi good day y'all, i'm making a code for newton divided difference but i'm having a hard time generating the right polynomial, can someone please help me? thank you so much.
y=[1 2 3 4];
x=[4.4 4.3 2 5];
n=size(x,2);
DD=zeros(n,n);
DD(:,1)=y';
for j=2:n
for i=1:(n-j+1)
num=DD(i+1,j-1)-DD(i,j-1);
den = (x(i+j-1)-x(i));
DD(i,j)=num./den;
end
end
array2table(DD)
ans = 4×4 table
DD1 DD2 DD3 DD4 ___ ________ _______ ______ 1 -10 -3.9855 8.4714 2 -0.43478 1.0973 0 3 0.33333 0 0 4 0 0 0
n=length(x);
a(1)=x(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
%
for j=2:n
a(j)=d(1,j-1);
end
yn=vpa(x);
d=vpa(d);
a=vpa(a);
clear x
syms x
%
Df(1)=vpa(1);
c(1)=a(1);
for j=2:n
Df(j)=(x-yn(j-1)).*Df(j-1);
c(j)=a(j).*Df(j);
end
format short
f=simplify(sum(c))
f = 

Accepted Answer

Vinayak Agrawal
Vinayak Agrawal on 27 Jun 2023
Edited: Vinayak Agrawal on 28 Jun 2023
Hi Michael,
an updated version of your code that computes the polynomial expression using symbolic calculations in MATLAB:
% Given data
y = [1 2 3 4];
x = [4.4 4.3 2 5];
n = length(x);
DD = zeros(n, n);
DD(:, 1) = y';
for j = 2:n
for i = 1:(n-j+1)
num = DD(i+1, j-1) - DD(i, j-1);
den = (x(i+j-1) - x(i));
DD(i, j) = num / den;
end
end
% Coefficients of the polynomial
a = diag(DD)';
yn = sym(x);
d = sym(diag(DD));
% Compute the polynomial expression
syms x;
f = a(1);
for j = 2:n
term = 1;
for k = 1:j-1
term = term * (x - yn(k));
end
f = f + a(j) * term;
end
% Simplify the polynomial expression
f = simplify(f);
disp(f);
In this updated code, try running this. I've used symbolic calculations (sym) to generate the polynomial expression based on the computed coefficients. The resulting polynomial expression is simplified using simplify for a more concise form.
When you run the code, it will display the simplified polynomial expression. You can further customize the output format or manipulate the polynomial expression as needed.
I hope this helps you generate the correct polynomial expression using Newton's divided difference interpolation.
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Polynomials in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!