Matlab: Part II: Modelling, Simulation & Control
Matlab: Part II: Modelling, Simulation & Control
MATLAB
Part II: Modelling, Simulation & Control
Hans-Petter Halvorsen, 2016.06.22
http://home.hit.no/~hansha
Table of Contents
1 Introduction ...................................................................................................................... 4
5 Optimization .................................................................................................................... 34
2 Differential Equations and
ODE Solvers
Task 1: Bacteria Population
birth rate=bx
𝑥 = 𝑏𝑥 − 𝑝𝑥 2
→ Simulate the number of bacteria in the jar after 1 hour, assuming that initially there are
100 bacteria present.
[End of Task]
Solution:
function dx = bacteriadiff(t,x)
% My Simple Differential Equation
b=1;
p=0.5;
dx = b*x - p*x^2;
tspan=[0 1];
x0=100;
6 Differential Equations and ODE Solvers
[t,y]=ode45(@bacteriadiff, tspan,x0);
plot(t,y)
𝑥 = 𝑎𝑥 + 𝑏
5
where 𝑎 = − ,where 𝑇 is the time constant
6
In this case we want to pass a and b as parameters, to make it easy to be able to change
values for these parameters
function dx = mysimplediff(t,x,param)
% My Simple Differential Equation
a=param(1);
b=param(2);
dx=a*x+b;
tspan=[0 25];
x0=1;
a=-1/5;
b=1;
param=[a b];
By doing this, it is very easy to changes values for the parameters a and b.
Note! We need to use the 5. argument in the ODE solver function for this. The 4. argument is
for special options and is normally set to “[]”, i.e., no options.
→ Write the code above
Read more about the different solvers that exists in the Help system in MATLAB
[End of Task]
Use the ode23 function to solve and plot the results of the following differential equation in
the interval [𝑡? , 𝑡A ]:
𝒘D + 𝟏. 𝟐 + 𝒔𝒊𝒏𝟏𝟎𝒕 𝒘 = 𝟎, 𝑡? = 0, 𝑡A = 5, 𝑤 𝑡? = 1
[End of Task]
Solution:
𝑤 D = − 1.2 + 𝑠𝑖𝑛10𝑡 𝑤
This gives:
function dw = diff_task3(t,w)
dw = -(1.2 + sin(10*t))*w;
tspan=[0 5];
w0=1;
This gives:
Use the ode23/ode45 function to solve and plot the results of the following differential
equation in the interval [𝑡? , 𝑡A ]:
𝟏 + 𝒕𝟐 𝒘 + 𝟐𝒕𝒘 + 𝟑𝒘 = 𝟐, 𝑡? = 0, 𝑡A = 5, 𝑤 𝑡? = 0, 𝑤 𝑡? = 1
Note! Higher order differential equations must be reformulated into a system of first order
differential equations.
Tip 2: Set:
𝑤 = 𝑥5
𝑤 = 𝑥2
[End of Task]
Solution:
2 − 2𝑡𝑤 − 3𝑤
𝑤=
1 + 𝑡2
In order to solve it using the ode functions in MATLAB it has to be a set with 1.order ode’s.
So we set:
𝑤 = 𝑥5
𝑤 = 𝑥2
This gives:
𝑥5 = 𝑥2
2 − 2𝑡𝑥2 − 3𝑥5
𝑥2 =
1 + 𝑡2
function dx = diff_secondorder(t,x)
[m,n] = size(x);
dx = zeros(m,n)
dx(1) = x(2);
dx(2) = (2-2*t*x(2)-3*x(1))/(1+t^2);
and:
tspan=[0 5];
x0=[0; 1];
This gives:
if we want to plot only 𝑤 = 𝑥5 we can use
plot(t,x(:,1))
Like this:
tspan=[0 5];
x0=[0; 1];
This gives:
So the solution to:
𝟏 + 𝒕𝟐 𝒘 + 𝟐𝒕𝒘 + 𝟑𝒘 = 𝟐, 𝑡? = 0, 𝑡A = 5, 𝑤 𝑡? = 0, 𝑤 𝑡? = 1
𝑥 = 𝑎𝑥
5
where 𝑎 = − , where 𝑇 is the time constant
6
ST
Note! 𝑥 =
SU
Find the discrete differential equation and plot the solution for this system using MATLAB.
Create a script in MATLAB (.m file) where we plot the solution 𝑥(𝑘).
[End of Task]
Solution:
Then we get:
𝑥XY5 − 𝑥X
= 𝑎𝑥X
𝑇Z
Which gives:
𝑥XY5 = 𝑥X (1 + 𝑇Z 𝑎)
MATLAB Code:
% Model Parameters
T = 5;
12
13 Discrete Systems
a = -1/T;
% Simulation Parameters
Ts = 0.1; %s
Tstop = 30; %s
x(1) = 1;
% Simulation
for k=1:(Tstop/Ts)
x(k+1) = (1+a*Ts).*x(k);
end
Plot:
birth rate=bx
𝑥 = 𝑏𝑥 − 𝑝𝑥 2
We will simulate the number of bacteria in the jar after 1 hour, assuming that initially there
are 100 bacteria present.
→ Find the discrete model using the Euler Forward method by hand and implement and
simulate the system in MATLAB using a For Loop.
[End of Task]
Solution:
We get:
𝑥XY5 − 𝑥X
= 𝑏𝑥X − 𝑝𝑥X2
𝑇Z
This gives:
clc
% Model Parameters
b = 1;
p = 0.5;
% Simulation Parameters
Ts=0.01;
x(1)=100;
k=1;
% Simulation Loop
for i=Ts:Ts:1
% Discrete model
x(k+1) = x(k) + Ts*(b.*x(k) - p*x(k).^2);
k=k+1;
end
𝑑𝑥5
= −𝑥2
𝑑𝑡
𝑑𝑥2
= 𝑥5
𝑑𝑡
MATLAB Course, Part II - Solutions
16 Discrete Systems
Find the discrete system and simulate the discrete system in MATLAB.
[End of Task]
Solution:
𝑥5 𝑘 + 1 = 𝑥5 𝑘 − 𝑇Z 𝑥2 𝑘
𝑥2 𝑘 + 1 = 𝑥2 𝑘 + 𝑇Z 𝑥5 𝑘
MATLAB Code:
% Model Parameters
T = 5;
a = -1/T;
% Simulation Parameters
Ts = 0.1; %s
Tstop = 10; %s
x1(1) = 1;
x2(1) = 1;
% Simulation
for k=1:(Tstop/Ts)
x1(k+1) = x1(k) - Ts.*x2(k);
x2(k+1) = x2(k) + Ts.*x1(k);
end
Plot:
Plot u versus T. Find the interpolated data and plot it in the same graph. Test out different
interpolation types.
[End of Task]
Solution:
MATLAB Script:
figure(1)
plot(u,T, '-o')
%Spline
new_u = linspace(2500,3200,length(u));
new_T = interp1(u, T, new_u, 'spline');
figure(2)
plot(u,T, new_u, new_T, '-o')
18
19 Numerical Techniques
ans =
215.0000
For spline, cubic we get almost the same:
This is because the points listed above are quite linear in their nature.
Plot u versus T.
𝑦 = 𝑎𝑥 + 𝑏
[End of Task]
Solution:
MATLAB Script:
a=p(1)
b=p(2),
x=u;
ymodel=a*x+b;
plot(u,T,'o',u,ymodel)
a =
0.6415
b =
-1.5057e+003
x y
10 23
20 45
30 60
40 82
50 111
60 140
70 167
80 198
90 200
100 220
→ Use the polyfit and polyval functions in MATLAB and compare the models using different
orders of the polynomial.
[End of Task]
Solution:
MATLAB Script:
x=[10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
y=[23, 45, 60, 82, 111, 140, 167, 198, 200, 220];
for n=2:5
p=polyfit(x,y,n);
ymodel=polyval(p,x)
subplot(2,2,n-1)
plot(x,y,'o',x,ymodel)
title(sprintf('Model of order %d', n));
end
→ Create a 1. (linear), 2. (quadratic) and 3.order (cubic) model. Which gives the best model?
Plot the result in the same plot and compare them. Add xlabel, ylabel, title and a legend to
the plot and use different line styles so the user can easily see the difference.
[End of Task]
Solution:
MATLAB Script:
clear, clc
% Real Data
height = [0, 1.7, 1.95, 2.60, 2.92, 4.04, 5.24];
flow = [0, 2.6, 3.6, 4.03, 6.45, 11.22, 30.61];
%linear-------------------------
polyorder = 1; %linear
p1 = polyfit(height, flow, polyorder) % 1.order model
new_flow1 = polyval(p1,new_height); % We use the model to find
new flow values
%quadratic-------------------------
polyorder = 2; %quadratic
p2 = polyfit(height, flow, polyorder) % 2.order model
new_flow2 = polyval(p2,new_height); % We use the model to find
new flow values
%cubic-------------------------
polyorder = 3; %cubic
p3 = polyfit(height, flow, polyorder) % 3.order model
new_flow3 = polyval(p3,new_height); % We use the model to find
new flow values
%Plotting
%We plot the original data together with the model found for
comparison
p1 =
5.3862 -5.8380
p2 =
1.4982 -2.5990 1.1350
p3 =
0.5378 -2.6501 4.9412 -0.1001
Where p1 is the linear model (1.order), p2 is the quadratic model (2.order) and p3 is the
cubic model (3.order).
This gives:
1. order model:
𝑝5 = 𝑎? 𝑥 + 𝑎5 = 5.4𝑥 − 5.8
2. order model:
3. order model:
Discussion:
As you see the cubic model (3.order) gives the best result, i.e., the cubic model fits the data
best.
𝑦 = 𝑥 b + 2𝑥 2 − 𝑥 + 3
Se
Find analytically (use “pen and paper”).
ST
Define a vector x from -5 to +5 and use the diff function to approximate the derivative y with
∆e
respect to x ( ).
∆T
Se
Compare the data in a 2D array and/or plot both the exact value of and the
ST
approximation in the same plot.
𝑦 = sin (𝑥)
𝑦 = 𝑥 i − 1
[End of Task]
Solution:
Analytically solution:
𝑑𝑦
= 3𝑥 2 + 4𝑥 − 1
𝑑𝑥
MATLAB Script:
x = -5:1:5;
Plot of 𝑦(𝑥):
Se
Plot of (Analytical vs. numerical solution):
ST
The values are:
dydx =
42 54
22 31
8 14
0 3
-2 -2
2 -1
12 6
28 19
50 38
78 63
NaN 94
𝑦 = sin (𝑥)
𝑑𝑦
= cos (𝑥)
𝑑𝑥
𝑦 = 𝑥 i − 1
𝑑𝑦
= 5𝑥 l
𝑑𝑥
→ The procedure is exactly the same and will not be shown here.
𝑦 = 𝑥 b + 2𝑥 2 − 𝑥 + 3
Se
Use Differentiation on the Polynomial to find
ST
[End of Task]
Solution:
Analytically solution:
𝑑𝑦
= 3𝑥 2 + 4𝑥 − 1
𝑑𝑥
The MATLAB Script:
p=[1 2 -1 3]
polyder(p)
p =
1 2 -1 3
ans =
3 4 -1
Which is correct.
3𝑥 2 + 6𝑥 + 9 𝑥 2 + 2𝑥
Another approach is to use define is to first use the conv(a,b) function to find the total
polynomial, and then use polyder(p) function.
[End of Task]
Solution:
MATLAB Script:
% Method 1
polyder(p1,p2)
% Method 2
p = conv(p1,p2)
polyder(p)
ans =
9 24 21
p =
3 12 21 18
ans =
9 24 21
→ As expected, the result are the same for the 2 methods used above.
𝑑( 3𝑥 2 + 6𝑥 + 9 𝑥 2 + 2𝑥 )
= 9𝑥 2 + 24𝑥 + 21
𝑑𝑥
𝑦 = 𝑥 b + 2𝑥 2 − 𝑥 + 3
m m
b 2
𝑥 l 2𝑥 b 𝑥 2
(𝑥 + 2𝑥 − 𝑥 + 3)𝑑𝑥 = + − + 3𝑥
n 4 3 2 n
1 l 2 1
= 𝑏 − 𝑎l + 𝑏 b − 𝑎b − 𝑏 2 − 𝑎2 + 3(𝑏 − 𝑎)
4 3 2
Compare the result with the exact solution.
𝑦 = sin 𝑥
𝑦 = 𝑥 i − 1
[End of Task]
Solution:
MATLAB Script:
clc
x = -1:0.1:1;
y = myfunc(x);
plot(x,y)
% Exact Solution
a=-1;
b=1;
Iab = 1/4*(b^4-a^4 )+2/3*(b^3-a^3 )-1/2*(b^2-a^2 )+3*(b-a)
% Method 1
avg_y = y(1:length(x)-1) + diff(y)/2;
A1 = sum(diff(x).*avg_y)
% Method 2
A2 = quad(@myfunc, -1,1)
% Method 3
A3 = quadl(@myfunc, -1,1)
function y = myfunc(x)
y = x.^3 + 2*x.^2 - x + 3;
The result is:
Iab =
7.3333
A1 =
7.3400
A2 =
7.3333
A3 =
7.3333
𝑦 = sin 𝑥
m
m
sin 𝑥 𝑑𝑥 = cos (𝑥) n = cos 𝑏 − cos (𝑎)
n
and:
𝑦 = 𝑥 i − 1
m m
i
𝑥o 𝑏o − 𝑎o
(𝑥 − 1)𝑑𝑥 = −𝑥 = − (𝑏 − 𝑎)
n 6 n
6
The MATLAB Code is not shown, but the procedure is exactly the same for these functions.
𝑦 = 𝑥 b + 2𝑥 2 − 𝑥 + 3
[End of Task]
Solution:
MATLAB Script:
p=[1 2 -1 3];
polyint(p)
ans =
0.2500 0.6667 -0.5000 3.0000 0
or:
m m
b 2
𝑥 l 2𝑥 b 𝑥 2
(𝑥 + 2𝑥 − 𝑥 + 3)𝑑𝑥 = + − + 3𝑥
n 4 3 2 n
𝑓 𝑥 = 𝑥 b − 4𝑥
[End of Task]
Solution:
MATLAB Code:
%Optimization
clear, clc
x = -3:0.1:3;
34
35 Optimization
f = mysimplefunc2(x);
plot(x, f)
[xmin,fmin]=fminbnd(@mysimplefunc2, -3, 3)
function f = mysimplefunc2(x)
f = x.^3 - 4*x;
This gives:
xmin =
1.1547
fmin =
-3.0792
[End of Task]
Solution:
The global minimum is inside a long, narrow, parabolic shaped flat valley. To find the valley is
trivial. To converge to the global minimum, however, is difficult.
MATLAB Script:
function f = bananafunc(x)
f = (1-x(1)).^2 + 100.*(x(2)-x(1).^2).^2;
This gives:
x =
1.0000
1.0000
fval =
8.1777e-010
clear,clc
f = (1-x).^2 + 100.*(y-x.^2).^2;
figure(1)
surf(x,y,f)
figure(2)
mesh(x,y,f)
figure(3)
surfl(x,y,f)
shading interp;
colormap(hot);
This gives:
38
7 Transfer Functions
Task 19: Transfer function
Use the tf function in MATLAB to define the transfer function above. Set K=2 and T=3.
Type “help tf” in the Command window to see how you use this function.
Example:
[End of Task]
Solution:
Transfer function:
𝐾
𝐻 𝑠 =
𝑇𝑠 + 1
With values:
2
𝐻 𝑠 =
3𝑠 + 1
MATLAB Script:
num=[K];
den=[T, 1];
H = tf(num, den)
MATLAB responds:
Transfer function:
39
40 Transfer Functions
-------
3 s + 1
Set 𝐾 = 1, 𝜔? = 1
→ Plot the step response (use the step function in MATLAB) for different values of 𝜁. Select
𝜁 as follows:
𝜁 > 1
𝜁 = 1
0 < 𝜁 < 1
𝜁 = 0
𝜁 < 0
[End of Task]
Solution:
Transfer function:
𝐾
𝐻 𝑠 = 2
𝑠 𝑠
+ 2𝜁 +1
𝜔? 𝜔?
We can use:
s = tf('s')
You can then specify transfer functions directly as rational expressions in s, e.g.,
s = tf('s');
H = (s+1)/(s^2+3*s+1)
clc
% Second order Transfer function
% H(s)=K/((s/?_0 )^2+2? s/?_0 +1)
% Define variables:
K=1;
w0=1;
zeta=1;
step(H)
We can easily change he value for 𝜁 in the script, we can e.g., use 𝜁 = 2, 1, 0.2, 0, −0.2
clc
% Second order Transfer function
% H(s)=K/((s/?_0 )^2+2? s/?_0 +1)
% Define variables:
K=1;
w0=1;
step(H)
pause
end
𝑠+1
𝐻 𝑠 =
𝑠2 − 𝑠 + 3
Plot the time response for the transfer function using the step function. Let the time-interval
be from 0 to 10 seconds, e.g., define the time vector like this:
t=[0:0.01:10]
[End of Task]
Solution:
MATLAB Script:
% Step Response
step(H,t);
% Step Response
step(H,t);
→ We see the system is unstable (which we could see from the transfer function!)
𝐾
𝐻 𝑠 =
𝑠
→ Plot the Step response: Use different values for 𝐾, e.g., 𝐾 = 0.2, 1, 5. Use the step
function in MATLAB.
[End of Task]
Solution:
Pole(s):
Im(s)
Re(s)
In MATLAB you may use the pole function in order to find the poles.
% Integrator
clc
K=1;
% Define the transfer function
num = K;
den = [1 0];
H = tf(num, den)
pole(H)
Step Response:
% Integrator
K=1
% Define the transfer function
s = tf('s');
H = K/s
% Step Response
step(H);
or:
% Integrator
clc
K=1;
% Define the transfer function
num = K;
den = [1 0];
H = tf(num, den)
% Step Response
step(H);
You can easily modify the program in order to try with different values for K. It is also easy to
modify the program using a For Loop, e.g.:
% Integrator
clc
for K = [0.2, 1, 5]
% Define the transfer function
num = K;
den = [1 0];
H = tf(num, den)
% Step Response
step(H);
hold on
end
We can also find the mathematical expression for the step response (𝑦(𝑡))
𝑦 𝑠 = 𝐻 𝑠 𝑢(𝑠)
Where
𝑈
𝑢 𝑠 =
𝑠
The Laplace Transformation pair for a step is as follows:
1
⇔1
𝑠
𝐾 𝑈 1
𝑦 𝑠 =𝐻 𝑠 𝑢 𝑠 = ∙ = 𝐾𝑈 2
𝑠 𝑠 𝑠
We use the following Laplace Transformation pair in order to find 𝑦(𝑡):
1
⇔𝑡
𝑠2
Then we get:
𝑦 𝑡 = 𝐾𝑈𝑡
Conclusion: A bigger K will give a bigger slope (In Norwegian: “stigningstall”) and the
integration will go faster.
𝐾
𝐻 𝑠 =
𝑇𝑠 + 1
• Step response 1: Use different values for K, e.g., K=0.5, 1, 2. Set T=1
• Step response 2: Use different values for T, e.g., T=0.2, 0.5, 1, 2, 4. Set K=1
[End of Task]
Solution:
Pole(s):
5
A 1.order system has a pole: 𝑝 = −
6
Im(s)
Re(s)
-1/T
In MATLAB you may use the pole function in order to find the poles.
Step Response:
MATLAB Script:
% 1.order system
clc
T=1;
num = K;
den = [T 1];
H = tf(num, den)
pole(H)
% Step Response
step(H);
Transfer function:
1
-----
s + 1
ans =
-1
We can easily modify the program for other values for K and T.
% 1.order system
clc
t=[0:0.5:10];
T=1;
K=0.5;
num = K;
den = [T 1];
H1 = tf(num, den);
K=1;
num = K;
den = [T 1];
H2 = tf(num, den);
K=2;
num = K;
den = [T 1];
H3 = tf(num, den);
% Step Response
step(H1,H2,H3,t);
legend('K=0.5', 'K=1', 'K=2');
We could also have used a simple For Loop for this.
→ Conclusion: K defines how much the input signal is amplified through the system.
MATLAB Code:
% 1.order system
clc
t=[0:0.5:10];
K=1;
T=0.2;
num = K;
den = [T 1];
H1 = tf(num, den);
T=0.5;
num = K;
den = [T 1];
H2 = tf(num, den);
T=1;
num = K;
den = [T 1];
H3 = tf(num, den);
T=2;
num = K;
den = [T 1];
H4 = tf(num, den);
% Step Response
step(H1,H2,H3,H4,t);
legend('T=0.2', 'T=0.5', 'T=1', 'T=2');
→ Coclusion: We see from Figure above that smaller T (Time constant) gives faster response.
We compare the simulation results above with the mathematical expression for the step
response (𝑦(𝑡)).
𝑦 𝑠 = 𝐻 𝑠 𝑢(𝑠)
Where
𝑈
𝑢 𝑠 =
𝑠
We use inverse Laplace and find the corresponding transformation pair in order to find
𝑦(𝑡). This gives:
𝐾 𝑈
𝑦 𝑠 = ∙
𝑇𝑠 + 1 𝑠
𝑘
⇔ 𝑘(1 − 𝑒 {U/6 )
𝑇𝑠 + 1 𝑠
This gives:
𝑦 𝑡 = 𝐾𝑈(1 − 𝑒 {U/6 )
𝐾𝜔? 2 𝐾
𝐻 𝑠 = = 2
𝑠 2 + 2𝜁𝜔? 𝑠 + 𝜔? 2 𝑠 𝑠
+ 2𝜁 +1
𝜔? 𝜔?
Where
• 𝐾 is the gain
• 𝜁 zeta is the relative damping factor
• 𝜔? [rad/s] is the undamped resonance frequency.
→ Plot the Step response: Use different values for 𝜁, e.g., 𝜁 = 0.2, 1, 2. Set 𝜔? = 1 and
K=1. Use the step function in MATLAB.
[End of Task]
Solution:
Poles:
𝑝5 , 𝑝2 = −𝜁𝜔? ± 𝜁 2 − 1 𝜔?
Step Response:
We create the following MATLAB Script in order to prove this:
% Define variables:
K=1;
w0=1;
zeta=1;
num = K;
den = [(1/w0)^2, 2*zeta/w0, 1]
H2 = tf(num, den)
zeta=2;
num = K;
den = [(1/w0)^2, 2*zeta/w0, 1]
H3 = tf(num, den)
Special case: When 𝜁 > 0 and the poles are real and distinct we have:
𝐾
𝐻 𝑠 =
(𝑇5 𝑠 + 1)(𝑇2 𝑠 + 1)
We see that this system can be considered as two 1.order systems in series.
𝐾 1 𝐾
𝐻 𝑠 = 𝐻5 𝑠 𝐻5 𝑠 = ∙ =
(𝑇5 𝑠 + 1) (𝑇2 𝑠 + 1) (𝑇5 𝑠 + 1)(𝑇2 𝑠 + 1)
Set 𝑇5 = 2 and 𝑇2 = 5
→ Plot the Step response. Set K=1. Set 𝑇5 = 1 𝑎𝑛𝑑 𝑇2 = 0, 𝑇5 = 1 𝑎𝑛𝑑 𝑇2 = 0.05, 𝑇5 =
1 𝑎𝑛𝑑 𝑇2 = 0.1, 𝑇5 = 1 𝑎𝑛𝑑 𝑇2 = 0.25, 𝑇5 = 1 𝑎𝑛𝑑 𝑇2 = 0.5, 𝑇5 = 1 𝑎𝑛𝑑 𝑇2 = 1. Use the
step function in MATLAB.
[End of Task]
Solution:
Poles:
1 1
𝑝5 = − , 𝑝2 = −
𝑇5 𝑇2
Step Response:
We will find the mathematical expression for the step response (𝑦(𝑡)) in order to analyze
the results from the simulations.
We use inverse Laplace and find the corresponding transformation pair in order to find
𝑦(𝑡)).
𝑦 𝑠 = 𝐻 𝑠 𝑢(𝑠)
Where
𝑈
𝑢 𝑠 =
𝑠
Then we get:
𝐾𝑈
𝑦 𝑠 =
(𝑇5 𝑠 + 1)(𝑇2 𝑠 + 1)𝑠
1 1 {
U
⇔ 1 + (𝑇5 𝑒 6~ − 𝑇2 𝑒 {U/6• )
(𝑇5 𝑠 + 1)(𝑇2 𝑠 + 1)𝑠 𝑇2 − 𝑇5
Then we get:
1 {
U
𝑦 𝑡 = 𝐾𝑈 1 + (𝑇5 𝑒 6~ − 𝑇2 𝑒 {U/6• )
𝑇2 − 𝑇5
We see that the step response is a (weighted) sum of two exponential functions. The
response will be with no overshot and it will be overdamped, as shown in the simulations
above.
𝑥5 = 𝑥2
→ Find the transfer function from the state-space model using MATLAB code.
[End of Task]
Solution:
First we do:
𝑥5 = 𝑥2
We have that:
𝑥 = 𝐴𝑥 + 𝐵𝑢
𝑦 = 𝐶𝑥 + 𝐷𝑢
This gives:
𝑥5 0 1 𝑥5 0 0 𝑢5
= +
𝑥2 −1 −3 𝑥2 2 4 𝑢2
„ …
𝑥5 𝑢5
𝑦= 5 6 𝑥 + 7 0 𝑢
2 2
† ‡
MATLAB Script:
59
60 Discrete Systems
sys = ss(A, B, C, D)
step(sys)
Step Response:
Note! This is a a MISO system (Multiple Input, Single Output).
𝑦 𝑦
𝐻5 = , 𝐻2 =
𝑢5 𝑢2
sys = ss(A, B, C, D)
H = tf(sys)
As you see we get 2 transfer functions because this is a MISO system (Multiple Input, Single
Output).
𝑦 𝑦
𝐻5 = , 𝐻2 =
𝑢5 𝑢2
Where c=damping constant, m=mass, k=spring constant, F=u=force
0 1 𝑥5 0
𝑥5 𝑘 𝑐 1 𝑢
= 𝑥2 +
𝑥2 − −
𝑚 𝑚 𝑚
𝑥5
𝑦= 1 0 𝑥
2
→Apply a step in F (u) and use the step function in MATLAB to simulate the result.
[End of Task]
Solution:
MATLAB Script:
clc
% Define variables
k = 50;
c = 1;
m = 1;
sys = ss(A, B, C, D)
step(sys)
Transfer function:
H = tf(sys)
This gives:
Transfer function:
1
------------
s^2 + s + 50
Find the state-space model from the block diagram below and implement it in MATLAB.
Set
𝑎5 = 5
𝑎2 = 2
[End of Task]
Solution:
𝑥 = 𝐴𝑥 + 𝐵𝑢
𝑦 = 𝐶𝑥 + 𝐷𝑢
𝑥5 = −𝑎5 𝑥5 − 𝑎2 𝑥2 + 𝑏𝑢
𝑥2 = −𝑥2 + 𝑢
𝑦 = 𝑥5 + 𝑐𝑥2
This gives:
𝑥5 −𝑎5 −𝑎2 𝑥5 𝑏
= + 𝑢
𝑥2 0 −1 𝑥2 1
„ …
𝑥5
𝑦= 1 𝑐 𝑥
2
†
Simulation:
0 1 𝑥5 0
𝑥5 𝑘 𝑐 1 𝑢
= 𝑥2 +
𝑥2 − −
𝑚 𝑚 𝑚
𝑥5
𝑦= 1 0 𝑥
2
[End of Task]
Solution:
Code:
clc
% Define variables
k = 50;
c = 1;
m = 1;
sys = ss(A, B, C, D)
A_disc = sysd.A
B_disc = sysd.B
C_disc = sysd.C
D_disc = sysd.D
A_disc =
0.7680 0.0874
-4.3715 0.6805
B_disc =
0.0046
0.0874
C_disc =
1 0
D_disc =
0
Note! We have to specify the sampling time when using the c2d function.
4
𝐻 𝑠 =
2𝑠 + 1
→ Set up the mathematical expressions for 𝐴(𝜔) and 𝜙(𝜔). Use “Pen & Paper” for this
Assignment.
→ Plot the frequency response of the system in a bode plot using the bode function in
MATLAB. Discuss the results.
→ Find 𝐴(𝜔) and 𝜙(𝜔) for the following frequencies using MATLAB code (use the bode
function):
𝝎 𝑨 𝝎 [𝒅𝑩] 𝝓 𝝎 (𝒅𝒆𝒈𝒓𝒆𝒆𝒔)
0.1
0.16
0.25
0.4
0.625
2.5
→ Find 𝐴(𝜔) and 𝜙(𝜔) for the same frequencies above using the mathematical
expressions for 𝐴(𝜔) and 𝜙(𝜔). Tip: Use a For Loop or define a vector w=[0.1, 0.16, 0.25,
0.4, 0.625, 2.5].
[End of Task]
Solutions:
Solution:
66
67 Frequency Response
1 1
𝜔= = = 0.5
𝑇 2
→ Set up the mathematical expressions for 𝐴(𝜔) and 𝜙(𝜔).
Solution:
→ Plot the frequency response of the system in a bode plot using the bode function in
MathScript.
Solution:
MATLAB Script:
clc
% Transfer function
K = 4;
T = 2;
num = [K];
den = [T, 1];
H = tf(num, den)
% Bode Plot
bode(H)
grid
→ Find 𝐴(𝜔) and 𝜙(𝜔) for the following frequencies using MathScript code:
Solution:
MATLAB Script:
clc
% Transfer function
K = 4;
T = 2;
num = [K];
den = [T, 1];
H = tf(num, den)
% Bode Plot
bode(H)
grid
magdB=20*log10(mag); %convert to dB
magdB
phase
Transfer function:
4
-------
2 s + 1
magdB(:,:,1) =
11.8709
magdB(:,:,2) =
11.6178
magdB(:,:,3) =
11.0721
magdB(:,:,4) =
9.8928
magdB(:,:,5) =
7.9546
magdB(:,:,6) =
-2.1085
phase(:,:,1) =
-11.3099
phase(:,:,2) =
-17.7447
phase(:,:,3) =
-26.5651
phase(:,:,4) =
-38.6598
phase(:,:,5) =
-51.3402
phase(:,:,6) =
-78.6901
𝜔 𝐴(𝜔) 𝜙(𝜔)
→ Find 𝐴(𝜔) and 𝜙(𝜔) for the same frequencies above using the mathematical
expressions for 𝐴(𝜔) and 𝜙(𝜔). Tip: Use a For Loop or define a vector w=[0.1, 0.16, 0.25,
0.4, 0.625, 2.5].
Solution:
MATLAB Script:
clc
% Transfer function
K = 4;
T = 2;
num = [K];
den = [T, 1];
H = tf(num, den)
% Frequency List
wlist=[0.1, 0.16, 0.25, 0.4, 0.625,2.5];
N= length(wlist);
for i=1:N
end
% Print to Screen
gain_data = [wlist; gain]'
phase_data=[wlist; phasedeg]'
%------------------------------------------
gain2dB=20*log10(gain2); %convert to dB
% Print to Screen
gain2dB
phase2
gain_data =
0.1000 11.8709
0.1600 11.6178
0.2500 11.0721
0.4000 9.8928
0.6250 7.9546
2.5000 -2.1085
phase_data =
0.1000 -11.3099
0.1600 -17.7447
0.2500 -26.5651
0.4000 -38.6598
0.6250 -51.3402
2.5000 -78.6901
→ We see the results are the same as the result found using the bode function.
(5𝑠 + 1)
𝐻 𝑆 =
2𝑠 + 1 (10𝑠 + 1)
→ Set up the mathematical expressions for 𝐴(𝜔) and 𝜙(𝜔). Use “Pen & Paper” for this
Assignment.
→ Plot the frequency response of the system in a bode plot using the bode function in
MATLAB. Discuss the results.
→ Find 𝐴(𝜔) and 𝜙(𝜔) for some given frequencies using MATLAB code (use the bode
function).
→ Find 𝐴(𝜔) and 𝜙(𝜔) for the same frequencies above using the mathematical
expressions for 𝐴(𝜔) and 𝜙(𝜔). Tip: use a For Loop or define a vector w=[0.01, 0.1, …].
[End of Task]
Solutions:
Solution:
1 1
𝜔5 = = = 1
𝑇5 5
1 1
𝜔2 = = = 0.5
𝑇2 2
1 1
𝜔b = = = 0.1
𝑇b 10
Solution:
→ Plot the frequency response of the system in a bode plot using the bode function in
MATLAB.
→ Find 𝐴(𝜔) and 𝜙(𝜔) for some given frequencies using MATLAB code.
Solution:
MATLAB Script:
clc
clf
% Transfer function
num=[5,1];
den1=[2, 1];
den2=[10,1]
den = conv(den1,den2);
H = tf(num, den)
% Bode Plot
bode(H)
grid
magdB=20*log10(mag); %convert to dB
% Print to Screen
magdB
phase
Bode Plot:
→ Find 𝐴(𝜔) and 𝜙(𝜔) for the same frequencies above using the mathematical
expressions for 𝐴(𝜔) and 𝜙(𝜔). Tip: use a For Loop or define a vector w=[0.01, 0.1, …].
Solution:
→ Same procedure as for previous Task, the code will not be shown here.
𝐾
𝐻™ =
𝑠
š›
Where 𝐾 = , where 𝐾Z = 0,556, 𝐴 = 13,4, 𝜚 = 145
œ„
𝐻ž = 𝐾ž
Where 𝐾ž = 1.
𝐾™
𝐻Ÿ = 𝐾™ +
𝑇𝑠
→ Define the Loop transfer function 𝑳(𝒔), Sensitivity transfer function 𝑺(𝒔) and Tracking
transfer function 𝑻(𝒔) and in MATLAB.
→ 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.
→ Plot the step response for the Tracking transfer function 𝑇(𝑠)
[End of Task]
Solution:
MATLAB Script:
%Model parameters:
Ks=0.556; %(kg/s)/%
A=13.4; %m2
rho=145; %kg/m3
transportdelay=250; %sec
figure(1)
bodemag(L,T,S), grid %Plots maginitude of L, T, and S in Bode
diagram
figure(2)
step(T), grid %Simulating step response for control system
(tracking transfer function)
I get the following values:
𝝎𝒄 = 𝟎. 𝟎𝟎𝟎𝟕𝟑
𝝎𝒔 = 𝟎. 𝟎𝟎𝟎𝟑𝟐
𝝎𝒕 = 𝟎. 𝟎𝟎𝟏𝟐
1
𝐻 𝑆 =
𝑠 𝑠+1 2
We will find the crossover-frequencies for the system using MATLAB. We will also find also
the gain margins and phase margins for the system.
Plot a bode diagram where the crossover-frequencies, GM and PM are illustrated. Tip! Use
the margin function in MATLAB.
[End of Task]
Solution:
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);
% Bode Plot
figure(1)
bode(H)
grid
magdB=20*log10(mag); %convert to dB
% Crossover Frequency-------------------------------------
[gm, pm, gmf, pmf] = margin(H);
gm_dB = 20*log10(gm);
gm_dB
pm
gmf
pmf
figure(2)
margin(H)
We can find the crossover-frequencies, the gain margins and phase margins for the system
from the plot above.
margin(H)
We can alos calculate the values using the same margin function:
This gives:
gm_dB =
6.0206
pm =
21.3877
gmf =
1
pmf =
0.6823
Use the ode45 function to solve and plot the results of the following differential equation in
the interval [𝑡? , 𝑡A ]:
𝟏
𝟑𝒘D + 𝒘 = 𝒄𝒐𝒔𝒕, 𝑡? = 0, 𝑡A = 5, 𝑤 𝑡? = 1
𝟏 + 𝒕𝟐
[End of Task]
Solution:
function dw = diff_task34(t,w)
dw = (cos(t) - (w/(1+t^2)))/3;
and:
tspan=[0 5];
w0=1;
82
83 Additional Tasks
Where c=damping constant, m=mass, k=spring constant, F=u=force
0 1 𝑥5 0
𝑥5 𝑘 𝑐 1 𝑢
= 𝑥2 +
𝑥2 − −
𝑚 𝑚 𝑚
Define the state-space model above using the ss function in MATLAB.
→ Solve and Plot the system using one or more of the built-in solvers (ode32, ode45) in
MATLAB. Apply a step in F (u).
[End of Task]
Solution:
MATLAB Script:
clc
tspan=[0 10];
x0=[0.5; 0];
[t,y]=ode23(@msd_diff, tspan,x0);
plot(t,y)
legend('x1', 'x2')
function dx = msd_diff(t,x)
% Define variables
k = 50;
c = 1;
m = 1;
u=1;
dx = A*x + B*u;
Results:
→ Find the work produced in a piston cylinder device by solving the equation:
«•
𝑊= 𝑃𝑑𝑉
«~
𝑃𝑉 = 𝑛𝑅𝑇
where
• P= pressure
• V=volume, m3
• n=number of moles, kmol
• R=universal gas constant, 8.314 kJ/kmol K
• T=Temperature, K
We also assume that the piston contains 1 mol of gas at 300K and that the temperature is
constant during the process. 𝑉5 = 1𝑚b , 𝑉2 = 5𝑚b
Use both the quad and quadl functions. Compare with the exact solution by solving the
integral analytically.
[End of Task]
Solution:
𝑘𝐽 5 𝑚b
𝑊 = 1 𝑘𝑚𝑜𝑙 ×8.314 ×300𝐾 × ln = 4014𝑘𝐽
𝑘𝑚𝑜𝑙 𝐾 1 𝑚b
Note! Because this work is positive, it is produced by (and not on) the system.
MATLAB Script:
clear, clc
quad(@piston_func, 1, 5)
quadl(@piston_func, 1, 5)
function P = piston_func(V)
% Define constants
n = 1;
R = 8.315;
T=300;
P=n*R*T./V;
ans =
4.0147e+003
ans =
4.0147e+003
𝑥5 = 𝑥2
𝑔 𝑏
𝑥2 = − 𝑥5 − 𝑥
𝑟 𝑚𝑟 2 2
where m is the mass, r is the length of the arm of the pendulum, g is the gravity, b is a
friction coefficient.
[End of Task]
Solution:
State-space model:
0 1 𝑥5
𝑥5 𝑔 𝑏
= 𝑥
𝑥2 − − 2 2
𝑟 𝑚𝑟
MATLAB Script:
clear,clc
tspan=[0 100];
x0=[0.5; 0];
[t,y]=ode23(@pendulum_diff, tspan,x0);
plot(t,y)
legend('x1', 'x2')
function dx = pendulum_diff(t,x)
dx = A*x;
Where c=damping constant, m=mass, k=spring constant, F=u=force
0 1 𝑥5 0
𝑥5 𝑘 𝑐 1 𝑢
= 𝑥2 +
𝑥2 − −
𝑚 𝑚 𝑚
𝑥5
𝑦= 1 0 𝑥
2
→ Simulate the system using the lsim function in the Control System Toolbox.
[End of Task]
Solution:
MATLAB Script:
clear, clc
% Define variables
k = 50;
c = 1;
m = 1;
t = 0:0.01:5;
u = eye(length(t),1);
x0=[0.5; 0];
lsim(sssys, u, t, x0)
E-mail: [email protected]
Blog: http://home.hit.no/~hansha/