Saturday 3 November 2012

1.5 THE COLON SYMBOL (:)

1.5 THE COLON SYMBOL (:)

The colon symbol (:) is one of the most important operators in MATLAB. It
can be used (1) to create vectors and matrices, (2) to specify sub-matrices and
vectors, and (3) to perform iterations. The statement
t1 = 1:6
will generate a row vector containing the numbers from 1 to 6 with unit increment.
MATLAB produces the result
t1 =
1 2 3 4 5 6
Non-unity, positive or negative increments, may be specified. For example,
the statement
t2 = 3:-0.5:1
will result in
t2 =
3.0000 2.5000 2.0000 1.5000 1.0000
The statement
t3 = [(0:2:10);(5:-0.2:4)]
will result in a 2-by-4 matrix
t3 =
0 2.0000 4.0000 6.0000 8.0000 10.0000
5.0000 4.8000 4.6000 4.4000 4.2000 4.0000
Other MATLAB functions for generating vectors are linspace and logspace.
Linspace generates linearly evenly spaced vectors, while logspace generates
logarithmically evenly spaced vectors. The usage of these functions is of the
form:
linspace(i_value, f_value, np)
logspace(i_value, f_value, np)
where
i_value is the initial value
f_value is the final value
np is the total number of elements in the vector.
For example,
t4 = linspace(2, 6, 8)
will generate the vector
t4 =
Columns 1 through 7
2.0000 2.5714 3.1429 3.7143 4.2857 4.8571
5.4286
Column 8
6.0000
Individual elements in a matrix can be referenced with subscripts inside parentheses.
For example, t2(4) is the fourth element of vector t2. Also, for matrix
t3, t3(2,3) denotes the entry in the second row and third column. Using the colon
as one of the subscripts denotes all of the corresponding row or column.
For example, t3(:,4) is the fourth column of matrix t3. Thus, the statement
t5 = t3(:,4)
will give
t5 =
6.0000
4.4000
Also, the statement t3(2,:) is the second row of matrix t3. That is the statement
t6 = t3(2,:)
will result in
t6 =
5.0000 4.8000 4.6000 4.4000 4.2000 4.0000
If the colon exists as the only subscript, such as t3(:), the latter denotes the
elements of matrix t3 strung out in a long column vector. Thus, the statement
t7 = t3(:)
will result in
t7 =
0
5.0000
2.0000
4.8000
4.0000
4.6000
6.0000
4.4000
8.0000
4.2000
10.0000
4.0000
Example 1.1
The voltage, v, across a resistance is given as (Ohm’s Law), v = Ri , where
i is the current and R the resistance. The power dissipated in resistor R is
given by the expression
P = Ri2
If R = 10 Ohms and the current is increased from 0 to 10 A with increments
of 2A, write a MATLAB program to generate a table of current, voltage and
power dissipation.
Solution:
MATLAB Script
diary ex1_1.dat
% diary causes output to be written into file ex1_1.dat
% Voltage and power calculation
R=10; % Resistance value
i=(0:2:10); % Generate current values
v=i.*R; % array multiplication to obtain voltage
p=(i.^2)*R; % power calculation
sol=[i v p] % current, voltage and power values are printed
diary
% the last diary command turns off the diary state
MATLAB produces the following result:
sol =
Columns 1 through 6
0 2 4 6 8 10
Columns 7 through 12
0 20 40 60 80 100
Columns 13 through 18
0 40 160 360 640 1000
Columns 1 through 6 constitute the current values, columns 7 through 12 are
the voltages, and columns 13 through 18 are the power dissipation values

No comments:

Post a Comment