Sie sind auf Seite 1von 62

Matlab the Symbolic Editor and

the Control System Toolbox


The Symbolic Editor
The Matlab Symbolic Editor allows us to solve discrete time functions
as symbolic equations. The following list a number of the symbolic
functions and utilities:
syms a b c x % define symbolic variable
Fx = a*x^2+b*x+c; % define a symbolic function Fx
y = int(Fx,x); % indefinite integral of function Fx
y = diff(Fx,x); % derivative of the function Fx
y = limit(Fx,x,x0); % limit of Fx @ x = x0
y = subs(Fx,x,x0); % value of the function Fx @ x = x0
Simplifications of Symbolic Objects:
y = simplify(Fx); % perform symbolic simplification
y = collect(Fx); % collect like terms
y = factor(Fx); % factor a symbolic function
Y = expand(Fx); % expansion of symbolic functions
[nx,dx] = numden(Fx) % returns numerator and denominator terms
% of symbolic object
The Symbolic Editor [cont’d]
Solution of Equations:
g = solve(fx,x) % solves the eqn f(x) = 0
g = solve(f1,f2,..fn) % solves [f1,f2,..,fn] = 0
y = finverse(Fx) % returns inverse function
y = subs(fx,x,x0) % substitutes the value of x = x0 in the
% symbolic equation fx, the returned value
% y is a symbolic variable.
Display of Symbolic Functions:
R = vpa(F,d) % uses variable-precision arithmetic (VPA) to
% compute each element of A to d decimal %
digits of accuracy, where d is the current % setting of digits
d = digits % set number of digits used in VPA accuracy.
The Symbolic Editor [cont’d]
Arithmetic Operations:
+ % addition
- % subtraction
* % multiplication
.* % array multiplication
/ % division
./ % array division
^ % scalar raised to a power
.^ % array raised to a power
‘ % complex conjugate transpose
Special Functions:
u = heaviside(t) % the Heaviside step function
d = dirac(t) % the Dirac impulse function
The Symbolic Editor [cont’d]
Conversion Functions:
F = char(f) % conversion of symbolic to character format
F = double(f) % symbolic to double object conversion
F = sym2poly(f) % symbolic to polynomial conversion. E.G.,
% f = s^2+3*s+5  [1 3 5].
f = poly2sym(F) % polynomial to symbolic conversion. E.G.,
% F = [1 2 5 0]  s^3+2*s^2+5*s
S = sym(A) % constructs an object S, of class 'sym',
% from A. If the input argument is a string,
% the result is a symbolic number or a
% variable. If the input argument is a %
numeric scalar or matrix, the result is a % symbolic
representation of the given % numeric values
r = findsym(S) % determines the symbolic variables in the
% function S
The Symbolic Editor [cont’d]
Integral Functions:
L = laplace(F) % is the Laplace transform of the scalar
% symbol F with default independent variable
% t. The default return is a function of s.
% The Laplace transform is applied to a %
function of t and returns a function of s.
F = ilaplace(L) % is the inverse Laplace transform of the
% scalar symbolic object L with default %
independent variable s. The default return % is a function of t.
The inverse Laplace % transform is applied to a function of s and
% returns a function of t
F = ztrans(f) %is the z-transform of the scalar symbol f
% with default independent variable n. The
% default return is a function of z
f = iztrans(F) % is the inverse z-transform of the scalar
% symbolic object F with default independent
% variable z. The default return is a %
function of n.
Symbolic Editor Example Sym1
Example 1: write an M-File program to plot the time function.

Solution: We build the time function using the Symbolic editor as a


general time function. Next we use the input command to enter the data
for a, f, tf, and the number of plot points between 0 and tf. The 1st subs
command is used to evaluate the time function for the values of a and f.
The 2nd subs function, within the for loop, is used to evaluate the time
function at values of t.
The function double converts the symbolic values to double format for the
purposes of plotting the data with the plot command. Note the data xt1 and
t1 are stored as arrays. Notice the use of num2str in the title command to
display the actual values of a and f.
Matlab Code for Example Sym1
'Damped cosine example'
syms t a f % define symbolic variables
xt = exp(-a*t)*cos(2*pi*f*t); % define symbolic time function
% specify function parameters using the keyboard
a = input('Enter the decay constant a = ');
f = input('Enter the sinusoidal frequency f = ');
tf = input('Enter the time period tf = ');
Npts = input('Enter the number of time samples Npts = ');
xt = subs(xt);
t1 = linspace(0,tf,Npts); % plot time period
xt1 = double(subs(xt,t,t1)); % convert to double from sym with t = t1
plot(t1,xt1); grid; % plot function
xlabel('time(sec)'); % add plot labels
ylabel('Amplitude');
% add fancy title using greek characters and num2str
title(['x(t)=e^{',num2str(a),'t}cos(2\pi',num2str(f),'t)'])
Matlab Output for Example Sym1
Symbolic Editor Example Sym2
Example 2: write an M-File program to plot the Magnitude and Phase for
the function.

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

'Laplace Transform Example'


syms a w t s % define symbolic variables
xt = exp(-a*t)*cos(w*t-pi/6);
% evaluate Laplace Transform
XS = laplace(xt);
% simplify and pretty print
XS = simplify(XS);
pretty(XS)
pause
% now print results using vpa
disp('Pretty Print with VPA')
pretty(vpa(XS,4))
Laplace Transform Example 2
Example 2: write an M-File program to determine the Inverse Laplace
Transform of the function

syms s t % define symbolic variables


XS = (s+1)/(s*(s^2+2*s+10));
% evaluate Inverse Laplace Transform
xt = ilaplace(XS);
% simplify and pretty print
xt = simplify(xt);
pretty(xt)
pause
% now print results using vpa
disp('Pretty Print with VPA')
pretty(vpa(xt,4))
Laplace Transform Example 3
Example 3: Use the subs function to plot syms s t % symbolic variables
XS = (s+1)/(s*(s^2+2*s+10));
the inverse Laplace Transform of the
% Get Inverse Laplace Transform
function of example 2 for 0 <t < 10
xt = ilaplace(XS);
% simplify
xt = simplify(xt);
% define time vector 0 to 10
t = 0:0.1:10;
% using subs to get time response
x = double(subs(xt));
% plot time response
plot(t,x)
xlabel('Time
(sec)');ylabel('Amplitude')
grid;
title('Time Plot for Example 3')
The Control System Toolbox
The Control System Toolbox provides functions designed for control
engineering. The Control System Toolbox is a collection of algorithms,
that implements common control system design, analysis, and
modeling techniques.
Control systems can be modeled as transfer functions, in zero-pole-
gain or state-space form. Representation in both continuous-time and
discrete-time systems is possible. Conversions between various model
representations such as transfer functions and zero, pole, gain frorm
are provided. Time responses, frequency responses, and root loci can
be computed and graphed.
Conversion functions provide the capability to convert transfer function
objects to symbolic function objects or symbolic function objects to
transfer function objects.
Useful Control System Toolbox Functions
The following list the Control System Toolbox functions most often used
in this course:
Fz = filt(num,den,Ts) % specify discrete transfer functions
% in DSP format. I.E., Fz =
num(z^- % 1)/den(z^-1), with sample time Ts.
Fs = tf(num,den) % specify a continuous time transfer
% function. I.E., Fs = num(s)/den(s).
Fz = tf(num,den,Ts) % specify a discrete time transfer
% function Fz = num(z)/den(z) with % sample time Ts.
[num,den] = tfdata(Fz,’z’) % get numerator and denominator
terms % of a discrete time transfer function.
[num,den] = tfdata(Fs,’s’) % get numerator and denominator
terms % of a continuous time transfer
% function.
F = minreal(F) % returns the minimum realization of a
% continuous or discrete time transfer
% function.
Useful Control System Toolbox Functions
Fz = filt(num,den,Ts) % specify discrete transfer functions
% in DSP format. I.E., Fz =
num(z^- % 1)/den(z^-1), with sample time Ts.
% The output Fz is a tf object.
Fs = tf(num,den) % specify a continuous time transfer
% function. I.E., Fs = num(s)/den(s). % The output Fs is a
tf object
Fz = tf(num,den,Ts) % specify a discrete time transfer
% function Fz = num(z)/den(z) with % sample time Ts. Fz is a tf
object.
Fs = zpk(z,p,k) % creates a continuous-time zero-pole-
% gain model with zeros z, poles p, and
% gain(s) k. The output Fs is a ZPK
% object
[num,den] = tfdata(Fz,’z’) % get numerator and denominator
terms % of a discrete time transfer function.
[num,den] = tfdata(Fs,’s’) % get numerator and denominator
terms % of a continuous time transfer FF
% function.
Useful Control System Toolbox Functions
[Z,P,K] = zpkdata(Fs,’s’) % returns the zeros, poles, and gain of
% a continuous time transfer function
% F(s).
[Z,P,K] = zpkdata(Fz,’z’) % returns the zeros, poles, and gain of
% a discrete time transfer function
% F(z).
Fz = c2d(Fs,Ts) % convert a continuous time system to a
% discrete time system. The default is
% a ZOH system, see help for other %
methods.
sys = series(sys1,sys2,..) % returns a cascade system LTI
% model of sys = sys1*sys2*···*sysn.
sys = feedback(sys1,sys2) % returns an LTI model sys for the
% negative feedback interconnection % sys = minreal(sys1/
(1+sys1*sys2)).
sys = parallel(sys1,sys2) % forms the basic parallel
connection % sys = sys1 + sys2.
Useful Control System Toolbox Functions
Time and Frequency Analysis of continuous time LTI systems
ltiview % opens the LTI viewer for time and
% frequency domain analysis of LTI % systems
bode(sys) % display the bode response of an LTI
% system sys.
margin(sys) % displays the gain and phase margin
% plots for the LTI system, sys.
step(sys) % computes the step response of a
% continuous time LTI system, sys.
impulse(sys) % computes the impulse response of the
% continuous time LTI system, sys.
[R,P,K]=residue(b,a) % finds the residues, poles, and direct
% term of a partial fraction expansion
% the polynomial h(s)=b(s)/a(s).
rltool % opens root locus window
sisotool % opens single input/single output
% design tool window. Displays both % root locus and
gain/phase margin.
Discrete Time Systems
Discrete time filter functions
y = filter(b,a,X) % filters the data in vector X with the
% filter described by numerator
% coefficient vector b and denominator
% coefficient vector a.
y = filter(hd,X) % filters the data in vector X with the
% filter object hd.
freqz(b,a,...) % plots the magnitude and unwrapped
% phase of the frequency response of % the filter. The
plot is displayed in % the current figure window.
freqz(Hd) % plots the magnitude and unwrapped
% phase of the frequency response of % the filter. The
plot is displayed in % fvtool. The input Hd is a dfilt
% filter object. See help for more % options
impz(b,a) % plots the impulse response of the
% filter hd = b(z)/a(z). See help for % more options.
Discrete Time Systems
Discrete time filter functions
fvtool(b,a) % opens FVTool and computes the
% magnitude response of the digital % filter defined with
numerator, b and % denominator, a. Using FVTool you can
% display the phase response, group
% delay, impulse response, step
% response,
pole-zero plot, and % coefficients of the filter.
fvtool(hd1,hd2,..,hdn) % computes and displays the magnitude
% response of the digital filter
% objects hd1,
hd2,..,hdn.
Control System Toolbox Ex1
Example 1: write an M-File program to plot the Magnitude and Phase for
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 object and use the final value theorem

To show that h(∞) = 1. Next use Sym2Poly_tfs to convert the symbolic


function to a polynomial function and use the impulse function to display
the results and verify that h(∞) = 1.
Solution: see the Matlab code and results on the following pages.
Sym2Poly_tfs Example: Code and Results
syms s % define s as symbolic
hs = 17/(s*(s^2+2*s+17)); % define symbolic transfer function
Hs = sym2poly_tfs(hs); % convert to polynomial transfer function
h_inf = limit(s*hs,s,0); % get h(t = inf) using final value theorem
% print h(t = inf) results using fprintf
fprintf('\n h(inf)= %6.2f \n',double(h_inf))
impulse(Hs,5); % Display impulse response
Poly2Sym_tfs M-File
Problem: develop an M-File program gs = Poly2Sym_tfs(Gs) whose
input is Gs a transfer function object (tf object) function of the variable
‘s’ and return is the transfer function symbolic object (sym object) gs
Solution: see code listing below
function gs = Poly2Sym_tf(Gs) % function definition
% Inputs:
% Gs = tf object transfer function of s
% Outputs:
% gs = sym object function gs = n(s)/d(s)
syms s % define 's' as symbolic
% check class of GS it must be tf or zpk format
hclass = class(GS);
if strcmp(hclass,'tf')==1 |strcmp(hclass,'zpk')==1
[b,a] =tfdata(GS,'s');
else
error('unknown function type')
end
bsym = poly2sym(b,'s'); % symbolic numerator
asym = poly2sym(a,'s'); % symbolic denominator
gs = bsym/asym; % symbolic transfer function
gs = simplify(gs); % simplify transfer function
Poly2Sym_tfs Example
Example : write an M-File program that uses the poly2sym_tfs function to
plot the impulse response of the function:

Solution: We 1st build the H(s) function as a polynomial function


(tf_object) and use Poly2Sym_tfs to convert it to a sym_object function,
h(s). Next we take the inverse Laplace Transform of the function h(s) and
plot its time function.
For comparison purposes, we take the inverse Laplace Transform of the
function h(s) and plot its time function. We use the subplot option to
display the two plots.
Poly2Sym_tfs Example Code
clear % clear workspace
d1s = [1 0]; % d1(s) = s
d2s = [1 2 17]; % d2(s) = s^2+2s+17
ds = conv(d1s,d2s); % d(s) = d1(s)*d2(s)
ns = 17*[1 1]; % n(s) = 17*(s+1)
Hs = tf(ns,ds); % H(s) = ns/ds
tf = 5; % plot time secs
subplot(2,1,1);
impulse(Hs,tf); % display impulse response
title('Impulse Response via Control System Toolbox function impulse')
% convert to symbolic transfer function
syms s t
hs = poly2sym_tfs(Hs);
ht = ilaplace(hs); % get inverse LT
ht = simplify(ht); % simplify and display time response
pretty(ht)
tk = linspace(0,5,500); % build time vector
%
Poly2Sym_tfs Example Code
% for loop builds impulse response time vector
for k = 1:length(tk)
htk(k) = double(subs(ht,t,tk(k)));
end
% plot results
subplot(2,1,2);
plot(tk,htk);
xlabel('Time (sec)')
ylabel('Amplitude');
title('Impulse Response via Symbolic Editor')
Poly2Sym Example Plots
Complex Poles and residue
Complex Pole Example: Given the function H(s) below determine the
following:
1.  Build the function as a zpk object
2.  Determine the residues and poles of the tf object
3.  From the residues and poles write the inverse Laplace Transform
of H(s).
4.  Convert the tf object transfer function to a symbolic transfer
function.
5.  Determine the inverse Laplace Transform of the symbolic
transfer function and compare it to the results of part 3.
Complex Poles and residue Example
Solution: The Matlab code and results are shown below. To determine
the time solution for the complex part we use the equation
Complex Poles and residue Example
z = [-4]; % define zeros of H(s)
% determine poles of H(s)using roots
pcmpx = roots([1 2 10]); % poles of s^2+2s+10
p = [-1 pcmpx(1) pcmpx(2)];
k = 50;
%Build and display transfer function
HS = zpk(z,p,k)
% determine residues and poles of H(s)
[b,a] = tfdata(HS,'s');
[R,P,K] = residue(b,a);
disp(' Residues Poles')
disp([R,P])
% convert to symbolic transfer function
syms s t
hs = poly2sym_tfs(HS); % build symbolic transfer function
ht = ilaplace(hs);
ht = simplify(ht);
disp('This is the symbolic solution')
pretty(ht)
Complex Poles and residue Example
Matlab Solution: The Matlab solution is shown below. Note that the
symbolic and solution via residues using the equation on the previous
page are identical.
A 2nd Complex Pole and residue
Example
Problem: Build and test a M-File program that uses the
residues and poles of the function H(s) to print the closed
form solution of it’s inverse Laplace Transform.
A 2nd Complex Pole and residue
Example-Matlab M-File
function f_residue(R,P)
% Function f_residue
% Purpose: determine closed form solution of L^-1{H(s)}
% Method: Use the method of residues to determine soln as
% fk(t) = Rk*exp(pk*t) for real poles
% fk(t) = 2exp(Re(pk*t)[Re(Rk*)cos(IM(pk*t))+IM(Rk*)sin(IM(pk*t))]
for
% cmplx poles
% Inputs:
% R = residues of the function H(s)
% P = Poles of the function H(s)
% Output:
% Printed values of the functions fk(t)
NPoles = length(P);
k = 0;
for n = 1:NPoles
if (abs(imag(P(n))) == 0)
k = k + 1;
fprintf(['\n f',num2str(k),'= %6.3fexp(%6.3f t)\n'],R(n),P(n))
A 2nd Complex Pole and residue
Example-Matlab M-File
else
if(imag(P(n)) < 0);
k = k + 1; % increment k and check next pole
Alpha = real(P(n));
A = 2*real(R(n));
B = 2*imag(R(n));
w = abs(imag(P(n)));
if (B < 0);
fprintf(['\n f',num2str(k),'= exp(%6.3f t)[%6.3fcos(%6.2f t)…
- %6.3fsin(%6.2f t)]\n'],...
Alpha,A,w,abs(B),w)
else
fprintf(['\n f',num2str(k),'= exp(%6.3f t)[%6.3fcos(%6.2f t)…
+ %6.3fsin(%6.2f t)]\n'],...
Alpha,A,w,abs(B),w)
end
else
end
end
end
A 2nd Complex Pole and residue
Example Problem
A 2nd Complex Pole and residue
Example Problem
'f_residue example'
Z = -4;
P = [-1 -1+3i -1-3i];
K = 1;
Hs = zpk(Z,P,K)
[b,a] = tfdata(Hs,'s');
[R,P,K] = residue(b,a);
f_residue(R,P)
Series RLC Circuit Problem
Problem: develop an M-File program to solve for the voltage across
the capacitor in a RLC series circuit in terms of the transfer function
H(s)=Vout(s)/Vin(s) in terms of the circuit parameters R, L, and C as a
symbolic transfer function.
Given R = 1 ohm, C = 1/16 F and L = ½ H enter the data from the
keyboard and use the subs function to determine the symbolic function
H(s).
Finally use the function Sym2Poly_tfs to convert the symbolic transfer
function to a tf object and use the Matlab step function to determine the
capacitor voltage due to a unit step function.
Solution: see Matlab code listing below. We develop the code in a
new M-File window and copy and paste it into the workspace to
execute it. Note that the the symbolic variable are s, R, Cx, and L. We
use Cx instead of C as Matlab tries to invoke the c compiler if we use
C. Note also the use of the input command to input the data for R, L,
and C from the keyboard.
Matlab Script for RLC Example
syms R Cx L s
zC = 1/(s*Cx); % capacitive impedance
zR = R; % resistor impedance
zL = s*L; % inductor impedance
zT = zR + zC + zL; % total series impedance
hs = zC/zT; % ratio of impedances
% simplify and collect like terms
hs = simplify(hs); hs = collect(hs);
pretty(hs) % pretty print symbolic transfer function
pause % wait for input from keyboard
R = input('Enter circuit resistor R = ');
Cx = input('Enter circuit capacitor Cx = ');
L = input('Enter circuit inductor L = ')
hs = subs(hs); % substitute values for R, L, and C
disp('updated symbolic tansfer function')
hs = simplify(hs); pretty(hs)
pause
% convert from sym to tf object form
HS = sym2poly_tfs(hs)
step(HS)
Matlab Output for RLC Circuit
Sym2Poly_tfz M-File
Problem: develop an M-File program Gz = Sym2Poly_tfz(gz,Ts) whose
input is gz a symbolic object function of the variable ‘z^-1’ and return is
the transfer function object Gz
Solution: see code listing below
function Gz = Sym2Poly_tfz(gz,Ts) % function definition
% Inputs:
% gz = symbolic function of z in DSP format
% Ts = sampling time (secs)
% Outputs:
% Gz = transfer function object in minimum form
syms z % define symbolic variable s
[numz_sym,denz_sym] = numden(gz); % get numerator and denominator terms
numz_tf = sym2poly(numz_sym,’z’); % convert numerator to tf object format
denz_tf = sym2poly(denz_sym,’z’); % convert denominator to tf object format
Gz = filt(numz_tf,denz_tf,Ts); % get transfer function as DSP polynomial
Poly2Sym_tfz M-File
Problem: develop an M-File program gz = Poly2Sym_tfz(Gz) whose
input is Gz a tf object function of the variable ‘z^-1’ and return is the
sym object transfer function gz.
Solution: see code listing on next page. Note the comment that the
terms b1(z) and a1(z) may not be the true numerator and denominator
of the symbolic transfer function. I.E., they may, in fact, be polynomial
terms with numerators and denominators. To accommodate this
condition the function numden is used to return the true numerator and
denominator terms of the symbolic transfer function.
Note, also, that the sampling time is not required to determine the
symbolic transfer function. This is because the function Gz includes
the sampling time.
Poly2Sym_tfz M-File
function gz = Poly2Sym_tfz(Gz) % function definition
% FUNCTION Poly2Sym_tfz converts a polynomial transfer
% function to a symbolic transfer function
% Input:
% GZ = tf or zpk discret time transfer function
% Output:
% gz = sym object transfer function
syms z
% check class of GS it must be tf or zpk format
hclass = class(GZ);
if strcmp(hclass,'tf')==1 |strcmp(hclass,'zpk')==1
[b,a] =tfdata(GZ,'z');
else
error('unknown function type')
end
b1= poly2sym(b,'z'); % convert to sym numerator
a1 = poly2sym(a,'z'); % convert to sym denominator
gz = b1/a1; % form gz = b(z)/a(z)
% get numerator and denominator terms of gz, they may
% not be the same as b1 and a1
[bz,az] = numden(gz);
gz = bz/az; % sym transfer function
gz = simplify(gz); % simplify final sym transfer function
Discrete Filter Example
Example : write an M-File program to build the filter 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

As a symbolic function and use the function Sym2Poly_tfz to convert the


symbolic filter function to a polynomial filter function. Use fvtool to
display the filter magnitude response.
Solution: See Matlab code and display results.
Discrete Filter Example1 Matlab code
syms z r n % define z,r, and n as symbolic
bz =0.5*(1 + r^n)*(1 - z^-n); % define symbolic numerator
az = (1 - r^n*z^-n); % define symbolic denominator
hz = bz/az;
n = 11, r = 0.98; % define filter parameters
hz = subs(hz); % evaluate filter function
HZ = sym2poly_tfz(hz,1);
[b,a]=tfdata(HZ,'z'); % get nums and dens of tf_object
fvtool(b,a) % plot magnitude response of filter
Discrete Filter Example1 Magnitude
Response
Discrete Filter Example 2
Example : write an M-File program to build the filter function

As a polynomial function and use the function Poly2Sym_tfz to convert


the tf_object filter function to a symbolic filter function. Use fvtool to
display the filter magnitude response of the polynomial filter function.
Also display the filters impulse response using the Matlab function impz.
Solution: See Matlab code and fvtool, and impz display results.
Discrete Filter Ex 2: Matlab code

r = 0.98; n =11; % define filter parameters


b = (1+r^n)*[1 zeros(1,n-1) -1]; % define poly numerator
a = 2*[1 zeros(1,n-1) -r^n]; % define poly denominator
HZ = filt(b,a,1) % Filter function
hz = poly2sym_tfz(HZ) % display symbolic filter function
fvtool(b,a) % display magnitude response
impz(b,a) % display impulse response
Discrete Filter Ex 2: Filter Plots

Das könnte Ihnen auch gefallen