Scilab Guide for Discrete Math Students
Scilab Guide for Discrete Math Students
Discrete Mathematics
by S. Lipschutz, M. Lipson And V. H. Patil1
Created by
Neerja Bahuguna
B.Tech. (pursuing)
Computer Engineering
Echelon Institute of Technology, Faridabad
College Teacher
Prashant Gupta, E. I. T. ,Faridabad
Cross-Checked by
Prashant Dave, IIT Bombay
Edition: 3
Year: 2009
ISBN: 9780070669123
1
Scilab numbering policy used in this document and the relation to the
above book.
For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means
a scilab code whose theory is explained in Section 2.3 of the book.
2
Contents
1 Set Theory 5
6 Counting 16
7 Probability Theory 23
8 Graph Theory 40
9 Directed graphs 43
12 Algebraic Systems 50
15 Boolean Algebra 52
16 Recurrence relations 54
3
List of Scilab Codes
4
Exa 6.14 Ordered partitions . . . . . . . . . . . . . . . . . . . . 21
Exa 6.15 Unordered partitions . . . . . . . . . . . . . . . . . . . 21
Exa 6.16 Inclusion exclusion principle revisited . . . . . . . . . 22
Exa 7.1 Sample space and events . . . . . . . . . . . . . . . . . 23
Exa 7.2 Finite probability spaces . . . . . . . . . . . . . . . . . 24
Exa 7.3 Equiprobable spaces . . . . . . . . . . . . . . . . . . . 25
Exa 7.4 Addition principle . . . . . . . . . . . . . . . . . . . . 25
Exa 7.5 Conditional probability . . . . . . . . . . . . . . . . . 26
Exa 7.6 Multiplication theorem for conditional probability . . 28
Exa 7.7 Independent events . . . . . . . . . . . . . . . . . . . . 29
Exa 7.8 Independent events . . . . . . . . . . . . . . . . . . . . 30
Exa 7.9 Independent repeated trials . . . . . . . . . . . . . . . 30
Exa 7.10 Repeated trials with two outcomes . . . . . . . . . . . 31
Exa 7.12 Random variables . . . . . . . . . . . . . . . . . . . . 32
Exa 7.13 Probability distribution of a random variable . . . . . 33
Exa 7.14 Probability distribution of a random variable . . . . . 34
Exa 7.15 Expectation of a random variable . . . . . . . . . . . . 35
Exa 7.16 Variance and standard deviation of a random variable 36
Exa 7.17 Binomial diatribution . . . . . . . . . . . . . . . . . . 37
Exa 7.18 Chebyshev inequality . . . . . . . . . . . . . . . . . . 38
Exa 7.19 Sample mean and Law of large numbers . . . . . . . . 38
Exa 8.1 Paths and connectivity . . . . . . . . . . . . . . . . . 40
Exa 8.2 Minimum spanning tree . . . . . . . . . . . . . . . . . 41
Exa 9.6 Adjacency matrix . . . . . . . . . . . . . . . . . . . . 43
Exa 9.8 Path matrix . . . . . . . . . . . . . . . . . . . . . . . 43
Exa 11.2 Division algorithm . . . . . . . . . . . . . . . . . . . . 45
Exa 11.4 Primes . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Exa 11.5 Greatest Common Divisor . . . . . . . . . . . . . . . . 46
Exa 11.6 Euclidean algorithm . . . . . . . . . . . . . . . . . . . 46
Exa 11.9 Fundamental theorem of Arithmetic . . . . . . . . . . 47
Exa 11.12 Congruence relation . . . . . . . . . . . . . . . . . . . 48
Exa 11.19 Linear congruence equation . . . . . . . . . . . . . . . 48
Exa 12.4 Properties of operations . . . . . . . . . . . . . . . . . 50
Exa 12.17 Roots of polynomial . . . . . . . . . . . . . . . . . . . 50
Exa 12.18 Roots of polynomial . . . . . . . . . . . . . . . . . . . 51
Exa 15.1 Basic definitions in boolean algebra . . . . . . . . . . . 52
Exa 15.2 Boolean algebra as lattices . . . . . . . . . . . . . . . 53
5
Exa 16.14 Linear homogenous recurrence relations with constant
coefficients . . . . . . . . . . . . . . . . . . . . . . . . 54
Exa 16.15 Solving linear homogenous recurrence relations with con-
stant coefficients . . . . . . . . . . . . . . . . . . . . . 55
Exa 16.16 Solving linear homogenous recurrence relations with con-
stant coefficients . . . . . . . . . . . . . . . . . . . . . 56
Exa 16.17 Solving linear homogenous recurrence relations with con-
stant coefficients . . . . . . . . . . . . . . . . . . . . . 57
Exa 16.18 Solving linear homogenous recurrence relations with con-
stant coefficients . . . . . . . . . . . . . . . . . . . . . 58
Exa 16.19 Solving general homogenous linear recurrence relations 59
6
Chapter 1
Set Theory
1 disp ( ’ To f i n d : number o f m a t h e m a t i c s s t u d e n t s t a k i n g
a t l e a s t one o f t h e l a n g u a g e s F r e n c h ( F ) , German (G)
and R u s s i a n (R) ’ )
2 F =65; // number o f s t u d e n t s s t u d y i n g F r e n c h
3 G =45; // number o f s t u d e n t s s t u d y i n g German
4 R =42; // number o f s t u d e n t s s t u d y i n g R u s s i a n
5 FandG =20; // number o f s t u d e n t s s t u d y i n g F r e n c h and
German
6 FandR =25; // number o f s t u d e n t s s t u d y i n g F r e n c h and
Russian
7 GandR =15; // number o f s t u d e n t s s t u d y i n g German and
Russian
8 FandGandR =8; // number o f s t u d e n t s s t u d y i n g French ,
German and R u s s i a n
9 //By i n c l u s i o n −e x c l u s i o n p r i n c i p l e
10 ForGorR = F + G +R - FandG - FandR - GandR + FandGandR ;
11 disp ( ForGorR , ’ t h e number o f s t u d e n t s s t u d y i n g
a t l e a s t one o f t h e l a n g u a g e s : ’ )
7
Scilab code Exa 1.9 Inclusion exclusion principle
1 disp ( ’ I n a c o l l e g e , 1 2 0 m a t h e m a t i c s s t u d e n t s can o p t
f o r e i t h e r F r e n c h ( F ) , German (G) o r R u s s i a n (R) ’ )
2 n =120; // t o t a l number o f s t u d e n t s
3 F =65; // number o f s t u d e n t s s t u d y i n g F r e n c h
4 G =45; // number o f s t u d e n t s s t u d y i n g German
5 R =42; // number o f s t u d e n t s s t u d y i n g R u s s i a n
6 FandG =20; // number o f s t u d e n t s s t u d y i n g F r e n c h and
German
7 FandR =25; // number o f s t u d e n t s s t u d y i n g F r e n c h and
Russian
8 GandR =15; // number o f s t u d e n t s s t u d y i n g German and
Russian
9 FandGandR =8; // number o f s t u d e n t s s t u d y i n g French ,
German and R u s s i a n
10 disp ( ’ u s i n g i n c l u s i o n −e x c l u s i o n p r i n c i p l e : ’ )
11 ForGorR = F + G +R - FandG - FandR - GandR + FandGandR ;
12 disp ( ForGorR , ’ number o f s t u d e n t s s t u d y i n g F r e n c h o r
German o r R u s s i a n ’ )
13 FGnR = FandG - FandGandR ;
14 disp ( FGnR , ’ number o f s t u d e n t s s t u d y i n g F r e n c h and
German but n o t R u s s i a n ’ )
15 FRnG = FandR - FandGandR ;
16 disp ( FRnG , ’ number o f s t u d e n t s s t u d y i n g F r e n c h and
R u s s i a n but n o t German ’ )
17 GRnF = GandR - FandGandR ;
18 disp ( GRnF , ’ number o f s t u d e n t s s t u d y i n g German and
R u s s i a n but n o t F r e n c h ’ )
19 OF =F - FGnR - FandGandR - FRnG ;
20 disp ( OF , ’ number o f s t u d e n t s s t u d y i n g Only F r e n c h ’ )
21 OG =G - FGnR - FandGandR - GRnF ;
22 disp ( OG , ’ number o f s t u d e n t s s t u d y i n g Only German ’ )
23 OR =R - FRnG - FandGandR - GRnF ;
24 disp ( OR , ’ number o f s t u d e n t s s t u d y i n g Only R u s s i a n ’ )
25 k =n - ForGorR ;
26 disp (k , ’ number o f s t u d e n t s n o t s t u d y i n g any o f t h e
languages ’)
8
Scilab code Exa 1.13 Power sets
1 A =[1 ,2 ,3 ,4 ,5]; // e a t a b l e s f o r s a l a d p r e p a r a t i o n 1=
o n i o n ,2= tomato ,3= c a r r o t ,4= cabbage ,5= cucumber
2 p = length ( A ) ; // t o t a l number o f e a t a b l e s a v a i l a b l e
3 n =2^ p -1; // no s a l a d can be made w i t h o u t a t l e a s t one
o f t h e e a t a b l e s . Hence n u l l s e t i s n ’ t c o u n t e d
4 disp (n , ’ number o f d i f f e r e n t s a l a d s t h a t can be
prepared using the given e a t a b l e s ’ )
1 U1 =1; // g i v e n
2 U2 =5; // g i v e n
3 P =[];
4 for i =1:2
5 P ( i ) =3^ i -2^ i ;
6 disp ( P ( i ) )
7 end
8 disp ( ’P ( 1 )=U( 1 ) and P ( 2 )=U( 2 ) ’ ) ;
9
9 disp ( ’ h e n c e Un=3ˆn−2ˆn f o r a l l n b e l o n g i n g t o N ’ ) ;
10
Chapter 3
1 function [ k ]= fact ( a )
2 k = -1;
3 if (a <0| a >200)
4 disp ( ” I n v a l i d ” ) ;
5 break ;
6 else
7 if ( a ==1| a ==0)
8 k =1;
9 else
10 k = a * fact (a -1) ;
11 end
12 end
13 endfunction
14 a =4;
15 p = fact ( a ) ;
16 disp (p , ’ t h e v a l u e o f 4 ! i s ’ )
11
1 x =1;
2 y =2;
3 z =3;
4 A =[ x ,y , z ];
5 disp ( ’ c a r d i n a l i t y o f s e t A i s : ’ )
6 length ( A )
7 B =[1 ,3 ,5 ,7 ,9]
8 disp ( ’ c a r d i n a l i t y o f s e t B i s : ’ )
9 length ( B )
10
11 // 3 . 9 ( b )
12 disp ( ’ t h e s e t E h a s t h e f o l l o w i n g e l e m e n t s )
13 E = [ 2 , 4 , 6 % i n f ] // s e t E i s t h e s e t o f a l l p o s i t i v e
e v e n numbers and N i s t h e s e t o f a l l n a t u r a l
numbers
14 d i s p ( ’ function f : N to E is defined . So , E has the same
cardinality as N ’)
15 disp ( ’ s e t E i s c o u n t a b l y i n f i n i t e : ’ )
16 for x =2:2: %inf
17 y =2* x ;
18 disp ( y )
19 end
1 x = poly (0 , ’ x ’ ) ;
2 p = 2* x ^3 -7* x ^2+4* x -15;
3 disp (p , ’ t h e p o l y n o m i a l i s ’ )
4 k = horner (p ,5) ;
5 disp (k , ’ v a l u e o f t h e p o l y n o m i a l a t x=5 i s ’ )
12
1 V = int32 ([258 ,60]) ;
2 thegcd = gcd ( V ) ;
3 disp ( thegcd , ’ t h e gcd o f t h e two numbers 258 and 60
is ’)
13
Chapter 5
1 u =[2 ,3 , -4];
2 v =[1 , -5 ,8];
3 u+v
4 5* u
5 -v
6 2* u -3* v
7 u .* v ;
8 k = sum ( u .* v ) ;
9 disp (k , ’ d o t p r o d u c t o f t h e two v e c t o r s ’ )
10 l = norm ( u ) ;
11 disp (l , ’ norm o r l e n g t h o f t h e v e c t o r u ’ )
1 u =[5 ,3 , -4] ’
2 v =[3 , -1 , -2] ’
3 2* u -3* v
4 k = sum ( u .* v ) ;
14
5 disp (k , ’ The d o t p r o d u c t o f t h e two v e c t o r s u and v
is : ’)
6 l = norm ( u ) ;
7 disp (l , ’ The l e n g t h o r norm o f t h e v e c t o r u i s : ’ )
1 a =[7 , -4 ,5];
2 b =[3 ,2 , -1] ’;
3 k=a*b;
4 disp (k , ’ p r o d u c t o f a and b i s ; ’ )
5 p =[6 , -1 ,8 ,3];
6 q =[4 , -9 , -2 ,5] ’;
7 l=p*q;
8 disp (l , ’ p r o d u c t o f p and q i s : ’ )
15
1 A =[1 3;2 -1];
2 B =[2 0 -4;5 -2 6];
3 A*B
4 A =[1 2;3 4]
5 B =[5 6;0 -2];
6 A*B
7 B*A
8 disp ( ’ m a t r i x m u l i t p l i c a t i o n i s n o t c o m m u t a t i v e s i n c e
AB may n o t be e q u a l t o BA ’ )
16
1 A =[5 4;2 3];
2 det ( A ) ;
3 disp ( det ( A ) , ’ d e t e r m i n a n t o f A ’ )
4 B =[2 1; -4 6]
5 det ( B ) ;
6 disp ( det ( B ) , ’ d e t e r m i n a n t o f B ’ )
7 C =[2 1 3;4 6 -1;5 1 0]
8 disp ( det ( C ) , ’ d e t e r m i n a n t o f C ’ )
17
Chapter 6
Counting
18
14 G = union (E , F ) ; // e v e n t o f c h o o s i n g a number which
i s prime or even
15 k = length ( G ) ;
16 disp (k , ’ number o f ways o f c h o o s i n g a number which i s
prime or even ’ )
1 disp ( ’ a l i c e n s e p l a t e c o n t a i n s two l e t t e r s f o l l o w e d
by t h r e e d i g i t s where f i r s t d i g i t can n o t be z e r o
’)
2 n =26; // number o f e n g l i s h l e t t e r s
3 n * n ; // number o f ways o f c h o o s i n g two l e t t e r s i n
the l i c e n s e p l a t e
4 p =10; // number o f d i g i t s (0 −9)
5 (p -1) * p * p ; // number o f ways t o s e l e c t t h e t h r e e
d i g i t s with the f i r s t d i g i t not being z e r o
6 k = n * n *( p -1) * p * p ;
7 disp (k , ’ t o t a l number o f l i c e n s e p l a t e s t h a t can be
printed ’)
8
9 disp ( ’ a p r e s i d e n t , a s e c r e t a r y and a t r e a s u r e r h a s
t o be e l e c t e d i n an o r g a −n i s a t i o n o f 26 members .
No p e r s o n i s e l e c t e d t o more t h a n one p o s t i o n ’ )
10 t =26; // t o t a l number o f members i n t h e o r g a n i s a t i o n
11 j = t *( t -1) *( t -2) ;
12 disp (j , ’ number o f ways t o e l e c t t h e t h r e e o f f i c e r s (
president , secretary , treasurer ’)
1 disp ( ’ To f i n d : f a c t o r i a l o f a 6 ’ )
2 facto2 =2*1;
19
3 facto3 =3* facto2
4 facto4 =3* facto3
5 facto4 =4* facto3
6 facto5 =5* facto4
7 facto6 =6* facto5
8
9 k =8*7* factorial (6) / factorial (6) ;
10 disp (k , ’ v a l u e o f 8 ! / 6 ! i s : ’ )
11 j =12*11*10* factorial (9) / factorial (9) ;
12 disp (j , ’ v a l u e o f 1 2 ! / 9 ! i s : ’ )
1 function [ k ]= func1 (n , r ) // c a l c u l a t i n g b i n o m i a l
coefficient
2 k = factorial ( n ) /( factorial ( r ) * factorial (n - r ) ) ;
3 endfunction
4 func1 (8 ,2)
5 func1 (9 ,4)
6 func1 (12 ,5)
7 func1 (10 ,3)
8 func1 (13 ,1)
9
10 p = factorial (10) /( factorial (10 -7) * factorial (7) ) ;
// c a l c u l a t i n g 10C7
11 q = factorial (10) /( factorial (10 -3) * factorial (3) ) //
c a l c u l a t i n g 10C3
12 disp (p , ’ v a l u e o f 10C7 i s ’ )
13 // 10−7=3 s o 10C7 can a l s o be computed a s 10C3
14 // b o t h p and q have same v a l u e s but s e c o n d method
s a v e s t i m e and s p a c e
20
1 disp ( ’ f i n d i n g t h e number o f t h r e e − l e t t e r words u s i n g
o n l y t h e g i v e n s i x l e t t e r s (A, B , C , D, E , F ) w i t h o u t
repetition ’)
2 n =6; // t o t a l number o f l e t t e r s
3 l1 = n ; // number o f ways i n which f i r s t l e t t e r o f t h e
word can be c h o s e n
4 l2 =n -1; // number o f ways i n which s e c o n d l e t t e r o f
t h e word can be c h o s e n
5 l3 = n -2; // number o f ways i n which t h i r d l e t t e r can
be c h o s e n
6 k = l1 * l2 * l3 ;
7 disp (k , ’ number o f t h r e e − l e t t e r words possible ’)
1 function [ k ]= funct1 (n ,p , q )
2 k = factorial ( n ) /( factorial ( p ) * factorial ( q ) ) ;
3 endfunction
4 k = funct1 (7 ,3 ,2) // i n ”BENZENE” t h r e e l e t t e r s are
a l i k e ( t h e t h r e e Es ) and two a r e a l i k e ( t h e two Ns
)
5 disp (k , ’ The number o f s e v e n − l e t t e r words t h a t can be
f o r m e d u s i n g l e t t e r s o f t h e word BENZENE ’ )
6
7 disp ( ’ a s e t o f 4 i n d i s t i n g u i s h a b l e r e d c o l o u r e d
f l a g s , 3 i n d i s t i n g u i s h a b l e w h i t e f l a g s and a b l u e
f l a g i s given ’)
8 j = funct1 (8 ,4 ,3) ;
9 disp (j , ’ number o f d i f f e r e n t s i g n a l s , e a c h c o n s i s t i n g
of eight flags ’)
21
1 disp ( ’ f o u r o b j e c t s a r e g i v e n ( a , b , c , d ) and t h r e e a r e
taken at a time ’ )
2 combinations = factorial (4) /( factorial (4 -3) *
factorial (3) ) ;
3 disp ( combinations , ’ number o f c o m b i n a t i o n s o f t h e
four objects given ’)
4 k = factorial (3) ; // number o f p e r m u t a t i o n s o f o b j e c t s
in a combination
5 permutations = combinations * k ;
6 disp ( permutations , ’ t o t a l number o f p e r m u a t a t i o n s f o r
the problem ’ )
1 function [ k ]= myfunc (n , r )
2 k = factorial ( n ) /( factorial (n - r ) * factorial ( r ) ) ;
3 endfunction
4 k = myfunc (8 ,3) ;
5 disp (k , ’ t h e number o f c o m m i t t e e s o f t h r e e t h a t can
be f o r m e d o u t o f e i g h t p e o p l e ’ )
6
7 cows = myfunc (6 ,3) // number o f ways t h a t a f a r m e r can
c h o o s e 3 cows o u t o f 6 cows
8 pigs = myfunc (5 ,2) // number o f ways t h a t a f a r m e r can
choose 2 p i g s out o f 5 p i g s
9 hens = myfunc (8 ,4) // number o f ways t h a t a f a r m e r can
choose 4 hens out o f 8 hens
10 p = cows * pigs * hens ;
11 disp (p , ’ t o t a l number o f ways t h a t a f a r m e r can
choose a l l these animals ’)
22
1 // e a c h s o l u t i o n t o t h e e q u a t i o n can be v i e w e d a s a
combination of o b j e c t s
2 r =18; // number o f o b j e c t s
3 M =3; // k i n d s o f o b j e c t
4 m = factorial ( r +( M -1) ) /( factorial ( r +( M -1) -(M -1) ) *
factorial (M -1) ) ;
5 disp (m , ’ number o f non n e g a t i v e i n t e g e r s o l u t i o n s o f
t h e g i v e n e q u a t i o n x+y+z =18 ’ )
1 p =12; // t o t a l number o f s t u d e n t s
2 t =3; // number o f teams o r p a r t i t i o n
3 disp ( ’ e a c h p a r t i t i o n o f t h e s t u d e n t s can be a r r a n g e d
i n 3 ! ways a s an o r d e r e d p a r t i t i o n ’ )
4 r = factorial (12) /( factorial (4) * factorial (4) * factorial
(4) ) // number o f o r d e r e d p a r t i t i o n s
23
5 m = r / factorial ( t ) ; // number o f u n o r d e r e d p a r t i t i o n s
6 disp (m , ’ number o f ways t h a t 12 s t u d e n t s can be
p a r t i t i o n e d i n t o t h r e e teams s o t h a t e a c h team
consists of 4 students ’)
1 U =1000; // number o f e l e m e n t s i n t h e s e t o f p o s i t i v e
i n t e g e r s not e x c e e d i n g 1000
2 A = U /3; // number o f e l e m e n t s i n t h e s u b s e t o f
i n t e g e r s d i v i s i b l e by 3
3 B = U /5; // number o f e l e m e n t s i n t h e s u b s e t o f
i n t e g e r s d i v i s i b l e by 5
4 C = U /7; // number o f e l e m e n t s i n t h e s u b s e t o f
i n t e g e r s d i v i s i b l e by 7
5 AandB = floor ( U /(3*5) ) // number o f e l e m e n t s i n t h e
s u b s e t c o n t a i n i n g numbers d i v i s i b l e by b o t h 3 and
5
6 AandC = floor ( U /(3*7) ) // number o f e l e m e n t s i n t h e
s u b s e t c o n t a i n i n g numbers d i v i s i b l e by b o t h 3 and
7
7 BandC = floor ( U /(5*7) ) // number o f e l e m e n t s i n t h e
s u b s e t c o n t a i n i n g numbers d i v i s i b l e by b o t h 5 and
7
8 AandBandC = floor ( U /(3*5*7) ) // number o f e l e m e n t s i n
t h e s u b s e t c o n t a i n i n g numbers d i v i s i b l e by 3 , 5
and 7
9 s =U -( A + B + C ) +( AandB + AandC + BandC ) -( AandBandC ) ; // By
i n c l u s i o n −e x c l u s i o n p r i n c i p l e
10 S = round ( s ) ;
11 disp (S , ’ The number o f i n t e g e r s i n t h e s e t U, which
a r e n o t d i v i s i b l e by 3 , 5 and 7 i s ’ )
24
Chapter 7
Probability Theory
1 S =[1 ,2 ,3 ,4 ,5 ,6]; // s a m p l e s p a c e f o r t h e
r o l l i n g of a die
2 A =[2 ,4 ,6]; // e v e n t t h a t an e v e n number
occurs
3 B =[1 ,3 ,5]; // e v e n t t h a t an odd number
occurs
4 C =[2 ,3 ,5]; // e v e n t t h a t a p r i m e number
occurs
5 disp ( union (A , C ) , ’ s a m p l e s p a c e f o r t h e e v e n t t h a t an
e v e n o r a p r i m e number o c c u r s ’ )
6 disp ( intersect (B , C ) , ’ s a m p l e s p a c e f o r t h e e v e n t t h a t
an odd p r i m e number o c c u r s ’ )
7 disp ( setdiff (S , C ) , ’ s a m p l e s p a c e f o r t h e e v e n t t h a t a
p r i m e number d o e s n o t o c c u r ’ ) // I t i s t h e
complement o f t h e s e t C .
8 intersect (A , B ) // I t i s a n u l l s e t o r n u l l v e c t o r
s i n c e t h e r e can ’ t o c c u r an e v e n and an odd
number s i m u l t a n e o u s l y
9
10 H =0; // ” head ” f a c e o f a c o i n
11 T =1; // ” t a i l ” f a c e o f a c o i n
25
12 S =[ ” 000 ” ,” 001 ” ,” 010 ” ,” 011 ” ,” 100 ” ,” 101 ” ,” 110 ” ,” 111 ” ]
; // s a m p l e s p a c e f o r t h e t o s s o f a c o i n t h r e e
times
13 A =[ ” 000 ” ,” 001 ” ,” 100 ” ]; // e v e n t t h a t two more o r
more h e a d s a p p e a r c o n s e c u t i v e l y
14 B =[ ” 000 ” ,” 111 ” ]; // e v e n t t h a t a l l t o s s e s
a r e t h e same
15 disp ( intersect (A , B ) , ’ s a m p l e s p a c e f o r t h e e v e n t i n
which o n l y h e a d s a p p e a r ’ )
16
17 disp ( ’ E x p e r i m e n t : t o s s i n g a c o i n u n t i l a head a p p e a r s
and t h e n c o u n t i n g t h e number o f t i m e s t h e c o i n
is tossed ’)
18 S =[1 ,2 ,3 ,4 ,5 , %inf ] // The s a m p l e s p a c e h a s
i n f i n i t e elements in i t
19 disp ( ” S i n c e e v e r y p o s i t i v e i n t e g e r i s an e l e m e n t o f
S , the sample space i s i n f i n i t e ”)
1 disp ( ’ E x p e r i m e n t : t h r e e c o i n s a r e t o s s e d and t h e
number o f h e a d s a r e o b s e r v e d ’ )
2 S =[0 ,1 ,2 ,3]; // t h e s a m p l e s p a c e f o r t h e e x p e r i m e n t
where 0 i m p l i e s no heads , 1 i m p l i e s o n l y one head
o u t o f t h e t h r e e c o i n s and s o on
3 disp ( ” t h e p r o b a b i l i t y s p a c e i s a s f o l l o w s ” )
4 P0 =1/8; // p r o b a b i l i t y o f g e t t i n g no head on any o f
t h e c o i n s i . e TTT
5 P1 =3/8; // p r o b a b i l i t y o f g e t t i n g o n l y one head on
any o f t h e c o i n s , o u t o f t h e t h r e e c o i n s i . e HTT,
THT,TTH
6 P2 =3/8; // p r o b a b i l i t y o f g e t t i n g two heads , o u t o f
t h e t h r e e c o i n s i . e THH, HTH,HHT
7 P3 =1/8; // p r o b a b i l i t y o f g e t t i n g a l l t h e t h r e e
h e a d s i . e HHH
26
8 disp ( ”A i s t h e e v e n t t h a t a t l e a s t one head a p p e a r s
and B i s t h e e v e n t t h a t a l l h e a d s o r a l l t a i l s
appear ”)
9 A =[1 ,2 ,3]; // HHH, HHT, HTH, HTT, THH, THT,TTH
10 B =[0 ,3]; //HHH, TTT
11 PA = P1 + P2 + P3 ;
12 disp ( PA , ’ p r o b a b i l i t y o f o c c u r r e n c e o f e v e n t A ’ )
13 PB = P0 + P3 ;
14 disp ( PB , ’ p r o b a b i l i t y o f o c c u r r e n c e o f e v e n t B ’ )
1 disp ( ” E x p e r i m e n t : a c a r d i s s e l e c t e d from a d e c k o f
52 c a r d s ” )
2 disp ( ”A i s t h e e v e n t o f t h e s e l e c t e d c a r d b e i n g a
spade ”)
3 disp ( ”B i s t h e e v e n t o f t h e s e l e c t e d c a r d b e i n g a
f a c e card ”)
4 t =52 ; // t h e t o t a l number o f c a r d s
5 s =13; // number o f s p a d e s
6 PA = s / t ;
7 disp ( PA , ’ p r o b a b i l i t y o f s e l e c t i n g a s p a d e ’ )
8 f =12; // number o f f a c e c a r d s ( j a c k , queen , k i n g )
9 PB = f / t ;
10 disp ( PB , ’ p r o b a b i l i t y o f s e l e c t i n g a f a c e c a r d ’ )
11 sf =3; // number o f s p a d e f a c e c a r d s
12 Psf = sf / t ;
13 disp ( Psf , ” p r o b a b i l i t y o f s e l e c t i n g a s p a d e f a c e c a r d
i s : ”)
27
1 disp ( ” E x p e r i m e n t : s e l e c t i o n o f a s t u d e n t o u t o f 100
students ”)
2 M =30; // no o f s t u d e n t s t a k i n g m a t h e m a t i c s
3 C =20; // no o f s t u d e n t s t a k i n g c h e m i s t r y
4 T =100; // t o t a l no . o f s t u d e n t s
5 PM = M / T // p r o b a b i l i t y o f t h e s e l e c t e d s t u d e n t
taking mathematics
6 PC = C / T // p r o b a b i l i t y o f t h e s e l e c t e d s t u d e n t
taking chemistry
7 MnC =10; // no o f s t u d e n t s t a k i n g m a t h e m a t i c s and
chemistry
8 PMnC = MnC / T // p r o b a b i l i t y o f t h e s e l e c t e d s t u d e n t
t a k i n g m a t h e m a t i c s and c h e m i s t r y b o t h
9 PMorC = PM + PC - PMnC ;
10 disp ( PMorC , ’ p r o b a b i l i t y o f t h e s e l e c t e d s t u d e n t
taking mathematics or chemistry ’ )
1
2
3 //EXAMPLE 7 . 5 ( a )
4
5
6 disp ( ” E x p e r i m e n t : A d i e i s t o s s e d and t h e o u t c o m e s
a r e o b s e r v e d ”);
7
8
9 disp ( ”To f i n d : p r o b a b i l i t y (PM) o f an e v e n t t h a t
one o f t h e d i c e i s 2 i f t h e sum i s 6 ” ) ;
10
11
12 E =[ ” ( 1 , 5 ) ” ,” ( 2 , 4 ) ” ,” ( 3 , 3 ) ” ,” ( 4 , 2 ) ” ,” ( 5 , 1 ) ” ]
//
e v e n t t h a t t h e sum o f t h e two numbers on t h e two
dice is 6
28
13
14
15 A =[ ” ( 2 , 1 ) ” ,” ( 2 , 2 ) ” ,” ( 2 , 3 ) ” ,” ( 2 , 4 ) ” ,” ( 2 , 5 ) ” ,” ( 2 , 6 ) ” ,”
( 1 , 2 ) ” ,” ( 3 , 2 ) ” ,” ( 4 , 2 ) ” ,” ( 5 , 2 ) ” ,” ( 6 , 2 ) ” ] // e v e n t
t h a t 2 a p p e a r s on a t l e a s t one d i e
16
17
18 B = intersect (A , E ) // p o s s i b l e c o m b i n a t i o n o f
numbers on two d i e s u c h t h a t t h e i r sum i s 6 and 2
a p p e a r s a t l e a s t on one d i e
19
20
21 PM =2/5 // s i n c e E h a s 5 e l e m e n t s and B h a s 2
elements
22
23
24
25
26
27 //EXAMPLE 7 . 5 ( b )
28
29 disp ( ”A c o u p l e h a s two c h i l d r e n ” ) ;
30
31
32 b =1; // boy c h i l d
33
34 g =2; // g i r l c h i l d
35
36 S =[11 ,12 ,21 ,22] ; // s a m p l e s p a c e where 11 i m p l i e s
b o t h c h i l d r e n b e i n g boys , 1 2 i m p l i e s f i r s t c h i l d
b e i n g a boy and t h e s e c o n d c h i l d b e i n g a g i r l
and s o on
37
38 disp ( ”To f i n d : p r o b a b i l i t y (PM) t h a t b o t h c h i l d r e n
a r e boys ”);
39
40
41
29
42 // 7 . 5 ( b ) . i
43
44 L = S (: ,1:3) // r e d u c e d s a m p l e s p a c e i f it i s known
t h a t one o f t h e c h i l d r e n i s a boy
45
46
47 PM =1/ length ( L )
48
49
50 // 7 . 5 ( b ) . i i
51
52 R = S (: ,1:2) // r e d u c e d s a m p l e s p a c e if it i s known
t h a t t h e o l d e r c h i l d i s a boy
53
54
55 PM =1/ length ( R )
30
Scilab code Exa 7.7 Independent events
1 H =1; // h e a d s o f a c o i n
2 T =2; // t a i l s o f t h e c o i n
3 S =[111 ,112 ,121 ,122 ,211 ,212 ,221 ,222] // s a m p l e s p a c e
f o r t h e t o s s o f a c o i n t h r e e t i m e s . 111 i m p l i e s
h e a d s a l l t h r e e t i m e s , 1 1 2 i m p l i e s h e a d s on f i r s t
two t o s s e s and t a i l s on t h e t h i r d t o s s
4 A =[111 ,112 ,121 ,122]; // e v e n t t h a t f i r s t t o s s i s
heads
5 B =[111 ,112 ,211 ,212]; // e v e n t t h a t s e c o n d t o s s i s
heads
6 C =[112 ,211]; // e v e n t t h a t e x a c t l y two h e a d s
a p p e a r i n a row
7 PA = length ( A ) / length ( S ) ;
8 disp ( PA , ’ p r o b a b i l i t y o f A i s ’ )
9 PB = length ( B ) / length ( S ) ;
10 disp ( PB , ’ p r o b a b i l i t y o f B i s ’ )
11 PC = length ( C ) / length ( S ) ;
12 disp ( PC , ’ p r o b a b i l i t y o f C i s ’ )
13 AnB = intersect (A , B )
14 AnC = intersect (A , C )
15 BnC = intersect (B , C )
16 PAnB = length ( AnB ) / length ( S ) ;
17 disp ( PAnB , ’ p r o b a b i l i t y o f t h e e v e n t AnB ’ )
18 PAnC = length ( AnC ) / length ( S ) ;
19 disp ( PAnC , ’ p r o b a b i l i t y o f t h e e v e n t AnC ’ )
20 PBnC = length ( BnC ) / length ( S ) ;
21 disp ( PBnC , ’ p r o b a b i l i t y o f t h e e v e n t BnC ’ )
22 if (( PA * PB ) == PAnB ) ,
23 disp ( ”A and B a r e i n d e p e n d e n t ” )
24 else
25 disp ( ”A and B a r e d e p e n d e n t ” )
26 end
31
27 if (( PA * PC ) == PAnC ) ,
28 disp ( ”A and C a r e i n d e p e n d e n t ” )
29 else
30 disp ( ”A and C a r e d e p e n d e n t ” )
31 end
32 if (( PB * PC ) == PBnC ) ,
33 disp ( ”B and C a r e i n d e p e n d e n t ” )
34 else
35 disp ( ”B and C a r e d e p e n d e n t ” )
36 end
1 disp ( ” E x p e r i m e n t :
A and B b o t h s h o o t a t a t a r g e t ” )
2 PA =1/4; // g i v e n p r o b a b i l i t y of A h i t t i n g the t a r g e t
3 PB =2/5; // g i v e n p r o b a b i l i t y of B h i t t i n g the t a r g e t
4 disp ( ”A and B a r e i n d e p e n d e n t e v e n t s s o PA∗PB w i l l
be e q u a l t o p r o b a b i l i t y o f t h e e v e n t o f A and B
b o t h h i t t i n g t h e t a r g e t i . e PAnB” )
5 PAnB = PA * PB ;
6 PAorB = PA + PB - PAnB ;
7 disp ( PAorB , ’ p r o b a b i l i t y o f a t l e a s t one o f them
h i t t i n g the t a r g e t ’ )
1 disp ( ” E x p e r i m e n t : Three h o r s e s r a c e t o g e t h e r t w i c e ” )
2 Ph1 =1/2; // p r o b a b i l i t y o f f i r s t h o r s e w i n n i n g t h e
race
3 Ph2 =1/3; // p r o b a b i l i t y o f s e c o n d h o r s e w i n n i n g t h e
race
4 Ph3 =1/6; // p r o b a b i l i t y o f t h i r d h o r s e w i n n i n g t h e
race
32
5 S =[11 ,12 ,13 ,21 ,22 ,23 ,31 ,32 ,33] // s a m p l e s p a c e where
11 i m p l i e s f i r s t h o r s e w i n n i n g t h e f i r s t and
s e c o n d r a c e both , 1 2 i m p l i e s f i r s t h o r s e w i n n i n g
t h e f i r s t r a c e and s e c o n d h o r s e w i n n i n g t h e
s e c o n d r a c e and s o on
6 P11 = Ph1 * Ph1 // p r o b a b i l i t y o f f i r s t h o r s e w i n n i n g
both r a c e s
7 P12 = Ph1 * Ph2 // p r o b a b i l i t y o f f i r s t h o r s e w i n n i n g
t h e f i r s t r a c e and s e c o n d h o r s e w i n n i n g t h e
second race
8 P13 = Ph1 * Ph3 // p r o b a b i l i t y o f f i r s t h o r s e w i n n i n g
t h e f i r s t r a c e and t h i r d h o r s e w i n n i n g t h e s e c o n d
race
9 P21 = Ph2 * Ph1 // p r o b a b i l i t y o f s e c o n d h o r s e w i n n i n g
t h e f i r s t r a c e and f i r s t h o r s e w i n n i n g t h e s e c o n d
race
10 P22 = Ph2 * Ph2 // p r o b a b i l i t y o f s e c o n d h o r s e w i n n i n g
both the r a c e s
11 P23 = Ph2 * Ph3 // p r o b a b i l i t y o f s e c o n d h o r s e w i n n i n g
t h e f i r s t r a c e and t h i r d h o r s e w i n n i n g t h e s e c o n d
race
12 P31 = Ph3 * Ph1 // p r o b a b i l i t y o f t h i r d h o r s e w i n n i n g
t h e f i r s t r a c e and f i r s t h o r s e w i n n i n g t h e s e c o n d
race
13 P32 = Ph3 * Ph2 // p r o b a b i l i t y o f t h i r d h o r s e w i n n i n g
t h e f i r s t r a c e and s e c o n d h o r s e w i n n i n g t h e
second race
14 P33 = Ph3 * Ph3 // p r o b a b i l i t y o f t h i r d h o r s e w i n n i n g
both the r a c e s
15 disp ( P31 , ’ p r o b a b i l i t y o f t h i r d h o r s e w i n n i n g t h e
f i r s t r a c e and f i r s t h o r s e w i n n i n g t h e s e c o n d
race i s ’)
33
1 n =6; // number o f t i m e s a f a i r c o i n i s t o s s e d and
g e t t i n g a heads i s a s u c c e s s
2 p =1/2; // p r o b a b i l i t y o f g e t t i n g a h e a d s
3 q =1/2 ; // p r o b a b i l i t y o f n o t g e t t i n g a h e a d s
4 P2 =( factorial (6) /( factorial (6 -2) * factorial (2) ) ) * p ^2*
q ^(6 -2) ;
5 disp ( P2 , ’ p r o b a b i l i t y o f g e t t i n g e x a c t l y two h e a d s ( i
. e k =2) ’ )
6
7 P4 =( factorial (6) /( factorial (6 -4) * factorial (4) ) ) * p ^4*
q ^(6 -4) ; // p r o b a b i l t y o f g e t t i n g f o u r h e a d s
8 P5 =( factorial (6) /( factorial (6 -5) * factorial (5) ) ) * p ^5*
q ^(6 -5) ; // p r o b a b i l t y o f g e t t i n g f i v e h e a d s
9 P6 =( factorial (6) /( factorial (6 -6) * factorial (6) ) ) * p ^6*
q ^(6 -6) ; // p r o b a b i l t y o f g e t t i n g f i v e h e a d s
10 PA = P4 + P5 + P6 ;
11 disp ( PA , ’ p r o b a b i l i t y o f g e t t i n g a t l e a s t f o u r h e a d s ( i
. e k =4 ,5 o r 6 ) ’ )
12
13 Pn = q ^6 // p r o b a b i l i t y o f g e t t i n g no h e a d s
14 Pm =1 - Pn ;
15 disp ( Pm , ’ p r o b a b i l i t y o f g e t t i n g one o r more h e a d s ’ )
34
Scilab code Exa 7.13 Probability distribution of a random variable
1 r =[1 ,2 ,3 ,4 ,5 ,6 ,5 ,4 ,3 ,2 ,1];
2 // number o f o u t c o m e s whose sum i s
2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 0 , 1 1 , 1 2 r e s p e c t i v e l y such that
t h e r e i s o n l y 1 outcome i . e ( 1 , 1 ) whose sum i s 2 ,
two o u t c o m e s ( 1 , 2 ) and ( 2 , 1 ) whose sum i s 3 and
s o on
3 t =36; // t o t a l number o f
elements in the sample space o f the experiment o f
tossing a pair of dice
4 for i =1:11;
5 p=r(i)/t;
6 disp ( p )
7 end
8 0.0277778 // p r o b a b i l i t y o f g e t t i n g a sum
of 2
9 0.0555556 // p r o b a b i l i t y o f g e t t i n g a
sum o f 3
10 0.0833333 // p r o b a b i l i t y o f g e t t i n g a sum
of 4
11 0.1111111 // p r o b a b i l i t y o f g e t t i n g a sum
of 5
12 0.1388889 // p r o b a b i l i t y o f g e t t i n g a sum
of 6
13 0.1666667 // p r o b a b i l i t y o f g e t t i n g a sum
of 7
14 0.1388889 // p r o b a b i l i t y o f g e t t i n g a sum
of 8
15 0.1111111 // p r o b a b i l i t y o f g e t t i n g a sum
of 9
16 0.0833333 // p r o b a b i l i t y o f g e t t i n g a sum
o f 10
17 0.0555556 // p r o b a b i l i t y o f g e t t i n g a sum
35
o f 11
18 0.0277778 // p r o b a b i l i t y o f g e t t i n g a sum
o f 12
19 x =[2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12]; // r a n g e s p a c e o f
random v a r i a b l e X which a s s i g n s t o e a c h p o i n t i n
s a m p l e s p a c e t h e sum o f t h e numbers
20 D =[ 2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12; 0.0277778 , 0.0555556
, 0.0833333 , 0.1111111 , 0.1388889 ,0.1666667 ,
0.1388889 ,0.1111111 , 0.0833333 , 0.0555556 ,
0.0277778];
21 disp (D , ’ d i s t r i b u t i o n t a b l e o f X where f i r s t row
g i v e s t h e r a n g e s p a c e and s e c o n d row g i v e s t h e
r e s p e c t i v e p r o b a b i l i t i e s i s as f o l l o w s ’ )
// p r o b a b i l i t y o f g e t t i n g 1 d e f e c t i v e i t e m
8 r2 =9*( factorial (3) /( factorial (3 -2) * factorial (2) ) )
36
// number o f s a m p l e s o f s i z e 3 g e t t i n g 2
d e f e c t i v e item
9 P2 = r2 / t
//
p r o b a b i l i t y o f g e t t i n g 2 d e f e c t i v e item
10 r3 =1; // number o f s a m p l e s o f
s i z e 3 g e t t i n g 3 d e f e c t i v e item
11 P3 = r3 / t // p r o b a b i l i t y o f g e t t i n g 3
d e f e c t i v e item
12 x =[0 ,1 ,2 ,3];
13 p =[ P0 , P1 , P2 , P3 ];
14 D =[0 ,1 ,2 ,3; P0 , P1 , P2 , P3 ];
15 disp (D , ’ d i s t r i b u t i o n t a b l e f o r random v a r i a b l e X t h e
u p p e r row b e i n g v a l u e s o f X ’ )
1 disp ( ”A f a i r c o i n i s t o s s e d s i x t i m e s ” ) ;
2 x =[0 ,1 ,2 ,3 ,4 ,5 ,6]; // number o f h e a d s which can
occur
3 p =[1/64 ,6/64 ,15/64 ,20/64 ,15/64 ,6/64 ,1/64]; //
p r o b a b i l i t y o f o c c u r r i n g o f h e a d s where 1 / 6 4 i s
p r o b a b i l i t y f o r o c c u r r e n c e o f a s i n g l e head , 6 / 6 4
t h a t o f o c c u r r e n c e o f two h e a d s and s o on .
4 r =0;
5 for i =1:7;
6 r = r + (x(i)*p(i));
7 end
8 disp (r , ’ mean o r e x p e c t e d number o f h e a d s a r e ’ )
9
10 disp ( ”X i s a random v a r i a b l e which g i v e s p o s s i b l e
number o f d e f e c t i v e i t e m s i n a s a m p l e o f s i z e 3 ” )
;
11 // Box c o n t a i n s 12 i t e m s o f which t h r e e a r e d e f e c t i v e
12 x =[0 ,1 ,2 ,3]; // p o s s i b l e number o f d e f e c t i v e i t e m s
37
in a smaple o f s i z e 3
13 p =[84/220 ,108/220 ,27/220 ,1/220]; // p r o b a b i l i t y o f
o c c u r r e n c e o f e a c h number i n x r e s p e c t i v e l y where
8 4 / 2 2 0 i s t h e p r o b a b i l i t y f o r g e t t i n g no
d e f e c t i v e item , 1 0 8 / 2 2 0 i s t h a t o f g e t t i n g 1
d e f e c t i v e i t e m and s o on .
14 r =0;
15 for i =1:4;
16 r = r + (x(i)*p(i));
17 end
18 disp (r , ’ e x p e c t e d number o f d e f e c t i v e i t e m s i n a
sample o f s i z e 3 are ’ )
19
20 Ph1 =1/2; // p r o b a b i l i t y o f w i n n i n g t h e r a c e by
f i r s t horse
21 Ph2 =1/3; // p r o b a b i l i t y o f w i n n i n g t h e r a c e by
second horse
22 Ph3 =1/6; // p r o b a b i l i t y o f w i n n i n g t h e r a c e by
third horse
23 //X i s t h e p a y o f f f u n c t i o n f o r t h e w i n n i n g h o r s e
24 X1 =2; //X p a y s $2 a s f i r s t h o r s e w i n s t h e r a c
25 X2 =6; //X p a y s $6 a s s e c o n d h o r s e w i n s t h e r a c e
26 X3 =9; //X p a y s $9 a s t h i r d h o r s e w i n s t h e r a c e
27 E = X1 * Ph1 + X2 * Ph2 + X3 * Ph3 ;
28 disp (E , ’ e x p e c t e d pay o f f f o r t h e r a c e i s ’ )
Scilab code Exa 7.16 Variance and standard deviation of a random vari-
able
38
p r o b a b i l i t i e s o f o c c u r r e n c e o f each value o f X ( x
) i n the d i s t r i b u t i o n such t h a t 1/64 g i v e s the
p r o b a b i l i t y o f o c c u r r e n c e o f no h e a d s a t a l l , 6 / 6 4
g i v e s t h a t o f o c c u r r e n c e o f h e a d s f o r o n l y one
t i m e and s o on
4 k =0;
5 for i =1:7;
6 k = k +(( x ( i ) -u ) ^2) * p ( i ) ;
7 end
8 disp (k , ’ V a r i a n c e o f X i s ’ )
9 s = sqrt ( k ) ;
10 disp (s , ’ S t a n d a r d d e v i a t i o n o f X i s ’ )
11
12 u =0.75; // mean
13 x =[0 ,1 ,2 ,3]; // v a l u e s o f random v a r i a b l e X a s x i n
the p r o b a b i l i t y d i s t r i b u t i o n of X
14 p =[84/220 ,108/220 ,27/220 ,1/220]; // p r o b a b i l i t y o f
v a l u e s i n x which a p p e a r i n d i s t r i b u t i o n t a b l e o f
X
15 g =0;
16 for i =1:4;
17 g = g +(( x ( i ) ) ^2) * p ( i ) ;
18 end
19 h =g -( u * u ) ;
20 disp (h , ’ v a r i a n c e o f X i s ’ )
21 sd = sqrt ( h ) ;
22 disp ( sd , ’ S t a n d a r d d e v i a t i o n f o r X ’ )
1 p =1/5; // p r o b a b i l i t y o f t h e man h i t t i n g a t a r g e t
2 q =1 -1/5; // p r o b a b i l i t y o f t h e man n o t h i t t i n g t h e
target
3 n =100; // number o f t i m e s t h e man f i r e s
4 e=n*p;
39
5 disp (e , ’ e x p e c t e d number o f t i m e s t h e man w i l l h i t
the t a r g e t ’ )
6 r = sqrt ( n * p * q ) ;
7 disp (r , ’ S t a n d a r d d e v i a t i o n ’ )
8
9 p =1/2; // p r o b a b i l i t y o f g u e s s i n g t h e c o r r e c t a n s w e r
i n a f i v e q u e s t i o n t r u e − f a l s e exam
10 n =5; // number o f q u e s t i o n s i n t h e exam
11 g = n * p ;
12 disp (g , ’ e x p e c t e d number o f c o r r e c t a n s w e r s i n t h e
exam ’ )
Scilab code Exa 7.19 Sample mean and Law of large numbers
40
1 disp ( ” a d i e i s t o s s e d 5 t i m e s w i t h t h e f o l l o w i n g
outcomes ”)
2 x1 =3;
3 x2 =4;
4 x3 =6;
5 x4 =1;
6 x5 =4;
7 xmean =( x1 + x2 + x3 + x4 + x5 ) /5 // mean o f t h e o u t c o m e s
8 disp ( ’ f o r a f a i r d i e t h e mean i s 3 . 5 . So law o f l a r g e
numbers t e l l s u s t h a t a s number o f o u t c o m e s
i n c r e a s e f o r t h i s experiment , there i s a g r e a t e r
l i k e l i h o o d t h a t themean w i l l g e t c l o s e r t o 3 . 5 ’ )
41
Chapter 8
Graph Theory
1 // r e f e r t o p a g e 8 . 6
2 disp ( ’ g i v e n a g r a p h w i t h 6 n o d e s v i z . node1 , node2
. . . . node6 ’ )
3 A =[0 1 0 1 1 0;1 0 1 0 1 0;0 1 0 0 0 1;1 0 0 0 0 0;1
1 0 0 0 0;0 0 1 0 0 0];
4 disp (A , ’ The a d j a c e n c y m a t r i x f o r A i s ’ )
5 disp ( ’ s e q u e n c e A i s a p a t h from node4 t o node6 ; but
i t i s n o t a t r a i l s i n c e t h e e d g e from node1 t o
node2 i s u s e d t w i c e ’ )
6 B =[0 0 0 1 1 0;0 0 0 0 1 1;0 0 0 0 0 0;1 0 0 0 0 0;1
1 0 0 0 0;0 1 0 0 0 0];
7 disp (B , ’ The a d j a c e n c y m a t r i x f o r B i s ’ )
8 disp ( ’ s e q u e n c e B i s n o t a p a t h s i n c e t h e r e i s no
e d g e from node2 t o node6 i s u s e d t w i c e ’ )
9 C =[0 0 0 1 1 0;0 0 1 0 1 0;0 1 0 0 1 0;1 0 0 0 0 0;1
1 1 0 0 1;0 0 0 0 1 0];
10 disp (C , ’ The a d j a c e n c y m a t r i x f o r C i s ’ )
11 disp ( ’ s e q u e n c e C i s a t r a i l s i n c e i s no e d g e i s u s e d
twice ’)
12 D =[0 0 0 1 1 0;0 0 0 0 0 0;0 0 0 0 1 1;1 0 0 0 0 0;1
0 1 0 0 0;0 0 1 0 0 0];
42
13 disp (D , ’ The a d j a c e n c y m a t r i x f o r D i s ’ )
14 disp ( ’ s e q u e n c e D i s a s i m p l e p a t h from node4 t o
node6 ’ )
1 disp ( ’ t o f i n d : m i n i m a l s p a n n i n g t r e e ’ )
2 disp ( ’ t h e a d j a c e n c y m a t r i x f o r t h e w e i g h t e d g r a p h (
nodeA , nodeB . . . nodeF ) o f 6 n o d e s i s : ’ )
3 K =[0 0 7 0 4 7;0 0 8 3 7 5;7 8 0 0 6 0;0 3 0 0 0 4;4
7 6 0 0 0;7 5 0 4 0 0]
4 disp ( ’ e d g e s o f t h e g r a p h ’ )
5 AC =7;
6 AE =4;
7 AF =7;
8 BC =8;
9 BD =3;
10 BE =7;
11 BF =5;
12 CE =6;
13 DF =4;
14 M =[ AC , AE , AF , BC , BD , BE , BF , CE , DF ]; // s e t o f a l l e d g e s
15 V = int32 ( M ) ;
16 L = gsort ( V ) // e d g e s s o r t e d i n d e c r e a s i n g o r d e r o f
their weights
17 disp ( ’ d e l e t i n g e d g e s w i t h o u t d i s c o n n e c t i n g t h e g r a p h
u n t i l 5 edges remain ’ )
18 N =[ BE , CE , AE , DF , BD ]; // e d g e s i n minimum s p a n n i n g
tree
19 Sum = sum ( N ) ;
20 disp ( Sum , ’ w e i g h t o f t h e m i n i m a l s p a n n i n g t r e e i s ’ )
21
22
23 disp ( ’ a n o t h e r method o f f i n d i n g a m i n i m a l s p a n n i n g
tree is : ’)
43
24 K = gsort (V , ’ g ’ , ’ i ’ ) // e d g e s s o r t e d i n i n c r e a s i n g
order
25 N2 =[ BD , AE , DF , CE , AF ]; // e d g e s i n minimum s p a n n i n g
tree
26 Sum2 = sum ( N2 ) ;
27 disp ( Sum2 , ’ w e i g h t o f t h e m i n i m a l s p a n n i n g t r e e i s ’ )
44
Chapter 9
Directed graphs
45
10 B4 ( i ) =1;
11 end
12 end
13 disp ( B4 , ’ R e p l a c i n g non z e r o e n t r i e s o f B4 w i t h 1 , we
g e t path ( r e a c h a b i l i t y ) matrix P i s : ’ )
14 disp ( ’ t h e r e a r e z e r o e n t r i e s i n P , t h e r e f o r e t h e
graph i s not s t r o n g l y connected ’ )
46
Chapter 11
1 disp ( ’ D i v i s i o n A l g o r i t h m ’ )
2 a =4461; // d i v i d e n d
3 b =16; // d i v i s o r
4 r = modulo (a , b ) // r e m a i n d e r
5 k = fix ( a / b ) // q u o t i e n t
6 j = b * k + r // d i v i d e n d= d i v i s o r ∗ q u o t i e n t+r e m a i n d e r
7
8 a = -262; // d i v i d e n d
9 b =3; // d i v i s o r
10 k = fix ( a / b ) // r e m a i n d e r
11 r = modulo (a , b ) // q u o t i e n t
12 j = b * k + r // d i v i d e n d= d i v i s o r ∗ q u o t i e n t+r e m a i n d e r
13 disp ( ’ a and j have e q u a l v a l u e s . Hence d i v i s i o n
algorithm i s proved ’ )
1 disp ( ’ D i v i s i b i l i t y and P r i m e s ’ )
47
2 x =50;
3 disp ( ’ p r i m e numbers l e s s t h a n 50 a r e ’ )
4 y = primes ( x )
5
6 disp ( ’ t h e p r i m e f a c t o r i s a t i o n o f 2 1 , 2 4 and 1 7 2 9
r e s p e c t i v e l y are : ’)
7 k = factor (21)
8 l = factor (24)
9 n = factor (1729)
1 disp ( ’ E u c l i d e a n A l g o r i t h m ’ )
2 a =[540 ,168 ,36 ,24];
3 b =[168 ,36 ,24 ,12];
4 for i =1:4
5 V = int32 ([ a ( i ) ,b ( i ) ]) ;
6 thegcd =[];
7 thegcd ( i ) = gcd ( V ) ;
48
8 disp ( thegcd ( i ) )
9 end
10
11 function []= myf ( dividend , divisor )
12 quotient = floor ( dividend / divisor ) ;
13 rem = modulo ( dividend , divisor ) ;
14 k = quotient * divisor + rem ;
15 disp ( k )
16 if ( rem ~=0) then
17 myf ( divisor , rem )
18 end
19 endfunction
20
21 myf (540 ,168)
22
23 disp ( ’ f o r t h e e q u a t i o n 5 40 ∗ x +168∗ y =12 , we a r e g i v e n ’ )
24 a =540;
25 b =168;
26 c =24;
27 d =36;
28 d =a -3* b ; // Eqn ( 1 )
29 c =b -4* d ; // Eqn ( 2 )
30 k =d -1* c ; // Eqn ( 3 )
31 5* d -1* b ; // Eqn ( 4 )
32 k =d - b +4* d ; // s u b s t i t u t i n g v a l u e o f c i n Eqn ( 3 )
from Eqn ( 2 )
33 r =5* a -16* b ;
34 if ( r == k ) then
35 disp ( ’ x=5 and y=16 ’ ) ;
36 end
1 a =2^4*3^3*7*11*13
2 b =2^3*3^2*5^2*11*17
49
3 V = int32 ([ a , b ]) ;
4 [ d ]= gcd ( V )
5 lcm1 =2^4*3^3*5^2*7*11*13*17 // lcm i s t h e p r o d u c t
o f t h o s e p r i m e s which a p p e a r i n e i t h e r a o r b
1 x = poly (0 , ’ x ’ ) ;
2 g =3* x ^2 -7* x +5
3 m = horner (g ,2) // v a l u e o f p o l y n o m i a l a t 2
4 n = horner (g ,8) // v a l u e o f p o l y n o m i a l a t 8
5 j =m - n
6 disp (n , ” f o r n = ” )
7 if ( modulo (j ,6) ==0) then
8 mprintf ( ’ %i i s c o n g r u e n t t o %i ( mod 6 ) ’ ,m , n )
9 end
1 disp ( ’ s o l v i n g f o r t h e c o n g r u e n c e e q u a t i o n 8 x @ 1 2 (
mod 2 8 ) , where @ i s t h e s i g n f o r c o n g r u e n c e ’ )
2 a =8;
3 b =12;
4 m =28;
5 V = int32 ([ a , m ]) ;
6 d = gcd ( V ) ;
7 a1 = a / d ;
8 b1 = b / d ;
9 m1 = m / d ;
10
11 function yd = f ( x )
12 yd = ( a1 * x ) - b1
13 endfunction
50
14
15 disp ( ’ k i s t h e u n i q u e s o l u t i o n o f t h e e q u a t i o n ’ )
16 for i =0 : m1
17 x=i;
18 p=f(x);
19 if ( modulo (p , m1 ) == 0)
20 k=x
21 break ;
22 end
23 end
24
25 s1 = k ;
26 s2 = k + m1 ;
27 s3 = k +( m1 *2) ;
28 s4 = k +( m1 *3) ;
29 disp ( ’ s o l u t i o n s o f t h e o r i g i n a l e q u a t i o n a t d=4 ’ )
30 disp ( s1 )
31 disp ( s2 )
32 disp ( s3 )
33 disp ( s4 )
51
Chapter 12
Algebraic Systems
1 a =(8 -4) -3
2 b =8 -(4 -3)
3 disp ( ’ s i n c e a and b a r e n o t e q u a l s o s u b t r a c t i o n is
non−c o m m u t a t i v e on Z ( s e t o f i n t e g e r s ) ’ )
4
5 a =[1 2;3 4]
6 b =[5 6;0 -2]
7 g= a*b
8 k= b*a
9 disp ( ’ s i n c e g and k a r e n o t e q u a l m a t r i x
m u l t i p l i c a t i o n i s non−c o m m u t a t i v e ’ )
10
11 h =(2^2) ^3
12 j =2^(2^3)
13 disp ( ’ s i n c e h and j are not equal so e x p o n e n t i a l
o p e r a t i o n i s non a s s o c i a t i v e on t h e s e t o f
positive integers N’)
52
1 t = poly (0 , ’ t ’ ) ;
2 f = t ^3+ t ^2 -8* t +4
3 g = factors ( f )
4 disp ( r = roots ( f ) , ’ r o o t s o f f ( t ) a r e a s f o l l o w s : ’ )
5
6 t = poly (0 , ’ t ’ ) ;
7 h = t ^4 -2* t ^3+11* t -10
8 disp ( r = roots ( h ) , ’ t h e r e a l r o o t s o f h ( t ) a r e 1 and −2
’)
1 t = poly (0 , ’ t ’ ) ;
2 f = t ^4 -3* t ^3+6* t ^2+25* t -39
3 g = factors ( f )
4 disp ( r = roots ( f ) , ’ r o o t s o f f ( t ) are as f o l l o w s : ’ )
53
Chapter 15
Boolean Algebra
1 // 0 d e n o t e s F a l s e and 1 d e n o t e s t r u e
2 b =[0 ,1];
3 // b i n a r y o p e r a t i o n + on t h e s e t o f b i t s
4 for i =1:2
5 for j =1:2
6 k = b(i)& b(j);
7 disp ( k )
8 end
9 end
10 // b i n a r y o p e r a t i o n ∗ on t h e s e t o f b i t s
11 for i =1:2
12 for j =1:2
13 k = b(i)| b(j);
14 disp ( k )
15 end
16 end
17 // u n a r y o p e r a t i o n ’ on t h e s e t o f b i t s
18 k =~ b
19 clear ;
20 D =[1 ,2 ,5 ,7 ,10 ,14 ,35 ,70];
21 a =35;
54
22 b =70;
23 V = int32 ([ a , b ]) ;
24 thelcm = lcm ( V ) // a+b=lcm ( a , b )
25 V = int32 ([ a , b ])
26 thegcd = gcd ( V ) // a ∗b=gcd ( a , b )
27 abar =70/ a // a ’=70/ a
55
Chapter 16
Recurrence relations
Scilab code Exa 16.14 Linear homogenous recurrence relations with con-
stant coefficients
1 a =[];
2 a (1) =1; // i n i t i a l c o n d i t i o n
3 a (2) =2; // i n i t i a l c o n d i t i o n
4 disp ( ’ f o r r e c u r r e n c e r e l a t i o n a ( n ) =5∗a ( n −1)−4∗a ( n
−2)+n ˆ2 ’ ) // t h i s i s a s e c o n d o r d e r r e c u r r e n c e
r e l a t i o n w i t h c o n s t a n t c o e f f i c i e n t s . I t i s non
homogenous b e c a u s e o f t h e n ˆ2
5 for n =3:4
6 a ( n ) =5* a (n -1) -4* a (n -2) + n ^2;
7 mprintf ( ’ V a l u e o f a ( %i ) i s : %i \n ’ ,n , a ( n ) )
8 end
9
10 a =[];
11 a (1) =1; // i n i t i a l c o n d i t i o n
12 a (2) =2; // i n i t i a l c o n d i t i o n
13 disp ( ’ f o r r e c u r r e n c e r e l a t i o n a ( n ) =2∗a ( n −1) ∗ a ( n −2)+n
ˆ2 ’ ) // t h i s r e c u r r e n c e r e l a t i o n i s n o t l i n e a r
14 for n =3:4
15 a ( n ) =2* a (n -1) * a (n -2) + n ^2;
16 mprintf ( ’ V a l u e o f a ( %i ) i s : %i \n ’ ,n , a ( n ) )
56
17 end
18
19 a =[];
20 a (1) =1; // i n i t i a l c o n d i t i o n
21 a (2) =2; // i n i t i a l c o n d i t i o n
22 disp ( ’ f o r r e c u r r e n c e r e l a t i o n a ( n )=n∗ a ( n −1)+3∗a ( n −2)
’) // t h i s i s a homogenous l i n e a r s e c o n d o r d e r
recurrence r e l a t i o n without constant c o e f f i c i e n t s
b e c a u s e t h e c o e f f i c i e n t o f a [ n −1] i s n , n o t a
constant
23 for n =3:4
24 a ( n ) = n * a (n -1) +3* a (n -2) ;
25 mprintf ( ’ V a l u e o f a ( %i ) i s : %i \n ’ ,n , a ( n ) )
26 end
27
28
29 a =[];
30 a (1) =1; // i n i t i a l c o n d i t i o n
31 a (2) =2; // i n i t i a l c o n d i t i o n
32 a (3) =1; // i n i t i a l c o n d i t i o n
33 disp ( ’ f o r r e c u r r e n c e r e l a t i o n a ( n ) =2∗a ( n −1)+5∗a ( n −2)
−6∗a ( n −3) ’ ) // t h i s i s a homogenous l i n e a r t h i r d
order r e c u r r e n c e r e l a t i o n with constant
c o e f f i c i e n t s . Thus we n e e d t h r e e , n o t two , i n i t i a l
c o n d i t i o n s to y i e l d a unique s o l u t i o n of the
recurrence relation
34 for n =4:6
35 a ( n ) =2* a (n -1) +5* a (n -2) -6* a (n -3) ;
36 mprintf ( ’ V a l u e o f a ( %i ) i s : %i \n ’ ,n , a ( n ) )
37 end
Scilab code Exa 16.15 Solving linear homogenous recurrence relations with
constant coefficients
1 disp ( ’ r e c u r r e n c e r e l a t i o n o f F i b o n a c c i numbers f [ n ]=
57
f [ n−1]+ f [ n −2] ’ )
2 x = poly (0 , ’ x ’ ) ;
3 g = x ^2 -x -1;
4 disp (g , ’ c h a r a c t e r s t i c e q u a t i o n o f t h e r e c u r r e n c e
relation is : ’)
5 j =[];
6 j = roots ( g ) ;
7 disp (j , ’ r o o t s o f t h e c h a r a c t e r s t i c e q u a t i o n j 1 , j 2 ’ )
8 disp ( ’ f o r g e n e r a l e q u a t i o n f n=Ar ˆn+Br ˆn , v a l u e s o f
Aand B r e s p e c t i v e l y a r e c a l c u l a t e d a s : ’ )
9 disp ( ’ i n i t i a l c o n d i t i o n a t n=0 and n=1 r e s p e c t i v e l y
are : ’)
10 f1 =1;
11 f2 =1;
12 // p u t t i n g t h e v a l u e s o f f 1 and f 2 we g e t t h e
equations to s o l v e
13 D =[ 1.6180340 -0.618034;(1.6180340) ^2 ( -0.618034)
^2];
14 K =[1 1] ’;
15 c =[];
16 c=D\K;
17 A = c (1)
18 B = c (2)
19
20 disp ( ’ t h u s t h e s o l u t i o n i s f [ n
] = 0 . 4 4 7 2 1 3 6 ∗ ( ( 1 . 6 1 8 0 3 4 ) ˆn−(− 0 . 4 4 7 2 1 3 6 ) ˆ n ) ] ’ )
Scilab code Exa 16.16 Solving linear homogenous recurrence relations with
constant coefficients
58
5 j =[];
6 j = roots ( g ) ;
7 disp (j , ’ r o o t s o f the c h a r a c t e r s t i c equation j1 , j 2 ’ )
8 disp ( ’ g e n e r a l s o l u t i o n t [ n ]= c 1 ∗( −1) ˆn+c 2 ∗4ˆ n ) ’ )
9 disp ( ’ i n i t i a l c o n d i t i o n a t n=0 and n=1 r e s p e c t i v e l y
are : ’)
10 t0 =0;
11 t1 =5;
12 // p u t t i n g t h e v a l u e s o f t 0 and t 1 we g e t t h e
equations to s o l v e
13 D =[1 1; -1 4]
14 K =[0 5] ’
15 c =[];
16 c=D\K;
17 c1 = c (1)
18 c2 = c (2)
19 disp ( ’ t h u s t h e s o l u t i o n i s t {n}=4ˆn−(−1) ˆ n ’ )
Scilab code Exa 16.17 Solving linear homogenous recurrence relations with
constant coefficients
59
equations to s o l v e
12 D=[1 0 ; 2 2 ]
13 K=[1 1 ] ’
14 c = linsolve (D , K )
15 D =[1 0;2 2]
16 K =[1 1] ’
17 c =[];
18 c=D\K;
19 c1 = c (1)
20 c2 = c (2)
21 disp ( ’ t h u s t h e s o l u t i o n i s t {n}=2∗n−n ∗ 2 ˆ ( n−1) ’ )
Scilab code Exa 16.18 Solving linear homogenous recurrence relations with
constant coefficients
60
16 c1 = c (1)
17 c2 = c (2)
18 disp ( ’ t h u s t h e s o l u t i o n i s a [ n ]=0.75∗(3ˆ n ) +0.25∗(1ˆ n
) ’)
Scilab code Exa 16.19 Solving general homogenous linear recurrence re-
lations
61