Final Lab Manual Python (2)
Final Lab Manual Python (2)
Dept. No.
Certified Bonafide Record of Practical work done
by Mr………………………………………………in the physics
Laboratory, Loyola College, Chennai -34, during the Academic
year .........................
___________
Professor
Examiners:
1.
2.
TABLE OF CONTENTS
1.
2.
3.
4.
5.
6.
7.
8.
Professor
UPH 6705 COMPUTATIONAL PHYSICS AND WORKSHOP
TECHNOLOGY
COMPUTATIONAL PHYSICS
List of Experiments:
1. Python programming – simple arithmetic operations I
2. Python programming – simple arithmetic operations II
3. Python programming – arrays
4. Python programming – matrices
5. Python programming – solving differential equations
6. Python programming – Trapezoidal and Simpson Rule
7. Python programming – Lagrange’s interpolation method
8. Programming – Newton’ Raphson method Python
WORKSHOP TECHNOLOGY
List of Experiments:
9. Shaping to the planned dimensions.
10. Cutting and bending.
11. Using drilling machines for various requirements.
12. Different types of welding.
13.Boxes of different sizes.
14. Wood work.
15. Experiments using lathe.
16.House wiring.
COMPUTATIONAL PHYSICS
1. Python Programming – Simple Arithmetic
Operations I
Aim:
To write, enter and execute a python program to perform simple arithmetic
operations.
ARITHMETIC OPERATION:
1. To execute the simple arithmetic operations.
num1 = float(input("enter the first number"))
num2 = float(input("enter the second number"))
# Add two numbers
sum = float(num1) + float(num2)
# Subtract two numbers
diff = float(num1) - float(num2)
# Multiply two numbers
prod = float(num1) * float(num2)
#Divide two numbers
div = float(num1) / float(num2)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
# Display the subtraction
print('The subtraction of {0} and {1} is {2}'.format(num1, num2, diff))
#Display the multiplication
print('The multiplication of {0} and {1} is {2}'.format(num1, num2, prod))
# Display the division
print('The division of {0} and {1} is {2}'.format(num1, num2, div))
Output
Enter the first number4
Enter the second number5
Output:
16
Output:
16.0
Output:
0.0
2.0
1.8708286933869
4. To calculate the sum of the digits
Output:
Enter a number 45
The total sum of digits is: 9
Result:
The above programs have been entered and the required output was obtained.
Staff Signature
2. Python Programming – Simple Arithmetic Operations II
Aim:
To write, enter and execute a python program to perform simple
arithmetic operations.
1. To find the factorial of a number provided by the user
#Python program to find the factorial of a number provided by the
user.
#To take input from the user
# num = int(input(“Enter a number:”))
factorial = 1
#Check if the number is negative, positive ort zero
if num<0:
print(“ Factorial does not exist for negative numbers”)
elif num ==0:
print (“The factorial of 0 is 1”)
else:
for i in range(1,num+1):
factorial=factorial * i
print(“The factorial of “, num, “is”, factorial)
Output:
Enter a number 3
The factorial of 3 is 1
The factorial of 3 is 2
The factorial of 3 is 6
2. To find the largest among three numbers
# Python program to find the largest number among the three input numbers
# take three numbers from user
num1 =float(input("Enter first number: "))
num2 =float(input("Enter second number: "))
num3 =float(input("Enter third number: "))
if(num1 > num2)and(num1 > num3):
largest = num1
elif(num2 > num1)and(num2 > num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
Output:
Output:
Largest element is: 99
4. Using loops to find the largest among a number in a list.
# Python program to find largest
# number in a list
# creating empty list
list1 =[ ]
# asking number of elements to put in list
num =int(input("Enter number of elements in list: "))
# iterating till num to append elements in list
for i in range(1, num +1):
else =int(input("Enter elements: "))
list1.append(ele)
# print maximum element
print("Largest element is:", max(list1))
Output:
Enter number of elements in list: 4
Enter elements: 12
Enter elements: 19
Enter elements: 1
Enter elements: 99
Largest element is: 99
Result:
The above programs have been entered and the required output was obtained.
Staff Signature
3. Python Programming – Arrays
Aim:
To write, enter and execute a python program to perform simple operations in an
array.
1. To create an array
Syntax
# This will sort the given list in ascending order.
# It returns a sorted list according to the passed parameter.
List_name.sort()
This function can be used to sort list of integers, floating point number, string
and others.
# List of Integers
numbers =[1, 3, 4, 2]
# Sorting list of Integers
numbers.sort()
print(numbers)
# List of Floating point numbers
Decimal number =[2.01, 2.00, 3.67, 3.28, 1.68]
# Sorting list of Floating point numbers
Decimal number.sort()
print(decimalnumber)
# List of strings
words =["I ", "love", "loyola"]
# Sorting list of strings
words.sort()
print(words)
Output:
[1, 2, 3, 4]
[1.68, 2.0, 2.01, 3.28, 3.67]
['I', 'love', 'loyola']
1. Python program to sort the elements of an array in ascending order
#Initialize array
temp = 0;
if(arr[i] >arr[j]):
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
print();
Output:
Elements of original array:
5 2 8 7 1 3 13 64 23 15 48 39 9
Elements of array sorted in ascending order:
1 2 3 5 7 8 9 13 15 23 39 48 64
# List of Integers
numbers =[1, 3, 4, 2]
# Sorting list of Integers
numbers.sort(reverse=True)
print(numbers)
# List of Floating point numbers
Decimal number =[2.01, 2.00, 3.67, 3.28, 1.68]
# Sorting list of Floating point numbers
Decimal number.sort(reverse=True)
print(decimal number)
# List of strings
words =["Geeks", "For", "Geeks"]
# Sorting list of strings
words.sort(reverse=True)
print(words)
Output:
[4, 3, 2, 1]
[3.67, 3.28, 2.01, 2.0, 1.68]
['Geeks', 'Geeks', 'For']
Result:
The above array programs has been entered, executed and the output was obtained.
Staff Signature
4. Python Programming – Matrices
Aim :
To write, enter and execute a python program to add, multiply and transpose
matrices.
1. Python Program to Add Two Matrices
X = [[1,2,3],
[4,5,6],
[7,8,9]]
Y = [[11,12,13],
[14,15,16],
[17,18,19]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
# iterate through rows
for i in range(len(X)):
# iterate through columns
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
Output:
[12, 14, 16]
[18, 20, 22]
[24, 26, 28]
2. Python Program to Multiply Two Matrices
X = [[1,2,3],
[4,5,6],
[7,8,9]]
Y = [[11,12,13],
[14,15,16],
[17,18,19]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
Output:
X = [[3,4],
[6,5],
[7,9]]
result = [[0,0,0],
[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[j][i] = X[i][j]
for r in result:
print(r)
Output:
[3, 6, 7]
[4, 5, 9]
Result:
The python program to add, multiply and to transpose matrices have been entered,
executed and the output was obtained.
Staff Signature
5. Python programming – solving differential equations
Aim:
Example :
Consider below differential equation
dy/dx = (x + y + xy)
with initial condition y(0) = 1
and step size h = 0.025.
Find y(0.1).
Solution:
f(x, y) = (x + y + xy)
x0 = 0, y0 = 1, h = 0.025
Now we can calculate y1 using Euler formula
y1 = y0 + h * f(x0, y0)
y1 = 1 + 0.025 *(0 + 1 + 0 * 1)
y1 = 1.025
y(0.025) = 1.025.
Similarly we can calculate y(0.050), y(0.075), ....y(0.1).
y(0.1) = 1.11167
Program:
Output :
Result:
Euler’s method of solving differential equations using a python program has been
entered, executed and the output was obtained.
Staff Signature
6. Python Programming – Trapezoidal And Simpson Rule
Aim :
1. Trapezoidal Method
#Trapezoidal Method
# Define function to integrate
def f(x):
return 1/(1 + x**2)
# Implementing trapezoidal method
def trapezoidal(x0,xn,n):
# calculating step size
h = (xn - x0) / n
# Finding sum
integration = f(x0) + f(xn)
for i in range(1,n):
k = x0 + i*h
integration = integration + 2 * f(k)
# Finding final integration value
integration = integration * h/2
return integration
# Input section
lower_limit = float(input("Enter lower limit of integration: "))
upper_limit = float(input("Enter upper limit of integration: "))
sub_interval = int(input("Enter number of sub intervals: "))
# Call trapezoidal() method and get result
result = trapezoidal(lower_limit, upper_limit, sub_interval)
print("Integration result by Trapezoidal method is: %0.6f" % (result) )
Output
Output of above Trapezoidal method Python program is:
Enter lower limit of integration: 0
Enter upper limit of integration: 1
Enter number of sub intervals: 6
Integration result by Trapezoidal method is: 0.784241
2. Simpson's 1/3 Rule
Result:
The python programs for trapezoidal and Simpson’s 1/3 rd rule has been
entered,executed and the result was obtained.
Staff Signature:
7. Python Programming – Lagrange’s Interpolation Method
Aim:
To enter and execute Lagrange’s interpolation using python programs.
# Lagrange Interpolation
# Importing NumPy Library
import numpy as np
# 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))
# Reading data points
print('Enter data for x and y: ')
for i in range(n):
x[i] = float(input( 'x['+str(i)+']='))
y[i] = float(input( 'y['+str(i)+']='))
# Reading interpolation point
xp = float(input('Enter interpolation point: '))
# Set interpolated value initially to zero
yp = 0
# Implementing Lagrange Interpolation
for i in range(n):
p=1
for j in range(n):
if i != j:
p = p * (xp - x[j])/(x[i] - x[j])
yp = yp + p * y[i]
# Displaying output
print('Interpolated value at %.3f is %.3f.' % (xp, yp))
Output: Language Interpolation
Enter number of data points: 5
Enter data for x and y:
x[0]=5
y[0]=150
x[1]=7
y[1]=392
x[2]=11
y[2]=1452
x[3]=13
y[3]=2366
x[4]=17
y[4]=5202
Enter interpolation point: 9
Interpolated value at 9.000 is 810.000.
Result:
Lagrange interpolation using python programs has been entered, executed and
the output was obtained.
Staff Signature
8. Programming – Newton’ Raphson Method Python
Aim:
To enter and execute Newton Raphson method using python programs.
if step > N:
flag = 0
break
condition = abs(f(x1)) > e
if flag==1:
print('\nRequired root is: %0.8f' % x1)
else:
print('\nNot Convergent.')
# Input Section
x0 = input('Enter Guess: ')
e = input('Tolerable Error: ')
N = input('Maximum Step: ')
# Converting x0 and e to float
x0 = float(x0)
e = float(e)
# Converting N to integer
N = int(N)
#Note: You can combine above three section like this
# x0 = float(input('Enter Guess: '))
# e = float(input('Tolerable Error: '))
# N = int(input('Maximum Step: '))
# Starting Newton Raphson Method
newtonRaphson(x0,e,N)
Result:
The above program has been entered, executed and the output was obtained.
Staff Signature