(io(k),k=i,1,-1),'.',(ioc(k),k=1,j) stop
end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 22 From Binary to Decimal:
Theory:
To convert a binary number: List the powers of 2 from right to left. Start at 20, next increase the exponent by 1 for each power. Stop when the amount of elements in the list is equal to the amount of digits in the binary number. For example, the number 10011 has 5 digits, so the corresponding decimal number = = 19
Algorithm:
1. Input: Binary number, total number of digits
2. Define the binary number as a ‘character’ so as to read each digit in it.
3. Find each digit of the number (either 0 or 1) and its position (n) counted from right to left.
4. The digit (0 or 1) found from step 3 is multiplied by 5. Obtain the sum in a do-loop.
6. Write the sum which is the decimal number.
FORTRAN Program:
C
C From Binary to Decimal C
write(*,*)'Give a Binary Number' read(*,*)number
write(*,*)'Total Number of Digits?' read(*,*)n
nsum=0 do i=1,n
nd=mod(number,10) number=number/10.
nsum=nsum+nd*2**(i-1) enddo
write(*,*)'Decimal Number = ',nsum stop
end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 23 From Octal to Decimal:
Theory:
To convert a octal number: List the powers of 8 from right to left. Start at 80, next increase the exponent by 1 for each power. Stop when the amount of elements in the list is equal to the amount of digits in the binary number. For example, the number 234 has 3 digits, so the corresponding decimal number = = 156
Algorithm:
1. Input: Octal number, total number of digits
2. Define the Octal number as a ‘character’ so as to read each digit in it.
3. Find each digit of the number (0, 1, 2, 3, 4, 5, 6 or 7) and its position (n) counted from right to left.
4. The digit (0 to 7) found from step 3 is multiplied by 5. Obtain the sum in a do-loop.
6. Write the sum which is the decimal number.
FORTRAN Program:
C
C From Octal to Decimal C
write(*,*)'Give an Octal Number' read(*,*)number
write(*,*)'Total Number of Digits?' read(*,*)n
nsum=0 do i=1,n
nd=mod(number,10) number=number/10.
nsum=nsum+nd*8**(i-1) enddo
write(*,*)'Decimal Number = ',nsum stop
end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 24 Matrices
Addition of Two Matrices:
Theory:
For the addition of two matrices, the rows and columns of the two matrices have to be equal.
The th element of a matrix is added with the same th element of another to obtain the th element of the new matrix: .
Algorithm:
1. Input and output Matrix elements are stored in memory.
2. Input: Dimension of Matrices, Matrix elements 3. Do loop for two indices, i and j, loops are nested.
4. The -th element of a matrix is added with the same th element of another to obtain the th element of the new matrix: .
5. Write down the matrix elements with implied do loop.
FORTRAN Program:
C Addition of Two Matrices C
dimension a(10,10),b(10,10),c(10,10) write(*,*)'Rows & Columns of Matrices' read(*,*)m,n
write(*,*)'Matrix elements of A?' do i=1,m
read(*,*)(a(i,j),j=1,n) enddo
write(*,*)'Matrix elements of B?' do i=1,m
read(*,*)(b(i,j),j=1,n) enddo
do i=1,m do j=1,n
c(i,j)= a(i,j)+b(i,j) enddo
enddo
Write(*,*)'The added Matrix' do i=1,m
write(*,*)(c(i,j),j=1,n) enddo
stop end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 25 Product of Two Matrices:
Theory:
∑ , for the Product of two matrices, the number of columns of the first matrix has to be the same as the number of rows of the 2nd matrix.
Algorithm:
1. Input and output Matrix elements are stored in memory.
2. Input: Dimensions of Matrices, Matrix elements 3. Do loop for three indices: i, j, k, loops are nested
4. Inside the k-loop, the th element of 1st matrix is multiplied with the th element of 2nd matrix, and then sum is taken.
5. Write down the matrix elements with implied do loop.
FORTRAN Program:
C Product of Two Matrices C
dimension a(10,10),b(10,10),c(10,10) write(*,*)'Rows & Columns of Matrix A' read(*,*)m,l
write(*,*)'Matrix elements of A?' do i=1,m
read(*,*)(a(i,j),j=1,l) enddo
write(*,*)'Rows & Columns of Matrix B' read(*,*)l,n
write(*,*)'Matrix elements of B?' do i=1,l
read(*,*)(b(i,j),j=1,n) enddo
do i=1,m do j=1,n
c(i,j)= 0.0 do k=1,l
c(i,j)= c(i,j)+a(i,k)*b(k,j) enddo
enddo enddo
Write(*,*)'The Product Matrix' Do i=1,m
write(*,*)(c(i,j),j=1,n) enddo
stop end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 26 Transpose of a Matrix:
Theory:
For the transpose of a matrix, the rows of the original matrix become the columns of the transposed matrix and vice versa. So what we do is that we read the matrix elements and find the elements of the transposed matrix:
Algorithm:
1. Matrix elements are stored in memory 2. Input: rows and columns, matrix elements 3. Do loop for i and j:
4. Transpose matrix elements written in implied do loop FORTRAN Program:
C Transpose of a Matrix C
dimension a(10,10),b(10,10)
write(*,*)'No. of rows and columns?' read(*,*)m,n
write(*,*)'Matrix elements?' do i=1,m
read(*,*)(a(i,j),j=1,n) enddo
C
do i=1,n do j=1,m
b(i,j)=a(j,i) enddo
enddo C
Write(*,*)'The Transposed Matrix' Do i=1,n
write(*,*)(b(i,j),j=1,m) enddo
stop end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 27 Numerical Analysis
Integration by Simpson’s 1/3rd Rule:
Theory:
[See APPENDIX I for the detailed theory and calculations.]
Algorithm:
1. The function is defined in a subroutine.
2. Input: Lower limit , Upper limit 3. Calculate :
4. Evaluate: , , )
5. Calculate the sum: ( ) 6. Multiply step 5 by
FORTRAN Program:
Integration by Simpson’s 1/3rd Rule (Modified):
Theory: ∫
b f(x)dx 3h f(a) 4f a2b f(b)a
( ) 4 ( ) ( ) ... ( ) 2 ( ) ( ) ... ( ) ( )
3 f x0 f x1 f x3 f xn 1 f x2 f x4 f xn 2 f xn
h
C Simpson’s 1/3 Rule C
write(*,*)'Lower limit, Upper Limit’
read(*,*)a,b h=(b-a)*0.5 x=(a+b)*0.5
sum=f(a)+4*f(x)+f(b) s=h/3*sum
write(*,*)'Value of Integral= ', s stop
end C
C Subroutine for function function f(x)
f=x**2 return end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 28 Algorithm:
1. The function is defined in a subroutine.
2. Input: Lower limit , Upper limit , sub-intervals . 3. Calculate:
4. Evaluate:
5. Do Loop over n starts. Evaluate the function: for every odd sub-interval multiply by 4, for every even sub-interval multiply by 2 and then add all of them.
6. Finally, multiply the sum by FORTRAN Program:
Least Square Method: Linear Fit
Theory: Slope = ∑ ∑ ∑
∑ ∑
,
Intercept = ∑ ∑ ∑ ∑ ∑ ∑ [See APPENDIX I for the detailed theory and calculations.]C Simpson’s 1/3 Rule (Modified) C
write(*,*)'Lower limit, Upper Limit,sub-intervals' read(*,*)a,b,n
h=(b-a)/n sum=f(a)+f(b) d=4
do k=1,n-1 x=a+k*h
sum=sum+d*f(x) d=6-d
enddo s=h/3*sum
write(*,*)'Value of Integral= ', s stop
end C
C Subroutine for function C
function f(x) f=x**2
return end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 29 Algorithm:
1. Input: x, y
2. Inside the Do Loop, run the sums to evaluate: ∑ , ∑ , ∑ , ∑
3. Calculation of slope and intercept
4. Few data points generated with obtained slope and intercept, the straight line drawn is superposed over the original data points.
C Least Square fit C
open(1,file='xy.dat') open(2,file='fit.dat')
write(*,*)'Number of Points?' read(*,*)n
sumx=0.0 sumy=0.0 sumsqx=0.0 sumxy=0.0
write(*,*)'Give data in the form: x,y' do i=1,n
read(*,*)x,y write(1,*)x,y sumx=sumx+x sumy=sumy+y
sumsqx=sumsqx+x*x sumxy=sumxy+x*y enddo
deno=n*sumsqx-sumx*sumx
slope=(n*sumxy-sumx*sumy)/deno b=(sumsqx*sumy-sumx*sumxy)/deno
write(*,*)'Slope, Intercept= ',slope,b C
write(*,*)'Give lower and upper limits of X' read(*,*)xmin, xmax
x=xmin
dx=(xmax-xmin)/2.0 do i=1,3
y=slope*x+b write(2,*)x,y x=x+dx
enddo stop end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 30 Note: The following programs are based on solving simple first order differential equations, basically by Euler’s scheme. Runge-Kutta method of different orders can be used when necessary.
But we avoided that here for this set of elementary programs.
Projectile Motion Theory:
Consider a particle projected at an angle from origin ( ), with the horizontal direction being x-axis and the vertical direction y-axis. The force on the particle, ⃗ ⃗. Components:
. For the purpose of computation, we take ; Mass, . Projection angle, and initial speed are given. All the quantities are in arbitrary units.
Algorithm:
1. Input: initial velocity , angle of projection . The interval of time is set.
2. Initial values: interval of time , Force components 3. Components of initial velocity: ,
4. Change in velocity components: , 5. New velocity components: ,
6. Change in position coordinates: , 7. New position coordinates: ,
8. Write the position coordinates in a file and then plot to see.
The trajectory of the particle projected at an angle (indicated in the figs.) and with some initial velocity
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 31 FORTRAN Program:
Motion under Central Force Theory:
Consider the Central Force, and Newton’s 2nd law of motion: . For computational purpose, we take , . Thus we have,
.
We solve for and then therefrom derive the position. Initial values for the velocity components, and are provided. We choose the time interval ( appropriately. All the quantities are in arbitrary units.Algorithm:
C Projectile Motion of a particle C
open(1,file='proj.dat')
write(*,*)'Angle (in degree) and initial speed?' read(*,*)theta,v0
dt=0.01 g=980
theta=3.14/180*theta fx=0
fy=-g
vx=v0*cos(theta) vy=v0*sin(theta) x=0
y=0
do while(y.gt.0.0) dvx=fx*dt
dvy=fy*dt vx=vx+dvx vy=vy+dvy dx=vx*dt dy=vy*dt x=x+dx y=y+dy
write(1,*)x,y enddo
stop end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 32 1. Initial position coordinates are given. Initial velocity components and are
given.
2. Time interval is chosen.
3. Components of Force:
,
4. √
5. Change in velocity components: , 6. New velocity components: ,
7. New position coordinates: ,
8. Write the position coordinates in a file and then plot to see.
FORTRAN Program:
C Motion under a Central Force C
open(1,file='central.dat') dt=0.01
vx=0.0 vy=1.0 x=1.0 y=0.0 ncount=0 do i=1,10000 r2=x*x+y*y r=sqrt(r2) f=-1/r2 fx=x/r*f fy=y/r*f vx=vx+fx*dt vy=vy+fy*dt x=x+dt*vx y=y+dt*vy n=mod(i,100)
if(n.eq.0)write(1,*)x,y enddo
stop end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 33 The Elliptical path of a particle under Central Force
Harmonic Oscillator Theory:
Consider the Oscillator equation in the form: . For computational purpose, we choose mass and the force constant a positive number (all in arbitrary units). The equation is solved to obtain the velocity and therefrom we obtain the position of the particle.
Algorithm:
1. Give the initial position ( ), initial time ( ) and velocity ( ). Set the time interval . 2. Value of spring constant is chosen appropriately, usually a small positive number.
3.
4. Updating of velocity:
5. Change in position:
6. Updating of position:
7. Updating of time:
8. Write the time and position in a file and then plot to see.
FORTRAN Program:
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 34 The Sine Curve obtained from the solution of Linear Harmonic Oscillator Equation.
**Equation for SHM [ ] is solved numerically in the above with the simple Euler scheme.
One can add a damping term (proportional to velocity) and tune the parameters to obtain well known results and phase diagrams. However, it would be interesting to play with the parameters and gain some insight through such numeric. See APPENDIX II.
C Harmonic Oscillator C
open(1,file='harmonic.dat') x=0.0
t=0 v=1.0 dt=0.01 k=5
do i=1,1000 dv=-k*x*dt v=v+dv dx=v*dt x=x+dx t=t+dt
write(1,*)t,x,v enddo
stop end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 35 Fourth Order Runge-Kutta (RK4) Method:
For the first order differential equation, with the initial value at , the following steps are performed in order to find the value at the next grid point.
For the grid point , we compute
Then the value of is given by . RK4 for Van der Pole Oscillator:
̈ ̇
̇ Fortran Program:
open(1,file=’shmrk4.dat') C
t1=0 x1=0 y1=0.1 h=0.01 C
write(1,*)t1,x1,y2 do i=1,10000
a1=f(t1,x1,y1) b1=g(t1,x1,y1)
a2=f(t1+0.5*h,x1+0.5*h*a1,y1+0.5*h*b1) b2=g(t1+0.5*h,x1+0.5*h*a1,y1+0.5*h*b1) a3=f(t1+0.5*h,x1+0.5*h*a2+y1+0.5*h*b2) b3=g(t1+0.5*h,x1+0.5*h*a2,y1+0.5*h*b2) a4=f(t1+h,x1+h*a3+y1+h*b3)
b4=g(t1+h,x1+h*a3,y1+h*b3) C
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 36 x2=x1+h/6*(a1+2*a2+2*a3+a4)
y2=y1+h/6*(b1+2*b2+2*b3+b4) t2=t1+h
write(1,*)x2,y2,t2 x1=x2
y1=y2 t1=t2 enddo c
stop end C
function f(t,x,y) f=y
return end C
function g(t,x,y) eps=10
g=-x-eps*(x**2-1)*y return
end
RK4 for Chaos Theory: Lorenz Equations:
The Values of the parameters, Lorez used, and We can solve this set of coupled 1st order equations by RK4 method.
FORTRAN Prog for RK4 Method:
C LORENZ EQUATIONS by RK4 implicit real*8(a-h,o-z)
open(1,file='rk4chaos.dat')
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 37 C
C Initial Values C
t1=0 x1=0 y1=1.0 z1=0 C
h=0.01 h2=h/2 h6=h/6 C
write(1,*)y2,z2,x2,t2 C
do i=1,10000
a1=f1(x1,y1,z1) b1=f2(x1,y1,z1) c2=f3(x1,y1,z1) C
x11=x1+h2*a1
y11=y1+h2*b1 z11=z1+h2*c1 C
a2=f1(x11,y11,z11) b2=f2(x11,y11,z11) c2=f3(x11,y11,z11) C
x12=x1+h2*a2 y12=y1+h2*b2 z12=z1+h2*c2 C
a3=f1(x12,y12,z12) b3=f2(x12,y12,z12) c3=f3(x12,y12,z12) C
x12=x1+h*a3 y12=y1+h*b3 z12=z1+h*c3 C
a4=f1(x13,y13,z13)
b4=f2(x13,y13,z13)
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 38
c4=f3(x13,y13,z13)
C
x2=x1+h6*(a1+2*2+2*a3+a4)
y2=y1+h6*(b1+2*b2+2*b3+b4)
z2=z1+h6*(c1+2*c2+2*c3+c4)
t2=t1+h
C
write(1,*)x2,y2,z2,t2 C
C Update…
C
x1=x2 y1=y2
z1=z2
t1=t2
enddo C
stop end C
function f1(x,y,z)
implicit real*8(a-h,o-z) sigma=10
f1=sigma*(y-x) return
end C
function f2(x,y,z)
implicit real*8(a-h,o-z) rho=28
f2=x*(rho-z)-y return
end C
function f3(x,y,z)
implicit real*8(a-h,o-z) beta=8/3.0
f3=x*y-beta*z return
end
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 39
* Plotting x against y and z against z, we get the attractors.
Real Roots by Bisection Method Theory:
Consider an equation, . If a real root exists between[ ], we search that root by bisecting the interval; the midpoint being . If , is the root. If not so, we choose either [ ] or [ ]. We check whether has opposite signs at the end points (over any of the intervals). Then we take that new interval and bisect again. Thus we keep searching the root according to some accuracy (we call it tolerance).
Algorithm:
1. Function defined in subroutine 2. Input:
1. If , No Root. Stop 2. Else set 3. If , is the root. Stop
4. If , set . Else set: . 5. Check if | | . Root = . Stop 6. Else go to step 3
Example:
Consider the equation: . Let us find a real root in the interval, [ ].
The following is the result of the iterations that is obtained by running the program.
FORTRAN Program:
Dr. Abhijit Kar Gupta, Dept. of Physics, PBC, email: [email protected] Page 40