Summary of MATLAB Commands
Summary of MATLAB Commands
Summary of MATLAB Commands
GUIDE
displays information about the cosine function in the command window, and
1 >> doc cos
Numbers
y = 2.0e-4; sets variable y to the value 2.0 × 10−4 . Note that, e.g, 103 corresponds to 1e3 not 10e3
disp(y); displays the value of y in the command window
format long displays numbers to 16 significant figures
format short displays numbers to 5 significant figures
Vectors
Defining vectors:
a = [1.0, 2.7, 3.2]; defines the row vector (1.0, 2.7, 3.2); the commas are optional
2
defines the column vector 3
b = [2; 3; 1];
1
c = b'; defines the transpose of b, i.e., the row vector (2, 3, 1)
Uniformly spaced vectors:
d = 0:10; defines the vector 0 to 10 in steps of 1: (0, 1, 2, . . ., 10),
e = [0:pi/10:pi]; defines the vector 0 to π in steps of π/10: (0, π/10, 2π/10, . . ., π)
f = linspace(0,pi,11); defines the vector 0 to π with 11 equally-spaced elements (the same vector as e).
g = logspace(0,2,5); defines the vector (100 , 100.5 , 101 , 101.5 , 102 )
Accessing vector elements:
a(3) is the third element of vector a, i.e., 3.2
d(2:4) lists the second to the fourth elements of d, i.e., (1, 2, 3)
f(end) is the last element of f (3.1416, using short format)
Basic operations
Arithmetic operations:
a+c vector addition
a-c vector subtraction
a.*c vector multiplication (element-by-element)
a./c vector division (element-by-element)
1./c take reciprocal (element-by-element)
a.ˆn raise all elements of a to the power n
Matrices
A = [1 2 3 ; 4 5 6]; defines a 2 row × 3 column matrix
size(X) (number of rows, number of columns)
X(4,5) element in 4th row and 5th column
X(:,5) all the elements in the 5th column
X*Y matrix multiplication
X.*Y element-by-element multiplication
Logical operators
If a logical operation is true then the value 1 is returned; if false then the number 0 is returned.
a < b true when a < b; similarly for a > b
a <= b true when a ≤ b; similarly for a >= b
a == b true when a = b
a ∼= b true when a , b
x & y true when both x and y are true (i.e. when x=1 and y=1)
x || y true when at least one of x and y is true (i.e. when x=1 and/or y=1)
∼x logical “not”: changes true to false and vice versa (e.g. if x=1 then ∼x=0)
While loops:
1 n = 1;
2 while n<10
3 n = n+2;
4 end
5 disp(n);
6 >> 11
2
LAB GUIDE PHYS2011/2911 Computational Physics
If statements:
1 x = 4;
2 if x < -1
3 a = -10;
4 elseif x <= 1
5 a = 7;
6 else
7 a = xˆ3;
8 end
9 disp(a);
10 >> 64
This function must be saved in an m-file named polar2cart.m. Functions are called from the command window,
e.g.:
1 [a,b] = polar2cart(2,pi/3);
2 disp(a);
3 >> 1
4 disp(b);
5 >> 1.7320
Plotting
plot(x,y) opens a new figure and plots y vs x in it
plot(x,y,'ko--') creates a plot of y vs x with black ('k') line color, circles ('o') for
markers, and dashed lines ('--') connecting them
plot(x1,y1,'k',x2,y2,'b',...) creates a plot of y1 vs x1 in black, y2 vs x2 in blue, etc on the same figure
The commands for other colours, markers and line shapes can be found using help plot.
figure creates a new figure
figure(n) creates a new figure numbered n, or if it exists, makes it the current figure
hold on allows more than one graph on the same figure for subsequent uses of plot
grid on superimposes grid lines on the graph
ginput allows the coordinates of a point to be read off the screen.
Why?
Ask Matlab why. Go on — try it!