Matrix Laboratory
Important!
You must include comments in your
programs you turn in - otherwise we will
have great difficulty knowing what you
are thinking and doing. You will have
difficulty knowing what you were doing
if you need to reuse the programs.
MATLABs 3 Windows
Command Window
- enter commands and data
- print results
Graphics Window
- display plots and graphs
Edit Window
- create and modify m-files
1 2 3 4
a 3 5 7 9
2 4 6 8
m n matrix
m rows (m = 3)
n columns (n = 4)
Scalar: k = 5
11 matrix
Vector:
b 1 2 3 4
14 matrix, or
a row vector
1
c 3
2
31 matrix, or a
column vector
Variable
may consist up to 31 characters
starting with a letter and followed by any
combination of letters, digits, and underscores
(Variable names must start with a letter)
punctuation marks and spaces should not be
included
e.g., CVEN_302, TIME, Time, time, velocity, force_x,
force_y
Data Type
All numbers are double precision
Text is stored as arrays of characters
You dont have to declare the type of data
(defined when running)
subtraction: a-b
division: a/b
a = 5;
b = 3;
a*b
ans =
15
d = ans * 3
d=
45
e = ans * 3;
e
e=
45
a^2
ans =
25
a/b
ans =
1.6667
format long
a/b
ans =
1.66666666666667
format short
a/b
ans =
1.6667
exp(2)
ans =
e2
7.3891
10^2
ans =
100
log(100)
ln 100
ans =
4.6052
log10(100)
ans =
log 100
2
pi
ans =
3.1416
Array Operations
An array operation is performed element-byelement - Need . in front of the operator
MATLAB: C = A.B;
subtraction: a-b
division: a./b
= ab
1
c 3
2
c = [1; 3; 2]
c=
1
; - a new line
3
2
d = c'
- transpose
d=
1 3 2
b 4 5 6
1
c 3
2
b*c
ans =
d 1 3 2
31
b.*d
ans =
b.*d = [4*1 5*3 6*2]
4 15 12
b./d = [4/1 5/3 6/2]
b./d
ans =
4.0000 1.6667 3.0000
f(2,3)
f = [b; d; [3 6 8]]
ans =
f=
2
b
4 5 6
size(f)
d
1 3 2
[3 6 8]
3 6 8
y 7 9 4
z x y 1 2 3 7 9 4
u x ; y 1 2 3
7 9 4
s = [1 2 3 4; 5 6 7 8]
t = [1 2 3 4....
5 6 7 8]
continue
Colon Operator
Creating new matrices from an existing matrix
C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4]
F = C(:, 2:3) = [2,5; 0,1; 2,-1; 1,4]
all columns
2
5
0
1
2 1
1 4
5
2
0
1
F
2 1
1
4
rows 2 to 3
1
1 0
E
3
2
1
3 2
G
0
1
a : step : b
x = 1:10
x=
1 2 3 4
t = 1 : 2 : 10
t=
1 3 5 7
k = 5:-1:-3
k=
5 4 3 2
-1
-2
-3
10
1.0000
y = linspace (0,1)
a number of 100 for
n_pts is assigned.
eye (m,n)
ones (m,n)
zeros (m,n)
zeros (2,3)
ans =
0 0 0
0 0 0
eye (n)
ones (n)
zeros (n)
Plotting
plot (x, y)
Plotting (continue)
xlabel ( label )
ylabel ( label )
title ( title of the plot )
text ( x_location, y_location, text )
axis ([ x_min x_max y_min y_max ])
xlabel ('x')
ylabel ('y')
title ('sine and cosine')
text (2, 1, 'This is a sine fuction')
axis ([0 2*pi -2 2])
hold off
- text string
Plotting (continue)
subplot ( m, n, figure number ) - break the
figure window into m by n small figures, and
plot the specified figure
figure
subplot (3, 2, 1)
plot (x, y)
subplot (3, 2, 2)
plot (x, z)
subplot (3, 2, 4)
plot (x, y-z)
1
3
5
2
4
6
Initializing Variables
Functions
distinguished from the script that the first line is of the
form
function x = function_name (input arguments)
function [x, y] = function_name (input arguments)
A function has to be stored as a stand-alone file ended
with .m. The name of the function is usually (but
not necessary) the same as the name of the file.
For Loops
(b)
While Loops
(c)
If-Else Structures
For Loops
for x = array
commands
end
Without this line you will get:
Warning: Reference to uninitialized
variable z.
z = 0;
for i = 1:10
y(11-i) = i;
z = z+i;
end
y
y=
10 9
disp(z)
55
Filename: lec2a.m
% Find sum of 1 to b
% Comments begin with %
% cd y:\cven302\
clear all
sum = 0;
b = input('what is the value of b?
for i = 1 : b
sum = sum + i
end
')
While Loops
while expression (is true)
commands
end
eps = 1;
count = 0;
while (1+eps) > 1
eps = eps/2;
count = count + 1;
end
eps = eps*2
display(count-1)
eps =
2.2204e-016
ans =
52
Determining machine
epsilon
If-Else Structures
if expression (is true)
commands
end
if expression
commands 1
else
commands 2
end
if expression 1
commands 1
elseif expression 2
commands 2
elseif expression 3
commands 3
:
:
else
commands n
end
Relational Operators
<
<=
>
>=
==
~=
less than
less than or equal to
greater than
greater than or equal to
equal to
not equal to
Logical Operators
&
|
~
and
or
not
eps = 1;
count = 0;
while (1+eps) > 1
eps = eps/2;
count = count + 1;
if count > 100
break
end
end
eps = eps*2
display(count)
eps = 1;
count = 0;
while (1+eps) > 1 & count < 100
eps = eps/2;
count = count + 1;
end
eps = eps*2
display(count)
')