# Section 3.6.1 Elimination Methods for Linear Equations

Solve:

4 x1 + 2 x2 =  1

x1 - 4 x2 = 5

1. Eliminate x1 from both equations by making the coefficients of x1in each the same and subtracting:
• divide first equation by 4

x1 + 0.5 x2 = 0.25
• subtract

(1-1) x1 + (-4 - 0.5) x2 = (5-0.25)
• leaving a single equation in x2
-4.5 x2 = 4.75
• Hence x2
x2 = -1.055556

2. Now substitute x2 back into the first equation as reduced in the first step of 1. above, i.e.:

x1 + 0.5 (-1.055556) = 0.25

This is in a convenient form for obtaining x1:

x1 = 0.25 + 0.5 (1.055556) = 0.77778

In symbols:

Eliminate x1

• by dividing first equation by a1,1 and and keep this modified equation for future use:
x1 + a1,2 / a1,1 x2 = b1 / a1,1
• multiply by a2,1, giving both equations same x1 coefficient:
• Subtract giving single equation:

( a2,2 - a2,1 a1,2 / a1,1 ) x2 = b2 - a2,1 b1 / a1,1
• Hence x2:

• Back substitute in saved' equation to get x1.
The equation, now rearranged for x1 is:

x1 = b1 / a1,1 - (a1,2 / a1,1) x2

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 ------------


### Using the Library Solver

       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
print *, 'Now enter coefficients ...'
print *, 'i.e. ',n+1, ' numbers per row...'
do i=1,n
print *, 'Elements for row ', i, ':'
`