MATLAB Basics - Help Sheet: David Lindsay November 7, 2002
MATLAB Basics - Help Sheet: David Lindsay November 7, 2002
David Lindsay
November 7, 2002
1
2 LIST OF FIGURES
Contents
1 Explanation of MATLAB screen layout 3
4 Plotting graphs 10
4.1 How to use the plot command 10
4.1.1 Setting the range 10
4.1.2 Selecting colour 10
4.1.3 Selecting line style 11
4.1.4 Selecting marker types 11
4.2 How to set axis titles 12
4.3 Building up multiple plots using hold 12
5 Basic programming 13
5.1 The if statement 13
5.2 The switch statement 14
5.3 The while and for loops 14
5.4 The break and continue statements 14
List of Figures
1 The MATLAB desktop layout 3
2 Example of a multiple plot 13
3 Plot of the function f (w) = 21 w12 + w22 − w1 w2 − 2w1 − 6w2 17
Explanation of MATLAB screen layout 3
Command window This is where all the magic happens; this is where you
will enter data, commands, run functions, see results etc.
4 LIST OF FIGURES
2. Load matrices from external data files (using the load command).
Note: If we added the ; to the end it would not display the matrix in the
command window.
prod(A) (when A is a matrix) returns row matrix whose elements are products of
column elements.
0 0 0 0
zeros(2,4) creates matrix
0 0 0 0
5 5
5*ones(3,2) creates matrix 5 5
5 5
1 0 0
eye(3) creates a 3 × 3 identity matrix 0 1 0
0 0 1
This could be done explicitly by specifying a list of numbers, but this can
become tedious if you wish to plot over a large range. This can be easily
achieved using the : colon symbol like so:
A=1:10 creates a row vector A of integer values ranging from
1 to 10 like so
A=[1 2 3 4 5 6 7 8 9 10]
4 5 6
B=[4 5 6; 7 8 9] this will create a 2×3 matrix
7 8 9
Often you will need to be able to search/extract a matrix for specific values.
You may also wish to find the position index of these values in that matrix.
This can be achieved like so:
A=[2.1 1.7 1.6 1.5 1.9 1.8 1.5 5.1 1.8 1.4 2.2 1.6 1.8]
explicitly creates the matrix A
A=A(A<2)
this searches and removes all elements in A which are greater than or
equal to 2
A=[1.7 1.6 1.5 1.9 1.8 1.5 5.1 1.8 1.4 1.6 1.8]
A=find(isprime(10*A))
this searches through matrix A (each element multiplied by 10) for all
elements which are prime and returns their index, eg. 1.9×10=19 which
is prime and has the 4th position in matrix A
A=[1 4]
4 Plotting graphs
c = cyan
m = magenta
Plotting graphs 11
y = yellow
r = red
g = green
b = blue
w = white
k = black
- = solid
-- = dashed
: = dotted
-. = dash-dot
none = no line
+ = plus sign
o = circle
* = star
x = cross
s = square
d = diamond
^ = up-triangle
v = down-triangle
> = right-triangle
< = left-triangle
p = pentagram
h = hexagram
12 LIST OF FIGURES
xlabel(’w_{1}’,’FontSize’,11)
this will place the text w1 on the horizontal axis
ylabel(’w_{2}’,’FontSize’,11)
this will place the text w2 on the vertical axis
5 Basic programming
For completeness MATLAB offers many of the basic programming statements
and routines commonly used in languages such as C++ and java. A brief
example of each of these is given below.
if isequal(A,B)
Using the same matrix notation as used earlier for the quadprog function we
can formulate the problem using the following matrices:
! ! 1 1 2 !
1 −1 −2 0
Q= , u= , A = −1 2 , a = 2 , c =
−1 2 −6 0
2 1 3
Figure 3 Plot of the function f (w) = 12 w12 + w22 − w1 w2 − 2w1 − 6w2 : This
plot clearly demonstrates a local minima in the surface of the function f (w).
Notice that the solution given by quadprog is located in the low blue area of
this plot.