A Fortran 90 Program To Solve A Set of Linear Equations by Using
A Fortran 90 Program To Solve A Set of Linear Equations by Using
Given
AX = B
We can multiply both sides by the inverse of A, provided this exists, to give
A−1AX = A−1B
But A−1A = I, the identity matrix. Furthermore, IX = X, because multiplying any
matrix by an identity matrix of the appropriate size leaves the matrix unaltered. So
X = A−1B
This result gives us a method for solving simultaneous equations. All we
need do is write them in matrix form, calculate the inverse of the matrix of
coefficients, and finally perform a matrix multiplication.
Example
Solve the simultaneous equations
X1 + X2 -3X3 = 9
-X1 + 2X2 = 6
X1 - X2 +X3 = -5
Provided you understand how matrices are multiplied together you will
realize that these can be written in matrix form as
ͳ ͳ െ͵ ݔଵ ͻ
െͳ ʹ Ͳ ൩ ݔଶ ൩ ൌ ൩
ͳ െͳ ͳ ݔଷ െͷ
ϭ
We need to calculate the inverse of A
ͳȀ͵ ͳȀ͵ ͳ
-1
A = ͳȀ ʹȀ͵ ͳȀʹ൩
െͳȀ ͳȀ͵ ͳȀʹ
ͳȀ͵ ͳȀ͵ ͳ ͻ
= ͳȀ ʹȀ͵ ͳȀʹ൩ ቈ
െͳȀ ͳȀ͵ ͳȀʹ െͷ
Ͳ
= ͵ ൩
െʹ
program solve_equations_by_inverse_matrix
implicit none
integer,parameter::n=3
real,dimension (n)::b,x
real,dimension(n,n)::a,a1
integer::i,j,k,l
real::z
data a/1,-1,1,1,2,-1,-3,0,1/
data a1/1,3*0,1,3*0,1/
data b/9,6,-5/
do i=1,n
write(*,60)(a(i,j),j=1,n) , (a1(i,j),j=1,n)
60 format(2x,3(f10.6),10x,3(f10.6))
enddo
print*,"********************"
subroutine multi(a1,b,x)
implicit none
integer,parameter::n=3
real,dimension (n)::b,x
real,dimension(n,n)::a1
integer::i,j,k
do i=1,n
do j=1,n
x(i)=0
do k=1,n
x(i)=x(i)+a1(i,k)*b(k)
enddo;enddo;enddo
end