Newton Forward Difference Interpolating Polynomials

204 次查看(过去 30 天)
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?

回答(2 个)

Sourabh ahlawat
Sourabh ahlawat 2019-11-11
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

gizem
gizem 2022-11-8
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 个评论
Aissam
Aissam 2022-12-6
编辑:Aissam 2022-12-6
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)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by