You are on page 1of 4

www.freemat.

info

Matlab/Freemat/Octave/Scilab: Solution of Linear Systems of Equations
A linear system of equations can be written as a matrix-vector equation
1
. Often a
linear system of equations has the same number of equations as it has unknowns. In
this case the matrix will be square and this is the case that is considered in this
document.

A linear system of equations can be written in the matrix-vector form

,

where is a square matrix, is a given vector and is the vector of unknowns.

The solution can be obtained by inverting
2
, and this method will be considered in
this document. However, there is also a direct method of division in
Matlab/Freemat/Octave/Scilab and other general methods to consider.

Solution by inverting

By writing the equation in the form

,

the solution of the linear system of equations can be found by first inverting and
multiplying the result by .

For example the equation

(


) (

) (

)
may be solved as follows.


















1
Linear Systems and 2x2 Matrices
2
Identity and Inverse Matrices
--> A=[2 1; 3 2]
A =
2 1
3 2
--> b=[7; 12]
b =
7
12
--> inv(A)*b
ans =
2
3
www.freemat.info

Solution by division

The recommended method is to use the division operator (\) to solve linear systems of
equations. For example the system above can be solved as follows.


















Given the example of a system

(



)(

) (

)
the solution (

)can be found in Matlab/Freemat as follows.
















--> A=[2 1; 3 2]
A =
2 1
3 2
--> b=[7; 12]
b =
7
12
--> A\b
ans =
2.0000
3.0000
A =
1 0 1
1 2 1
0 2 1
--> b=[1;2;3]
b =
1
2
3
--> A\b
ans =
-1.0000
0.5000
2.0000
www.freemat.info

Efficiency Considerations and LU factorisation
The standard method for solving a linear system of equations is LU factorisation
followed by forward and back substitution
3
, and this should be the presumed method
behind the matrix division operation described earlier. Both LU factorisation and
methods for inverting a matrix are fairly computationally intensive operations; both
are O(n
3
), where n is the dimension of the matrix
4
, although matrix inversion is a
factor more computationally intensive than LU factorisation. [Note the forward and
back substitution that following LU factorisation is O(n
2
).]
Although the simplicity of the operations like x=A\b and x=inv(A)*b in
Matlab/Freemat/Octave/Scilab is very welcome (compared to the amount of coding,
or searching for an appropriate function that is required in most other languages),
such simple lines of code tend to hide the computational cost in carrying out such
operations. Like all computational methods, the computational cost does not need to
be considered when n is small. However in many practical problems n can be large
and for large n (say n>100) the computational cost of the operation should be
considered by the programmer.
From what has been said, from a computational point of view, in solving

,

x=A\b is preferable to x=inv(A)*b, in fact it is very rare that the inverse of a matrix is
explicitly required; the goal can almost always be reached by a less computationally
intensive method.
Another important scenario also often occurs. Presume we have to solve

,

but followed by a set of one or more similar problems, with the same matrix, but with
various vectors

for i=1,2, .

From what has been said so far the method that would follow is
x=A\b
x1=A\b1
x2=A\b2
:

3
LU factorisation of a Matrix
4
Big O Notation
www.freemat.info

If the vectors

are all known in advance then a computationally efficient method for


carrying this out would be to form a matrix B made up from the columns of the b-
vectors, for example
B=[b;b1;b2;b3;b4]
and solve as
X=A\B
with the resulting columns of the matrix X being the solutions x,x1,x2,x3 and x4.
However, in this cases where all the b-vectors are not known in advance then it is
more computationally efficient to decouple the LU factorisation method and the
forward and back substitution methods that underlies the x=A\b command. In
Matlab/Freemat/Octave/Scilab the command
[L, U, P]=lu(A)
carries out the LU factorisation of the matrix A. The solution x can be found by the
forward and back substitution method. This could be directly programmed or using a
code such as LUfbsub
5
:
[x]=LUfbsub(L,U,n,P,b).

The other solutions can be found by the commands
[x1]=LUfbsub(L,U,n,P,b1)
[x2]=LUfbsub(L,U,n,P,b2)
:

By this approach, the computationally intensive O(n
3
) LU factorisation method is
carried out once-and-for-all and the O(n
2
) forward and back substitution methods are
carried out each time a solution is sought.




5
LUfbsub

You might also like