You are on page 1of 9

Example 1.

20 Inverse of a matrix

In EES there is no direct routine to find the inverse of a matrix, since this is not needed in an
equation solver. Normally inverse is needed for solving a set of equations. Since, EES does
that by using the Newton-Raphson algorithm so special routine to find inverse is not given.
However, inverse of a matrix can be found very easily by following the simple logic given
below:

A A−1 =I ; where, A is a given matrix and I is the identity matrix which is known. So it is
much easier to feed this to EES script to find the inverse of A which is shown in the equations
window of panel 1.24.

a[1,1..n]=[1,2,3,4] { a is the matrix whose inverse is wanted and that is stored in AI matrix}
a[2,1..n]=[2,3,4,1]
a[3,1..n]=[3,4,1,2]
a[4,1..n]=[4,1,2,3]

c[1,1..n]=[1,0,0,0] { c is the identity matrix}


c[2,1..n]=[0,1,0,0]
c[3,1..n]=[0,0,1,0]
c[4,1..n]=[0,0,0,1]
n=4
duplicate i=1,n
duplicate j=1,n
c[i,j]=sum(a[i,k]*ai[k,j],k=1,n) { C = A * AI }
end
end

Panel 1.24 Equations window for finding out inverse of matrix A

The first row of matrix A is written on the first line and then the 2 nd, 3rd and 4th rows are
written. Then the identity matrix C is written. Since, A is a 4 x 4 matrix so n is specified to be
4. In the duplicate loop we simply multiply the matrix A and it’s inverse AI and equate that to
C. The equation solver solves for the elements of AI. The arrays table here shows the matrix
A and its inverse stored in AI which is computed by the EES solver.

Panel 1.24a Arrays table which shows inverse of matrix A as AI


Example 1.21 Determinant of a matrix

As direct routines for matrix inversion is not available in EES, so also direct routines for
determinants are also not available. Normally determinants are not required in an equation
solving tool, so they are not provided in EES. But we can write a simple routine to find the
determinant of a matrix. Let us proceed to find the determinant of the matrix A of Example
1.20. In order to do this we have to find the LU decomposition of the matrix A. LU
decomposition means one lower triangular matrix and another upper triangular matrix when
multiplied together gives the desired matrix A.

1 0 0 U 11 U 12 U 13 A 11 A12 A13
L U= A
[ L31 L32 1 0 ][
L21 1 0 x 0 U 22 U 23 = A 21
0 U 33 A 31 ][ A22
A32
A23
A33 ]
L is the lower triangular matrix and U is the upper triangular matrix shown for a 3x3 system
for simplicity. Their product should give the desired matrix A which is already known. In A
there are 9 known elements, in L there are 6 unknown elements and in U there are also 6
unknown elements. This way we have 12 unknowns and 9 equations from each elements of
A. There is a shortage of 3 equations, so in order to solve the system we have made the
diagonal elements of L to be all 1. Now the system of equations can be solved by EES. We
have to just specify the known elements of L and the U matrix and then carry out a product in
two duplicate loops to obtain the matrix A. This will give us the entire unknowns of the L and
the U matrix. Then the determinant of matrix A is the product of the diagonal elements of U.
Panel 1.25 shows here the EES equations window which finds the determinant of A.
procedure determinant(n, b[1..4,1..4]: det) { multiply diagonals of U matrix which is upper triangular to get
the determinant}
i=1; p=1
repeat
p=p*b[ i, i ]
i=i+1
until ( i > n)
det=p
end

a[1,1..n]=[1,2,3,4] { a is the given matrix for which determinant is needed}


a[2,1..n]=[2,3,4,1]
a[3,1..n]=[3,4,1,2]
a[4,1..n]=[4,1,2,3]
L[1,1..4]=[1,0,0,0] { L is lower triangular matrix}
L[2,2..4]=[1,0,0]
L[3,3..4]=[1,0]
L[4,4]=1
u[2,1]=0; { U is upper triangular matrix}
u[3,1]=0; u[3,2]=0
u[4,1]=0; u[4,2]=0; u[4,3]=0

n=4
duplicate i=1,n
duplicate j=1,n
a[i,j]=sum(L[i,k]*u[k,j],k=1,n)
end
end
call determinant(n,u[1..4,1..4]: det)
Panel 1.25 Equation window to find the determinant of a matrix

In the main equations window the known matrix A is first specified, then the lower triangular
matrix L is specified for its known elements and then the known elements of U are specified.
Then we carry out the matrix multiplication of L and U to obtain the matrix A which is shown
in the duplicate loop. The U matrix which is the upper triangular matrix is now passed to the
determinant routine to compute the determinant of A. The determinant routine is a procedure
which is first written at the beginning of the equations. The solution now gives the
determinant to be 160 and the arrays table shows here the matrix A along with the L and the
U matrix. For small size of the matrix we can find the determinant or its inverse this way but
when the matrix is large the present method may be too boring. So we have to adopt other
means of doing it or switch over to MATLAB for this purpose since EES is not made to
handle matrices.

Panel 1.25a Arrays table which shows the L U decomposition on matrix A

Example 1.22 Solving problems on probability in EES can be a fun

Normally the problems involving the computation of probability need ideas about
permutation and combination. Many a time students make mistakes in the conception and
many a time it becomes too abstract to think even the problem, so the formulation becomes
too hard. However with little idea of programming one can solve many types of problems in
probability by simply doing numerical simulation of the actual process. We will show here
some problems which would trigger definitely the interest of many students.

Let us solve the problem given here: 6 red, 4 white and 8 black balls are there in a bag. If 3
balls are drawn at random, without replacement, then determine the probability that (a) all 3
are red, (b) all 3 are black, (c) 2 are white and 1 is red, (d) none is red, (e) the balls are drawn
in the order red, white and black, (f) 1 of each color is drawn.

May be from first year probability course one would be able to solve the first two cases but
there after it becomes really difficult. So we would do a numerical simulation of the actual
process. There are altogether 18 (6+4+8) balls and we keep that in an array of numbers with a
name ‘p’. So our array would contain 6 ones which we say are red, 4 twos which are for
white and 8 threes which would represent the black balls. We can fill the array in any way.
We can fill that in an orderly manner or really randomly but this storing of numbers would
really not influence our end result. So, p[1..18] is first written in the procedure ‘find’.

We want to draw three balls without replacement. So we first generate a random number x
between1 to 18. Now test p[x] and see if that is 1 or not. If that is 1 then that is a red ball.
Before the second draw we must assure that p[x] has been removed. So we set p[x]=p[1], this
means we bring the value at position 1 to position x. Then we generate the random number y
from 2 to 18 for our second draw. Test whether p[y] is 1 or not. If that is true then shift p[2]
to p[y] which basically tells that p[y] has been removed to position 2. Then we generate
another random number z from 3 to 18 for the 3 rd draw and test whether that is 1 or not. If
that is one then we move the counter by 1. This way we have drawn three balls from the bag.
Now we have to go back and repeat the same procedure for a second trial. But before doing
so we must reset the initial array because that has been disturbed due to the non-replacement
of the balls. Then we would repeat the process for a very large number of times say n=40000.
After that we would see how much is the counter? Then the probability of the event is simply
the ratio of counter to the number of trials ‘n’. This is a simple numerical experiment which
would yield almost accurate result for the probability of the event. Now let us simulate the
process in the equations window shown in Panel 1.26.

procedure find(nj : x_find)


p[1..18]=[2,3,1,3,1,2,3,2,3,1,3,2,1,3,1,3, 1, 3] { 1 = red, 2=white, 3=black}
{ p stores the position of the balls randomly}
j=1; c=0
repeat
x= trunc(random(1,18.99)); y= trunc(random(2,18.99)); z=trunc(random(3,18.99))
if( p[x] =1) then { once a ball is picked it is pushed to position 1 and random number is generated from
2 to 18, this means the ball is taken out}
p[x]=p[1]
if ( p[y]= 1) then { when the 2nd ball is taken out random number is found from 3 to 18 and the ball is
pushed to position 2}
p[y]=p[2]
if (p [z]=1 ) then
c=c+1
endif
endif
endif
p[1..18]=[2,3,1,3,1,2,3,2,3,1,3,2,1,3,1,3, 1, 3]
j=j+1
until(j > nj)
x_find=c
end

n=40000
call find(n : x_find)
prob=x_find/n

Panel 1.26 Equations window for example problem 1.22, solution on probability

The procedure is exactly written the way it has been described above. We simply call the
procedure with a value of ‘n’ and then compute the probability in the main routine. The
solution would show that the probability of getting all three balls to be red is around 0.023 to
0.026. The exact answer is 0.0245. So our numerical simulation is pretty ok and interesting.
By increasing ’n’ from 40000 to 60000 the answer would come closer to 0.0245. Now let us
solve part (b) of the problem. Here we need that all the 3 draws should give all black balls.
For this we have to simply change the ‘1’ to ‘3’ in the testing loop. The simulation will yield
a probability of 0.0686 where the exact answer is 0.0686.

Now the solution of part (c) of the problem needs that 2 are to be white and 1 has to be red.
Here there is a catch. We may say that the first two are white and the last one is red, or the
first one is red and the last two are white or the middle one is red and the first and the last are
white. So we have three combinations here. We can test for any one of the combinations and
then multiply the answer by 3 to get the actual probability. So, set p[x]=2, p[y]=2 and p[z]= 1
in the testing loop of the procedure. The solution then yields the probability to be .0145 and
when that is multiplied by 3 we get the actual probability to be 0.0435 where the exact
answer is 0.044.

Now lets us solve part (d) of the problem where we want that not a single draw is red. For
this we have to set p[x] = p[y] = p[z] = 2 or 3. Then we should increase the counter by 1. This
gives the probability to be 0.268 whereas the exact answer is 0.2696. In party (e) of the
solution we need that the balls are to be drawn in the order red, white and black. For this we
set p[x]=1, p[y]=2 and p[z]=3 in the testing loop. This gives the result to be 0.0389 whereas
the actual answer is 0.0392. In part (f) of the solution we need to know the probability so that
one of each color is drawn. Here there is a minor trick. It is not told which color is coming
first and which one then follows. So if we draw in the order red, white and black then we
have 3! choices for this option to be satisfied. So the answer would be simply 6 times that of
the previous answer which is .0389*6 = 0.2334 whereas the exact answer is 0.2352.

The program developed here could solve all the cases what we proposed at the beginning and
the solution could be obtained by simple numerical simulation which suits to our simple
thought process without ever going to the details of probability theory or permutations and
combinations.

Example 1.23 A problem on binomial probability

In the last example we solved the case of drawing three balls from a bag and formulated
various conditions attached to the balls for different types of choices of withdrawal. Here in
this example we would formulate numerical means of simulating binomial probability. The
simulation would be clear through an example problem which is stated here.

What is the probability of getting a 7 exactly once in 3 throws with a pair of dice. What is the
probability of getting a 9 at least 2 times in 5 throws with the same pair of dice.

For the numerical simulation of the problem let us throw a pair of dice and add their face
numbers. This is done by simply generating a random number x between 1and 6 and another
number y between 1 and 6 and obtaining the value x+y for a single throw. If x+y =7 then we
increase the inner counter by 1 and we repeat this for 3 times. Then we test the inner counter
if that is exactly 1. If that is true then we increase the outer counter by 1 and then we repeat
the entire process for a large number of times say, n=20000. The probability of the event will
be the ratio of outer counter to n. This looks simple to program and we show that here in the
equations window in panel 1.27.
procedure find(nj : x_find)
c=0; i=1;
repeat { i loop}
j=1; ic=0
repeat { j loop}
x= trunc(random(1, 6.99)); y= trunc(random(1, 6.99));
if(x +y =7) then
ic=ic+1
endif
j=j+1
until(j > 3) { 3 = number of throws} { end of j loop}
if (ic =1 ) then { ic=1 means exactly once, ic >=2 means at least 2}
c=c+1
endif
i=i+1;
until (i > nj) { end of i loop }
x_find=c
end

n=20000
call find(n : x_find)
prob=x_find/n

Panel 1.27 Equation window for example problem 1.23

The solution of the equations shows that the probability is around 0.344 whereas the actual
result is 0.347. Let us now proceed to solve the second part of the problem. We have to
obtain a 9 so we make the change x+y =9 in the ’if loop’. The number of throws is now 5
instead of 3 so make a change in the statement (j > 3) to (j > 5). We have to obtain at least 2
times 9 so we set the inner counter to (ic >= 2). The solution now shows that the probability
is around 0.098 which is the exact result.

Example 1.24 A problem with permutations

Problems involving permutations in probability are normally tricky and many a time mistakes
are done very easily. Here is an example which we solve by numerical simulations where
permutations would be normally required by mathematicians to attack the problem. The
problem is like this:

5 red blocks and 4 white blocks are there on a pile and all of them are to be placed at random
in a row. What is the probability that (a) the two extreme blocks are red, (b) the two extreme
ones are white?

We have to do a numerical simulation of the entire process. Set the counter to zero, set the I
loop and then set the j loop. At the beginning of I loop set the array[1..9] = [1,1,1,1,1,2,2,2,2].
In the array there are 5 ones and 4 twos, which signify 5 red (1 for red) blocks and 4 white (2
for white) blocks. These are to be placed randomly on a row. So, we generate a random
number x between j and 9 in the j loop. Set p[j] = arr[x] and arr[x] = arr[j] and repeat the loop
until j = 9. This way we generate 9 randomly placed blocks in the array with a name ‘p’.
Then we test whether the first position of array ‘p’ is 1 and the last position of array p is 1 or
not. If it is true then we increase the counter by 1. Reset the initial array again and repeat the I
loop for a large number of times say, n=20000. Then the probability of the event would be
the ratio of counter to n. The equations are shown in the panel 1.28.
procedure find(nj : x_find)
c=0; i=1; arr[1..9]=[1,1,1,1,1,2,2,2,2] { 1 = red block and 2 = white block}
repeat { i loop}
j=1;
repeat { j loop}
x= trunc(random(j,9.99));
p[j]=arr[x]; arr[x]=arr[j]
j=j+1
until(j > 9) { end of j loop}
if (p[1] =2) and (p[9] =2) then { extreme blocks are both red}
c=c+1
endif
i=i+1; arr[1..9]=[1,1,1,1,1,2,2,2,2] { reset the initial array}
until (i > nj) { end of i loop}
x_find=c
end

n=20000
call find(n : x_find)
prob=x_find/n

Panel 1.28 Equation window for example problem 1.24, problem with permutation

Solution of the equations will now show the probability to be 0.277 which is the exact
answer. If the end blocks are white then we change the testing loop where we set p[1]=2 and
p[9]=2. The probability is computed to be 0.166 which is the correct result. If it is asked to
know what is the probability that the first three blocks are red? Then we simply change the
testing loop to p[1]=p[2]=p[3]=1 and solve the equations. The result would be 0.118 and the
exact result is 0.119. If it is asked to find the probability for the case where the first three
blocks are white of the first 4 blocks white then what would be the result? The result is
0.0476 for the first three blocks to be white and if the first 4 blocks are to be white then the
probability is 0.0078 which is close to the exact answer of 0.00793.

Add this to end of exercise problems

Determinant and inverse

Find the determinants of the following matrices:

The entries are made row wise.

D1. ((1, 2, 3, 4); (2, -3, 4, 1); (3, 4,-1, 2); (4, 1, 2,-3)) Ans: -344

D2. ((1, 2, 0, 3); (4, 2, 1, 3); (2, 3, 4, 5); (5, 2, 1, 4)) Ans: -24

D3. ((1, 2, 3, 4); (4, 3, 2, 1); (3, 4, 5, 6); (3, 5, 4, 6)) Ans: 0

D4. ((7, 8, 9, 0); (2, 4, 3, 1); (4, 3, 2, 1); (2, 0, 5, 6)) Ans: -311
D5. ((7, 8, 9, 0,2); (2, 4, 3, 1, 3); (4, 3, 2, 1, 4); (2, 0, 5, 6, 4); (3, 1, 2, 4, 5)) Ans: 192

D6. ((7, 8, 9, 0,2); (2, -4, 3, 1, 3); (4, 3, -2, 1, 4); (2, 0, 5, -6, 4); (3, 1, 2, 4, -5)) Ans: 10982

D7. Find the inverse of the matrix D1, D2, D4 and D5. Answers are here written rows wise.

Inverse of D1 matrix

((-.1977, .2209, .2674, -.0116); (.2209, -.2616, -.093, .1453); (.2674, -.093, -.244, .1628);
(-.0116, .145, .1628, -.1919))

Inverse of D2 matrix

((-.208, .291, -.083, .041); (.291, 1.79, -.083, -1.458); (-.375, .125, .25, -1.25); (.208, -1.292, .
083, .958))

Inverse of D4 matrix

((.035, -.34, .36, -.0032); (-.115, .479, .0032, -.0804); (.186, -.16, -.283, .0739); (-.167, .247, .
115, .106))

Inverse of D5 matrix

((.2708, -.437, -.395, -.375, .7708); (.328, .296, -1.422, -.781, 1.453); (-.281, .0312, 1.219, .
812, -1.531); (.474, -.0156, -1.943, -.906, 2.099); (-.494, .203, 1.589, .781, -1.62))

Probability

B1. What is the probability of getting a 5 exactly twice in 4 throws with a pair of dice? Ans: .06

B2. What is the probability of getting a 7 at least 2 times in 5 throws with a pair of dice. Ans: 0.196

B3. What is the probability of getting a 8 at least 2 times in 5 throws with a pair of dice. Ans: 0.146

B4. A dice is thrown 9 times. What is the probability that it will yield (a) exactly 2 ones, (b) What is the
probability of obtaining at least 2 ones? Ans. 0.276, 0.457

B5. 6 red blocks and 5 white blocks are to be placed at random in a row. What is the probability that (a) the two
extreme blocks are red, (b) are white, (c) the last three blocks are red, (d) the last three blocks are white.
Ans: .274, .183, .122, .061

B6. A coin is thrown 9 times. What is the probability that it will yield (a) exactly 3 heads, (b) What is the
probability of obtaining at least 3 heads? Ans. 0.161, 0.914

B7. A bag has 2 white and 3 black balls. A, B , C and D are to draw a ball without replacing
it back in the order of A, B, C ,D. What is the probability of each drawing a white ball ?
Ans: .4, .3, .2, .1

B8. 8 discs are there in a bag numbered 1,2,3,4,5,6,7,8. What is the probability that the sum
of the numbers on three discs pulled randomly from the bag is (a) greater equal to 15, (b)
greater than 15 and (c) greater than 16. Ans: .389, .283, .195
B9. In problem B8 we draw 4 discs instead of 3. What is the probability that the sum of the
numbers on the disc is now greater than 20. Ans: .241

B10. 6 red blocks and 4 white blocks are are to be placed at random in a row. What is the probability that (a) the
two middle blocks are red, (b) are white (c) and are of same color. Ans: .334, .132, .466}

You might also like