You are on page 1of 6

Project 4:

Solving linear systems in MATLAB


Abstract:
Our goal is to explore the built-in functions in Matlab used to solve
systems of linear equations (Ax = b) in order to compare different
methods in terms of accuracy and efficiency. systems of linear
equations can be divided into two large groups: direct and iterative
methods. Matlab has many built-in functions designed to solve systems
of linear equations using both methods.

Introduction:
We will discuss only direct methods such as Gaussian elimination (the
preferred method of solving system of equation) which is based on LU-
or PLU-decomposition of the matrix of the system where we reduce the
matrix A to an upper triangular form using forward elimination and
then solve the resulting triangular system by backward substitution. In
this process, we factor the matrix A into a product of two matrices: a
lower triangular matrix L and an upper triangular matrix U. We need
the permutation matrix P in our decomposition, which enables us to
interchange two rows in cases where the process can not be carried out
without interchanging of rows. If the matrix A is square and invertible,
we can multiply both sides of the system Ax=b by the inverse A-1 on the
left, and obtain the formula for the solution of the system: x =A-1 b. This
method has many disadvantages such as it is more costly, and its
accuracy of the resulting solution will be worse than if LU-
decomposition is used. The last method is to use reduced row echelon
form, this method is theoretically equivalent to using Gauss-Jordan
elimination, but the solution of this method has larger error than Gauss
elimination and take more time to compute.
Results
1. We will generate matrix A which is magic matrix (a square
matrix that sum of elements of each row equal to the sum of
elements of each column) and b is a column vector.
 Backslash “\” operator is based on LU- or P LU decomposition of
the matrix A and x=A\b will produce the solution of the system
Ax=b with the highest possible accuracy and shortest
computational time.
 r is the residual which is the difference between the approximate
numerical solution (Ax) and the actual solution of system (b)
r=Ax-b
the resulting r as expected is very close to zero as shown in figure
3.1 which means that the solution will be close enough to the
exact solution

Fig 3.1

2. In this method, where A = PLU:


 The results of X1 are exactly the same as X as the err1 is
approximately the zero vector.
 r1 is very close to zero as shown in fig 3.2

Fig 3.2

3. In this method we are using the inverse matrix of A multiplied


by b:
 Err2 is relatively greater than err1
 R2 is larger than r and r1
 This method has the least accuracy and efficiency among
all methods
Fig 3.3

4. This method is the Reduced Row echelon form:


 r3 is much larger than r
 this method is less efficient method relative to backslash
solver and consumes the most time.

As shown in this figures, all three methods are compared in terms of


the computational efficiency in theFigexample
3.4 of a large matrix A with the
dimensions Num x Num. num is set 100,500,700. The computational
time increases with the increment of size of the matrix as observed and
the difference between elapsed times increase

You might also like