! Main Program
PROGRAM SolveLinearSystem
IMPLICIT NONE
REAL, DIMENSION(3, 3) :: A
REAL, DIMENSION(3) :: b
INTEGER :: n
! Define the system of equations Ax = b
n = 3
A = RESHAPE([2.0, -3.0, -2.0, 1.0, -1.0, 1.0, -1.0, 2.0, 2.0], SHAPE(A))
b = [8.0, -11.0, -3.0]
! Solve the system
CALL GaussElimination(A, b, n)
! Print the solution
PRINT *, 'Solution: ', b
END PROGRAM SolveLinearSystem
! Main Program
PROGRAM SolveLinearSystem
IMPLICIT NONE
REAL, DIMENSION(3, 3) :: A
REAL, DIMENSION(3) :: b
INTEGER :: n
! Define the system of equations Ax = b
n = 3
A = RESHAPE([2.0, -3.0, -2.0, 1.0, -1.0, 1.0, -1.0, 2.0, 2.0], SHAPE(A))
b = [8.0, -11.0, -3.0]
! Solve the system
CALL GaussElimination(A, b, n)
! Print the solution
PRINT *, 'Solution: ', b
END PROGRAM SolveLinearSystem
MATLAB VERIFICATION:
Example 1
We are solving the system equations
2x -3y -2z = 8
-3x -y + z = -11
-x +2y +2z = -3
clc A=[2,-3,-2;1,-1,1;-1,2,2] A = A' % transpose to match with F90's array b = [8;-11;-3] A\b
Note:
It should be reminded that F90 orders array by column-first. We for example,
A = RESHAPE([2.0, -3.0, -2.0, 1.0, -1.0, 1.0, -1.0,2.0, 2.0], SHAPE(A))
will give
A =. 2. 1. -1
-3. -1. 2
-2. 1. 2
F90 Output
Example 2
2x +y -z + h = 8
-3x -y + 2z + 4h = -11
-2x + y + 2z + 3h = -3
4x +3y +2z -h = 4
In F90, matrix A and vector b are defined as
! Main Program
PROGRAM SolveLinearSystem
IMPLICIT NONE
REAL, DIMENSION(4, 4) :: A
REAL, DIMENSION(4) :: b
INTEGER :: n
! Define the system of equations Ax = b
n = 4
A = RESHAPE([2.0, -3.0, -2.0, 4.,1.0, -1.0, 1.0,3., -1.0, 2.0, 2.0,2.0,1.,4.,3.,-1.], SHAPE(A))
b = [8.0, -11.0, -3.0,4.]
clc A=[2,-3,-2,4;1,-1,1,3;-1,2,2,2;1,4,3,-1] A = A'. % transpose to match with F90's array b = [8;-11;-3;4] A\b