Summary of MATLAB Commands

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

LAB

GUIDE

Summary of MATLAB Commands


Help
To access help documentation either click on the Help option on the menu bar, or use the help or doc commands:
1 >> help cos

displays information about the cosine function in the command window, and
1 >> doc cos

opens a pop-up window with additional visual aids.

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

PHYS2011/2911 Computational Physics 1


LAB GUIDE PHYS2011/2911 Computational Physics

Standard mathematical functions:


abs(a) absolute value of every element in a
sqrt(a) square root of every element in a
exp(a) e to the power of every element in a; note that exp(1) returns e
log(a) base-e (natural) logarithm of every element in a
log10(a) base-10 logarithm of every element in a
mod(a,n) modulo n of every element in a
Trigonometric functions:
sin(a) sine of every element in a, where a is in radians
sind(a) sine of every element in a, where a is in degrees
asin(a) arcsine (inverse sine) in radians of every element in a
asind(a) arcsine (inverse sine) in degrees of every element in a
atan2(y,x) arctangent valid for all quadrants (see doc atan2)
Other functions:
length(a) number of elements in a
sum(a) sum of all the elements of a
norm(a) norm of the vector a
max(a) largest (i.e., most positive) element in a
min(a) smallest (i.e., most negative) element in a

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)

Loops and if statements


For loops:
1 for n = 1:4
2 vec(n) = nˆ2;
3 end
4 disp(vec);
5 >> [1 4 9 16]

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

Functions and Scripts


Functions and scripts must have the file extension .m. An example function is:
1 function [x,y] = polar2cart(r,theta)
2 x = r*cos(theta);
3 y = r*sin(theta);
4 end

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

An example script (named myscript.m) is:


1 x = 2;
2 y = x+3;

Scripts are also run from the command window, e.g.:


1 myscript;
2 disp(y);
3 >> 5

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!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy