Newton Forward Difference Interpolating Polynomials
267 views (last 30 days)
Show older comments
Hi guys, I have a quick question. I'm building a Newton Forward Difference method in Matlab, but I don't get how to set up an equations.
Here is what I have so far;
function yi = Newton_FD(x, y, xi)
% this function computes the interpolating polynomials
% for the given data, x and y, using Newton's forward-
% difference formula. The polynomials of degree
% 1, 2, ..., n are computed, where n is one less than the
% number of data points. The polynomials are then evaluated
% at xi.
% n is the maximum degree possible with the data
n=length(y)-1;
% Initialize difference table
d=y;
for i=1:n
for j=n+1:-1:i+1,
d(j)=(d(j-1)-d(j))/(x(j-1)-x(j));
end
% use nested multiplication to evaluate the
% polynomial at xi
yi=d(i+1);
for j=i:-1:1,
yi=d(j)+(xi-x(j))*yi;
end
% display coefficients of Newton form of interpolating
% polynomial, and the value at xi
disp(sprintf('Degree %d:',i))
fprintf('\n')
fprintf('Coefficients=')
fprintf('\n\n')
Coefficients=d(1:i+1);
disp(Coefficients)
fprintf('Value=')
fprintf('\n\n')
Value=yi;
disp(Value)
end
and given values are:
x=[1;2;3;4;5;6;7;8;9;10];
y=[1;0.4444;0.2632;0.1818;0.1373;0.1096;0.0929;0.0775;0.0675;0.0597];
But, somehow it is giving me a wrong values for coefficients. Any suggestions?
0 Comments
Answers (3)
Sourabh ahlawat
on 11 Nov 2019
function yint = Newtint(x,y,xx)
% Newtint: Newton interpolating polynomial
% yint = Newtint(x,y,xx): Uses an (n - 1)-order Newton
% interpolating polynomial based on n data points (x, y)
% to determine a value of the dependent variable (yint)
% at a given value of the independent variable, xx.
% input:
% x = independent variable
% y = dependent variable
% xx = value of independent variable at which
% interpolation is calculated
% output:
% yint = interpolated value of dependent variable
% compute the finite divided differences in the form of a
% difference table
n = length(x);
if length(y)~=n, error('x and y must be same length'); end
b = zeros(n,n);
% assign dependent variables to the first column of b.
b(:,1) = y(:); % the (:) ensures that y is a column vector.
for j = 2:n
for i = 1:n-j+1
b(i,j) = (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));
end
end
% use the finite divided differences to interpolate
xt = 1;
yint = b(1,1);
for j = 1:n-1
xt = xt*(xx-x(j));
yint = yint+b(1,j+1)*xt;
end
0 Comments
gizem
on 8 Nov 2022
Do you have any suggestions on what kind of code I should write so that the coefficients are given in front of the x?
For example;ax^5+bx^4+cx^3...............
1 Comment
Aissam
on 6 Dec 2022
Edited: Aissam
on 6 Dec 2022
if true
% code
function p = newtint(x, y)
n=length(y);
d=zeros(n);
d(:,1)=y;
for j=2:n
for i=j:n
d(i,j)=(d(i,j-1)-d(i-1,j-1))/(x(i)-x(i-j+1));
end
end
d
pstr="";
pstr=string(d(1,1));
for i=2:n
pstr=pstr+"+";
for j=1:i-1
pstr=pstr+"(x-"+string(x(j))+")*";
end
pstr=pstr+string(d(i,i));
end
syms x p(x);
p(x)=simplify(str2sym(pstr));
end
Example:
x=[1:5]'
y=log(x)
p=newtint(x,y)
p(1.5)
sakshi kumari
on 4 Apr 2024 at 14:11
function yi = Newton_FD(x, y, xi)
d=y;
for i=1:n
for j=n+1:-1:i+1,
d(j)=(d(j-1)-d(j))/(x(j-1)-x(j));
end
% use nested multiplication to evaluate the
% polynomial at xi
yi=d(i+1);
for j=i:-1:1,
yi=d(j)+(xi-x(j))*yi;
end
% display coefficients of Newton form of interpolating
% polynomial, and the value at xi
disp(sprintf('Degree %d:',i))
fprintf('\n')
fprintf('Coefficients=')
fprintf('\n\n')
Coefficients=d(1:i+1);
disp(Coefficients)
fprintf('Value=')
fprintf('\n\n')
Value=yi;
disp(Value)
end
x=[1.0;1.1;1.2;1.3;1.4;1.5;1.6];
y=[7.989;8.403;8.781;9.129;9.451;9.750;10.031];
0 Comments
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!