Академический Документы
Профессиональный Документы
Культура Документы
Basic MATLAB -
matrices
operators
script and function files
flow control
plotting
Basics of Simulink -
state-space models (1st order ordinary diff eqns)
setting integration properties
setting initial conditions
input types
getting data to the workspace
Basic MATLAB
optional windows
workspace
current directory
type commands here
command window
separate window
MATLAB Variables
>>a = 3;
same assignment for “a”
>>a = 3.0;
if a variable is defined,
typing the variable name returns its value
>>a=45.57;
>>a
a=
45.57 Matlab returns the value
>>a=23.2;
>>
>>a=23.2
a=
23.2 Matlab echoes the command
>>
MATLAB Variables
Vectors
column vectors row vectors
⎧1 ⎫
⎪ ⎪
a = ⎨2 ⎬ a = {1 2 3}
⎪3 ⎪
⎩ ⎭
>>a=[1;2;3]; >>a=[1,2,3];
>>a >>a
a= a=
1 1 2 3
2
3
use semi-colon use comma
to separate rows to separate columns
MATLAB Variables
Matrices
⎡1 2 3⎤
2-dimensional matrices a=⎢ ⎥
⎣ 4 5 6 ⎦
>>a=[1,2,3;4,5,6];
>>a
a=
1 2 3
4 5 6
>>a=[1,2,3];
>>a(2) could also reference by a(1,2)
ans =
2 note, a(2,1) would produce an error
because “a” only has one row
MATLAB Variables
Indexing Matrix elements
more examples
⎡1 2 3⎤
a=⎢ ⎥ >>a=[1,2,3;4,5,6];
⎣ 4 5 6 ⎦
assigning
addressing
>>a(2,2)=9;
>>a(2,3) >>a
ans = a=
6 1 2 3
4 9 6
MATLAB Variables
complex-valued numbers
i = −1
z = a + ib Re(z) = a
Im(z) = b
MATLAB Variables
complex-valued numbers
>>a=[1,2]; >>a(1)=1+i*5;
>>a >>a
a= a=
1 2 1.00+5.00i 2.00+0.00i
MATLAB Variables
structures
cell arrays
MATLAB Operations
Basic operations
addition +
subtraction -
multiplication *
division
right division /
?
left division \
>>a=3;b=4;
>>c1=a/b; c1=0.75
so, be careful!
>>c2=a\b; c2=1.3333….
MATLAB Operations
Mixed Real and Complex valued Variables
>>y=x^0.5;
y= x >>y=x^(1/2);
>>y=sqrt(x);
⎡4 5⎤
⎡1 2 3⎤ ⎡2 3 4⎤
A=⎢ ⎥ B=⎢ ⎥ C = ⎢6 7 ⎥
⎣ 4 5 6 ⎦ ⎢ ⎥
⎣ 5 6 7 ⎦ ⎢⎣8 9 ⎥⎦
No error Error
>>D=A+B; >>D=A+C;
>>D=A-B; >>D=A*B;
>>D=A*C; Matrix multiplication
>>D=B*A;
>>D=C*A; not commutative
MATLAB Operations
Left(\) and Right(/) Matrix “division”
C = A −1B >>C=A\B;
C = BA −1 >>C=B/A;
⎡1 4⎤
B = ⎢2 5⎥ ⎡1 − j2 ⎤
⎢ ⎥ b=⎢ ⎥
⎣ 3 − j4 ⎦
⎢⎣ 3 6 ⎥⎦
MATLAB m-files
script files
collection of commands that Matlab executes
when the script is “run”
function files
collection of commands which together
represent a function, a procedure or a method
>>outter
y =
outter.m script
36.0
>>
MATLAB m-files
Function Files
>>r=3; >>h=x2(4.2);
>>d=x2(r); >>h
>>d h =
d = 17.64
9.0 >>
>>
MATLAB m-files
Function Files
loop stops when index > end (or index < end)
MATLAB Flow Control
example
Additional functions
grid on
grid off
axis([xmin,xmax,ymin,ymax])
MATLAB Plotting
example y = sin(t)
“legend” remembers
the order the graphs
were plotted
Simulink Basics
x1 = y x& 1 = x 2
x 2 = y& x& 2 = bo f − c1x 2 − co x1
⎧ x1 ⎫ ⎡ 0 1 ⎤ ⎡0⎤
x=⎨ ⎬ A=⎢ B=⎢ ⎥
⎩x 2 ⎭ ⎣ −co −c1 ⎥⎦ ⎣ bo ⎦
u=f C = [1 0]
Simulink Example
1st Order State-Space Models
Simulink Example
Multi Input Multi Output Systems
use Mux and Demux blocks to combine and extract vector signals