Solve:
This is in a convenient form for obtaining x1:
In symbols:

Eliminate x1

Substituting:
Set of n equations

|
(1) |
This may be written in matrix-vector form:
!Start the elimination loop.....
do i=1,n-1
!... go down the matrix by rows i...
!.. reducing all the other rows k..
do k=i+1,n
pivot=(a(k,i)/a(i,i))
!This is the number to multiply each
!row of equn i by before subtracting it..
!.. along each row k ..
do j=1,n
a(k,j)=a(k,j)-pivot*a(i,j)
end do
! .. and also the constant.
b(k)=b(k)-pivot*b(i)
!
end do
!.. go on to next row with current elimination
!end do
!.. and then eliminate next equation
!
!.. and finally solve the one remaining
!equation for x(n)
x(n)=b(n)/a(n,n)
!--- Elimination Complete ---------
!Back substitute for other x() ...
do l=n-1,1,-1
x(l)=b(l)
do j=l+1,n
x(l)=x(l)-a(l,j)*x(j) end do
x(l)=x(l)/a(l,l)
end do
!----- Finished ------------
program lintest
implicit none
!test linear solver
integer :: n,i,j,ifg
integer, parameter :: max=50
real :: a(max,max),x(max),c(max)
print *, 'Solver for small sets of linear equations...'
print *, 'Enter number of equations, less than ',max
read *, n
print *, 'Now enter coefficients ...'
print *, 'i.e. ',n+1, ' numbers per row...'
do i=1,n
print *, 'Elements for row ', i, ':'
read *, (a(i,j), j=1,n), c(i)
end do
.......
print *, 'Matrix and constants are:'
do i=1,n
!check print
write(6,'(7f10.4)') (a(i,j),j=1,n), c(i)
end do
call solve_linear (a,max,x,n,c,ifg)
write(6,*) 'flag =',ifg
write(6,*) 'solution..'
write(6,*) (x(i),i=1,n)
end program
Next - Section 3.6.2: Example Questions
Return to Section 3 Index