PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
328
A. A Mathematical Introduction to Matrices
next the bottom left entry, namely c2,1 (which is formed by multiplying the
second row of A by the rst column of B)
a b
c d
a+b
c+d
=
a+b
;
and nally the bottom right entry, namely c2,2 (which is formed by mu
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
B.3 Plotting Commands
357
gives
9
8
7
6
5
4
3
2
1
0
0
10
20
30
40
50
60
70
We can use a second argument for hist to dene the number of bins.
hold Stops overwriting of current gure.
5
hold on
hold off
hold
4
2
% Turns the hold on
% Turns the hold off
% Tog
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
356
B. Glossary of Useful Terms
we use the code:
9
xy = [0 0;1 0;
1 1; 0.5 0.5];
A = [0 1 0 0;
1 0 1 1;
0 1 0 1;
0 1 1 0];
gplot(A,xy,*)
axis equal
axis off
8
6
7
grid Add a grid to a plot (or turn it o).
5
grid on
grid off
grid
4
2
% Turns the grid on
%
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
B.3 Plotting Commands
355
returns the list printed on page 348.
get Extracts a particular attribute from a list, retrieved for example by gca
or gcf.
>
>
>
>
>
x = 1:12; y = x.2;
plot(x,y)
h = gcf
a = gca
get(h,Color)
ans =
0.8000
0.8000
0.8000
> get(a,YT
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
352
B. Glossary of Useful Terms
If only one argument is supplied it uses x = 1:m where m is the number of
values of y.
clf This clears the current gure, in fact it removes all children with visible
handles. It removes any axes from the current gure. It is
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
B.3 Plotting Commands
351
#
x = [1 2 3 4 5];
y = [13 12 9 8 15];
bar(x,y)
"
!
This gives:
15
10
5
0
1
2
3
4
5
If only one argument is supplied it uses x = 1:m where m is the number of
values of y.
barh Produces a horizontal bar chart of the data.
#
x = [1
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
350
B. Glossary of Useful Terms
9
t = linspace(0,2*pi);
x = cos(t);
y = sin(t);
z = t;
plot3(x,y,z)
grid on
axis([1.5 1.5 1.5 1.5 pi/2 2*pi])
8
6
7
This gives the plot:
6
5
4
3
2
1
0
1
1.5
1
1.5
0.5
1
0
0.5
0
0.5
0.5
1
1
1.5
1.5
We have used grid on to
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
B.3 Plotting Commands
359
3
10
2
10
1
10
0
10
1
10
2
10
3
10
2
10
1
10
0
10
1
10
2
10
see also semilogx and semilogy.
plot Used to set up gures and plotting of data. The simplest form would be
plot(x,y). This command is extremely powerful and has many var
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
358
B. Glossary of Useful Terms
5
x = 1:10;
y = x.2;
z = sin(x);
plot(x,y,x,z)
legend(x2,sin x, 0)
4
2
3
This gives
4
2
x
sin x
3.5
3
2.5
2
1.5
1
0.5
0
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
The arguments for legend consist of the labelling for the lines a
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
184
6. Matrices
Example 6.9 Consider the matrices constructed using the commands:
> x = 2*ones(3,1);
> y = 2*ones(4,1);
> A = diag(y,0)+diag(x,1);
> B = diag(y,1)+diag(x,2);
The rst command sets x to be a column vector (threebyone) full of 2s
and the
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
366
B. Glossary of Useful Terms
#
angle(sqrt(1)
angle(2)
angle(2)
"
% This gives pi/2
% This gives 0
% This gives pi
!
This can be used for an array of values and returns a vector of the same
size full of the corresponding arguments.
atan2 Gives the arc
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
B.4 General MATLAB Commands
365
25
y = (x3)2
20
15
10
5
0
2
1.5
1
0.5
0
0.5
1
1.5
2
x values
zoom Permits zooming into gures: right click enlarge, left click to reduce. This
has the syntax:
5
zoom on
zoom off
zoom
4
2
% Turns the zoom on
% Turns the zoom
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
364
B. Glossary of Useful Terms
title Used to set the title of a plot or subplot. This has quite simple syntax
and attaches it to the current set of axes.
1
(
x = 0.0:0.1:5.0;
y = 1exp(x);
plot(x,y)
title(f(x) = 1ecfw_x,FontSize,18)
0
)
which gives:
f
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
362
B. Glossary of Useful Terms
1
x = 1:12;
y = 1./x;
h = plot(x,y)
set(h,LineWidth,4)
0
(
)
This gives
1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0
2
4
6
8
10
12
For a list of the attributes of an object (and their values) use get(h), where
h is dened direc
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
360
B. Glossary of Useful Terms
5
x = 0.:0.1:3.0;
y = x.2;
z = 3*x+1;
plot3(x,y,z)
grid on
4
2
3
This gives
10
9
8
7
6
5
4
3
2
1
10
8
3
2.5
6
2
4
1.5
1
2
0.5
0
0
print Used to output the contents of gures to les, for Postscript use print
dps2 output.ps.
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
348
B. Glossary of Useful Terms
Selected = off
SelectionHighlight = on
Tag =
Type = line
UserData = []
Visible = on
The variable h allows us to access all of these commands, which can be changed
using the command set, for instance set(h,MarkerSize,10).
ax
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
B.1 Arithmetic and Logical Operators
337
#
A = [4 3; 2 1];
B = [1 2; 3 4];
C = A.*B;
"
!
This does the calculation elementwise and gives the result:
C=
41
23
32
14
4
6
=
6
4
.
This command can also be used on values which are scalars, so for instance
A =
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
336
B. Glossary of Useful Terms
It is also worth noting that these operations will add (or subtract) scalar
quantities from matrices. For instance:
#
A = ones(3);
B = A + 2;
C = 3  A;
"
!
This produces a threebythree matrix full of threes in B and a th
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
332
A. A Mathematical Introduction to Matrices
These equations can be solved using conventional means. To do this we rst
subtract (A.1a) from (A.1b) to give
x1 + 2x2 (x1 + x2 ) = 5 3
or
x2 = 2,
and now substituting back into either equation (let us use (A
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
B
Glossary of Useful Terms
This appendix is provided purely as a guide. MATLAB has a very informative
help feature help command which is supplemented with several other features
lookfor maths. You can also access the help les on the web helpdesk.
This app
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
A.2 Inverses of Matrices
331
This is simply a matrix whose elements are all equal to zero3 . Not surprisingly,
the zero matrix has no eect when it is added to another matrix (of the same
size). So A + 0 = A = 0 + A. We will make use of this matrix to init
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
A.2 Inverses of Matrices
333
Example A.8 Determine the vector x which satises the equation Ax = b
where
1 2
3 4
5
4 3
10
2 1
A=
1 0 1 0 and b = 15 .
1 1 1 1
20
We enter the matrix A and vector b directly using
> A = [1 2 3 4; .
4 3 2 1; .
1 0 1 0; .
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
330
A. A Mathematical Introduction to Matrices
The next example requires scalar multiplication and the use of the rst of the
results in this example; we have
2
0
1 3
T
3C + 2 (AB) = 3
1
6
21 18
+2
=
6
0
3 9
+
=
4
39
2
42
.
12
45
12
36
,
,
The nal two calc
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
A. A Mathematical Introduction to Matrices
329
Calculate the quantities: AB, BA, A+BT , AC, AT C, 3C+2(AB)T , (AB) C
and nally A (BC), where possible (and if not, state the reason why the calculations cannot be performed).
We shall start (for the rst coup
PROGRAM INTERVENTION, IMPLEMENTATION, AND MONITORING II
HBHE 743

Summer 2014
338
B. Glossary of Useful Terms
#
x = 1:5;
f = x./sin(x);
g = (3*x+4)./(x+2);
"
!
This gives us x = [1 2 3 4 5] and then: f = x/ sin x evaluated at
those points, i.e. [1/sin(1) 2/sin(2) 3/sin(3) 4/sin(4) 5/sin(5)];
and g = (3x + 4)/(x + 2) at the points.