Grade Lecturer in Mathematics
Govt Victoria College, Palakkad
BISECTION METHOD
c
40
50
PROGRAM bs
To find zero of an equation by bisection method
write(*,40)
format(1x,'To find a real root of an equation using Bisection',\)
write(*,50)
format(' method.')
write(*,*)
write(*,*)'Enter numbers between which the root is to be found:'
read(*,*)a,b
IF((f(a).GT.0).AND.(f(b).LT.0))THEN
w=a
a=b
b=w
ENDIF
20
30
10
write(*,*)'Input error value:'
read(*,*)e
c=(a b)/2
IF(abs(f(c)).LT.e) GOTO 30
IF(f(c).LT.0)THEN
a=c
ELSE
b=c
ENDIF
GOTO 20
write(*,10)c
format(1x,'The approximate root of the function is:',F8.3)
STOP
END
function f(c)
f=c*c-5*c 6
return
END
GAUSS ELIMINATION
c
10
20
PROGRAM gelmn
Gauss elimination method to solve a linear system of eqns
dimension a(30,30),x(30)
write(*,10)
format(1x,'To solve a linear system of equations using Gauss ',\)
write(*,20)
format('Elimination method with pivoting(using subroutine).')
write(*,*)'Enter the number of variables:'
read(*,*)n
write(*,*)'Enter the coefficients in the equations:'
read(*,*)((a(i,j),j=1,n 1),i=1,n)
DO 30 k=1,n-1
100
90
call pivot(a,k,n)
DO 90 i=k 1,n
u=a(i,k)/a(k,k)
DO 100 j=k,n 1
a(i,j)=a(i,j)-u*a(k,j)
continue
continue
30
continue
IF(abs(a(n,n)).LE.(.00001))THEN
write(*,*)'Ill conditioned equations.'
STOP
ENDIF
70
60
110
80
x(n)=a(n,n 1)/a(n,n)
DO 60 i=n-1,1,-1
sum=0
DO 70 j=i 1,n
sum=sum a(i,j)*x(j)
continue
x(i)=(a(i,n 1)-sum)/a(i,i)
continue
write(*,*)'Values of the variables are as follows:'
DO 80 i=1,n
write(*,110)x(i)
format(1x,F10.3)
continue
STOP
END
subroutine pivot(a,k,n)
dimension a(30,30)
real mx
integer p,q
mx=abs(a(k,k))
40
50
p=k
DO 40 m=k 1,n
IF(abs(a(m,k)).GT.mx)THEN
mx=abs(a(m,k))
p=m
ENDIF
continue
IF(mx.LE.(.00001))THEN
write(*,*)'Ill-conditioned equations.'
STOP
ENDIF
DO 50 q=k,n 1
temp=a(k,q)
a(k,q)=a(p,q)
a(p,q)=temp
continue
return
END
EULER’ METHOD
PROGRAM eu
c
Euler's method to solve a first order de
dimension x(50),y(50)
write(*,*)'To solve a differential equation using Euler method.'
write(*,*)'Enter initial values:'
read(*,*)a,c
write(*,*)'Enter value at which result is to be found:'
read(*,*)b
write(*,*)'Enter the number of subintervals required:'
read(*,*)n
x(1)=a
y(1)=c
h=(b-a)/n
write(*,*)'The function values at each step is given below.'
write(*,*)
write(*,*)'
x
Function value'
write(*,10)x(1),y(1)
10
format(1x,F10.4,5x,F10.4)
DO 20 i=1,n
x(i 1)=x(i) h
y(i 1)=y(i) h*f(x(i),y(i))
write(*,10)x(i 1),y(i 1)
20
continue
STOP
END
function f(x,y)
f=(y-x)/(y x)
return
END
REGULA FALSI METHOD
PROGRAM regfal
Regula falsi Method to a find a root of an eqn
write(*,40)
40
format(1x,'To find the approximate root of a function using ')
write(*,50)
50
format(\,'Method of False Position.')
write(*,*)'Enter the numbers between which root is to be found:'
read(*,*)a,b
c
IF((f(a).GT.0).AND.(f(b).LT.0))THEN
w=a
a=b
b=w
ENDIF
20
10
30
write(*,*)'Input error value:'
read(*,*)e
c=(a*f(b)-b*f(a))/(f(b)-f(a))
IF(abs(f(c)).LT.e) GOTO 10
IF(f(c).LT.0)THEN
a=c
ELSE
b=c
ENDIF
GOTO 20
write(*,30)c
format(1x,'The approximate root of the function is:',F8.3)
STOP
END
function f(c)
f=c**3-9*c 1
return
END
GCD OF A SEQUENCE OF NUMBERS
PROGRAM ss
integer x(30)
write(*,*)'To find the gcd of a set of numbers.'
write(*,*)'Enter number of numbers:'
read(*,*)n
write(*,*)'Enter the numbers:'
read(*,*)(x(i),i=1,n)
j=x(1)
DO 10 i=2,n
k=gg(j,x(i))
j=k
10
continue
write(*,20)j
20
format(1x,' GCD= ',I4)
STOP
END
30
function gg(a,b)
integer a,b
IF(a.GT.b)THEN
j=a
a=b
b=j
ENDIF
i=mod(b,a)
IF(i.EQ.0)THEN
gg=a
return
ELSE
b=a
a=i
GOTO 30
ENDIF
END