generating polynomial using newton divided difference
113 views (last 30 days)
Show older comments
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)
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))
0 Comments
Accepted Answer
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
More Answers (0)
See Also
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!