Newton Forward difference method

43 次查看(过去 30 天)
Sunday
Sunday 2024-9-15,18:53
编辑: Torsten 2024-9-15,23:11
% Confirmed case Sample data points (x, y)
data = [ 1, 88; 2, 49; 3, 47; 4, 8; 5, 34; 6, 762; 7, 98; 8, 40];
% Extract x and y values
x = data(:, 1);
y = data(:, 2);
n = length(x);
% Calculate forward differences
forward_diff = zeros(n, n);
forward_diff(:, 1) = y;
for j = 2:n
for i = 1:n-j+1
forward_diff(i, j) = forward_diff(i+1, j-1) - forward_diff(i, j-1);
end
end
% Construct the Newton Forward Difference polynomial
syms X;
P = y(1);
for j = 1:n-1
term = forward_diff(1, j);
for i = 1:j
term = term * (X - x(i));
end
P = P + term / factorial(j);
end
disp('Newton Forward Difference Polynomial:');
Newton Forward Difference Polynomial:
disp(simplify(P));
Result Newton Forward Difference Polynomial: - (725*X^7)/1008 + (1651*X^6)/80 - (173233*X^5)/720 + (70651*X^4)/48 - (91321*X^3)/18 + (146407*X^2)/15 - (1996741*X)/210 + 3658
The problem now is that when I substitute each value of x, I don't get the corresponding value of y as stated in the table. I need help on how to get the correct polynomial equation.

采纳的回答

Torsten
Torsten 2024-9-15,20:28
编辑:Torsten 2024-9-15,23:11
Three coding errors:
% Confirmed case Sample data points (x, y)
data = [ 1, 88; 2, 49; 3, 47; 4, 8; 5, 34; 6, 762; 7, 98; 8, 40];
% Extract x and y values
x = data(:, 1);
y = data(:, 2);
n = length(x);
% Calculate forward differences
forward_diff = zeros(n, n);
forward_diff(:, 1) = y;
for j = 2:n
for i = 1:n-j+1
%forward_diff(i, j) = (forward_diff(i+1, j-1) - forward_diff(i, j-1))
forward_diff(i, j) = (forward_diff(i+1, j-1) - forward_diff(i, j-1)) / (x(i+j-1) - x(i));
end
end
% Construct the Newton Forward Difference polynomial
syms X;
P = y(1);
for j = 1:n-1
%term = forward_diff(1, j);
term = forward_diff(1, j+1);
for i = 1:j
term = term * (X - x(i));
end
%P = P + term / factorial(j);
P = P + term;
end
disp('Newton Forward Difference Polynomial:');
Newton Forward Difference Polynomial:
disp(simplify(P));
double(subs(P,X,x))
ans = 8×1
88 49 47 8 34 762 98 40
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
For all data series with deltax = 1 (as the one above), there is one error in your code. But note that this is a special case and the above code holds for arbitrary deltax.
% Confirmed case Sample data points (x, y)
data = [ 1, 88; 2, 49; 3, 47; 4, 8; 5, 34; 6, 762; 7, 98; 8, 40];
% Extract x and y values
x = data(:, 1);
y = data(:, 2);
n = length(x);
% Calculate forward differences
forward_diff = zeros(n, n);
forward_diff(:, 1) = y;
for j = 2:n
for i = 1:n-j+1
forward_diff(i, j) = forward_diff(i+1, j-1) - forward_diff(i, j-1);
end
end
% Construct the Newton Forward Difference polynomial
syms X;
P = y(1);
for j = 1:n-1
%term = forward_diff(1, j);
term = forward_diff(1, j+1);
for i = 1:j
term = term * (X - x(i));
end
P = P + term / factorial(j);
end
disp('Newton Forward Difference Polynomial:');
Newton Forward Difference Polynomial:
disp(simplify(P));
double(subs(P,X,x))
ans = 8×1
88 49 47 8 34 762 98 40
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

更多回答(0 个)

类别

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

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by