MATLAB

Help on conv function help conv

Online MATLAB help at mathworks.com

End a command with a semicolon, ";", to suppress the output. All examples below end commands with a semicolon. Remove the semicolon to print the results for that line.
Use a percent, "%", to start a comment

i=1 % everything after the "%" is ignored - a comment
n=i+2; % assign n and don't print the results
m=i+10 % assign m and print the results

Create: $n(z)=10z^3+5z+12$ n=[10 0 5 12]; % as an array of coefficients
% alternate method
T=0.1; % sample time
z=tf('z',T);
n=10*z^3+5*z+12

Multiply: $d(z)=(z^2+10)(z^3+3z^2+z+2)$ d=conv([10 0 10],[1 3 1 2]);
% alternate method
T=0.1; % sample time
z=tf('z',T);
d=(z^2+10)*(z^3+3*z^2+z+2)

Create from polynomials: $G(z)=\frac{z-0.3}{z^2-0.1z+2.1}$ with a sample time $T=0.1s$ num=[1 -0.3];
den=[1 -0.1 2.1];
T=0.1; % sample time
g=tf(num,den,T)

Create from poles and zeros: $zeros: -0.3;\quad poles: -0.5,0.2;\quad gain: 23$ T=0.1; % sample time
g=zpk([-0.3],[-0.5 0.2], 23,T)

Create transfer functions symbolically: $G(z)=\frac{z-0.3}{z^2-0.1z+2.1}$ with a sample time $T=0.1s$ T=0.1; % sample time T=0.1s
z=tf('z',T); % create z;
g=(z-0.3)/(z^2 -0.1*z+2.1)

Extract numerator and denominator polynomials from a transfer function T=0.1; % sample time T=0.1s
gz=tf([1 0.1],[1 -0.5], T); % create a transfer function
[num,den]=tfdata(gz,'v') % extracts the numerator and demoninator from gz

Series: $G(s)H(S)$ T=0.1; % sample time
g=tf([1],[1 4]);
h=tf([1 2],[1 2 3]);
gh=g*h; % method 1
gh=series(g,h) % method 2

Feedback: create system with $d(z)=(z^2+10)(z^3+3z^2+z+2)$ and unity negative feeback T=0.1; % sample time
g=tf([1 0 10],[1 3 1 2],T);
fb=feedback(g,1); % method 1
fb=minreal(g/(1+g)) % method 2, minreal takes care of pole/zero cancellation

Step response for: $G(z)=\frac{5}{z-0.3}$ with a sample time $T=0.1s$ T=0.1; % sample time
g=tf([1],[1 0.3],T);
step(g);

System response for: $G(z)=\frac{5}{z-0.3}$ with a sample time $T=0.1s$ u=ones(size(0:10)); % Define input to be "1" from samples 0 to 10. This can be any sequency you want
num=[5]; % Numerator coefficients of z
den=[1 -0.3]; % Denominator coefficients of z
y=dlsim(num,den,u); % Do the simulation, displaying resulting samples
plot(0:10,y,'o',0:10,y) % Plot samples as "o" connected by lines

Discretize $G(s)=\frac{5}{s+5}$ with a sample time $T=0.1$ via trapezoidal (tustins) integration. g=tf(5,[1 5]); % continuous transfer function
T=0.1; % sampling time
gtrap=c2d(g,T,'tustin') % g(z) via trapezoidal integratin

Discretize $G(s)=\frac{5}{s+5}$ with a sample time $T=0.1$ via tustin integration with prewarping. g=tf(5,[1 5]); % continuous transfer function
T=0.1; % sampling time
wc=6; % prewarp frequency
gprewarp=c2d(g,T,'prewarp',wc) % g(z) via bilinear with prewarping

Discretize $G(s)=\frac{5}{s+5}$ with a sample time $T=0.1$ via pole zero matching. g=tf(5,[1 5]); % continuous transfer function
T=0.1; % sampling time
gpzmap=c2d(g,T,'matched') % g(z) via pole zero matching

Discretize $G(s)=\frac{5}{s+5}$ with a sample time $T=0.1$ via Zero Order Hold. g=tf(5,[1 5]); % continuous transfer function
T=0.1; % sampling time
gzoh=c2d(g,T,'zoh'); % g(z) via a zero-order-hold on the input. (default)
gzoh=c2d(g,T) % can omit 'zoh' since it is the default mapping

Convert fransfer function $G(s)=\frac{s+10}{s^2+5}$ to state space form $\dot{\bold{x}} = \bold{Ax}+ \bold{Bu}, \bold(y)=\bold{Cx}+\bold{Du}$ num = [1 10]; % transfer function numerator
den = [1 0 5]; % transfer function denominator
[A,B,C,D]=tf2ss(num,den) % convert to state space form

Transform state space to new state variables using transform $\bold{z} = \bold{Tx}$ % create state space form num = [1 10 2]; % transfer function numerator
den = [1 0 5 10]; % transfer function denominator
[A,B,C,D]=tf2ss(num,den); % convert to state space form
T= flipud(eye(3)); % transfrom to reverse the states. T can be any nonsingular matrix
[Al,Bl,Cl,Dl] = ss2ss(A,B,C,D,T) % transform A,B,C,D to from with new state orientation

Convert state space form to a transfer function % system in state space form
A = [0 1 0;0 0 1; -4 -3 -2];
B = [0;0;1];
C=[6 5 1];
D=0;
[num,den] = ss2tf(A,B,C,D) % convert SS form to Transfer Function for

Find eigen values and vectors of a matrix A = [0 1 0;0 0 1; -4 -3 -2];
[vectors,values] = eig(A)

Find inverse of a matrix A = [0 1 0;0 0 1; -4 -3 -2];
Ainv = inv(A) % inverse of A

Transfrom state space system to canonical form % system in state space form
A = [0 1 0;0 0 1; -4 -3 -2];
B = [0;0;1];
C=[6 5 1];
D=0;
[Ac,Bc,Cc,Dc] = canon(A,B,C,D,'modal') % transform to canonical form

Convert A,B,C,D form to MATLAB LTI form % system in state space form
A = [0 1 0;0 0 1; -4 -3 -2];
B = [0;0;1];
C=[6 5 1];
D=0;
sys = SS(A,B,C,D) % convert to SS LTI model

Plot step response for system in state space form % system in state space form
A = [0 1 0;0 0 1; -4 -3 -2];
B = [0;0;1];
C=[6 5 1];
D=0;
sys= ss(A,B,C,D); % create LTI model
[y,t,x]=step(sys); % return the step response values step(sys) % plot the step response values

Plot initial response for system in state space form % system in state space form
A = [0 1 0;0 0 1; -4 -3 -2];
B = [0;0;1];
C=[6 5 1];
D=0;
x0 = [1 0 0]; % initial value of state variable
sys= ss(A,B,C,D); % create LTI model
[y,t,x]=initial(sys,x0); % return the step response values initial(sys,x0); % plot the step response

Linear simulation for system in state space form % system in state space form
A = [0 1 0;0 0 1; -4 -3 -2];
B = [0;0;1];
C=[6 5 1];
D=0;
t = 0:0.1:10; % time vector
u=ones(size(t)); % Define input to be "1" for all time. This can be any sequence you want.
[y,x]=lsim(A,B,C,D,u,t); % Do the simulation, displaying resulting samples
lsim(A,B,C,D,u,t); % Plot the simulation response

Linear simulation for system in state space form - alternate approach % system in state space form
A = [0 1 0;0 0 1; -4 -3 -2];
B = [0;0;1];
C=[6 5 1];
D=0;
t = 0:0.1:10; % time vector
u=ones(size(t)); % Define input to be "1" for all time. This can be any sequence you want.
sys=ss(A,B,C,D); % convert to a system
[y,x]=lsim(sys,u,t); % Do the simulation, displaying resulting samples
lsim(sys,u,t); % Plot the simulation response

Pole placement for a model $\bold{\dot{x}} = \bold{Ax} + \bold{B}u$. Works for continuous and discrete systems. % discrete state space system
A = [1 0.1; 0 1];
B = [0.005; 0.1];
p = [0.8, 0.1]; % desired closed loop poles in z-plane
K= place(A,B,p) % find gains via pole placement
% alternate method
[K, Prec, Message] = place(A,B,p) % find gains via pole placement
% Prec is how close the closed looped poles are to
% Message is an error message

Find the controllability matrix for a placement for a model $\bold{\dot{x}} = \bold{Ax} + \bold{B}u$. Works for continuous and discrete systems. % discrete state space system
A = [1 0.1; 0 1];
B = [0.005; 0.1];
CB = ctrb(A,B) % find the controllability matrix
rank(CB) % determine the rank of CB