0% found this document useful (0 votes)
14 views16 pages

Python Program for Newton's Difference Tables

Uploaded by

gostperson12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Python Program for Newton's Difference Tables

Uploaded by

gostperson12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical No.

04

Name:Chikhale Yogini sanjay


Class:[Link](C.S)
Div.:B Batch:D
Roll No.: 12
Subject: MTC-242-MNP Practical on mathematics for computer science-I
Name: Writing python program to prepare newtons forword difference
table and newtons backword difference table.
Date: 04/07/2025
Q.1 Newtons forward difference table
(1) Construct a forward difference table of the following values

X 0 5 10 15 20 25

Y 6 10 13 17 23 31

import numpy as np
def difference_table(x,y,table):
n=len(x)
table[ :,0]=y
for colm in range(1,n):
for row in range(n-colm):
table[row][colm]=table[row+1][colm-1]
print("\n Newton forward difference table:")
print("x\tf(x)\tdf\t2f\td3f\d4f\td5f")
print(" ")
for row in range(n):
print(x[row],end="\t")
for colm in range(n-row):
print(table[row][colm],end="\t")
print()
x=[0,5,10,15,20,25]
y=[6,10,13,17,23,31]
n=len(x)
table=[Link]((n,n))
difference_table(x,y,table)
Output:
Newton forward difference table:

x f(x) df 2f d3f d4f d5f

0 6.0 10.0 0.0 0.0 0.0 0.0

5 10.0 13.0 0.0 0.0 0.0

10 13.0 17.0 0.0 0.0

15 17.0 23.0 0.0

20 23.0 31.0

25 31.0

Newton forward difference table:

x f(x) df 2f d3f d4f d5f

0 6.0 10.0 13.0 0.0 0.0 0.0

5 10.0 13.0 17.0 0.0 0.0

10 13.0 17.0 23.0 0.0

15 17.0 23.0 31.0

20 23.0 31.0

25 31.0

Newton forward difference table:

x f(x) df 2f d3f d4f d5f


0 6.0 10.0 13.0 17.0 0.0 0.0

5 10.0 13.0 17.0 23.0 0.0

10 13.0 17.0 23.0 31.0

15 17.0 23.0 31.0

20 23.0 31.0

25 31.0

Newton forward difference table:

x f(x) df 2f d3f d4f d5f

0 6.0 10.0 13.0 17.0 23.0 0.0

5 10.0 13.0 17.0 23.0 31.0

10 13.0 17.0 23.0 31.0

15 17.0 23.0 31.0

20 23.0 31.0

25 31.0

Newton forward difference table:

x f(x) df 2f d3f d4f d5f

0 6.0 10.0 13.0 17.0 23.0 31.0

5 10.0 13.0 17.0 23.0 31.0

10 13.0 17.0 23.0 31.0

15 17.0 23.0 31.0

20 23.0 31.0

25 31.0

(2) ) Construct a forward difference table of the following values


X 5 10 15 20 25 30

Y 9962 9848 9659 9397 9065 9660

import numpy as np
def difference_table(x,y,table):
n=len(x)
table[ :,0]=y
for colm in range(1,n):
for row in range(n-colm):
table[row][colm]=table[row+1][colm-1]
print("\n Newton forward difference table:")
print("x\tf(x)\tdf\t2f\td3f\d4f\td5f")
print(" ")
for row in range(n):
print(x[row],end="\t")
for colm in range(n-row):
print(table[row][colm],end="\t")
print()
x=[5,10,15,20,25,30]
y=[9962,9848,9659,9397,9065,9660]
n=len(x)
table=[Link]((n,n))
difference_table(x,y,table)
Output:
Newton forward difference table:

x f(x) df 2f d3f\d4fd5f

5 9962.0 9848.0 0.0 0.0 0.0 0.0

10 9848.0 9659.0 0.0 0.0 0.0

15 9659.0 9397.0 0.0 0.0


20 9397.0 9065.0 0.0

25 9065.0 9660.0

30 9660.0

Newton forward difference table:

x f(x) df 2f d3f\d4fd5f

5 9962.0 9848.0 9659.0 0.0 0.0 0.0

10 9848.0 9659.0 9397.0 0.0 0.0

15 9659.0 9397.0 9065.0 0.0

20 9397.0 9065.0 9660.0

25 9065.0 9660.0

30 9660.0

Newton forward difference table:

x f(x) df 2f d3f\d4fd5f

5 9962.0 9848.0 9659.0 9397.0 0.0 0.0

10 9848.0 9659.0 9397.0 9065.0 0.0

15 9659.0 9397.0 9065.0 9660.0

20 9397.0 9065.0 9660.0

25 9065.0 9660.0

30 9660.0

Newton forward difference table:

x f(x) df 2f d3f\d4fd5f

5 9962.0 9848.0 9659.0 9397.0 9065.0 0.0


10 9848.0 9659.0 9397.0 9065.0 9660.0

15 9659.0 9397.0 9065.0 9660.0

20 9397.0 9065.0 9660.0

25 9065.0 9660.0

30 9660.0

Newton forward difference table:

x f(x) df 2f d3f\d4fd5f

5 9962.0 9848.0 9659.0 9397.0 9065.0 9660.0

10 9848.0 9659.0 9397.0 9065.0 9660.0

15 9659.0 9397.0 9065.0 9660.0

20 9397.0 9065.0 9660.0

25 9065.0 9660.0

30 9660.0

(3) ) Construct a forward difference table of the following values

X 10 15 20 25 30 35

Y 19.97 21.51 22.47 23.52 24.65 25.89

import numpy as np
def difference_table(x,y,table):
n=len(x)
table[ :,0]=y
for colm in range(1,n):
for row in range(n-colm):
table[row][colm]=table[row+1][colm-1]
print("\n Newton forward difference table:")
print("x\tf(x)\tdf\t2f\td3f\d4f\td5f")
print(" ")
for row in range(n):
print(x[row],end="\t")
for colm in range(n-row):
print(table[row][colm],end="\t")
print()
x=[10,15,20,25,30,35]
y=[19,21,22,23,24,25]
n=len(x)
table=[Link]((n,n))
difference_table(x,y,table)
Output:
Newton forward difference table:

x f(x) df 2f d3f\d4fd5f

10 19.0 21.0 0.0 0.0 0.0 0.0

15 21.0 22.0 0.0 0.0 0.0

20 22.0 23.0 0.0 0.0

25 23.0 24.0 0.0

30 24.0 25.0

35 25.0

Newton forward difference table:

x f(x) df 2f d3f\d4fd5f

10 19.0 21.0 22.0 0.0 0.0 0.0

15 21.0 22.0 23.0 0.0 0.0

20 22.0 23.0 24.0 0.0

25 23.0 24.0 25.0

30 24.0 25.0

35 25.0
Newton forward difference table:

x f(x) df 2f d3f\d4fd5f

10 19.0 21.0 22.0 23.0 0.0 0.0

15 21.0 22.0 23.0 24.0 0.0

20 22.0 23.0 24.0 25.0

25 23.0 24.0 25.0

30 24.0 25.0

35 25.0

Newton forward difference table:

x f(x) df 2f d3f\d4fd5f

10 19.0 21.0 22.0 23.0 24.0 0.0

15 21.0 22.0 23.0 24.0 25.0

20 22.0 23.0 24.0 25.0

25 23.0 24.0 25.0

30 24.0 25.0

35 25.0

Newton forward difference table:

x f(x) df 2f d3f\d4fd5f

10 19.0 21.0 22.0 23.0 24.0 25.0

15 21.0 22.0 23.0 24.0 25.0

20 22.0 23.0 24.0 25.0

25 23.0 24.0 25.0


30 24.0 25.0

35 25.0

Q.2 Newtons backword difference table


(1) ) Construct a backword difference table of the following values

X 0 5 10 15 20 25

Y 6 10 13 17 23 31

import numpy as np
def backword_difference_table(x,y,table):
n=len(x)
table[ :,0]=y
for colm in range(1,n):
for row in range(n-colm):
table[row][colm]=table[row][colm-1]-table[row-1][colm-1]
print("\n Newton backword difference table:")
print("x\tf(x)\tdf\t2f\td3f\d4f\td5f")
print(" ")
for row in range(n):
print(x[row],end="\t")
for colm in range(row+1):
print(table[row][colm],end="\t")
print()
x=[0,5,10,15,20,25]
y=[6,10,13,17,23,31]
n=len(x)
table=[Link]((n,n))
backword_difference_table(x,y,table)
Output:
Newton backword difference table:

x f(x) df 2f d3f\d4fd5f
0 6.0

5 10.0 4.0

10 13.0 3.0 0.0

15 17.0 4.0 0.0 0.0

20 23.0 6.0 0.0 0.0 0.0

25 31.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f\d4fd5f

0 6.0

5 10.0 4.0

10 13.0 3.0 -1.0

15 17.0 4.0 1.0 0.0

20 23.0 6.0 0.0 0.0 0.0

25 31.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f\d4fd5f

0 6.0

5 10.0 4.0

10 13.0 3.0 -1.0

15 17.0 4.0 1.0 0.0

20 23.0 6.0 0.0 0.0 0.0

25 31.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:


x f(x) df 2f d3f\d4fd5f

0 6.0

5 10.0 4.0

10 13.0 3.0 -1.0

15 17.0 4.0 1.0 0.0

20 23.0 6.0 0.0 0.0 0.0

25 31.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f\d4fd5f

0 6.0

5 10.0 4.0

10 13.0 3.0 -1.0

15 17.0 4.0 1.0 0.0

20 23.0 6.0 0.0 0.0 0.0

25 31.0 0.0 0.0 0.0 0.0 0.0

(2) Construct a backword difference table of the following values

X 5 10 15 20 25 30

Y 9962 9848 9659 9397 9065 9660

import numpy as np
def backword_difference_table(x,y,table):
n=len(x)
table[ :,0]=y
for colm in range(1,n):
for row in range(n-colm):
table[row][colm]=table[row][colm-1]-table[row-1][colm-1]
print("\n Newton backword difference table:")
print("x\tf(x)\tdf\t2f\td3f\d4f\td5f")
print(" ")
for row in range(n):
print(x[row],end="\t")
for colm in range(row+1):
print(table[row][colm],end="\t")
print()
x=[5,10,15,20,25,30]
y=[9962,9848,9659,9397,9065,9660]
n=len(x)
table=[Link]((n,n))
backword_difference_table(x,y,table)
Output:
Newton backword difference table:

x f(x) df 2f d3f\d4fd5f

5 9962.0

10 9848.0 -114.0

15 9659.0 -189.0 0.0

20 9397.0 -262.0 0.0 0.0

25 9065.0 -332.0 0.0 0.0 0.0

30 9660.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f\d4fd5f

5 9962.0

10 9848.0 -114.0
15 9659.0 -189.0 -75.0

20 9397.0 -262.0 -73.0 0.0

25 9065.0 -332.0 0.0 0.0 0.0

30 9660.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f\d4fd5f

5 9962.0

10 9848.0 -114.0

15 9659.0 -189.0 -75.0

20 9397.0 -262.0 -73.0 0.0

25 9065.0 -332.0 0.0 0.0 0.0

30 9660.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f d4f d5f

5 9962.0

10 9848.0 -114.0

15 9659.0 -189.0 -75.0

20 9397.0 -262.0 -73.0 0.0

25 9065.0 -332.0 0.0 0.0 0.0

30 9660.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f d4f d5f


5 9962.0

10 9848.0 -114.0

15 9659.0 -189.0 -75.0

20 9397.0 -262.0 -73.0 0.0

25 9065.0 -332.0 0.0 0.0 0.0

30 9660.0 0.0 0.0 0.0 0.0 0.0

(3) Construct a backword difference table of the following values

X 10 15 20 25 30 35

Y 19.97 21.51 22.47 23.52 24.65 25.89

import numpy as np
def backword_difference_table(x,y,table):
n=len(x)
table[ :,0]=y
for colm in range(1,n):
for row in range(n-colm):
table[row][colm]=table[row][colm-1]-table[row-1][colm-1]
print("\n Newton backword difference table:")
print("x\tf(x)\tdf\t2f\td3f\d4f\td5f")
print(" ")
for row in range(n):
print(x[row],end="\t")
for colm in range(row+1):
print(table[row][colm],end="\t")
print()
x=[10,15,20,25,30,35]
y=[19,21,22,23,24,25]
n=len(x)
table=[Link]((n,n))
backword_difference_table(x,y,table)
Output:
Newton backword difference table:

x f(x) df 2f d3f d4f d5f

10 19.0

15 21.0 2.0

20 22.0 1.0 0.0

25 23.0 1.0 0.0 0.0

30 24.0 1.0 0.0 0.0 0.0

35 25.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f d4f d5f

10 19.0

15 21.0 2.0

20 22.0 1.0 -1.0

25 23.0 1.0 0.0 0.0

30 24.0 1.0 0.0 0.0 0.0

35 25.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f d4f d5f

10 19.0

15 21.0 2.0

20 22.0 1.0 -1.0

25 23.0 1.0 0.0 0.0


30 24.0 1.0 0.0 0.0 0.0

35 25.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f d4f d5f

10 19.0

15 21.0 2.0

20 22.0 1.0 -1.0

25 23.0 1.0 0.0 0.0

30 24.0 1.0 0.0 0.0 0.0

35 25.0 0.0 0.0 0.0 0.0 0.0

Newton backword difference table:

x f(x) df 2f d3f d4f d5f

10 19.0

15 21.0 2.0

20 22.0 1.0 -1.0

25 23.0 1.0 0.0 0.0

30 24.0 1.0 0.0 0.0 0.0

35 25.0 0.0 0.0 0.0 0.0 0.0

You might also like