Python Program to Generate Backward Difference Table

Table of Contents

In numerical analysis, method like Newton's Backward Interpolation relies on Backward Difference Table.

In this program, we are going to generate backward difference table in Python programming language.

Python Source Code: Backward Difference Table


# Backward Difference Table Generation 

# Importing NumPy Library
import numpy as np
import sys

# Reading number of unknowns
n = int(input('Enter number of data points: '))

# Making numpy array of n & n x n size and initializing 
# to zero for storing x and y value along with differences of y
x = np.zeros((n))
y = np.zeros((n,n))


# Reading data points
print('Enter data for x and y: ')
for i in range(n):
    x[i] = float(input( 'x['+str(i)+']='))
    y[i][0] = float(input( 'y['+str(i)+']='))
    
# Generating backward difference table
for i in range(1,n):
    for j in range(n-1,i-2,-1):
        y[j][i] = y[j][i-1] - y[j-1][i-1]

        
print('\nBACKWARD DIFFERENCE TABLE\n');

for i in range(0,n):
    print('%0.2f' %(x[i]), end='')
    for j in range(0, i+1):
        print('\t%0.2f' %(y[i][j]), end='')
    print()

Python Output: Backward Difference Table

Enter number of data points: 4
Enter data for x and y: 
x[0]=0
y[0]=1
x[1]=1
y[1]=2
x[2]=2
y[2]=1
x[3]=3
y[3]=10

BACKWARD DIFFERENCE TABLE

0.00	1.00
1.00	2.00	1.00
2.00	1.00	-1.00	-2.00
3.00	10.00	9.00	10.00	12.00