FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc.
Mechanical Engineering Dep.

Solution a Set of Linear Equations by
Cramer’s rule
The method of solution of linear equations by determinantes is called
Cramer's Rule.
Let's use the following system of equations:
2x + y + z = 3
x– y–z=0
x + 2y + z = 0
We have the left-hand side of the system with the variables (the "coefficient
matrix") and the right-hand side with the answer values. Let D be the determinant
of the coefficient matrix of the above system, and let Dx be the determinant formed
by replacing the x-column values with the answer-column values:
system of
equations
coefficient
matrix's
determinant
answer
column
Dx: coefficient determinant
with answer-column
values in x-column
2x + 1y + 1z = 3
1x – 1y – 1z = 0
1x + 2y + 1z = 0
Similarly, Dy and Dz would then be: Copyright © Elizabeth Stapel 2004-2011 All
Rights Reserved
1
Evaluating each determinant, we get:
Cramer's Rule says that x = Dx / D , y = Dy / D, and z = Dz / D. That is:
x = 3/3 = 1, y = –6/3 = –2, and z = 9/3 = 3
Cramer's Rule is a handy way to solve for just one of the variables without
having to solve the whole system of equations.
program cramer_rule
implicit none
integer::i,j,d1,d2,d3 ,det
integer,parameter::n=3
real,dimension(n,2*n-1)::a,a1,a2,a3
real,dimension(n)::b
real::x,y,z
data b/3,0,0/
read(*,*)((a(i,j),j=1,n),i=1,n)
call determinant (a,det)
a1=a;a2=a;a3=a
do i=1,n
a1(i,1)=b(i)
2
enddo
call determinant (a1,d1)
x=d1/det
do i=1,n
a2(i,2)=b(i)
enddo
call determinant (a2,d2)
y=d2/det
do i=1,n
a3(i,3)=b(i)
enddo
call determinant (a3,d3)
z=d3/det
print*,"x=",x ,"y=" ,y , "z=",z
;
subroutine determinant (a,det)
implicit none
integer,parameter::n=3
real,dimension(n,2*n-1)::a
integer::i,j ,d1,d2,det
do i=1,n
do j=1,n-1
a(i,n+j)=a(i,j)
enddo
;
enddo
do i=1,n; d1=1;d2=1
do j=1,n
d1=d1*a(j,i+j-1)
d2=d2*a(j,2*n-i-j+1)
enddo
det=det+d1-d2
;
enddo
end
3
end