Page 31 - MATLAB an introduction with applications
P. 31
16 ——— MATLAB: An Introduction with Applications
a
inv(A) Returns the inverse of a square >> = [1 2 3; 4 6 8; –1 2 3];
A
matrix A. >> inv( )
ans =
–
–0.5000 0.0000 0.5000
–5.0000 1.5000 1.0000
3.5000 –1.0000 –0.5000
1.12 RANDOM NUMBERS GENERATION
There are many physical processes and engineering applications that require the use of random numbers in
the development of a solution.
MATLAB has two commands rand and rand n that can be used to assign random numbers to variables.
The rand command: The rand command generates uniformly distributed over the interval [0, 1]. A seed value
is used to initiate a random sequence of values. The seed value is initially set to zero. However, it can be
changed with the seed function.
The command can be used to assign these numbers to a scalar, a vector or a matrix as shown in Table 1.20.
Table 1.20 The rand command
Command Description Example
rand Generates a single random >> rand
number between 0 and 1. ans = 0.9501
rand(1, n) Generates an n elements row >> a = rand(1, 3)
vector of random numbers a = 0.4565 0.0185 0.8214
between 0 and 1.
b
rand( )n Generates an n × n matrix >> = rand(3)
with random numbers b =
between 0 and 1. 0.7382 0.9355 0.8936
0.1763 0.9165 0.0579
0.4057 0.4103 0.3529
c
rand(m, n) Generates an m × n matrix >> = rand(2, 3)
with random numbers c =
between 0 and 1. 0.2028 0.6038 0.1988
0.1987 0.2722 0.0153
randperm (n) Generates a row vector with >> randperm(7)
n elements that are random ans =
permutation of integers 1 5 2 4 7 1 6 3
through n.
1.12.1 The Random Command
MATLAB will generate Gaussian values with a mean of zero and a variance of 1.0 if a normal distribution is
specified. The MATLAB functions for generating Gaussian values are as follows:
randn(n): Generates an n × n matrix containing Gaussian (or normal) random numbers with a mean of 0 and a
variance of 1.
randn(m, n): Generates an m × n matrix containing Gaussian (or normal) random numbers with a mean of 0 and
a variance of 1.
F:\Final Book\Sanjay\IIIrd Printout\Dt. 10-03-09