Matlab and The Control System Toolbox
Matlab and The Control System Toolbox
Solution: 1st we build the H(s) function using the Symbolic editor as a
general function of s. 2nd we build the frequency log vector ω of 1028
values. The 3rd step is to use the double and subs functions to calculate
H(jω). 4th we use the log function to determine the magnitude function |
H(jω)| in dB and convert radians to degrees to get the phase function.
The last step is to plot the magnitude and phase functions using the subplot
and semilogx plot options.
Matlab code Example Sym2
'Matlab Script'
syms s % define s as symbolic
hs = 26/(s^2+2*s+26); % define symbolic function
% generate log frequency vector 10^0.01 to 10^2 using 1928 points
Omega = logspace(0.01,2,1028);
NOmega = length(Omega); % get length of frequency vector
Hjw = double(subs(hs,s,Omega*i));
HMag = 20*log10(abs(Hjw)); % convert to dB
HPhase = 180*angle(Hjw)/pi; % get phase angle
% Using subplot and semilogx to plot Magnitude and Phase
subplot(2,1,1); semilogx(Omega,HMag);
xlabel('Frequency (radians/sec)')
ylabel('Magnitude (dB)');grid
title('Magnitude Plot |H(j\omega)|')
subplot(2,1,2); semilogx(Omega,HPhase);
xlabel('Frequency (radians/sec)')
ylabel('Phase (degrees)')
title('Phase Plot P(j\omega)'); grid
Matlab Code Example Sym2
title('Magnitude Plot |H(j\omega)|')
subplot(2,1,2); semilogx(w,HPhase);
xlabel('Frequency (radians/sec)')
ylabel('Phase (degrees)')
title('Phase Plot P(j\omega)')
grid on
Matlab Plot Example Sym2
Laplace Transform Examples
Example 1: write an M-File program to determine the Laplace Transform
of the function
Solution: We 1st build the H(s) function using the Control System Toolbox
function Hs = tf(ns,ds), where ns = 26, and ds = [1 2 26]. Next we use the
bode function to plot the magnitude and phase responses of H(s).
For comparisons, compare the results here with those obtained using the
symbolic editor and the subs function.
Ex1 Matlab script and Bode Plot
b = 26; % b(s) = 26 numerator polynomial
a = [1 2 26]; % a(s) = s^2+2s+26, denominator polynomial
hs = tf(b,a); % build and echo back H(s)=b(s)/a(s)
bode(hs); % display bode mag/phase plot
Laplace Transforms and residue
Example: Build the transfer function H(s) as a zpk object and use the
residue program to determine the residues and poles of H(s). From the
residues and poles write the time solution h(t)=L-1{H(s)}
Solution:
See Matlab script next slide
Laplace Transforms and residue
'Matlab Script'
% define poles, zeros, and gain of H(s)
p = [0 -2 -4 -100];
z = [-1 -3];
k = 600;
HS = zpk(z,p,k) % build transfer function
% using tfdata to get a(s), and b(s)
[b,a,] = tfdata(HS,'s');
% using residue to get residues and poles
[R,P,K] =residue(b,a);
% Display poles and residues
disp(' Residues Poles')
disp([R,P])
Laplace Transforms via residue
The system residues and poles are shown above. From the residues and
poles we write the time solution as:
Sym2Poly_tfs M-File
Problem: develop an M-File program Gs = Sym2Poly_tfs(gs) whose
input is gs a symbolic object function of the variable ‘s’ and return is the
transfer function object Gs
Solution: see code listing below
function Gs = Sym2Poly_tf(gs) % function definition
% Inputs:
% gs = symbolic function of s
% Outputs:
% Gs = transfer function object in minimum form
syms s % define symbolic variable s
[nums_sym,dens_sym] = numden(gs); % get numerator and denominator terms
nums_tf = sym2poly(nums_sym); % convert numerator to tf object format
dens_tf = sym2poly(dens_sym); % convert denominator to tf object format
Gs = tf(nums_tf,dens_tf); % get transfer function as polynomial
Gs = minreal(Gs); % get minimum form
Sym2Poly_tfs Example
Example : Use the symbolic editor to build the function
As a symbolic function and plot the 1st 500 samples of its step response.
Solution: We 1st build the filter function hz and then multiply it by the z-
transform of the step function to form the a-transform of its step response.
Next we take the inverse z-transform to get the sample time response and
then plot its step response.
See Matlab code and display results.
Discrete Filter Example
syms z n M
hz = 0.8*z^-M/(1 + 0.8*z^-M);
M = 11;
% subs for M and display filter function
hz = subs(hz); hz = simplify(hz); pretty(hz)
uz = 1/(1 - z^-1); % build step function
hz_step = hz*uz; % this is the step response
hn_step = iztrans(hz_step); % take inverse z-transform
nk = 0:500; % define # of samples
hn_step = subs(hn_step,n,nk); % replace n with nk
hn_step = double(hn_step); % convert to double
stem(nk,hn_step,'fill'); % plot response
xlabel('n samples');
ylabel('Amplitude');
title('Step Response')
Discrete Filter Example
Discrete Filter Example
Example : write an M-File program to build the filter function
As a polynomial function and plot the 1st 500 samples of its step response.
Solution: We 1st build the filter function hz and then use the function stepz
to plot its step response.
See Matlab code and display results.
Discrete Filter Example
'Discrete Filter Example using tf object'
M = 11;
b = [zeros(1,M) 0.8]; % numerator function b(z)
a = [1 zeros(1,M-1) 0.8]; % denominator a(z)
hz = filt(b,a,1) % build filter function
stepz(b,a,500) % plot step response
Discrete Filter Example
Example : write an M-File program to build the filter function