EE4107 -­โ Cybernetics Advanced Exercise 9: Frequency Response Analysis (Solutions) The Gain Margin, GM (Δ๐พ), the Phase Margin, PM (๐) and the cross-­โover frequencies, ๐! and ๐!"# are found in a Bode diagram as illustrated below: ๐๐๐๐ is the gain margin frequency, in radians/second. A gain margin frequency indicates where the model phase crosses -­โ180 degrees. GM (Δ๐พ) is the gain margin of the system. ๐๐ is phase margin frequency, in radians/second. A phase margin frequency indicates where the model magnitude crosses 0 decibels. PM (๐) is the phase margin of the system. Note! ๐๐๐๐ and ๐๐ are called the crossover-­โfrequencies The definitions are as follows: Gain Crossover-­โfrequency -­โ ๐๐ : ๐ฟ ๐๐! = 1 = 0๐๐ต Phase Crossover-­โfrequency -­โ ๐๐๐๐ : ∠๐ฟ ๐๐!"# = −180! Gain Margin -­โ GM (๐ซ๐ฒ): ๐บ๐ ๐๐ต = − ๐ฟ ๐๐!"# ๐๐ต Phase margin PM (๐): Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01 2 ๐๐ = 180! + ∠๐ฟ(๐๐! ) We have that: 1. Asymptotically stable system: ๐๐ < ๐๐๐๐ 2. Marginally stable system: ๐๐ = ๐๐๐๐ 3. Unstable system: ๐๐ > ๐๐๐๐ MathScript has several built-­โin functions for Frequency response, e.g.: Function tf bode bodemag semilogx log10 atan series feedback bode bodemag margin Description Example Creates system model in transfer function form. You also can use this function to state-­โspace models to transfer function form. Creates the Bode magnitude and Bode phase plots of a system model. You also can use this function to return the magnitude and phase values of a model at frequencies you specify. If you do not specify an output, this function creates a plot. Creates the Bode magnitude plot of a system model. If you do not specify an output, this function creates a plot. Generates a plot with a logarithmic x-­โscale. >num=[1]; >den=[1, 1, 1]; >H = tf(num, den) >num=[4]; >den=[2, 1]; >H = tf(num, den) >bode(H) >[mag, wout] >[mag, wout] [wmin wmax]) >[mag, wout] wlist) >semilogx(w, = bodemag(SysIn) = bodemag(SysIn, = bodemag(SysIn, gain) Computes the base 10 logarithm of the input elements. The base 10 logarithm of zero is -­โinf. Computes the arctangent of x >log(x) Connects two system models in series to produce a model SysSer with input and output connections you specify Connects two system models together to produce a closed-­โloop model using negative or positive feedback connections Creates the Bode magnitude and Bode phase plots of a system model. You also can use this function to return the magnitude and phase values of a model at frequencies you specify. If you do not specify an output, this function creates a plot. Creates the Bode magnitude plot of a system model. If you do not specify an output, this function creates a plot. >Hseries = series(H1,H2) Calculates and/or plots the smallest gain and phase margins of a single-­โinput single-­โoutput (SISO) system model. The gain margin indicates where the frequency response crosses at 0 decibels. The phase margin indicates where the frequency response crosses -­โ180 degrees. Use the margins function to return all gain and phase margins of a SISO model. >atan(x) >SysClosed = feedback(SysIn_1, SysIn_2) >num=[4]; >den=[2, 1]; >H = tf(num, den) >bode(H) >[mag, wout] = bodemag(SysIn) >[mag, wout] = bodemag(SysIn, [wmin wmax]) >[mag, wout] = bodemag(SysIn, wlist) >num = [1] >den = [1, 5, 6] >H = tf(num, den) margin(H) >[gmf, gm, pmf, pm] = margin(H) Task 1: Stability Analysis Given the following transfer function: ๐ฟ ๐ = 1 ๐ ๐ +1 ! Task 1.1 Use the standard bode function in MathScript to plot the frequency response in a Bode diagram. EE4107 -­โ Cybernetics Advanced 3 Find the following: • • • The crossover-­โfrequencies ๐! and ๐!"# The gain margin, GM (Δ๐พ) The phase margin, PM (๐) Print out the Bode diagram and illustrate how you find ๐! , ๐!"# Δ๐พ, ๐ from the diagram Solutions: MathScript Code for creating Bode diagram: clear clc % Transfer unction num=[1]; den1=[1,0]; den2=[1,1] den3=[1,1] den = conv(den1,conv(den2,den3)); H = tf(num, den) % Bode Diagram bode(H) subplot(2,1,1) grid on subplot(2,1,2) grid on Note! In order to define the denominator (๐ ๐ + 1 ! ), we can use the conv function as shown above, or we can manually calculate: ๐ ๐ +1 ! = ๐ ! + 2๐ ! + ๐ and use like this: den=[1,2,1,0] Since we are going to find ๐! , ๐!"# , Δ๐พ and ๐ from the plot, it is a good idea to rescale the ๐ฅ and ๐ฆ axes: bode(H) subplot(2,1,1) grid on axis([0.1, 10, -40, 50]) subplot(2,1,2) grid on axis([0.1, 10, -200, -100]) EE4107 -­โ Cybernetics Advanced 4 Note! You can also do this manually by clicking on the ๐ฅ and ๐ฆ axes on the plot. From the graph above we find the following (an image editor is used to draw on the chart): So the results are as follows: ๐! ≈ 0.7 ๐!"# ≈ 1 ๐บ๐ ≈ 6 ๐๐ต ๐๐ ≈ 20° Task 1.2 Now you shall use the margin function in MathScript instead. Example: margin(H); → Here you will plot a Bode diagram where ๐! , ๐!"# Δ๐พ, ๐ will be illustrated and: [gm, pm, w180, wc] = margin(H); → Here you will get the numerical values for ๐! , ๐!"# Δ๐พ, ๐ . EE4107 -­โ Cybernetics Advanced 5 Compare your results with the previous subtask. Solutions: We use margin in 2 different ways: … margin(H) [gm, pm, w180, wc] = margin(H); Note! Using “help margin” in MathScript does not give the correct information about the return parameters return by the margin function!! The correct is: [gm, pm, w180, wc] = margin(H); Using margin(H) makes the gm, fm, gmf and pmf automatically plotted in the Bode diagram: Using [gm, pm, w180, wc] = margin(H) gives numerical values: wc = 0.6823 w180 = 0.9993 gm_dB = EE4107 -­โ Cybernetics Advanced 6 6.0086 pm = 21.3864 Note! gm has to be converted to dB. gm_dB = 20*log10(gm) Summarizing, the results are as follows: ๐๐ = ๐. ๐๐ ๐๐๐๐ = ๐ ๐ฎ๐ด = ๐ ๐ ๐ฉ ๐ท๐ด = ๐๐° Note! We see that the system is an asymptotically stable system because ๐๐ < ๐๐๐๐ Task 1.3 Find the poles for the system. Draw the poles in the imaginary plane. Is the system stable? Discuss the results. Solutions: Poles: ๐! = 0, ๐! = ๐! = −1 In the imaginary plane: MathScript code: … poles(H) EE4107 -­โ Cybernetics Advanced 7 pzmap(H) Note! We see from the poles as well that the system is an asymptotically stable system. Total MathScript Code for Task 1.1-­โ1.3: clear, clc % Transfer function num=[1]; den1=[1,0]; den2=[1,1] den3=[1,1] den = conv(den1,conv(den2,den3)); H = tf(num, den) poles(H) figure(1) pzmap(H) % Bode Plot figure(2) bode(H) subplot(2,1,1) grid on subplot(2,1,2) grid on % Margins and Phases wlist=[0.01, 0.1, 0.2, 0.5, 1, 10, 100]; [mag, phase,w] = bode(H, wlist); magdB=20*log10(mag); %convert to dB % [mag, phase,w] = bode(H); mag_data = [w, magdB] phase_data = [w, phase] % Crossover Frequency------------------------------------figure(3) margin(H) [gm, pm, w180, wc] = margin(H); wc EE4107 -­โ Cybernetics Advanced 8 w180 % Convert to dB. gm_dB = 20*log10(gm) pm Task 2: Stability Analysis with time-­โdelay Given the following system with time-­โdelay: 2.5๐ !! ๐ฟ ๐ = 3๐ + 1 Task 2.1 Use the standard bode function in MathScript to plot the frequency response in a Bode diagram. Find the following: • • • The crossover-­โfrequencies ๐! and ๐!"# The gain margin, GM (Δ๐พ) The phase margin, PM (๐) Print out the Bode diagram and illustrate how you find ๐! , ๐!"# Δ๐พ, ๐ from the diagram Task 2.2 Now you shall use the margin function in MathScript instead. Compare your results with the previous subtask. Task 2.3 Find the poles for the system. Draw the poles in the imaginary plane. Is the system stable? Discuss the results. Solutions for Task 2.1-­โ2.3: MathScript Code: clear, clc s=tf('s'); K=2.5; T=3; H1=tf(K/(T*s+1)); delay=1; H=set(H1,'inputdelay',delay); EE4107 -­โ Cybernetics Advanced 9 bode(H); subplot(2,1,1) grid on subplot(2,1,2) grid on margin(H) [gm, pm, w180, wc] = margin(H); wc w180 % Convert to dB. gm_dB = 20*log10(gm) pm Ordinary Bode Plot using the bode function (I have manually adjusted the scaling): → We have to find GM, PM, wc and w180 manually from the plot. We can use the margin function (gm, fm, gmf and pmf are automatically plotted in the Bode chart) as well: EE4107 -­โ Cybernetics Advanced 10 Using [gm, pm, w180, wc] = margin(H); gives: wc = 0.7638 w180 = 1.756 gm_dB = 6.6279 pm = 69.8178 Note! gm has to be converted to dB. gm_dB = 20*log10(gm) So the results are as follows: ๐๐ = ๐. ๐๐ ๐๐๐๐ = ๐. ๐๐ ๐ฎ๐ด = ๐. ๐ ๐ ๐ฉ ๐ท๐ด = ๐๐° EE4107 -­โ Cybernetics Advanced 11 We see that the system is an asymptotically stable system because ๐๐ < ๐๐๐๐ Task 3: Control System Given the following system: Process transfer function: ๐ป! = Where ๐พ = !! !" ๐พ !!" ๐ ๐ , where ๐พ! = 0,556, ๐ด = 13,4, ๐ = 145 and ๐ = 250 Measurement (sensor) transfer function: ๐ป! = ๐พ! Where Km = 1 %/m. Controller transfer function (PI Controller): ๐ป! = ๐พ! + ๐พ! ๐! ๐ Set ๐พ๐ = 1.5 og ๐๐ = 1000๐ . Task 3.1 Define the different transfer functions in MathScript. ๐ป! ๐ป! ๐ป! Solutions: We define the different transfer functions. There are multiple ways to define the different transfer functions, and we will show some alternative solutions. The tricky part is the time delay ๐ !!" in the process transfer function. Here we can use different approaches and different functions. We can use the built-­โin sys_order1, we can use the built-­โin pade function or create our own Pade’ approximation (e.g. a 1.order or 2.order approximation). Method1: Here we use combinations of functions tf and sys_order1. EE4107 -­โ Cybernetics Advanced 12 clear clc close all % Model parameters: Ks=0.556; %(kg/s)/% A=13.4; %m2 rho=145; %kg/m3 transportdelay=250; %sec %Defining the process transfer function: K=Ks/(rho*A); num1 = [K]; den1 = [1, 0]; H1 = tf(num1, den1); H2 = sys_order1(1, 0, transportdelay); disp('Process:') Hp = series(H1, H2) % Defining sensor transfer function: Km=1; %percent per meter disp('Sensor:') Hs=tf(Km) % Defining controller transfer function: Kp=1.5; Ti=1000; num = Kp*[Ti, 1]; den = [Ti, 0]; disp('Controller:') Hc = tf(num,den) Note! ๐ป! = ๐พ! + ๐พ! ๐พ! ๐! ๐ + ๐พ! ๐พ! (๐! ๐ + 1) = = ๐! ๐ ๐! ๐ ๐! ๐ We can also use the pade function in order to create a Pade’approximation: n=5; % Order of Pade approximation H2 = pade(transportdelay, n) Or we can create our own Pade approximation and then use the tf function: % 2.order approx. using tf k1=transportdelay/2; k2=transportdelay^2/12; num=[k2, -k1, 1]; EE4107 -­โ Cybernetics Advanced 13 den=[k2, k1, 1]; H2=tf(num, den) For a 2.order Pade’ approximation (๐ = ๐) we get the following transfer function: We get: ๐ !!" ≈ 1 − ๐! ๐ + ๐! ๐ ! 1 + ๐! ๐ + ๐! ๐ ! Where: ๐ ๐! = 2 ๐! = ๐! 12 Method 2: This is a new method we haven’t used before. We define s as the Laplace operator and then we can use s directly in our equations. clear clc close all s=tf('s'); %Model parameters: Ks=0.556; %(kg/s)/% A=13.4; %m2 rho=145; %kg/m3 transportdelay=250; %sec %Defining the process transfer function: K=Ks/(rho*A); padeorder=5; %Order of Pade-approximation of time-delay. Order 5 is usually ok. Hp1=set(tf(K/s),'inputdelay',transportdelay);%Including transportdelay in process transfer function Hp=pade(Hp1,padeorder);%Deriving process transfer function incl. Pade-approx of time-delay %Defining sensor transfer function: Km=1; Hs=tf(Km); %Defining sensor transfer function (just a gain in this example) %Defining controller transfer function: Kp=1.5; Ti=1000; Hc=Kp+Kp/(Ti*s); %PI controller transfer function Task 3.2 EE4107 -­โ Cybernetics Advanced 14 Set up the mathematical expression and define the Loop transfer function ๐ณ(๐). Tip! Use the built-­โin function series in Mathscript. Set up the mathematical expression and define the Sensitivity transfer function ๐บ(๐) Tip! Use the built-­โin function feedback in Mathscript. Set up the mathematical expression and define the Tracking transfer function ๐ป(๐) Solutions: Control system: Defining ๐ฟ (๐ ): ๐ฟ ๐ = ๐ป! ๐ป! ๐ป! We get the following compact system: MathScript Code: EE4107 -­โ Cybernetics Advanced 15 % Calculating loop tranfer function L=series(Hc,series(Hp,Hs)); %Calculating tracking transfer function T=feedback(L,1); % Calculating sensitivity transfer function S=1-T; Task 3.3 Plot the Loop transfer function ๐ฟ (๐ ), the Tracking transfer function ๐ (๐ ) and the Sensitivity transfer function ๐(๐ ) in a Bode diagram. Use, e.g., the bodemag function in MathScript. Discuss the results. Solutions: Code: % Bode Diagram figure(1) bodemag(L,T,S) grid Bode diagram: Task 3.4 Based on the Bode diagram of ๐ฟ (๐ ), ๐ (๐ ) ๐(๐ ), find the different bandwidths ๐! , ๐! , ๐! (see the sketch below). EE4107 -­โ Cybernetics Advanced 16 Where we have the following definitions: ๐๐ – crossover-­โfrequency – the frequency where the gain of the Loop transfer function ๐ฟ (๐๐) has the value: 1 = 0๐๐ต ๐๐ – the frequency where the gain of the Tracking function ๐ (๐๐) has the value: 1 ≈ 0.71 = −3๐๐ต 2 ๐๐ -­โ the frequency where the gain of the Sensitivity transfer function ๐(๐๐) has the value: 1− 1 2 ≈ 0.29 = −11๐๐ต Discuss the results. Solutions: Values for ๐! , ๐! , ๐! can be found in the plot as shown below (We change the scaling for more details): EE4107 -­โ Cybernetics Advanced 17 We get the following values: ๐๐ = ๐. ๐๐๐๐๐ ๐๐๐ /๐ ๐๐ = ๐. ๐๐๐๐๐ ๐๐๐ /๐ ๐๐ = ๐. ๐๐๐๐ ๐๐๐ /๐ Task 3.5 Plot the step response for the Tracking transfer function ๐ (๐ ) Discuss the results. Solutions: MathScript Code: % Simulating step response for control system (tracking transfer function) figure(2) step(T) grid Plot: EE4107 -­โ Cybernetics Advanced 18 We see that the system is asymptotically stable. Task 3.6 Find the stability margins (GM, PM) of the system (๐ฟ(๐ )). Discuss the results. Solutions: MathScript Code: % Calcutating stability margins and crossover frequencies: [gm, pm, w180, wc] = margin(L) % Plotting L and stability margins and crossover frequencies in Bode diagram figure(3) margin(L) grid This gives: EE4107 -­โ Cybernetics Advanced 19 Shown in the Output Window of Mathscript: GM=12.764, and PM = 25.645 deg. ๐! < ๐!"# → The system is asymptotically stable GM is ok, but PM somewhat too small (should have been at least 30 deg). May try to increase ๐! to e.g. 1200s. Decreasing ๐พ! does not help (normally it does, but it can be shown that for this system, containing two integrators in series (PI controller and process), reducing gain may actually reduce stability (normally the stability is increased if gain is reduced). Below we see the complete code for the Task: clear clc close all EE4107 -­โ Cybernetics Advanced 20 % Model parameters: Ks=0.556; %(kg/s)/% A=13.4; %m2 rho=145; %kg/m3 transportdelay=250; %sec %Defining the process transfer function: K=Ks/(rho*A); num1 = [K]; den1 = [1, 0]; H1 = tf(num1, den1); H2 = sys_order1(1, 0, transportdelay); disp('Process:') Hp = series(H1, H2) % Defining sensor transfer function: Km=1; %percent per meter disp('Sensor:') Hs=tf(Km) % Defining controller transfer function: Kp=1.5; Ti=1000; num = Kp*[Ti, 1]; den = [Ti, 0]; disp('Controller:') Hc = tf(num,den) % Calculating loop tranfer function L=series(Hc,series(Hp,Hs)); %Calculating tracking transfer function T=feedback(L,1); % Calculating sensitivity transfer function S=1-T; % Bode Diagram figure(1) bodemag(L,T,S) grid % Simulating step response for control system (tracking transfer function) figure(2) EE4107 -­โ Cybernetics Advanced 21 step(T) grid % Calcutating stability margins and crossover frequencies: [gm, pm, w180, wc] = margin(L) % Plotting L and stability margins and crossover frequencies in Bode diagram figure(3) margin(L) grid Additional Resources • http://home.hit.no/~hansha/?lab=mathscript Here you will find tutorials, additional exercises, etc. EE4107 -­โ Cybernetics Advanced