Computational Physics 5 TH Sem
Computational Physics 5 TH Sem
COMPUTATIONAL PHYSICS
SULAIMAN .MK
Assistant Professor of Physics
Govt. College Malappuram
Module-1:
Introduction to
Python Programming
16 Hours
Errors are Displayed after Entire Program Errors are Displayed for Every
is checked Instruction Interpreted
Eg: C, Fortran Eg:
Sulaiman M.K, Assistant Professor of
Malappuram
Physics,Basic,
Govt. CollegePython
8
Module-1:
Introduction to
Python Programming
(Lecture-3)
18 Hours
Note:- To read any data as itself using input ( ) function, use eaval ( )
Sulaiman M.K, Assistant Professor of Physics, Govt. College
2
Malappuram
Output Functions
• Output of a program may number, text or graphics
• print ( ) statement for numbers and texts
• show( ), imshow( ),savefig( ) etc. for graphics
!= Not equal x != y
18 Hours
• Can be sliced
>>> a1[1:8:2]
'otCl'
Sulaiman M.K, Assistant Professor of Physics, Govt. College
3
Malappuram
• Can be added (+ concatenation)
>>> a='govt '
>>> b= 'college'
>>> a+b
'govt college'
• Multiplied (* repetition)
>>> a*3
'govt govt govt '
• len( ) will give number of elements
>>> len(b)
7
• Membership can be checked using in, not in
>>> 'c' in b
True
Sulaiman M.K, Assistant Professor of Physics, Govt. College
4
Malappuram
Module-1:
Introduction to
Python Programming
(Lecture-6)
18 Hours
• Can be indexed
• Can be sliced
• Can concatenate by +
• Repetition possible by *
>>>str(a)
“{1: ‘Govt', 2: ‘aided', 3: ‘self ’}”
3)type (dict)
s=set([1,2,3,4,’a’]) g=set([1,2,3,4,5,7,’b’,’c’,’a’])
• Pickle module provides functions for storing any kind of data type
• Important functions used are open ( ), close ( ), load( ), dump ( ) etc.
>>> a=array([[1,2,3],[1,3,5]])
>>> a
array([[1, 2, 3],
[1, 3, 5]])
>>> arange(2,50,5,dtype=float)
array([ 2., 7., 12., 17., 22., 27., 32., 37., 42., 47.])
>>> arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> arange(1,10,1.5)
array([ 1. , 2.5, 4. , 5.5, 7. , 8.5])
• >>> identity(3,dtype=int)
• array([[1, 0, 0],
• [0, 1, 0],
• [0, 0, 1]])
Sulaiman M.K.
Assistant Professor
Department of Physics, Govt. College Malappuram
= e12+ e2 2+ e3 2+ e4 2+……………+ en 2
• Now we will minimize the S
Straight Line Fitting
• For straight line fitting, we take the equation of the straight line as f(x)
i.e. f(x)=a0+a1x
• We chose in this form because we can easily then extend to higher order polynomial
by adding higher terms
• The sum of squares of errors when fitting this expression on the data (xi,yi),
i=1,2,3….n will be
S=[y1-f(x1)]2+[y2-f(x2)]2+[y3-f(x3)]2+…………+[yn-f(xn)]2
=[y1-(a0+a1x1)]2+[y2-(a0+a1x2)]2+[y3-(a0+a1x3)]2+…………+[yn-(a0+a1xn)]2
𝝏𝑺 𝝏𝑺
• To minimize S, we set =𝟎 ; = 𝟎 and find the coefficients a0 and a1
𝝏a 0 𝝏a 𝟏
𝝏𝑺
= −2[y1−(a0+a1x1)]−2[y2−(a0+a1x2)]−2[y3−(a0+a1x3)]−…………−2[yn−(a0+a1xn)]
𝝏a 0
=-2σ𝑛𝑖=1[𝑦𝑖 − (𝑎0 + 𝑎1 𝑥𝑖 )]
=-2[(y1+y2+…yn)-(𝑎0 +𝑎0 +….+𝑎0 )-a1(x1+x2+….+xn)]=0
Similarly
𝝏𝑺
= −2x1[y1−(a0+a1x1)]−2x2[y2−(a0+a1x2)]−2x3[y3−(a0+a1x3)]−…………−2xn[yn−(a0+a1xn)]
𝝏a𝟏
=-2[(x1y1+x2y2+…+xnyn)-𝑎0 (x1+x2+….+xn)-a1(x12+x22+….+xn2)]=0
i.e. 𝑎0 (x1+x2+….+xn)+a1(x12+x22+….+xn2)=(x1y1+x2y2+…+xnyn)
n𝑎0 +a1(x1+x2+….+xn)=(y1+y2+…yn)
𝑛𝑎0 + 𝑎1 𝑥𝑖 = 𝑦𝑖
𝑎0 (x1+x2+….+xn)+a1(x12+x22+….+xn2)=(x1y1+x2y2+…+xnyn)
2
𝑎0 𝑥𝑖 +𝑎1 𝑥𝑖 = 𝑥𝑖 𝑦𝑖
(1,4.5),(2,7.8),(5,10.8),(8,15),(10,20)
LEAST SQUARES CURVE FIT
LINEAR FIT ILLUSTRATION
WITH PYTHON
SULAIMAN MK
Assistant Professor of Physics
Govt. College Malappuram
• Also write the python code to solve the above and plot the
fitted curve
Linear Least Square Curve Fit Equations
𝑛𝑎0 + 𝑎1 𝑥𝑖 = 𝑦𝑖
2
𝑎0 𝑥𝑖 +𝑎1 𝑥𝑖 = 𝑥𝑖 𝑦𝑖
σ 𝑦𝑖 σ 𝑥𝑖 𝑛 σ 𝑦𝑖
2
σ 𝑥𝑖 𝑦𝑖 σ 𝑥𝑖 σ 𝑥𝑖 σ 𝑥𝑖 𝑦𝑖
𝑎0 = and 𝑎1 =
𝑛 σ 𝑥𝑖 𝑛 σ 𝑥𝑖
2 2
σ 𝑥𝑖 σ 𝑥𝑖 σ 𝑥𝑖 σ 𝑥𝑖
Python Produced Table
Python Program For Printing Data Table
#printing table least squares fit
x=array([1,2,5,8,10])
y=array([4.5,7.8,10.8,15,20])
print ('-’*70)
print ('%20s\t%10s\t%10s\t%10s'%( 'x', 'y','x*x','x*y’))
print ('-’*70)
for i in range(5):
print ('%20.2f\t%10.2f\t%10.2f\t%10.2f'%(x[i],y[i],x[i]**2,x[i]*y[i]))
print ('-’*70)
print ('sum=%10.2f\t%10.2f\t%10.2f\t%10.2f'%(sum(x),sum(y),sum(x**2),sum(x*y)))
print ('-’*70)
Python Produced Table
Python Code for Solution
from pylab import *
x=array([1,2,5,8,10])
y=array([4.5,7.8,10.8,15,20])
d=array([len(x),sum(x),sum(x),sum(x**2)]).reshape(2,2)
d1=array([sum(y),sum(x),sum(x*y),sum(x**2)]).reshape(2,2)
d2=array([len(x),sum(y),sum(x),sum(x*y)]).reshape(2,2)
from numpy.linalg import *
a0=det(d1)/det(d)
a1=det(d2)/det(d)
print ('y=',a0,'+',a1,'x’) y= 3.4857142857142853 + 1.5642857142857152 x
plot(x,y,'*')
f=a0+a1*x
plot(x,f)
xlabel('x values')
ylabel('y values')
title('Least Squares Linear Fit')
show( )
y= 3.4857142857142853 + 1.5642857142857152 x
BISECTION METHOD
WITH PYTHON CODE
SULAIMAN MK
Assistant Professor of Physics
Govt. College Malappuram
ROOT OF A FUNCTION?
• Root of a function f(x) means the value (or values) of x for which f(x)=0
Method
• We reduce the root containing interval to so small as that we need
the accuracy
• i.e. we bisect the interval, then find the root containing interval
from the two new intervals and proceed like this
Steps
• Find a and b such that f(a) and f(b) are of opposite signs
(we assume f(a) is –ve and f(b) is +ve for the convenience )
• Now root lies between a and b
(𝒂+𝒃)
• Find 𝒙𝟎 = and check whether f(𝒙𝟎 ) is +ve or –ve
𝟐
• If f(𝒙𝟎 ) is +ve, then f(𝒙𝟎 ) and f(a) are of opposite signs
• Hence root lies between a and 𝒙𝟎
• This continues till the interval reduces to our desired accuracy
Example
Problem: Find one of the roots of the equation x3-x-1=0 correct to
4 decimal points
Solution:
• Initially we find two points a and b, for which f(a) and f(b) are of
opposite signs
• We select a=1; f(a)=f(1)=-1 and b=3; f(b)=f(3)=23
(𝒂+𝒃) (𝟏+𝟑)
• Next find 𝒙𝟎 = = = 𝟐; f(2)=5
𝟐 𝟐
• So now root lies between a=1 and 𝒙𝟎 = 𝟐
• i.e. we replace b by 𝒙𝟎 or new a=1 and b=2
• And proceeds till the interval reduces to 0.00001 or less
----------------------------------------------------------------------------------------------------
n a b x0 f(x0)
----------------------------------------------------------------------------------------------------
0 1.00000000 3.00000000 2.00000000 5.00000000
1 1.00000000 2.00000000 1.50000000 0.87500000
2 1.00000000 1.50000000 1.25000000 -0.29687500
3 1.25000000 1.50000000 1.37500000 0.22460938
4 1.25000000 1.37500000 1.31250000 -0.05151367
5 1.31250000 1.37500000 1.34375000 0.08261108
6 1.31250000 1.34375000 1.32812500 0.01457596
7 1.31250000 1.32812500 1.32031250 -0.01871061
8 1.32031250 1.32812500 1.32421875 -0.00212795
9 1.32421875 1.32812500 1.32617188 0.00620883
10 1.32421875 1.32617188 1.32519531 0.00203665
11 1.32421875 1.32519531 1.32470703 -0.00004659
12 1.32470703 1.32519531 1.32495117 0.00099479
13 1.32470703 1.32495117 1.32482910 0.00047404
14 1.32470703 1.32482910 1.32476807 0.00021371
15 1.32470703 1.32476807 1.32473755 0.00008355
16 1.32470703 1.32473755 1.32472229 0.00001848
17 1.32470703 1.32472229 1.32471466 -0.00001406
----------------------------------------------------------------------------------------------------
the root=1.3247
----------------------------------------------------------------------------------------------------
Python for Bisection Method
#Bisection method
while 1:
a,b=input('enter the interval a,b:')
def f(x):
return x**3-x-1
if f(a)*f(b)<0.0:
print'-'*100
print'\nn \ta\t \tb\t \tx\t \tf(x)'
print'-'*100
i=0
while abs(a-b)>=0.00001:
x=(a+b)/2.0
print'%d\t%.8f\t%.8f\t%.8f\t%.8f'%(i,a,b,x,f(x))
i=i+1
if f(x)*f(a)>0.0:
a=x
else:
b=x
print'-'*100
print'\n the root=%.4f'%a
print'-'*100
break
else:
print'no roots in this interval'
print'enter another interval'
----------------------------------------------------------------------------------------------------
n a b x0 f(x0)
----------------------------------------------------------------------------------------------------
0 1.00000000 3.00000000 2.00000000 5.00000000
1 1.00000000 2.00000000 1.50000000 0.87500000
2 1.00000000 1.50000000 1.25000000 -0.29687500
3 1.25000000 1.50000000 1.37500000 0.22460938
4 1.25000000 1.37500000 1.31250000 -0.05151367
5 1.31250000 1.37500000 1.34375000 0.08261108
6 1.31250000 1.34375000 1.32812500 0.01457596
7 1.31250000 1.32812500 1.32031250 -0.01871061
8 1.32031250 1.32812500 1.32421875 -0.00212795
9 1.32421875 1.32812500 1.32617188 0.00620883
10 1.32421875 1.32617188 1.32519531 0.00203665
11 1.32421875 1.32519531 1.32470703 -0.00004659
12 1.32470703 1.32519531 1.32495117 0.00099479
13 1.32470703 1.32495117 1.32482910 0.00047404
14 1.32470703 1.32482910 1.32476807 0.00021371
15 1.32470703 1.32476807 1.32473755 0.00008355
16 1.32470703 1.32473755 1.32472229 0.00001848
17 1.32470703 1.32472229 1.32471466 -0.00001406
----------------------------------------------------------------------------------------------------
the root=1.3247
----------------------------------------------------------------------------------------------------
NEWTON-RAPHSON METHOD
WITH PYTHON CODE
SULAIMAN MK
Assistant Professor of Physics
Govt. College Malappuram
Newton-Raphson Method
• Let x0 be an approximation root of f(x) =0 and let x1=x0+h be the correct root, i.e. f(x1)=0.
• We can expand f(x0+h) by Taylor series,
f(x0 +h)=f(x0)+hf ' (x0)+(h2 /2)f '' (x0)+…… =0
• Neglecting the second and higher order derivatives
Therefore f(x0 +h)=f(x0)+hf ' (x0)=0 ; hence h = - [f(x0)/ f ' (x0)]
𝒇 𝒙𝟎
𝒙𝟏 = 𝒙𝟎 −
𝒇 ′ 𝒙𝟎
𝒙𝟐 = 𝒙𝟏 − [𝒇(𝒙𝟏)/ 𝒇 ′ (𝒙𝟏)]
………………….
𝒙𝒏+𝟏 = 𝒙𝒏 − [𝒇(𝒙𝒏)/ 𝒇 ‘(𝒙𝒏)]
Convergence Condition