Interactive computing with MATLAB
Interactive computing with MATLAB
with Matlab
Gerald W. Recktenwald
Department of Mechanical Engineering
Portland State University
[email protected]
Starting Matlab
Launch Pad/Workspace:
Used to browse documentation,
or view values of variables in
the workspace
Command History/Current
Directory: View and re-enter
previously typed commands,
or change directories
>> ans/2
ans =
2
>> b = 6
b =
6
>> c = b/a
c =
1.2000
>> sin(ans/4)
ans =
0.7071
• In Matlab version 6 and later the doc function opens the on-line
version of the manual. This is very helpful for more complex commands.
>> doc plot
Syntax:
help functionName
Example:
>> help log
produces
LOG Natural logarithm.
LOG(X) is the natural logarithm of the elements of X.
Complex results are produced if X is not positive.
Syntax:
lookfor string
produces
ACOS Inverse cosine.
ACOSH Inverse hyperbolic cosine.
COS Cosine.
COSH Hyperbolic cosine.
Type variable name and omit the semicolon to print the value of a variable
(that is already defined)
>> x = 5;
>> y = sqrt(59);
>> z = log(y) + x^0.25
z =
3.5341
>> y
y =
7.6811 ( = log(sqrt(59)) + 5^0.25 )
c =
74.2099
Name Meaning
ans value of an expression when that expression is not assigned to
a variable
eps floating point precision
pi π, (3.141492 . . .)
realmax largest positive floating point number
realmin smallest positive floating point number
Inf ∞, a number larger than realmax,
the result of evaluating 1/0.
NaN not a number, the result of evaluating 0/0
Rule: Only use built-in variables on the right hand side of an expression.
Reassigning the value of a built-in variable can create problems
with built-in functions.
√
Exception: i and j are preassigned to −1. One or both of i or j are
often reassigned as loop indices. More on this later.
Consequences:
• Rules of linear algebra apply to addition, subtraction and multiplication.
• Elements in the vectors and matrices are addressed with Fortran-like
subscript notation, e.g.,, x(2), A(4,5). Usually this notation is clear
from context, but it can be confused with a function call,
y = sqrt(5) sqrt is a built-in function
z = f(3) Is f a function or variable?
For manual entry, the elements in a vector are enclosed in square brackets.
When creating a row vector, separate elements with a space.
>> v = [7 3 9]
v =
7 3 9
>> w = v’
w =
2
4
1
7
>> A = [1 2 3; 4 5 6; 7 8 9 ]
A =
1 2 3
4 5 6
7 8 9
>> B = A’
B =
1 4 7
2 5 8
3 6 9
>> y = [1 2 3 4]
y =
1 2 3 4
>> y = y’
y =
1
2
3
4
Note that ones and zeros can also be used to create vectors.
Examples:
>> u = linspace(0.0,0.25,5)
u =
0 0.0625 0.1250 0.1875 0.2500
>> u = linspace(0.0,0.25);
Examples:
>> D = ones(3,3)
D =
1 1 1
1 1 1
1 1 1
>> E = ones(2,4)
E =
1 1 1 1
1 1 1 1
ones and zeros are also used to create vectors. To do so, set either
nrows or ncols to 1.
>> s = ones(1,4)
s =
1 1 1 1
>> t = zeros(3,1)
t =
0
0
0
The eye function creates identity matrices of a specified size. It can also
create non-square matrices with ones on the main diagonal.
Syntax:
A = eye(n)
A = eye(nrows,ncols)
Examples:
>> C = eye(5)
C =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
The diag function can either create a matrix with specified diagonal
elements, or extract the diagonal elements from a matrix
Syntax:
A = diag(v)
v = diag(A)
>> w = diag(B)
w =
1
6
11
>> B = [4 2 2; 3 6 9; 1 1 7];
>> v = diag(B) Extract the diagonal of a matrix
v =
4
6
7
If A is a matrix, A(i,j) selects the element in the ith row and jth column.
Subscript notation can be used on the right hand side of an expression to
refer to a matrix element.
>> A = [1 2 3; 4 5 6; 7 8 9];
>> b = A(3,2)
b =
8
>> c = A(1,1)
c =
1
>> A(4,4) = 11
A =
1 2 3 0
4 5 6 0
7 8 9 0
0 0 0 11
Colon notation is very powerful and very important in the effective use of
Matlab. The colon is used as both an operator and as a wildcard.
Syntax:
startValue:endValue
startValue:increment:endValue
>> t = 0:0.1:0.4
t =
0 0.1000 0.2000 0.3000 0.4000
>> v = 1:5’
v =
1 2 3 4 5
>> A(2,:)
ans =
4 5 6
>> A(1:2,2:3)
ans =
ans =
2 3
5 6
Note: The rand function generates random elements between zero and one.
Repeating the preceding statements will, in all likelihood, produce different
numerical values for the elements of v.
Symbol Operation
.* element-by-element multiplication
./ element-by-element “right” division
.\ element-by-element “left” division
.^ element-by-element exponentiation
Array operators are a very important tool for writing vectorized code.
>> x = u./v
x =
0.2500 0.4000 0.5000
>> A*B
??? Error using ==> *
Inner matrix dimensions must agree.
>> A = [1 2 3 4; 5 6 7 8];
>> B = [8 7 6 5; 4 3 2 1];
>> A*B’
ans =
60 20
164 60
Note: x and y must have the same shape, x1 and y1 must have the
same shape, x2 and y2 must have the same shape, etc.
1
Example: A simple line plot 0.8
−0.2
−0.4
−0.6
−0.8
−1
0 1 2 3 4 5 6 7
The curves for a data set are drawn from combinations of the color,
symbol, and line types in the following table.
Color Symbols Line
y yellow . point ^ triangle (up) - solid
m magenta o circle < triangle (left) : dotted
c cyan x x-mark > triangle (right) -. dashdot
r red + plus p pentagram -- dashed
g green * star h hexagram
b blue s square
w white d diamond
k black v triangle
(down)
Examples:
Put yellow circles at the data points:
plot(x,y,’yo’)
Put black diamonds at each data point and connect the diamonds with
black dashed lines:
plot(x,y,’kd--’)
Note: As expected, use of logarithmic axis scaling for data sets with
negative or zero values results in a error. Matlab will complain
and then plot only the positive (nonzero) data.
10
Example:
8
>> x = linspace(0,3);
6
>> y = 10*exp(-2*x);
>> plot(x,y); 4
0
0 1 2 3
1
10
>> semilogy(x,y);
0
10
-1
10
-2
10
0 1 2 3
Repeat the values of nrows and ncols for all plots in a single figure
window. Increment thisPlot for each plot
Example:
>> x = linspace(0,2*pi);
>> subplot(2,2,1);
>> plot(x,sin(x)); axis([0 2*pi -1.5 1.5]); title(’sin(x)’);
>> subplot(2,2,2);
>> plot(x,sin(2*x)); axis([0 2*pi -1.5 1.5]); title(’sin(2x)’);
>> subplot(2,2,3);
>> plot(x,sin(3*x)); axis([0 2*pi -1.5 1.5]); title(’sin(3x)’);
>> subplot(2,2,4);
>> plot(x,sin(4*x)); axis([0 2*pi -1.5 1.5]); title(’sin(4x)’);
sin(x) sin(2x)
1.5 1.5
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
-1.5 -1.5
0 2 4 6 0 2 4 6
sin(3x) sin(4x)
1.5 1.5
1 1
0.5 0.5
0 0
-0.5 -0.5
-1 -1
-1.5 -1.5
0 2 4 6 0 2 4 6