%% Common usage
% Hjelp!
help tf % How does the tf command work?
doc tf % More in depth
abs((1+3)/sin(1/2)+sqrt(i+1)) % You can use Matlab as a calculator
ls % The command prompt also has many similarities with a linux shell
A=[1 2 3;4 5 6] % Define matrix A: space separates columns, semicolon rows
At=A.'; % Matrix transpose. Semicolon mutes output of the computation
who % Which variables are defined?
edit myfile.m % Create/edit a program "m-file" such as this one
% Executing the commands in an m-file is done by typing the name of the
% file at the prompt (with, or without the ending ".m").
% Matlab has constructs for controlling execution flow, such as if, for,
% while, pause, input.
help for % You can figure out how they work, using the help documentation
%% System representation
% Tramsfer function, using the Laplace variable s
s=tf('s') % s only has to be defined once
G=2/(s+3)
% transfer function, using polynomial coefficient vectors
G=tf(2,[1 3])
% State space system
A=[1 2;0 1]
B=[0;1]
C=[1 1]
D=0;
G=ss(A,B,C,D)
% Transfer function, defined by its poles, zeros and static gain
p=-[1 2]
n=[-3]
K=5
G=zpk(n,p,K)
% Adding a time delay (of 2 seconds )
G.iodelay=2 % or...
G=G*exp(-2*s)
%% Change of representation
% System as transfer function
G=tf(G)
% System on state space form
G=ss(G)
%% Combining systems
P=1/(s+1); C=2;
% Series
G0=P*C
% Parallel
H=P+C
% Negative feedback
Gc=feedback(G0,1)
%% Poles and zeros
pole(Gc)
zero(Gc)
%% Stabilitet och robusthet
% Check stability
isstable(Gc)
min(real(pole(G)))>=0 % An alternative way of doing it
% Amplitude-, phase- and delay margin
margin(G0)
allmargin(G0) % Similar to margin, but no pretty picture
%% Time domain plots
% Impulse and step responses
impulse(G)
step(G) % It is possible to change the time span, type help step to see how
% Simulation with general input - a longer example
t=0:.1:10; % Quick way to obtain the vector [0, 0.1, ..., 0.9, 10]
w=5; % Angular frequency: 5 rad/s
u=sin(t); % Input signal. Many functions work element-wise on vectors
y=lsim(G,u,t); % Output signal from simulation
figure % New figure
plot(t,u,'b') % Plot the input signal in blue
hold on % Prevent next plot from erasing previous one
plot(t,y,'r') % Plot the output in red
% Does the result look the way you expected?
%% Frequency response plots
% Evaluation of system response at certain frequency
fr=evalfr(G0,3) % Frequency response at 3 rad/s
abs(fr) % Amplitude
angle(fr)*180/pi % Phase (in degrees)
% Nyquist curve (Matlab draws for both positive and negative frequencies)
figure % New figure
nyquist(G0)
% Bode plot at certain frequencies
figure
w=logspace(-2,3,100); % Frequencies to plot at - here a logaritmic range
bode(G0,w) % Automatic frequency selection if second argumnt omitted
%% Observability and controllability
A=[1 0;1 1];
B=[1;0];
C=[0 1];
D=0;
G=ss(A,B,C,D);
% Observability
Wo=obsv(G) % Observability matrix
Wo=obsv(A,C) % Same as above
det(Wo)% Observable if full rank, i.e, if determinat non zero
% Controllability
Ws=ctrb(G) % Controllability matrix
Ws=ctrb(A,B) % Same as row above
det(Ws) % Is the system controllable?
%% Symbolic manipulations
% Matlab has some support for symbolic computations. Hint: this could be
% useful for the basic course lab preparations
syms P Q % define symbolic variables P and Q
G0=Q/P % Open loop transfer function
G=G0/(1+G0) % Closed loop transfer function
simplify(G) % Simplified form