Professional Documents
Culture Documents
Matrix Laboratory
La disciplina
METALIMBAJE
Metodologia de rezolvare a
problemei
MATLAB Review
Program Documentation
Comments!!!
MATLAB Windows
Command Window
-- enter commands and data
-- print results
Graphics Window
-- display plots and graphs
Edit Window
-- create and modify m-files
MATLAB Syntax
No complicated rules
Perhaps the most important thing to
remember is semicolons (;) at the end of a
line suppress output
Type more on to keep text from leaving
screen too fast
diary filename saves a text record of
session
diary off turns it off
MATLAB
A 5.27
5.02
C B' 2.3
7.21
3 2
2
4
3
.
2
9
.
5
0.5 1 7.2 2
>> pi
ans =
3.1416
>> size(pi)
ans =
1 1
pi 3.1416
>> a=[1 2 3; 4 5 6]
a=
1 2 3
4 5 6
>> size(a)
ans =
2 3
1 2 3
a
4 5 6
a=
MATLAB Example
x=3+5-0.2
x =
7.8000
y=3*x^2+5
y =
187.5200
z=x*sqrt(y)
z =
106.8116
A=[1 2 3; 4 5 6]
A =
1
2
3
4
5
6
b=[3;2;5]
b =
3
2
5
C=A*b
C =
22
52
who
Your variables are:
A
C
b
x
whos
Name
A
C
b
x
y
z
y
z
Size
2x3
2x1
3x1
1x1
1x1
1x1
Bytes
48
16
24
8
8
8
Class
double
double
double
double
double
double
array
array
array
array
array
array
save
Saving to: matlab.mat
default filename
save matrix1
filename matrix1
Data types
MATLAB is case-sensitive!!!
Variable Names
Initializing Variables
B = [1 3 5; -6 4 -1]
(continuation)
1 -2; 3 5 1 -3]
E = [A; 1; A];
F = [C(2,3); A]
Matrix Concatenation
x 1 2 3 ;
z x
y 7
9 4
y 1 2 3 7
9 4
u x ; y 1 2 3
7 9 4
v x y ; y x 1 2 3 7 9 4
7 9 4 1 2 3
Colon Operator
1 2
5
1 0
C
3 2 1
4
0 1
2
0
5
1
F
2 1
4
1
Colon Operator
1 2
1 0
1
C
3 2 1
4
0 1
1
1 0
E
3 2 1
Colon Operator
1
1
3
0
2
5
0
1
2 1
1
4
3 2
G
0 1
Colon Operator
Variable_name = a:step:b
time = 0.0:0.5:2.5
time = [0.0, 0.5, 1.0, 1.5, 2.0, 2.5]
values = 10:-1:2
values = [10, 9, 8, 7, 6, 5, 4, 3, 2]
Special Matrices
1 0 0
eye( 3) 0 1 0
0 0 1
0 0
zeros(3,2) 0 0
0 0
1 1 1
ones( 3) 1 1 1
1 1 1
1 1 1 1
ones(2,4)
1 1 1 1
Arithmetic Operations
Between Two Scalars
Operation
addition
subtraction
multipliat ion
division
exponentiation
Algebraic Form
ab
ab
ab
a
b
b
a
Example: x = (a + b*c)/d^2
count = count + 1
MATLAB
ab
ab
a* b
a/b
a^ b
Precedence of
Arithmetic Operations
1
Array Operations
Element-by-Element Operations
Operation
addition
subtraction
multipliat ion
division
exponentiation
Algebraic Form
ab
ab
ab
a
b
b
a
MATLAB
ab
ab
a .* b
a. / b
a .^ b
Array Operation
a 3
5
b 4
6
ab 7
11
1* 2
2
a .* b 3 * 4 12
5 * 6
30
C A .* B 2 12 40
512 1
F ( 3 ).^ B 3 81 243 9
1
2
3
4
A 1 : 4; - 1 : -1 : -4; 3 1 2 - 1 1 2 3 4
3
1
2 1
10
15
20
B A .* 5 5 10 15 20
15
5
10
5
1
8
27
64
C A .^ 3 1 8 27 64
27
1
8
1
Matrix Transpose
x 4 2 3 ;
x'
2
3
y 3 1 2
y'
3
1
- 2
4
4 8
12
x'* y 2 3 1 2 6 2
4
3
9
3 6
3
x * y' 4 2 3 1 ( 4 )( 3 ) ( 2 )( 1 ) 3( 2 ) 4
- 2
Data Files
MAT Files
-- memory efficient binary format
-- preferable for internal use by MATLAB program
ASCII files
-- in ASCII characters
-- useful if the data is to be shared (imported or
exported to other programs)
MATLAB Input
To read files in
Save Files
Load Files
Load Files
Temperature
75.0
73.2
72.6
74.8
79.3
83.2
MATLAB Output
Formatted Output
fprintf (format-string, var, .)
%[flags] [width] [.precision] type
Examples of type fields
%e display in exponential notation
%f display in fixed point or decimal notation
%g display using %e or %f, depending on which
is shorter
%% display %
2 decimals
4 decimals
15 decimals
, , blank
3.14
3.1416e 00
3.141592653589793e 00
x = [5 -2 3 0 1 -2]; format +
x = [+ - + + -]
fprintf( ) of Scalar
temp = 98.6;
fprintf(The temperature is %8.1f degrees F.\n, temp);
The temperature is
98.6 degrees F.
fprintf( ) of
Matrices
Score = [1 2 3 4; 75 88 102 93; 99 84 95 105]
fprintf(Game %1.0f score: Houston: %3.0f Dallas: %3.0f \n,Score)
Built-in Functions
3
a 5 ;
4
sin(3)
exp(3)
sin( a ) sin(5) ; exp(a) exp(5)
sin(4)
exp(4)
Sequence
Selection
Repetition
Logical IF Statements
If (condition) executable_statements
end
y
1
-1
Relation Operators
MATLAB
==
~=
<
<=
>
>=
&
|
~
Interpretation
is equal to
is not equal to
is less than
is less than or equal to
is greater than
is greater than or equal to
and, true if both are true
or, true if either one is true
not
Logical Operators
0 - 1 matrix
0: false ; 1: True
a 2 4 6
b 3 5 1
ab
ans 1 1 0
a ~ b
ans 1 1 1
a b&b c
ans 1 0 0
a b | b c
ans 1 1 0
c 4 3 2
Nested IF Statement
if (condition)
statement block
elseif (condition)
another statement block
else
another statement block
end
Nested IF Statements
y
1
-1
Do loops
Repetition
for i=1:m
for j=1:n
a(i,j)=(i+1)^2*sin(0.2*j*pi);
end
end
For Loops
for k = 1:length(d)
if d(k) < 30
velocity(k) = 0.5 - 0.3*d(k).^2;
else
velocity(k) = 0.6 + 0.2*d(k)-0.01*d(k).^2
end
end
While Loops
MATLAB Graphics
Plotting Commands
plot(x,y) defaults to a blue line
plot(x,y,ro) uses red circles
plot(x,y,m*) uses magenta asterisks
If you want to put two plots on the same
graph, use hold on
plot(a,b,r:)
hold on
plot(a,c,ko)
x=0:0.1:5;
y=2*x.^3-12*x.^2+8*x-6;
H=plot(x,y,'b',x,y,'r*');
set(H,'LineWidth',3,'MarkerSize',12)
xlabel('x'); ylabel('y');
title('f(x)=2x^3-12x^2+8x-6');
print -djpeg075 poly.jpg
element-by-element
operations x.^n
x=0:0.1:10;
y=sin(2.*pi*x)+cos(pi*x);
H1=plot(x,y,'m'); set(H1,'LineWidth',3); hold on;
H2=plot(x,y,'bO'); set(H2,'LineWidth',3,'MarkerSize',10); hold off;
xlabel('x'); ylabel('y');
title('y = sin(2*pi*x)+cos(pi*x)');
print -djpeg075 function.jpg
Plotting Commands
plot (x, y)
Graphics Commands
MATLAB Example
MATLAB Subplots
MATLAB Subplots
subplot ( m, n, p ) -breaks the figure window into m by n small figures,
select the p-th figure for the current plot
figure (3)
subplot (3, 2, 1)
plot (t,wv1)
subplot (3, 2, 2)
plot (t,wv2)
subplot (3, 2, 4)
plot (t, wv1+wv2)
subplot (3, 2, 6)
plot (t, wv1-wv2)
Subplot (m,n,p)
Multiple plots
ba
a f ( x)dx 2 f (a) f (b)
area
a
Functions
One variable
function y = function_name(input arguments)
More than one output variables
function [y, z] = function_name(input arguments)
my_func.m
function y = my_func(x)
% function 1/x^3
y = x.^(-3);
function q = basic_trap(f, a, b)
% basic trapezoid rule
basic_trap.m ya = feval(f, a);
yb = feval(f, b);
q = (b - a)* (ya + yb)/2;
y = basic_trap ('my_func', 1,3)
y=
1.0370
1
dx
3
x
Filename: trap_ex.m
function t = trap_ex(a, b)
t = (b - a) * (a^(-3) + b^(-3)) / 2;
y = trap_ex (1, 3)
y=
1.0370
S = feval(f, a);
for i = 1 : n-1
x(i) = a + h*i;
S = S + 2*feval(f, x(i));
end
S = S + feval(f, b);
I =h*S/2;
one segment
two segments
four segments
eight segments
16 segments
100 segments
500 segments
1000 segments