EXPERIMENT 5
AIM : Demonstration of newton forward and backward interpolation
OBJECTIVE : To accurately estimate unknown function values using Newton’s
Forward and Backward Interpolation methods and demonstrate their effectiveness in
numerical analysis.
PROBLEM STATEMENT :
Find f(0.5) using Newton’s forward difference formula
x = [0 1 2 3 4];
y = [1 7 23 55 109];
CODING :
clc;
clear;
function y_interp = newton_forward_interpolation(x, y, x_interp)
if length(x) ~= length(y)
error('Lengths of x and y must be the same');
end
n = length(x);
f_diff = zeros(n, n);
f_diff(:, 1) = y;
for j = 2:n
for i = 1:n-j+1
f_diff(i, j) = f_diff(i+1, j-1) - f_diff(i, j-1);
end
end
h = x(2) - x(1);
p = (x_interp - x(1)) / h;
y_interp = y(1);
p_term = 1;
fact = 1;
for j = 2:n
p_term = p_term * (p - (j-2));
fact = fact * (j-1);
y_interp = y_interp + (p_term / fact) * f_diff(1, j);
end
end
x = [0 1 2 3 4];
y = [1 7 23 55 109];
x_interp = 0.5;
y_interp = newton_forward_interpolation(x, y, x_interp);
disp(['f(0.5) = ', num2str(y_interp)]);
OUTPUT :
f(0.5) = 3.125
PROBLEM STATEMENT :
Find f(300) using Newton’s backward difference formula
18
x = [50 100 150 200 250];
y = [618 724 805 906 1032];
CODING :
clc;
clear;
function y_interp = newton_backward_interpolation(x, y, x_interp)
if length(x) ~= length(y)
error('Lengths of x and y must be the same');
end
n = length(x);
f_diff = zeros(n, n);
f_diff(:, 1) = y;
for j = 2:n
for i = n:-1:j
f_diff(i, j) = f_diff(i, j-1) - f_diff(i-1, j-1);
end
end
h = x(2) - x(1);
p = (x_interp - x(n)) / h;
y_interp = y(n);
p_term = 1;
fact = 1;
for j = 2:n
p_term = p_term * (p + (j-2));
fact = fact * (j-1);
y_interp = y_interp + (p_term / fact) * f_diff(n, j);
end
end
x = [50 100 150 200 250];
y = [618 724 805 906 1032];
x_interp = 300;
y_interp = newton_backward_interpolation(x, y, x_interp);
disp(['f(300) = ', num2str(y_interp)]);
OUTPUT :
f(300) = 1148
PROBLEM STATEMENT :
Find f(300) using Newton’s forward difference formula
x = [50 100 150 200 250];
y = [618 724 805 906 1032];
CODING :
clc;
clear;
function y_interp = newton_forward_interpolation(x, y, x_interp)
if length(x) ~= length(y)
error('Lengths of x and y must be the same');
end
19
n = length(x);
f_diff = zeros(n, n);
f_diff(:, 1) = y;
for j = 2:n
for i = 1:n-j+1
f_diff(i, j) = f_diff(i+1, j-1) - f_diff(i, j-1);
end
end
h = x(2) - x(1);
p = (x_interp - x(1)) / h;
y_interp = y(1);
p_term = 1;
fact = 1;
for j = 2:n
p_term = p_term * (p - (j-2));
fact = fact * (j-1);
y_interp = y_interp + (p_term / fact) * f_diff(1, j);
end
end
x = [50 100 150 200 250];
y = [618 724 805 906 1032];
x_interp = 300;
y_interp = newton_forward_interpolation(x, y, x_interp);
disp(['f(300) = ', num2str(y_interp)]);
OUTPUT :
f(300) = 1148
PROBLEM STATEMENT :
Find f(0.5) using Newton’s backward difference formula
x = [0 1 2 3 4];
y = [1 7 23 55 109];
CODING :
clc;
clear;
function y_interp = newton_backward_interpolation(x, y, x_interp)
if length(x) ~= length(y)
error('Lengths of x and y must be the same');
end
n = length(x);
f_diff = zeros(n, n);
f_diff(:, 1) = y;
for j = 2:n
for i = n:-1:j
f_diff(i, j) = f_diff(i, j-1) - f_diff(i-1, j-1);
end
end
h = x(2) - x(1);
20
p = (x_interp - x(n)) / h;
y_interp = y(n);
p_term = 1;
fact = 1;
for j = 2:n
p_term = p_term * (p + (j-2));
fact = fact * (j-1);
y_interp = y_interp + (p_term / fact) * f_diff(n, j);
end
end
x = [0 1 2 3 4];
y = [1 7 23 55 109];
x_interp = 0.5;
y_interp = newton_backward_interpolation(x, y, x_interp);
disp(['f(0.5) = ', num2str(y_interp)]);
OUTPUT :
f(0.5) = 3.125
CONCLUSION :
Newton’s Forward and Backward Interpolation methods were successfully applied to
estimate function values from given data points. Forward interpolation was used for
points near the beginning, while backward interpolation was used for points near the
end. The results demonstrate the effectiveness of these methods in numerical
approximation and function estimation.
NAME : SUMIT MANDAL
ROLL : 2305903
SEC : CSE - 30
21