MATLAB Course - Part 2
MATLAB Course - Part 2
MATLAB Course - Part 2
https://www.halvorsen.blog
Modelling, Simulation and
Control in MATLAB
University of South-Eastern Norway
MATLAB
Modelling, Simulation & Control
Hans-Petter Halvorsen, 2019.08.06
http://www.halvorsen.blog
Preface
Copyright You cannot distribute or copy this document without
permission from the author. You cannot copy or link to this document
directly from other sources, web pages, etc. You should always link to the
proper web page where this document is located, typically
http://www.halvorsen.blog
In this MATLAB Course, you will learn basic MATLAB and how to use
MATLAB in Control and Simulation applications. An introduction to
Simulink and other Tools will also be given.
This is a self-paced course based on this document and some short videos
on the way. This document contains lots of examples and self-paced tasks
that the users will go through and solve on their own. The user may go
through the tasks in this document in their own pace and the instructor
will be available for guidance throughout the course.
1. Introduction to MATLAB
2. Modelling, Simulation and Control
3. Simulink and Advanced Topics
In Part 2 of the course you will learn how to use MATLAB in Modelling,
Control and Simulation.
The course consists of lots of Tasks you should solve while reading this
course manual and watching the videos referred to in the text.
ii
Make sure to bring your headphones for the videos in this
course. The course consists of several short videos that will give you an
introduction to the different topics in the course.
Prerequisites
MATLAB Basics:
http://www.halvorsen.blog/documents/programming/matlab/matlab_basi
cs.php
iii
MATLAB Videos:
http://www.halvorsen.blog/documents/video/matlab_basics_videos.php
On these web pages you find video solutions, complete step by step
solutions, downloadable MATLAB code, additional resources, etc.
iv
Table of Contents
Preface ............................................................................................ ii
Table of Contents.............................................................................. v
1 Introduction ............................................................................... 1
v
vi Table of Contents
5 Optimization ............................................................................. 42
Interpolation............................................................................. 79
Optimization ................................................................................ 80
http://www.halvorsen.blog/documents/programming/matlab
1
2 Differential Equations
and ODE Solvers
MATLAB have lots of built-in functionality for solving differential equations.
MATLAB includes functions that solve ordinary differential equations (ODE)
of the form:
𝑑𝑦
= 𝑓(𝑡, 𝑦), 𝑦(𝑡0 ) = 𝑦0
𝑑𝑡
𝑑𝑦
= 𝑦 ′ = 𝑦̇
𝑑𝑡
Example:
Given the following differential equation:
𝑥̇ = 𝑎𝑥
1
where 𝑎 = − 𝑇 ,where 𝑇 is the time constant
𝑑𝑥
Note! 𝑥̇ =
𝑑𝑡
𝑥(𝑡) = 𝑒 𝑎𝑡 𝑥0
We shall plot the solution for this differential equation using MATLAB.
2
3 Differential Equations and ODE
Solvers
We will create a script in MATLAB (.m file) where we plot the solution 𝑥(𝑡)
in the time interval 0 ≤ 𝑡 ≤ 25
T = 5;
a = -1/T;
x0 = 1;
t = [0:1:25]
x = exp(a*t)*x0;
plot(t,x);
grid
[End of Example]
This works fine, but the problem is that we first have to find the solution
to the differential equation – instead we can use one of the built-in solvers
for Ordinary Differential Equations (ODE) in MATLAB. In the examples and
tasks below we will learn how we can use these built-in ODE solvers.
[t,y] = solver(odefun,tspan,y0,options,…)
where “solver” is one of the ODE solver functions (ode23, ode45, etc.).
Note! If you don’t specify the resulting array [t, y], the function create a
plot of the result.
Example:
Given the following differential equation:
𝑥̇ = 𝑎𝑥
1
where 𝑎 = − ,where 𝑇 is the time constant
𝑇
We use the ode23 solver in MATLAB for solving the differential equation
(“runmydiff.m”):
tspan = [0 25];
x0 = 1;
[t,x] = ode23(@mydiff,tspan,x0);
plot(t,x)
function dx = mydiff(t,x)
a = -1/5;
dx = a*x;
This gives the same results as shown in the previous example above and
MATLAB have solved the differential equation for us (numerically).
[End of Example]
birth rate=bx
𝑥̇ = 𝑏𝑥 − 𝑝𝑥 2
→ Simulate (i.e., create a plot) the number of bacteria in the jar after 1
hour, assuming that initially there are 100 bacteria present.
[End of Task]
𝑥̇ = 𝑎𝑥 + 𝑏
1
where 𝑎 = − 𝑇 ,where 𝑇 is the time constant
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 𝑎 and
𝑏 without changing the code for the differential equation.
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.
𝑥̇ = 𝑏𝑥 − 𝑝𝑥 2
You should also read more about the different solvers (ode34, ode 45,
etc.) 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 [𝑡0 , 𝑡𝑓 ]:
[End of Task]
Example:
Given the differential equations:
𝑑𝑦
=𝑥
𝑑𝑡
𝑑𝑥
= −𝑦
𝑑𝑡
function dy = mydiff(t,y)
dy(1) = y(2);
dy(2) = -y(1);
dy = [dy(1); dy(2)];
plot(t,y)
title('solution of dy/dt=x and dx/dt=-y')
legend('y', 'x')
The equations are solved in the time span [−1, 1] with initial values [1, 1].
𝑑𝑥1
= −𝑥2
𝑑𝑡
𝑑𝑥2
= 𝑥1
𝑑𝑡
𝑥1 𝑥̇
Where we have the vector 𝑥 = [𝑥 ] and 𝑥̇ = [ 1 ]
2 𝑥̇ 2
dxdt1 = -x2;
dxdt2 = x1;
But if we have more than one differential equation (in this case we
have 2), we need to use vectors!
dxdt(1) = -x(2);
dxdt(2) = x(1);
dxdt(1) = -x(2);
dxdt(2) = x(1);
dxdt = dxdt';
Note! The function mydiff must return a column vector, that’s why we
need to transpose it.
tspan = [-1,1];
x0 = [1,1];
plot (t,x)
legend('x1', 'x2')
[End of Example]
Use the ode23/ode45 function to solve and plot the results of the following
differential equation in the interval [𝑡0 , 𝑡𝑓 ]:
𝑑2 𝑤
Note! 𝑤̈ = 𝑑𝑡 2
Tip 2: Set:
𝑤 = 𝑥1
𝑤̇ = 𝑥2
[End of Task]
Example:
Given the following differential equations:
𝑥̇ 1 = −0.9𝑥1 + 0.1𝑥2 + 1
𝑥̇ 3 = −0.7𝑥1 − 0.1𝑥3
MATLAB:
dxdt = dxdt';
clear
clc
tspan = [0,30];
x0 = [0,0,0];
plot (t,x)
[End of Example]
Below we see a continuous signal vs the discrete signal for a given system
with discrete time interval 𝑇𝑠 = 0.1𝑠.
3.1 Discretization
In order to discretize a continuous model there are lots of different
methods to use. One of the simplest is Euler Forward method:
𝑥𝑘+1 − 𝑥𝑘
𝑥̇ ≈
𝑇𝑠
12
13 Discrete Systems
Example:
Given the following differential equation:
𝑥̇ = −𝑎𝑥 + 𝑏𝑢
Note!
𝑑𝑥
𝑥̇ is the same as 𝑑𝑡
Where:
𝑎, 𝑏 - Constants
𝑇𝑠 - Sampling Interval
Then we get:
𝑥𝑘+1 − 𝑥𝑘
= −𝑎𝑥𝑘 + 𝑏𝑢𝑘
𝑇𝑠
% Model Parameters
a = 0.25;b = 2;
% Simulation Parameters
Ts = 0.1; %s
Tstop = 30; %s
uk = 1; % Step Response
x(1) = 0;
% Simulation
for k=1:(Tstop/Ts)
x(k+1) = (1-a*Ts).*x(k) + Ts*b*uk;
end
[End of Example]
𝑥̇ = 𝑎𝑥
1
where 𝑎 = − 𝑇 , where 𝑇 is the time constant
𝑑𝑥
Note! 𝑥̇ = 𝑑𝑡
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]
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]
𝑑𝑥1
= −𝑥2
𝑑𝑡
𝑑𝑥2
= 𝑥1
𝑑𝑡
Find the discrete system and simulate the discrete system in MATLAB.
Solve the equations, e.g., in the time span [−1 1] with initial values [1, 1].
[End of Task]
In the code example below the following simple differential equation will
be used:
𝑥̇ = 𝑎𝑥 + 𝑏𝑢
MATLAB Code 1;
clear, clc
tic
a=-10; b=0.5;
x=0; u=1;
dt=0.01;
N=1000000000;
X(1)=x;
for i=1:N
X(i+1)=X(i)+dt*(a*X(i)+b*u);
end
toc
MATLAB Code 2:
clear, clc
tic
a=-10; b=0.5;
x=0; u=1;
dt=0.01;
N=1000000000;
X=zeros(N,1);
for i=1:N
X(i+1)=X(i)+dt*(a*X(i)+b*u);
end
toc
MATLAB Code 3:
clear, clc
tic
a=-10; b=0.5;
x=0; u=1;
dt=0.01;
N=1000000000;
X=zeros(N,1);
for i=1:N
X(i)=x;
x=x+dt*(a*x+b*u);
end
toc
Try the different code examples and note the execution time. Try with
different values of N, etc.
[End of Example]
Example:
In the code example below the following simple differential equation will
be used:
𝑥̇ = −𝑎𝑥 + 𝑏𝑢
MATLAB Code 1;
% Model Parameters
a = 0.25;b = 2;
% Simulation Parameters
Ts = 0.01;
Tstop = 10000000;
x(1) = 0;
N = Tstop/Ts;
u = linspace(0,1,N);
% Simulation
tic
for k=1:N
x(k+1) = (1-a*Ts).*x(k) + Ts*b*u(k);
end
toc
grid on
MATLAB Code 2:
% Model Parameters
a = 0.25;b = 2;
% Simulation Parameters
Ts = 0.01;
Tstop = 10000000;
uk = 1;
N = Tstop/Ts;
u = linspace(0,1,N);
%Preallocation
x = zeros(N,1);
% Simulation
tic
for k=1:N
x(k+1) = (1-a*Ts).*x(k) + Ts*b*u(k);
end
toc
MATLAB Code 3:
% Model Parameters
a = 0.25;b = 2;
% Simulation Parameters
Ts = 0.01;
Tstop = 10000000;
uk = 1;
x = 0;
X(1)=x;
N = Tstop/Ts;
u = rand(N,1);
u = linespace(0,1,N);
% Simulation
X = zeros(N,1);
tic
for k=1:N
x = (1-a*Ts)*x + Ts*b*u(k);
X(k+1)=x;
end
toc
There is not a “best way” that can be used for all kind of systems. It will
be your responsibility to find the best solution for your system. That’s why
it is important that you know about different ways to do things, so you are
able to find the best solution in a given situation.
[End of Example]
4.1 Interpolation
Interpolation is used to estimate data points between two known points.
The most common interpolation technique is Linear Interpolation.
Example:
Given the following data:
x y
0 15
1 10
2 9
3 6
4 2
5 0
x=0:5;
y=[15, 10, 9, 6, 2, 0];
plot(x,y ,'-o')
The answer is 4, from the plot below we see this is a good guess:
21
22 Numerical Techniques
[End of Example]
The default is linear interpolation, but there are other types available,
such as:
• linear
• nearest
• spline
• cubic
• etc.
Type “help interp1” in order to read more about the different options.
Example:
In this example we will use a spline interpolation on the same data as in
the example above.
x=0:5;
y=[15, 10, 9,6, 2, 0];
new_x=0:0.2:5;
new_y=interp1(x,y,new_x, 'spline')
The result is as we plot both the original point and the interpolated points
in the same graph:
[End of Example]
Task 8: Interpolation
Plot u versus T. Find the interpolated data and plot it in the same graph.
Test out different interpolation types. Discuss the results. What kind of
interpolation is best in this case?
[End of Task]
MATLAB has built-in curve fitting functions that allows us to create empiric
data model. It is important to have in mind that these models are good
only in the region we have collected data.
Here are some of the functions available in MATLAB used for curve fitting:
These techniques use a polynomial of degree N that fits the data Y best in
a least-squares sense.
𝑦 = 𝑎𝑥 + 𝑏
Example:
Given the following data:
x y
0 15
1 10
2 9
3 6
4 2
5 0
𝑦 = 𝑎𝑥 + 𝑏
x=[0, 1, 2, 3, 4 ,5];
y=[15, 10, 9, 6, 2 ,0];
n=1; % 1.order polynomial
p = polyfit(x,y,n)
ans =
-2.9143 14.2857
𝑦 = −2.9143𝑥 + 14.2857
We can also plot the measured data and the model in the same plot:
x=[0, 1, 2, 3, 4 ,5];
y=[15, 10, 9, 6, 2 ,0];
n=1; % 1.order polynomial
p=polyfit(x,y,n);
a=p(1);
b=p(2);
ymodel=a*x+b;
plot(x,y,'o',x,ymodel)
[End of Example]
Plot u versus T.
𝑦 = 𝑎𝑥 + 𝑏
[End of Task]
Example:
Given the following data:
x y
0 15
1 10
2 9
3 6
4 2
5 0
We will use the polyfit and polyval functions in MATLAB and compare the
models using different orders of the polynomial.
x=[0, 1, 2, 3, 4 ,5];
y=[15, 10, 9, 6, 2 ,0];
ymodel=polyval(p,x);
subplot(2,2,n-1)
plot(x,y,'o',x,ymodel)
title(sprintf('Model of order %d', n));
end
p =
0.0536 -3.1821 14.4643
p =
-0.0648 0.5397 -4.0701 14.6587
p =
0.1875 -1.9398 6.2986 -9.4272 14.9802
p =
-0.0417 0.7083 -4.2083 10.2917 -11.7500 15.0000
As expected, the higher order models match the data better and better.
Note! The fifth order model matches exactly because there were
only six data points available.
[End of Example]
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]
[End of Task]
𝑑𝑦 ∆𝑦 𝑦2 − 𝑦1
= =
𝑑𝑥 ∆𝑥 𝑥2 − 𝑥1
Example:
𝑑𝑦
We will use Numerical Differentiation to find on the following function:
𝑑𝑥
𝑦 = 𝑥2
x y
-2 4
-1 1
0 0
1 1
2 4
First, we will plot the data points together with the real function 𝑦 = 𝑥 2
using the following code:
x=-2:0.1:2;
y=x.^2;
plot(x,y)
hold on
x=-2:2;
y=x.^2;
plot(x,y, '-oc')
𝑑𝑦
Then we want to find the derivative 𝑑𝑥
𝑑𝑦
= 2𝑥
𝑑𝑥
We will use this to compare the results from the numerical differentiation
with the exact solution (see above).
x=-2:2;
y=x.^2;
dydx_num=diff(y)./diff(x);
dydx_exact=2*x;
dydx=[[dydx_num, NaN]', dydx_exact']
This gives the following results (left column is from the numerical
derivation, while the right column is from the exact derivation):
dydx =
-3 -4
-1 -2
1 0
3 2
NaN 4
[End of Example]
𝑦 = 𝑥 3 + 2𝑥 2 − 𝑥 + 3
𝑑𝑦
Find 𝑑𝑥
analytically (use “pen and paper”).
𝑑𝑦
Compare the data in a 2D array and/or plot both the exact value of and
𝑑𝑥
the approximation in the same plot.
𝑦 = sin(𝑥)
𝑦 = 𝑥5 − 1
[End of Task]
Example
Given the polynomial
𝑝(𝑥) = 2 + 𝑥 3
𝑝(𝑥) = 1 ∙ 𝑥 3 + 0 ∙ 𝑥 2 + 0 ∙ 𝑥 + 2
>> p=[1, 0, 0, 2]
We know that: 𝑝′ = 3𝑥 2
>> p=[1, 0, 0, 2]
p =
1 0 0 2
>> polyder(p)
ans =
3 0 0
𝑝(𝑥)′ = 3 ∙ 𝑥 2 + 0 ∙ 𝑥 2 + 0
𝑝1 = 3, 𝑝2 = 0, 𝑝3 = 0
[End of Example]
𝑦 = 𝑥 3 + 2𝑥 2 − 𝑥 + 3
𝑑𝑦
Use Differentiation on the Polynomial to find 𝑑𝑥
[End of Task]
[End of Task]
An integral can be seen as the area under a curve. Given 𝑦 = 𝑓(𝑥) the
approximation of the Area (A) under the curve can be found dividing the
area up into rectangles and then summing the contribution from all the
rectangles:
𝑛−1
trapezoidal method
quad Numerically evaluate integral, adaptive Simpson >>
quadrature.
Q = QUAD(FUN,A,B) tries to approximate the
integral of calar-valued function FUN from A to B.
FUN is a function handle. The function Y=FUN(X)
should accept a vector argument X and return a
vector result Y, the integrand evaluated at each
element of X. Uses adaptive Simpson quadrature
method
quadl Same as quad, but uses adaptive Lobatto >>
quadrature method
polyint Integrate polynomial analytically. POLYINT(P,K) >> I = polyint(p)
xmax
Example:
Given the function:
𝑦 = 𝑥2
We will use the trapezoid rule and the diff function in MATLAB to solve the
numerical integral of 𝑥 2 from 0 to 1.
x=0:0.1:1;
y=x.^2;
Note!
A = sum(diff(x).*avg_y)
A =
0.3350
quad('x.^2', 0,1)
ans =
0.3333
quadl('x.^2', 0,1)
ans =
0.3333
A = trapz(x,y)
A=
0.3350
[End of Example]
Use diff, quad, quadl, trapz and integral on the following equation:
𝑦 = 𝑥 3 + 2𝑥 2 − 𝑥 + 3
𝑏 𝑏
3 2
𝑥 4 2𝑥 3 𝑥 2
∫ (𝑥 + 2𝑥 − 𝑥 + 3)𝑑𝑥 = ( + − + 3𝑥)|
𝑎 4 3 2 𝑎
1 4 2 1
= (𝑏 − 𝑎4 ) + (𝑏 3 − 𝑎3 ) − (𝑏 2 − 𝑎2 ) + 3(𝑏 − 𝑎)
4 3 2
𝑦 = sin(𝑥)
𝑦 = 𝑥5 − 1
[End of Task]
𝑦 = 𝑥 3 + 2𝑥 2 − 𝑥 + 3
[End of Task]
Example:
Given the following function:
𝑓(𝑥) = 𝑥 2 + 2𝑥 + 1
42
43 Optimization
x = -5:1:5;
f = mysimplefunc(x);
plot(x, f)
function f = mysimplefunc(x)
f = x.^2 + 2.*x + 1;
This gives:
x_min =
-1
→ The minimum of the function is -1. This can also be shown from the
plot.
[End of Example]
Note! If a function has more than one variable, we need to use the
fminsearch function.
Example:
function f = myfunc(x)
This gives:
x =
0.7500
1.5000
fval =
0.6250
clear,clc
f = 2.*(x-1).^2 + x - 2 + (y-2).^2 + y;
figure(1)
surf(x,y,f)
figure(2)
mesh(x,y,f)
figure(3)
surfl(x,y,f)
shading interp;
colormap(hot);
[End of Example]
𝑓(𝑥) = 𝑥 3 − 4𝑥
[End of Task]
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. But MATLAB will hopefully do the job for us.
[End of Task]
47
48 Control System Toolbox
7.1 Introduction
Transfer functions are a model form based on the Laplace transform.
Transfer functions are very useful in analysis and design of linear dynamic
systems.
𝑦(𝑠)
𝐻(𝑆) =
𝑢(𝑠)
𝑦(𝑠) 𝐾
𝐻(𝑠) = =
𝑢(𝑠) 𝑇𝑠 + 1
there
𝐾 is the Gain
49
50 Transfer Functions
A first order transfer function with time-delay has the following transfer
function:
𝑦(𝑠) 𝐾
𝐻(𝑠) = = 𝑒 −𝜏𝑠
𝑢(𝑠) 𝑇𝑠 + 1
Before you start, you should use the Help system in MATLAB to read more
about these functions. Type “help <functionname>” in the Command
window.
Use the tf function in MATLAB to define the transfer function above. Set
𝐾 = 2 and 𝑇 = 3.
Type “help tf” in the Command window to see how you use this function.
Example:
[End of Task]
𝐾
𝐻(𝑠) = 2
𝑠 𝑠
(𝜔 ) + 2𝜁 𝜔 + 1
0 0
Where
𝐾 is the gain
Set 𝐾 = 1, 𝜔0 = 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]
𝑠+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]
• Integrator
• 1. Order system
• 2. Order system
𝐾
𝐻(𝑠) =
𝑠
→ Plot the Step response: Use different values for 𝐾, e.g., 𝐾 = 0.2, 1, 5. Use
the step function in MATLAB.
[End of Task]
𝐾
𝐻(𝑠) =
𝑇𝑠 + 1
[End of Task]
𝐾𝜔0 2 𝐾
𝐻(𝑠) = = 2
𝑠 2 + 2𝜁𝜔0 𝑠 + 𝜔0 2 𝑠 𝑠
(𝜔 ) + 2𝜁 𝜔 + 1
0 0
Where
• 𝐾 is the gain
• 𝜁 zeta is the relative damping factor
• 𝜔0 [rad/s] is the undamped resonance frequency.
→ Plot the Step response: Use different values for 𝜁, e.g., 𝜁 = 0.2, 1, 2. Set
𝜔0 = 1 and K=1. Use the step function in MATLAB.
[End of Task]
Special case: When 𝜻 > 0 and the poles are real and distinct we
have:
𝐾
𝐻(𝑠) =
(𝑇1 𝑠 + 1)(𝑇2 𝑠 + 1)
𝐾 1 𝐾
𝐻(𝑠) = 𝐻1 (𝑠)𝐻1 (𝑠) = ∙ =
(𝑇1 𝑠 + 1) (𝑇2 𝑠 + 1) (𝑇1 𝑠 + 1)(𝑇2 𝑠 + 1)
Set 𝑇1 = 2 and 𝑇2 = 5
[End of Task]
8.1 Introduction
A state-space model is a structured form or representation of a set of
differential equations. State-space models are very useful in Control
theory and design. The differential equations are converted in matrices
and vectors, which is the basic elements in MATLAB.
𝑥̇ 1 𝑥1 𝑢
𝑎11 ⋯ 𝑎𝑛1
𝑥2 𝑏11 ⋯ 𝑏𝑛1 𝑢1
𝑥̇ 2
[ ]=[ ⋮ ⋱ ⋮ ][ ]+ [ ⋮
⋮ ⋱ ⋮ ] [ ⋮2 ]
⋮
⏟𝑎1𝑚 ⋯ 𝑎𝑛𝑚
𝑥 ⏟𝑏1𝑚 ⋯ 𝑏𝑛𝑚 𝑢
⏟𝑥̇ 𝑛 ⏟ 𝑛 ⏟ 𝑛
𝐴 𝐵
𝑥̇ 𝑥 𝑢
𝑦1 𝑥1 𝑢
𝑦2
𝑐11 ⋯ 𝑐𝑛1
𝑥 𝑑11 ⋯ 𝑑𝑛1 𝑢1
[⋮]=[ ⋮ ⋱ ⋮ ] [ 2] + [ ⋮ ⋱ ⋮ ] [ ⋮2 ]
⋮
𝑦𝑛 ⏟𝑐1𝑚 ⋯ 𝑐𝑛𝑚
𝑥𝑛 ⏟𝑑1𝑚 ⋯ 𝑑𝑛𝑚 𝑢
⏟ 𝐶
⏟ ⏟ 𝑛
𝐷
𝑦 𝑥 𝑢
𝑥̇ = 𝐴𝑥 + 𝐵𝑢
57
58 State-space Models
𝑦 = 𝐶𝑥 + 𝐷𝑢
Example:
Given the following equations:
1 1
𝑥̇ 1 = − 𝑥2 + 𝐾𝑝 𝑢
𝐴𝑡 𝐴𝑡
𝑥̇ 2 = 0
1 𝐾𝑝
𝑥̇ 0 − 𝑥1
[ 1] = [ ] [
𝐴𝑡 𝑥2 ] + [ 𝐴𝑡 ] 𝑢
𝑥̇ 2
⏟0 0 ⏟ 0
𝐴 𝐵
𝑥1
𝑦 = [⏟
1 0] [𝑥 ]
2
𝐶
[End of Example]
Example:
% Creates a state-space model
A = [1 3; 4 6];
B = [0; 1];
C = [1, 0];
D = 0;
SysOutSS = ss(A, B, C, D)
[End of Example]
Before you start, you should use the Help system in MATLAB to read more
about these functions. Type “help <functionname>” in the Command
window.
8.2 Tasks
Task 26: State-space model
𝑥̇ 1 = 𝑥2
→ Find the transfer function from the state-space model using MATLAB
code.
[End of Task]
0 1 𝑥 0
𝑥̇ 𝑐 ] [ 1] + [ 1 ] 𝑢
[ 1] = [ 𝑘 𝑥2
𝑥̇ 2 − −
𝑚 𝑚 𝑚
𝑥1
𝑦 = [1 0] [𝑥 ]
2
→Apply a step in F (u) and use the step function in MATLAB to simulate
the result.
[End of Task]
Find the state-space model from the block diagram below and implement
it in MATLAB.
Set
𝑎1 = 5
𝑎2 = 2
[End of Task]
𝑥̇ = 𝐴𝑥 + 𝐵𝑢
𝑦 = 𝐶𝑥 + 𝐷𝑢
𝑦𝑘 = 𝐶𝑥𝑘 + 𝐷𝑢𝑘
𝑦𝑘 = 𝐶𝑥𝑘 + 𝐷𝑢𝑘
Before you start, you should use the Help system in MATLAB to read more
about these functions. Type “help <functionname>” in the Command
window.
0 1 𝑥 0
𝑥̇ 𝑐 ] [ 1] + [ 1 ] 𝑢
[ 1] = [ 𝑘 𝑥2
𝑥̇ 2 − −
𝑚 𝑚 𝑚
𝑥1
𝑦 = [1 0] [𝑥 ]
2
[End of Task]
9.1 Introduction
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 a certain amplitude and a certain
frequency.
𝑢(𝑡) = 𝑈 𝑠𝑖𝑛𝜔𝑡
𝑦(𝑡) = 𝑈𝐴
⏟ sin(𝜔𝑡 + 𝜙)
𝑌
𝑌
Where 𝐴 = 𝑈 is the ratio between the amplitudes of the output signal and
the input signal (in steady-state).
63
64 Frequency Response
𝑦(𝑠)
𝐻(𝑆) =
𝑢(𝑠)
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, 𝜙(𝜔).
𝐴(𝜔) = |𝐻(𝑗𝜔)|
𝜙(𝜔) = ∠𝐻(𝑗𝜔)
margin Calculates and/or plots the smallest gain and >num = [1]
>den = [1, 5, 6]
phase margins of a single-input single-output >H = tf(num, den)
(SISO) system model. The gain margin indicates margin(H)
where the frequency response crosses at 0
Example:
Here you will learn to plot the frequency response in a Bode diagram.
𝑦(𝑠) 1
𝐻(𝑠) = =
𝑢(𝑠) 𝑠 + 1
Below we see the script for creating the frequency response of the
system in a bode plot using the bode function in MATLAB. Use the grid
function to apply a grid to the plot.
[End of Example]
Before you start, you should use the Help system in MATLAB to read more
about these functions. Type “help <functionname>” in the Command
window.
9.2 Tasks
Task 30: 1. order system
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]
(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]
𝐿(𝑠) = 𝐻𝑐 𝐻𝑝 𝐻𝑚
Where
𝑦(𝑠) 𝐻𝑐 𝐻𝑝 𝐻𝑚 𝐿(𝑠)
𝑇(𝑠) = = = = 1 − 𝑆(𝑠)
𝑟(𝑠) 1 + 𝐻𝑐 𝐻𝑝 𝐻𝑚 1 + 𝐿(𝑠)
The Tracking Property is good if the tracking function T has value equal
to or close to 1:
|𝑇| ≈ 1
𝑒(𝑠) 1
𝑆(𝑠) = = = 1 − 𝑇(𝑠)
𝑟(𝑠) 1 + 𝐿(𝑠)
|𝑆| ≈ 0 𝑜𝑟 |𝑆| ≪ 1
Note!
𝐿(𝑠) 1
𝑇(𝑠) + 𝑆(𝑠) = + ≡1
1 + 𝐿(𝑠) 1 + 𝐿(𝑠)
|𝐿(𝑗𝜔)| ≫ 1
|𝐿(𝑗𝜔)| ≪ 1
1 = 0𝑑𝐵
𝝎𝒕 – the frequency where the gain of the Tracking function 𝑇(𝑗𝜔) has the
value:
1
≈ 0.71 = −3𝑑𝐵
√2
1
1− ≈ 0.29 = −11𝑑𝐵
√2
𝐾
𝐻𝑝 =
𝑠
𝐻𝑚 = 𝐾𝑚
Where 𝐾𝑚 = 1.
𝐾𝑝
𝐻𝑐 = 𝐾𝑝 +
𝑇𝑖 𝑠
→ 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]
• Unstable system
The Gain Margin – GM (Δ𝐾) is how much the loop gain can increase
before the system become unstable.
The Phase Margin - PM (𝜑) is how much the phase lag function of the
loop can be reduced before the loop becomes unstable.
Where:
Gain Crossover-frequency - 𝝎𝒄 :
|𝐿(𝑗𝜔𝑐 )| = 1 = 0𝑑𝐵
∠𝐿(𝑗𝜔180 ) = −180𝑜
or:
𝑃𝑀 = 180𝑜 + ∠𝐿(𝑗𝜔𝑐 )
We have that:
1
𝐻(𝑆) =
𝑠(𝑠 + 1)2
[End of Task]
Use the ode45 function to solve and plot the results of the following
differential equation in the interval [𝑡0 , 𝑡𝑓 ]:
𝟏
𝟑𝒘′ + 𝒘 = 𝒄𝒐𝒔𝒕, 𝑡0 = 0, 𝑡𝑓 = 5, 𝑤(𝑡0 ) = 1
𝟏 + 𝒕𝟐
[End of Task]
0 1 𝑥 0
𝑥̇ 𝑐 ] [ 1] + [ 1 ] 𝑢
[ 1] = [ 𝑘 𝑥2
𝑥̇ 2 − −
𝑚 𝑚 𝑚
Set 𝑐 = 1, 𝑚 = 1, 𝑘 = 50.
74
75 Additional Tasks
→ Solve and Plot the system using one or more of the built-in solvers
(use, e.g., ode32) in MATLAB. Apply a step in 𝐹 (which is the control
signal 𝑢).
[End of Task]
𝑃𝑉 = 𝑛𝑅𝑇
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. 𝑉1 = 1𝑚3 , 𝑉2 = 5𝑚3
Use both the quad and quadl functions. Compare with the exact solution
by solving the integral analytically.
[End of Task]
𝑥̇ 1 = 𝑥2
𝑔 𝑏
𝑥̇ 2 = − 𝑥1 − 𝑥
𝑟 𝑚𝑟 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]
0 1 𝑥 0
𝑥̇ 1 𝑐 ] [ 1] + [ 1 ] 𝑢
[ ]=[ 𝑘 𝑥2
𝑥̇ 2 − −
𝑚 𝑚 𝑚
→ Simulate the system using the lsim function in the Control System
Toolbox.
[End of Task]
First, plot the same plot as shown above using the following data:
t 0 1 2 3 4 5 6 7 8 9 10 11 12
v 0 .45 1.7 4.0 7.1 11.1 16.0 21. 29.0 29.0 29.0 29.0 29.0
9 2 5 8 9 9 5 5 5 5 5
t 13 14 15 16 17 18 19 20 21 22 23 24
v 22.4 17. 17. 17. 17. 14.3 11.0 8.9 6.54 2.03 0.55 0
2 9 9 9 9 4 1
You could put the data into a text file and import it like this:
velocitydata = importdata('velocitydata.txt');
t = velocitydata(:,1);
v = velocitydata(:,2);
Tip! The Total Distance is the area under the curve (the integral) given
above. Use e.g., the trapz() function.
Finally, calculate and plot the Cumulative Distance Traveled. Tip! Use
the cumtrapz() function.
[End of Task]
Interpolation
MATLAB offers functions for interpolation, e.g.:
Curve Fitting
Here are some of the functions available in MATLAB used for curve fitting:
79
80 Appendix A – MATLAB Functions
Numerical Differentiation
MATLAB offers functions for numerical differentiation, e.g.:
Numerical Integration
MATLAB offers functions for numerical integration, such as:
trapezoidal method
quad Numerically evaluate integral, adaptive Simpson >>
quadrature.
Q = QUAD(FUN,A,B) tries to approximate the
integral of calar-valued function FUN from A to B.
FUN is a function handle. The function Y=FUN(X)
should accept a vector argument X and return a
vector result Y, the integrand evaluated at each
element of X. Uses adaptive Simpson quadrature
method
quadl Same as quad, but uses adaptive Lobatto >>
quadrature method
polyint Integrate polynomial analytically. POLYINT(P,K) >> I = polyint(p)
xmax
Optimization
MATLAB offers functions for local minimum, such as:
margin Calculates and/or plots the smallest gain and >num = [1]
>den = [1, 5, 6]
phase margins of a single-input single-output >H = tf(num, den)
(SISO) system model. The gain margin indicates margin(H)
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.
margins Calculates all gain and phase margins of a single- >[gmf, gm, pmf, pm] = margins(H)
E-mail: [email protected]
Blog: http://www.halvorsen.blog