https://www.halvorsen.blog

Frequency Response with MATLAB Examples Control Design and Analysis

Hans-Petter Halvorsen Contents • Introduction to PID Control • Introduction to Response • Frequency Response using Bode Diagram • Introduction to Complex Numbers (which Frequency Response Theory is based on) • Frequency Response from Transfer Functions • Frequency Response from Input/output Signals • PID Controller Design and Tuning (Theory) • PID Controller Design and Tuning using MATLAB • Stability Analysis using MATLAB • Stability Analysis of Feedback Systems • Stability Analysis of Feedback Systems – a Practical Example • The Bandwidth of the Control System • Practical PI Controller Example https://www.halvorsen.blog

PID Control

Hans-Petter Halvorsen Control System 푣

푟 푒 푢 Controller Actuators Process − 푦

Filtering Sensors

푟 – Reference Value, SP (Set-point), SV (Set Value) 푦 – Measurement Value (MV), Process Value (PV) 푒 – Error between the reference value and the measurement value (푒 = 푟 – 푦) 푣 – Disturbance, makes it more complicated to control the process 푢 - Control Signal from the Controller The PID Algorithm 푡 퐾푝 푢 푡 = 퐾푝푒 + න 푒푑휏 + 퐾푝푇푑푒ሶ 푇푖 0

Where 푢 is the controller output and 푒 is the Tuning Parameters: control error: 푒 푡 = 푟 푡 − 푦(푡) 퐾푝 Proportional Gain 푇 Integral Time [sec. ] 푟 is the Reference Signal or Set-point 푖 푦 is the Process value, i.e., the Measured value 푇푑 Derivative Time [sec. ] The PI Algorithm 푡 퐾푝 푢 푡 = 퐾푝푒 + න 푒푑휏 푇푖 0

Where 푢 is the controller output and 푒 is the Tuning Parameters: control error: 푒 푡 = 푟 푡 − 푦(푡) 퐾푝 Proportional Gain 푇 Integral Time [sec. ] 푟 is the Reference Signal or Set-point 푖 푦 is the Process value, i.e., the Measured value PI(D) Algorithm in MATLAB

• We can use the pid() function in MATLAB • We can define the PI(D) using the tf() function in MATLAB • We can also define and implement a discrete PI(D) algorithm Discrete PI Controller Algorithm

We start with: 푡 퐾푝 푢 푡 = 푢0 + 퐾푝푒 푡 + න 푒푑휏 푇푖 0 In order to make a discrete version using, e.g., Euler, we can derive both sides of the equation: 퐾푝 푢ሶ = 푢ሶ 0 + 퐾푝푒ሶ + 푒 푇푖 If we use Euler Forward we get: 푢푘 − 푢푘−1 푢0,푘 − 푢0,푘−1 푒푘 − 푒푘−1 퐾푝 = + 퐾푝 + 푒푘 푇푠 푇푠 푇푠 푇푖 Then we get: 퐾푝 푢푘 = 푢푘−1 + 푢0,푘 − 푢0,푘−1 + 퐾푝 푒푘 − 푒푘−1 + 푇푠푒푘 푇푖

Where 푒푘 = 푟푘 − 푦푘 We can also split the equation above in 2 different parts by setting: ∆푢푘 = 푢푘 − 푢푘−1 This gives the following PI control algorithm: 푒푘 = 푟푘 − 푦푘 퐾푝 ∆푢푘 = 푢0,푘 − 푢0,푘−1 + 퐾푝 푒푘 − 푒푘−1 + 푇푠푒푘 푇푖 푢푘 = 푢푘−1 + ∆푢푘 This algorithm can easily be implemented in MATLAB Discrete PI Controller Algorithm

Discrete PI control algorithm:

푒푘 = 푟푘 − 푦푘 퐾푝 ∆푢푘 = 푢0,푘 − 푢0,푘−1 + 퐾푝 푒푘 − 푒푘−1 + 푇푠푒푘 푇푖 푢푘 = 푢푘−1 + ∆푢푘 PID Controller –Transfer Function

We have: 푡 퐾푝 푢 푡 = 퐾푝푒 + න 푒푑휏 + 퐾푝푇푑푒ሶ 푇푖 0 Laplace gives:

푢(푠) 퐾푝 퐻푃퐼퐷 푠 = = 퐾푝 + + 퐾푝푇푑푠 푒(푠) 푇푖푠 or: 2 푢(푠) 퐾푝(푇푑푇푖푠 + 푇푖푠 + 1) 퐻푃퐼퐷 푠 = = 푒(푠) 푇푖푠 PI Controller –Transfer Function We have: 푡 퐾푝 푢 푡 = 퐾푝푒 + න 푒푑휏 푇푖 0 Laplace gives:

푢(푠) 퐾푝 퐾푝푇푖푠 + 퐾푝 퐾푝(푇푖푠 + 1) 퐻푃퐼 푠 = = 퐾푝 + = = 푒(푠) 푇푖푠 푇푖푠 푇푖푠 Finally: 푢(푠) 퐾푝(푇푖푠 + 1) 퐻푃퐼 푠 = = 푒(푠) 푇푖푠 Define PI Transfer function in MATLAB

clear, clc

% PI Controller Transfer function Kp = 0.52; 푢(푠) 퐾푝(푇푖푠 + 1) Ti = 18; 퐻푃퐼 푠 = = 푒(푠) 푇푖푠 num = Kp*[Ti, 1]; den = [Ti, 0];

Hpi = tf(num,den)

... PI Controller – State space model

Given: 퐾푝 푢 푠 = 퐾푝푒 푠 + 푒 푠 푇푖푠 1 We set 푧 = 푒 ⇒ 푠푧 = 푒 ⇒ 푧ሶ = 푒 푠 This gives: 푧ሶ = 푒 퐾푝 푢 = 퐾푝푒 + 푧 푇푖 Where 푒 = 푟 − 푦 PI Controller – Discrete State space model

Using Euler: 푧 − 푧 푧ሶ ≈ 푘+1 푘 푇푠 Where 푇푠 is the Sampling Time. This gives: 푧푘+1 − 푧푘 = 푒푘 푇푠 퐾푝 푢푘 = 퐾푝푒푘 + 푧푘 푇푖 Finally: 푒푘 = 푟푘 − 푦푘 퐾푝 푢푘 = 퐾푝푒푘 + 푧푘 푇푖 푧푘+1 = 푧푘 + 푇푠푒푘 PI Controller – Discrete State space model implemented in MATLAB clear, clc ...

for i=1:N

...

e = r - y; u = Kp*e + z; z = z + dt*Kp*e/Ti;

...

end

plot(...) https://www.halvorsen.blog

Frequency Response

Hans-Petter Halvorsen Theory Frequency Response • The frequency response of a system is a frequency dependent function which expresses how a sinusoidal signal of a given frequency on the system input is transferred through the system. Each frequency component is a sinusoidal signal having certain and a certain frequency. • The frequency response is an important tool for analysis and design of signal filters and for analysis and design of control systems. • The frequency response can be found experimentally or from a transfer function model. • The frequency response of a system is defined as the steady-state response of the system to a sinusoidal input signal. When the system is in steady-state, it differs from the input signal only in amplitude/gain (A) and lag (휙). Input Signal Theory Frequency Response Output Signal

Input Signal Dynamic Output Signal 푢 푡 = 푈 ∙ 푠푖푛휔푡 System 푦 푡 = 푈퐴ด 푠푖푛(휔푡 + 휙) 푌 Amplitude Frequency

Gain Phase Lag The frequency response of a system expresses how a sinusoidal signal of a given frequency on the system input is transferred through the system. The only difference is the gain and the phase lag. Frequency Response - Definition Theory 푢 푡 = 푈 ∙ 푠푖푛휔푡 푦 푡 = 푈퐴ด 푠푖푛(휔푡 + 휙) 푌

Input Dynamic Output System

휔 = 1 푟푎푑/푠 휔 = 1 푟푎푑/푠 and the same for Frequency 2, 3, 4, 5, 6, etc.

• The frequency response of a system is defined as the steady-state response of the system to a sinusoidal input signal. • When the system is in steady-state, it differs from the input signal only in amplitude/gain (A) (Norwegian: “forsterkning”) and phase lag (ϕ) (Norwegian: “faseforskyvning”). Frequency Response - Simple Example Theory Outside Temperature

T = 1 year

frequency 1 (year) Dynamic System frequency 1 (year) Inside Temperature

Note! Only the gain and phase are different

Assume the outdoor temperature is varying like a sine function during a year (frequency 1) or during 24 hours (frequency 2). Then the indoor temperature will be a sine as well, but with different gain. In addition it will have a phase lag. Frequency Response - Simple Example Theory Outside Temperature

T = 24 hours

frequency 2 (24 hours)

Dynamic System frequency 2 (24 hours) Inside Temperature

Note! Only the gain and phase are different

Assume the outdoor temperature is varying like a sine function during a year (frequency 1) or during 24 hours (frequency 2). Then the indoor temperature will be a sine as well, but with different gain. In addition it will have a phase lag. https://www.halvorsen.blog

Frequency Response using Bode Diagram

Hans-Petter Halvorsen Bode Diagram You can find the Bode diagram from experiments on the physical process or from the transfer function (the model of the system). A simple sketch of the Bode diagram for a given system:

The Bode diagram gives a simple Graphical overview of the Frequency Response for a 휔푐 퐿표푔 휔 ω [rad/s] given system. A Tool for Analyzing the 0푑퐵 ∆퐾 Stability properties of the Control System.

With MATLAB you can easily create Bode diagram from the Transfer function model using the bode() function

휑 휔180 퐿표푔 휔 ω [rad/s] Bode Diagram from experiments Find Data for different We find 퐴 and 휙 for each of the frequencies, e.g.:

...

Based on that we can plot the Frequency Response in a so-called Bode Diagram: Bode Diagram The x-scale is logarithmic Gain (“Forsterkningen”)

Note! The y-scale is in [푑퐵]

푥 푑퐵 = 20푙표푔10푥

Phase lag (“Faseforkyvningen”) The y-scale is in [푑푒푔푟푒푒푠] 2휋 푟푎푑 = 360°

Normally, the unit for frequency is Hertz [Hz], but in frequency response and Bode 휔 = 2휋푓 diagrams we use radians ω [rad/s]. The relationship between these are as follows: Frequency Response – MATLAB Transfer Function:

MATLAB Code: clear clc close all

% Define Transfer function num=[1]; den=[1, 1]; H = tf(num, den)

% Frequency Response bode(H); The frequency response is an important tool for analysis and design grid on of signal filters and for analysis and design of control systems. Frequency Response – MATLAB Transfer Function: clear clc close all

% Define Transfer function num = [1]; Instead of Plotting the Bode Diagram we den = [1, 1]; H = tf(num, den) can also use the bode function for % Frequency Response calculation and showing the data as well: bode(H); grid on freq_data = % Get Freqquency Response Data wlist = [0.01, 0.1, 1, 2 ,3 ,5 ,10, 100]; 0.0100 -0.0004 -0.5729 [mag, phase, w] = bode(H, wlist); 0.1000 -0.0432 -5.7106 1.0000 -3.0103 -45.0000 for i=1:length(w) 2.0000 -6.9897 -63.4349 magw(i) = mag(1,1,i); phasew(i) = phase(1,1,i); 3.0000 -10.0000 -71.5651 end 5.0000 -14.1497 -78.6901 10.0000 -20.0432 -84.2894 magdB = 20*log10(magw); % Convert to dB 100.0000 -40.0004 -89.4271 freq_data = [wlist; magdB; phasew]' Bode Diagram – MATLAB Example

MATLAB Code: 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) clear, clc % Bode Diagram bode(H) % Transfer function subplot(2,1,1) num=[1]; den=[1,2,1,0]; grid on H = tf(num, den) subplot(2,1,2) grid on % Bode Diagram bode(H) subplot(2,1,1) or: grid on subplot(2,1,2) grid on Example

We will use the following system as an example: 1 퐻 = 퐻푐 = 퐾푝 푝 푠(푠 + 1) 푟 푒 푢 Controller Process − 푦

Sensors 1 퐻 = 푚 3푠 + 1 Bode Diagram – MATLAB Example

MATLAB Code: 1 clear 퐻푝 = clc 푠(푠 + 1)

num = 1; den = [1,1,0];

Hp = tf(num,den)

bode(Hp) grid on https://www.halvorsen.blog

Complex Numbers

Background Theory for Frequency Response

Hans-Petter Halvorsen Complex Numbers

A is given by: 푧 = 푎 + 푗푏

Where 푗 = −1 퐼푚푎푔푖푛푎푟푦 퐴푥푖푠 (퐼푚) We have that: 푎 = 푅푒 푧 푏 푧 = 푎 + 푗푏 푏 = 퐼푚(푧)

푅푒푎푙 0 푎 퐴푥푖푠(푅푒) Complex Numbers 푗 = −1 Polar form: 푧 = 푟푒푗휃

Where: 푟 = 푧 = 푎2+푏2 푏 휃 = 푎푡푎푛 푎 퐼푚푎푔푖푛푎푟푦 퐴푥푖푠 (퐼푚) Note!

푗휃 푎 = 푟 cos 휃 푏 푧 = 푟푒 푟 푏 = 푟 sin 휃 휃 푅푒푎푙 0 푎 퐴푥푖푠(푅푒) Complex Numbers 푗 = −1 1 Rectangular form of a complex number 2 Exponential/polar form of a complex number 퐼푚푎푔푖푛푎푟푦 퐼푚푎푔푖푛푎푟푦 퐴푥푖푠 (퐼푚) 퐴푥푖푠 (퐼푚)

푧 = 푎 + 푗푏 푗휃 푏 푏 푧 = 푟푒 푟 휃 푅푒푎푙 푅푒푎푙 0 푎 퐴푥푖푠(푅푒) 0 푎 퐴푥푖푠(푅푒)

Length (“Gain”): Angle (“Phase”): 푏 푟 = 푧 = 푎2 + 푏2 휃 = 푎푡푎푛 푎 https://www.halvorsen.blog

Frequency response from Transfer function

Hans-Petter Halvorsen Manually find the Frequency Response Theory from the Transfer Function For a transfer function: 푦(푠) 퐻 푆 = 푢(푠) 푠 = 푗휔 We have that: 퐻 푗휔 = 퐻(푗휔) 푒푗∠퐻(푗휔)

Where 퐻(푗휔) is the frequency response of the system, i.e., we may find the frequency response by setting 푠 = 푗휔 in the transfer function. Bode diagrams are useful in frequency response analysis. The Bode diagram consists of 2 diagrams, the Bode magnitude diagram, 퐴(휔) and the Bode phase diagram, 휙(휔). The Gain function: 퐴 휔 = 퐻(푗휔) The Phase function: 휙 휔 = ∠퐻(푗휔)

The 퐴(휔)-axis is in (dB), where the decibel value of x is calculated as: 푥 푑퐵 = 20푙표푔10푥 The 휙(휔)-axis is in degrees (not radians!) Mathematical expressions for 퐴(휔) and 휙(휔)

We find the Mathematical expressions for 퐴(휔) and 휙(휔) by setting 푠 = 푗휔 in the transfer function given by: 푦(푠) 퐾 퐻 푠 = = 푢(푠) 푇푠 + 1

The Frequency Response (we replace 푠 with 푗휔) then becomes: 퐾 퐾 퐻 푗휔 = = 푇푗휔 + 1 ณ1 + 푗 푇휔ด 푅푒 퐼푚 Polar form: 퐾 퐻 푗휔 = 푇휔 푗 푎푟푐푡푎푛 12 + 푇휔 2푒 1 퐾 = 푒푗 −푎푟푐푡푎푛(푇휔) 1 + 푇휔 2 Finally: 퐾 퐻 푗휔 = 푒푗 −푎푟푐푡푎푛(푇휔) 1 + 푇휔 2 cont. next page cont. from previous page

The Gain function becomes: 퐾 퐴 휔 = 퐻(푗휔) = 1 + 푇휔 2

Or in 푑퐵 (used in the ): 2 퐴 휔 푑퐵 = 퐻(푗휔) 푑퐵 = 20푙표푔퐾 − 20푙표푔 1 + 푇휔

The Phase function becomes ( 푟푎푑 ):

휙 휔 = ∠퐻 푗휔 = 푎푟푔 퐻 푗휔 = −푎푟푐푡푎푛(푇휔)

Or in degrees ° (used in the Bode plot): 180 휙 휔 = ∠퐻 푗휔 = −푎푟푐푡푎푛(푇휔) ∙ 휋 Note: 2휋 푟푎푑 = 360° Transfer function: 푨(흎) og 흓(흎): 2 1 퐻(푗휔) 푑퐵 = 20푙표푔1 − 20푙표푔 (휔) +1 퐻 푠 =

푠 + 1 ∠퐻(푗휔) = −arctan(휔)

degrees,we have towith:multiply Note! 2 4 퐻(푗휔) 푑퐵 = 20푙표푔4 − 20푙표푔 (2휔) +1 퐻 푠 = ∠퐻(푗휔) = −arctan(2휔) 2푠 + 1 order in In phase the to find 2 2 5 퐻(푗휔) 푑퐵 = 20푙표푔5 − 20푙표푔 (휔) +1 − 20푙표푔 (10휔) +1 퐻 푆 = 푠 + 1 (10푠 + 1) ∠퐻(푗휔) = −arctan(휔) − arctan(10휔) 2 2 퐻(푗휔) 푑퐵 = −20푙표푔 (휔) − 2푥20푙표푔 (휔) +1 1 = 20푙표푔휔 − 40푙표푔 (휔)2+1 퐻 푆 = 2 푠 푠 + 1 ∠퐻(푗휔) = −90 − 2 arctan(휔)

−2푠 2 3.2푒 퐻(푗휔) 푑퐵 = 20푙표푔3.2 − 20푙표푔 (3휔) +1 퐻 푠 = 3푠 + 1 ∠퐻(푗휔) = −2휔 − arctan(3휔) 퐻(푗휔) = 20푙표푔 (5휔)2+1 − 20푙표푔 (2휔)2+1 − 20푙표푔 (10휔)2+1 5푠 + 1 푑퐵 180 퐻 푆 = 휋 2푠 + 1 (10푠 + 1) ∠퐻(푗휔) = arctan(5휔) − arctan(2휔) − arctan(10휔) Manually find the Frequency Response from the Transfer Function

Given the following transfer function: 4 퐻 푆 = 2푠 + 1 Bode Plot:

The mathematical expressions for 퐴(휔) and 휙(휔) become:

2 퐻(푗휔) 푑퐵 = 20푙표푔4 − 20푙표푔 (2휔) +1

∠퐻(푗휔) = −arctan(2휔)

These equations can easily be implemented in MATLAB (See next slide) clear clc MATLAB Code % Transfer function num=[4]; clear den=[2, 1]; clc H = tf(num, den) w = [0.1, 0.16, 0.25, 0.4, 0.625, 2.5, 10]; % Bode Plot figure(1) % Alt 2: Use Mathematical expressions for H and

% Alt 1: Use bode function directly mag_data2 = [w; gain] disp('----- Alternative 1 -----') phase_data2 = [w; phasedeg] w = [0.1, 0.16, 0.25, 0.4, 0.625, 2.5, 10]; figure(2) [magw, phasew] = bode(H, w); subplot(2,1,1) semilogx(w,gain) for i=1:length(w) grid on mag(i) = magw(1,1,i); phase(i) = phasew(1,1,i); subplot(2,1,2) end semilogx(w,phasedeg) grid on magdB = 20*log10(mag); %convert to dB mag_data = [w; magdB] phase_data = [w; phase] Transfer functions with Time delay Transfer functions with Time delay

A general transfer function for a 1.order system with time delay is: 퐾 퐻 푠 = 푒−푇푠 푇푠 + 1

Frequency Response functions for gain and phase margin becomes:

퐴 휔 푑퐵 = 20푙표푔(퐾) − 20푙표푔 (푇휔)2+1 휙 휔 = − 푎푟푐푡푎푛 푇휔 − 휔 ∙ 휏

Or 휙 휔 in degrees: 180 휙 휔 [푑푒푔푟푒푒푠] = − arctan 푇휔 − 휔 ∙ 휏 휋 Transfer functions with Time delay in MATLAB Different ways to implement a time delay in MATLAB:

Alt 2 K = 3.5; Alt 1 T = 22; K = 3.5; Tau = 2; T = 22; Alt 4: Use Pade approximation num = [K]; Tau = 2; den = [T, 1]; K = 3.5; H1 = tf(num, den); T = 22; num = [K]; Tau = 2; den = [T, 1]; H = set(H1,'inputdelay',Tau) num = [K]; H1 = tf(num, den); den = [T, 1]; bode(H); H1 = tf(num, den); s = tf('s') N=5; H2 = exp(-Tau*s); Alt 3 K = 3.5; H2 = pade(Tau, N) T = 22; [num_pade,den_pade] = pade(T,N) H = H1 * H2 Tau = 2; Hpade = tf(num_pade,den_pade); s = tf('s'); H = series(H1, Hpade); bode(H) H = K*exp(-Tau*s)/(T*s+1) bode(H); bode(H); 1. order system with Time delay

Given the following transfer function: The mathematical expressions for 퐴(휔) and 휙(휔): 3.2푒−2푠 퐻 푠 = 2 3푠 + 1 퐻(푗휔) 푑퐵 = 20푙표푔3.2 − 20푙표푔 (3휔) +1

Poles: ∠퐻(푗휔) = −2휔 − arctan(3휔) 1 푝1 = − = −0.33 180 3 Or in degrees:∠퐻 푗휔 = −2휔 − arctan(3휔) ∙ 휋 Zeros: None Break frequency: 1 1 휔 = = = 0.33 푟푎푑/푠 푇 3 clear, clc or: clear, clc s = tf('s'); s=tf('s'); K=3.2; T=3; K=3.2; T=3; Tau = 2; H1=tf(K/(T*s+1)); num = K; den = [T, 1]; delay = 2; H1 = tf(num, den); H = set(H1,'inputdelay',delay) s = tf('s') bode(H); H2 = exp(-Tau*s); p = pole(H) H = H1 * H2 z = zero (H) bode(H);

p = pole(H) H = z = zero (H) 3.2 exp(-2*s) * ------3 s + 1

Continuous-time transfer function.

p = -0.3333

z = Empty matrix: 0-by-1 https://www.halvorsen.blog

Frequency response from Input/Output Signals

Hans-Petter Halvorsen Frequency Response from sinusoidal Theory input and output signals We can find the frequency response of a system by exciting the system (either the real system or a model of the system) with a sinusoidal signal of amplitude 퐴 and frequency 휔 [푟푎푑/푠] (Note: 휔 = 2휋푓) and observing the response in the output variable of the system. Frequency Response - Definition Theory 푢 푡 = 푈 ∙ 푠푖푛휔푡 푦 푡 = 푈퐴ด 푠푖푛(휔푡 + 휙) 푌

Input Dynamic Output System

휔 = 1 푟푎푑/푠 휔 = 1 푟푎푑/푠 and the same for Frequency 2, 3, 4, 5, 6, etc.

• The frequency response of a system is defined as the steady-state response of the system to a sinusoidal input signal. • When the system is in steady-state, it differs from the input signal only in amplitude/gain (A) (Norwegian: “forsterkning”) and phase lag (ϕ) (Norwegian: “faseforskyvning”). Frequency Response from sinusoidal Theory input and output signals The input signal is given by: 푢 푡 = 푈 ∙ 푠푖푛휔푡 The steady-state output signal will then be: 푦 푡 = 푈퐴ด 푠푖푛(휔푡 + 휙) 푌 The gain is given by: 푌 퐴 = 푈 The phase lag is given by: 휙 = −휔Δ푡 [푟푎푑] Frequency Response from sinusoidal Theory input and output signals You will get plots like this for each frequency: The gain is given by: 푌 퐴 = 푈

The phase lag is given by: 휙 = −휔Δ푡 [푟푎푑]

t Find the gain (퐴) and the phase (휙) for the given frequency from the plot 휔 = 1 rad/s Solutions From the Plot we get:

t

Cont. next page -> Solutions Conversion to dB 퐴 푑퐵 퐴 푑퐵 = 20푙표푔(퐴) or the other way: 퐴 = 10 20

Example:

퐴 = 0.68 퐴 푑퐵 = 20푙표푔(퐴)=20푙표푔(0.68) ≈ −3.35푑퐵

Or the other way:

퐴 푑퐵 = −3.35푑퐵 −3.35 퐴 푑퐵 = 10 20 ≈ 0.68 From the Bode diagram we can verify that our calculations are correct:

퐴 = −3.35 푑퐵

휙 = −45.9 °

휔 = 1 rad/s The following MATLAB Code is used to create the Plot:

clear, clc

K = 1; T = 1; num = [K]; den = [T, 1]; We use the following transfer function: H = tf(num, den); figure(1) 1 bode(H), grid on 퐻 푠 = % Define input signal 푠 + 1 t = [1: 0.1 : 12]; w = 1; U = 1; u = U*sin(w*t); The Frequency used: figure(2) plot(t, u)

% Output signal 휔 = 1 rad/s hold on lsim(H, ':r', u, t) grid on hold off legend('input signal', 'output signal') https://www.halvorsen.blog

PID Controller Design/Tuning

Hans-Petter Halvorsen PID Controller Design

A lot of PID Tuning methods exist, e.g., • Skogestad's method • Ziegler-Nichols’ methods • Trial and Error Methods • PID Tuning functionality built into MATLAB • Auto-tuning built into commercial PID controllers • ... Skogestad’s method • The Skogestad’s method assumes you apply a step on the input (푢) and then observe the response and the output (푦), as shown below. • If we have a model of the system (which we have in our case), we can use the following Skogestad’s formulas for finding the PI(D) parameters directly.

푇푐 is the time-constant of the control system which the user must specify Originally, Skogestad defined the factor 푐 = 4. This gives good set-point tracking. But the disturbance compensation may become quite sluggish. To obtain Figure: F. Haugen, Advanced Dynamics and Control: TechTeach, 2010. faster disturbance compensation, you can use 푐 = 1.5 Ziegler–Nichols Frequency Response method

Assume you use a P controller only 푇푖 = ∞, 푇푑 = 0. Then you need to find for Theory which 퐾푝 the closed loop system is a marginally stable system (휔푐 = 휔180). This 퐾푝 is called 퐾푐 (critical gain). The 푇푐 (critical period) can be found from the damped oscillations of the closed loop system. Then calculate the PI(D) parameters using the formulas below. Marginally stable system: Controller 퐾푝 푇푖 푇푑 휔푐 = 휔180

P 0.5퐾푐 ∞ 0 푇 PI 0.45퐾 푐 0 푐 1.2 2휋 푇푐 푇푐 푇푐 = PID 0.6퐾푐 휔 2 8 푇푐 180

퐾푐- Critical Gain 푇푐- Critical Period https://en.wikipedia.org/wiki/Ziegler–Nichols_method https://www.halvorsen.blog

Controller Design/Tuning using MATLAB

Hans-Petter Halvorsen Controller Design/Tuning using MATLAB

• Frequency Design and Analysis • pidtune() MATLAB function • PID Tuner (Interactive Tools) • ...

Validate with simulations! pidtune() MATLAB function clear, clc

%Define Process num = 1; den = [1,1,0]; Hp = tf(num,den)

% Find PI Controller [Hpi,info] = pidtune(Hp,'PI')

%Bode Plots figure(1) bode(Hp) grid on figure(2) bode(Hpi) grid on 퐾푝 = 0.33 % Feedback System T = feedback(Hpi*Hp, 1); 1 1 figure(3) 푇푖 = = ≈ 43.5푠 step(T) 퐾푖 0.023 pidtune() MATLAB function To improve the response time, you can set a higher target crossover frequency than the result that pidtune() automatically selects, 0.32. Increase the crossover frequency to 1.0.

[Hpi,info] = pidtune(Hp,'PI', 1.0)

The new controller achieves the higher crossover frequency, but at the cost of a reduced phase margin. MATLAB PID Tuner

Define Controller Type Tuning

Define your Process

Add Additional Plots Step Response and other Plots can be shown

PID Parameters https://www.halvorsen.blog

Stability Analysis using MATLAB

Hans-Petter Halvorsen Stability Analysis How do we figure out that the Feedback System is stable before we test it on the real System? 1. Poles 2. Frequency Response/Bode 3. Simulations (Step Response) We will do all these things using MATLAB Stability Analysis 3 Time domain 2 Asymptotically stable system Loop lim 푦 푡 = 푘 transfer 푡→∞ function

Tracking Marginally stable system transfer function

Unstable system

1 The Complex domain Poles

Asymptotically stable system: 휔푐 < 휔180 Tracking Marginally stable system: 휔 = 휔 transfer 푐 180 function Unstable system: 휔푐 > 휔180 Figure: F. Haugen, Advanced Dynamics and Control: TechTeach, 2010. 1 Asymptotically stable system: Poles Each of the poles of the transfer function lies strictly in the left half plane (has strictly negative real part).

3 Unstable system: At least one pole lies in the right half 2 Marginally stable system: plane (has real part greater than zero).

One or more poles lies on the imaginary axis (have real part equal to zero), and all these Or: There are multiple and coincident poles are distinct. Besides, no poles lie in the poles on the imaginary axis. 1 right half plane. Example: double integrator 퐻(푠) = 푠2 Theory Stability Analysis Asymptotically stable system: Marginally stable system: Unstable system: lim 푦 푡 = 푘 lim 푦 푡 = ∞ 푡→∞ 푡→∞

Figures: F. Haugen, Advanced Dynamics and Control: TechTeach, 2010. 0 < lim 푦 푡 < ∞ 푡→∞

At least one pole lies in the right half plane (has real part greater than zero).

Or: There are multiple and coincident poles Each of the poles of the transfer function lies One or more poles lies on the imaginary axis (have on the imaginary axis. Example: double strictly in the left half plane (has strictly negative real part equal to zero), and all these poles are 1 integrator 퐻(푠) = real part). distinct. Besides, no poles lie in the right half plane. 푠2 https://www.halvorsen.blog

Stability Analysis of Feedback Systems

Hans-Petter Halvorsen Theory Stability Analysis of 1 Loop Transfer Function Feedback Systems (“Sløyfetransferfunksjonen”):

Hr = ... Hp = ... Hm = ... L = series(series(Hr, Hp), Hm)

2 The Tracking Function (“Følgeforholdet”):

L = ... T = feedback(L, 1)

3 The Sensitivity Function (“Sensitivitetsfunksjonen”):

T = ... S = 1-T Frequency Response and Stability Analysis Theory

Bode Diagram 휔푐 and 휔180 are called the crossover-frequencies (“kryssfrekvens”)

Δ퐾 is the gain margin (GM) of the system (“Forsterkningsmargin”). How much the loop gain can increase before the system becomes unstable

휙 is the phase margin (PM) of the system (“Fasemargin”). How much phase shift the system can tolerate before it becomes unstable.

Asymptotically stable system: 휔푐 < 휔180 Marginally stable system: 휔푐 = 휔180 Unstable system: 휔푐 > 휔180 Frequency Response and Stability Analysis Theory The definitions are as follows:

Gain Crossover-frequency - 휔푐 : 휔180 is the gain margin frequency, in radians/second. A gain margin frequency indicates 퐿 푗휔푐 = 1 = 0푑퐵 where the model phase crosses -180 degrees.

GM (Δ퐾) is the gain margin of the system. Phase Crossover-frequency - 휔180 : 휔 is phase margin frequency, in radians/second. A 표 푐 ∠퐿 푗휔180 = −180 phase margin frequency indicates where the model magnitude crosses 0 . 훥퐾 Gain Margin - GM ( ): PM (휑) is the phase margin of the system.

퐺푀 푑퐵 = − 퐿 푗휔180 푑퐵 We have that: Phase margin PM (휑): 1. Asymptotically stable system: 휔푐 < 휔180 2. Marginally stable system: 휔푐 = 휔180 표 푃푀 = 180 + ∠퐿(푗휔푐) 3. Unstable system: 휔푐 > 휔180 https://www.halvorsen.blog

Stability Analysis of Feedback System - Example

Hans-Petter Halvorsen Example

We will use the following system as an example: 1 퐻 = 퐻푐 = 퐾푝 푝 푠(푠 + 1)

푟 푒 푢 푦 Controller Process −

Sensors

1 퐻 = 푚 3푠 + 1 Analysis of the Feedback System Loop transfer function: 푳(풔) We need to find the Loop transfer function 퐿(푠) using MATLAB. The Loop transfer function is defined as:

퐿 푠 = 퐻푐퐻푝퐻푚 We will use the built-in function series() in MATLAB.

Tracking transfer function: 푻(풔) We need to find the Tracking transfer function 푇(푠) using MATLAB. The Tracking transfer function is defined as:

푦(푠) 퐻푐퐻푝퐻푚 퐿(푠) 푇 푠 = = = We will use the built-in function feedback() in MATLAB. 푟(푠) 1+퐻푐퐻푝퐻푚 1+퐿(푠) Sensitivity transfer function: 푺(풔) We need to find the Sensitivity transfer function 푆(푠) using MATLAB. The Sensitivity transfer function is defined as: 푒(푠) 1 푆 푠 = = = 1 − 푇(푠) 푟(푠) 1+퐿(푠) Stability Analysis

• Plot the Bode plot for the system using e.g., the bode() function in MATLAB • Find the crossover-frequencies (휔180, 휔푐) and stability margins GM (퐴(휔)), PM (휙(휔)) of the system (퐿(푠)) from the Bode plot. • Plot also Bode diagram where the crossover-frequencies, GM and PM are illustrated. Tip! Use the margin() function in MATLAB. • Use also the margin() function in order to find values for 휔180, 휔푐, 퐴(휔), 휙(휔) directly. • You should compare and discuss the results. • How much can you increase 퐾푝 before the system becomes unstable? MATLAB Code: clear, clc, clf

% The Process Transfer function Hp(s) bode(L) num_p=[1]; den1=[1, 0]; den2=[1, 1]; den_p = conv(den1,den2); Hp = tf(num_p, den_p)

% The Measurement Transfer function Hm(s) num_m=[1]; den_m=[3, 1]; Hm = tf(num_m, den_m) margin(L) % The Controller Transfer function Hr(s) Kp = 0.35; Hr = tf(Kp)

% The Loop Transfer function L = series(series(Hr, Hp), Hm)

% Bode Diagram figure(1) bode(L),grid on figure(2) wc = 0.2649 rad/s margin(L) w180 = 0.5774 rad/s [gm, pm, w180, wc] = margin(L); gm = 3.8095 wc w180 gmdB = 11.6174 dB gm gmdB = 20*log10(gm) pm = 36.6917 degrees pm From the Bode plot we can get:

∆퐾 = 11.6 푑퐵

휔푐 = 0.26

휙 = 37 °

0.1 0.3 휔180 = 0.58 10 1 0.2 0.5 Stable vs. Unstable System

• We will find and use different values of 퐾푝 where we get a marginally stable system, an asymptotically stable system and an unstable system. • We will Plot the time response for the tracking function using the step() function in MATLAB for all these 3 categories. How can we use the step response to determine the stability of the system? • We will find 휔180, 휔푐, 퐴 휔 and 휙(휔) in all 3 cases. We will see how we use 휔푐 and 휔180 to determine the stability of the system. • We will find and plot the poles and zeros for the system for all the 3 categories mentioned above. We will see how we can we use the poles to determine the stability of the system. • Bandwidth: We will plot the Loop transfer function 퐿(푠), the Tracking transfer function 푇(푠) and the Sensitivity transfer function 푆(푠) in a Bode diagram for the system for all the 3 categories mentioned above. wc = 0.2649 rad/s Stable System w180 = 0.5774 rad/s 퐾푝 = 0.35 gm = 3.8095 gmdB = 11.6174 dB For what 퐾푝 becomes the system marginally stable? pm = 36.6917 degrees 퐾푝푚 = 0.35 × Δ퐾 = 0.35 × 3.8 ≈ 1,32 lim 푦 푡 = 1 휔푐 < 휔180 푡→∞ Poles in the left half plane wc = 0.5744 rad/s Marginally Stable System w180 = 0.5774 rad/s gm = 1.0101 gmdB = 0.0873 dB 퐾푝 = 1.32 pm = 0.2500 degrees

0 < lim 푦 푡 < ∞ 휔푐 = 휔180 푡→∞ Poles at the imaginary axis wc = 0.7020 Unstable System w180 = 0.5774 gm = 0.6667 gmdB = -3.5218 퐾푝 = 2 pm = -9.6664

lim 푦 푡 = ∞ Poles in the right half plane 휔푐 > 휔180 푡→∞ MATLAB Code ... clear, clc, clf % Bode Diagram % The Process Transfer function Hp(s) figure(1) num_p=[1]; bode(L), grid on den1=[1, 0]; den2=[1, 1]; figure(2) den_p = conv(den1,den2); margin(L), grid on Hp = tf(num_p, den_p) [gm, pm, w180, wc] = margin(L); % The Measurement Transfer function Hm(s) wc num_m=[1]; w180 den_m=[3, 1]; gm Hm = tf(num_m, den_m) gmdB = 20*log10(gm) pm % The Controller Transfer function Hr(s) Kp = 0.35; % Stable System % Simulating step response for control system %Kp = 1.32; % Marginally Stable System (tracking transfer function) %Kp = 2; % Unstable System figure(3) Hr = tf(Kp) step(T) % The Loop Transfer function % Poles L = series(series(Hr, Hp), Hm) pole(T) % Tracking transfer function figure(4) T=feedback(L,1); pzmap(T) % Sensitivity transfer function S=1-T; % Bandwidth figure(5) ... bodemag(L,T,S), grid on Theory “Golden rules” of Stability Margins for a Control System Reference: F. Haugen, Advanced Dynamics and Control: TechTeach, 2010.

Gain Margin (GM): (Norwegian: “Forsterkningsmargin”)

Phase Margin (PM): (Norwegian: “Fasemargin”) https://www.halvorsen.blog

The Bandwidth of the Control System

Hans-Petter Halvorsen The Bandwidth of the Control System

• You should plot the Loop transfer function 퐿(푠), the Tracking transfer function 푇(푠) and the Sensitivity transfer function 푆(푠) in the same Bode diagram. 퐿 • Use, e.g., the bodemag() function in MATLAB (only the gain diagram is of 푇 interest in this case, not the phase diagram). • Use the values for 퐾 and 푇 found in a 푆 푝 푖 previous Tasks. • You need to find the different bandwidths 흎풕, 흎풄, 흎풔 (see the sketch below). The Bandwidth of the Control System Theory The bandwidth of a control system is the frequency which divides the frequency range of good tracking and poor tracking. 3 different Bandwidth definitions:

Good Poor 푆 Tracking Tracking BW

Good Set-point Tracking: |푆(푗휔)| ≪ 1, |푇 (푗휔)| ≈ 1, |퐿(푗휔)| ≫ 1 퐾푝 = 0.35 Good Set-point Tracking: |푆(푗휔)| ≪ 1, |푇 (푗휔)| ≈ 1, |퐿(푗휔)| ≫ 1

휔푠 = 0.092 휔푐 = 0.27 휔푡 = 0.46 0.05 0.1 0.2 0.3 1 https://www.halvorsen.blog

PI Controller Example

Hans-Petter Halvorsen PI Controller - Example

We will use the following system as an example: 1 퐾푝(푇푖푠 + 1) 퐻푝 = 퐻푐 = 푠(푠 + 1) 푇푖푠 푟 푒 푢 Controller Process − 푦

Sensors 1 퐻 = 푚 3푠 + 1 Ziegler–Nichols Frequency Response method

Assume you use a P controller only 푇푖 = ∞, 푇푑 = 0. Then you need to find for Theory which 퐾푝 the closed loop system is a marginally stable system (휔푐 = 휔180). This 퐾푝 is called 퐾푐 (critical gain). The 푇푐 (critical period) can be found from the damped oscillations of the closed loop system. Then calculate the PI(D) parameters using the formulas below. Marginally stable system: Controller 퐾푝 푇푖 푇푑 휔푐 = 휔180

P 0.5퐾푐 ∞ 0 푇 PI 0.45퐾 푐 0 푐 1.2 2휋 푇푐 푇푐 푇푐 = PID 0.6퐾푐 휔 2 8 푇푐 180

퐾푐- Critical Gain 푇푐- Critical Period https://en.wikipedia.org/wiki/Ziegler–Nichols_method PI Controller using Ziegler–Nichols

Ziegler–Nichols (PI Controller):

퐾 = 0.45퐾 푝 푐 This gives the following PI Parameters: 푇 푇 = 푐 푖 1.2 퐾푝 = 0.45퐾푐 = 0.45 ∙ 1.32 ≈ 0.6 2휋 From previous Simulations: 푇 푇 = 푐 = 0.58 ≈ 9푠 푖 1.2 1.2 퐾푐 = 1.32 2휋 2휋 푇푐 = = 휔180 0.58 Skogestad’s method

• The Skogestad’s method assumes you apply a step on the input (푢) and then observe the response and the output (푦), as shown below. • If we have a model of the system (which we have in our case), we can use the following Skogestad’s formulas for finding the PI(D) parameters directly. Figure: F. Haugen, Advanced Dynamics and Control: TechTeach, 2010. Our Process: 1 퐻 = 푝 푠(푠 + 1)

푇 is the time-constant of the control system which the user must specify 푐 퐾 = 1, 푇 = 1, 휏 = 0 We set, e.g., 푇퐶 = 5 푠 and 푐 = 1.5: 1 1 1 퐾푝 = = = = 0.2 푇푖 = 푐 푇푐 + 휏 = 1.5 5 + 0 = 7.5푠 퐾(푇푐 + 휏) 1(10 + 0) 5 MATLAB Ziegler–Nichols and Skogestad’s Formulas: % Ziegler-Nicols Method

Kc = 1.32; % Critical Gain Tc = 2*pi/w180; % Tc - Critical Period

Kp = 0.45 * Kc Ti = Tc/1.2

% Skogestad's Method Tc = 5; % time-constant of the control system which the user must specify c = 1.5;

% H=K*e(-Tau*s)/(T*s+1)*s Kp = 1/K*(Tc) Ti = c*(Tc+Tau) pidtune() MATLAB function clear, clc

%Define Process num = 1; den = [1,1,0]; Hp = tf(num,den)

% Find PI Controller [Hpi,info] = pidtune(Hp,'PI')

%Bode Plots figure(1) bode(Hp) grid on figure(2) bode(Hpi) grid on 퐾푝 = 0.33 % Feedback System T = feedback(Hpi*Hp, 1); 1 1 figure(3) 푇푖 = = ≈ 43.5푠 step(T) 퐾푖 0.023 MATLAB PID Tuner

Define Controller Type Tuning

Define your Process

Add Additional Plots Step Response and other Plots can be shown

PID Parameters MATLAB Simulations 퐾푝 = 0.33, 푇푖 = 43.5푠

퐺푀 = 11.3푑퐵, 푃푀 = 33.1° MATLAB Simulations clear, clc, clf ...

% The Process Transfer function Hp(s) % Bode Diagram num_p=[1]; figure(1) den1=[1, 0]; bode(L), grid on den2=[1, 1]; den_p = conv(den1,den2); figure(2) Hp = tf(num_p, den_p); margin(L), grid on

% The Measurement Transfer function Hm(s) [gm, pm, w180, wc] = margin(L); num_m=[1]; den_m=[3, 1]; wc Hm = tf(num_m, den_m); w180 gm % The PI Controller Transfer function Hc(s) gmdB = 20*log10(gm) %Kp = 0.6; Ti = 9; % Ziegler?Nichols pm %Kp = 0.2; Ti = 7.5; % Skogestad Kp = 0.33; Ti = 43.5; % MATLAB pidtune() function % Simulating step response for control system (tracking transfer function) num = Kp*[Ti, 1]; figure(3) den = [Ti, 0]; step(T) Hc = tf(num,den); % Poles % The Loop Transfer function pole(T) L = series(series(Hc, Hp), Hm); figure(4) % Tracking transfer function pzmap(T) T=feedback(L,1); % Sensitivity transfer function % Bandwidth S=1-T; figure(5) bodemag(L,T,S), grid on ...

References 1. D. Ruscio, System Theory - State Space Analysis and , Lecture Notes, 2015 2. F. Haugen, Advanced Dynamics and Control: TechTeach, 2010. 3. R. C. Dorf and R. H. Bishop, Modern Control Systems. Eleventh Edition: Pearson Prentice Hall. 4. https://www.halvorsen.blog Hans-Petter Halvorsen

University of South-Eastern Norway www.usn.no

E-mail: [email protected] Web: https://www.halvorsen.blog