Part 2 Modelling and Simulation in MATLAB - Overview
Part 2 Modelling and Simulation in MATLAB - Overview
Lessons
1. Solving Differential Equations
2. Interpolation/Curve Fitting
3.
4.
5.
6.
7.
Numerical Differentiation/Integration
Optimization
Transfer Functions/State-space Models
Discrete Systems
Frequency Response
Do as many Exercises
as possible!
Learning by Doing!
What is MATLAB?
Script Editor
Workspace
Current
Folder
Plot Window
Command Window
Installation
http://www.mathworks.com/academia/student_version
2 different options:
Lesson 1
Solving ODEs in MATLAB
- Ordinary Differential Equations
Example:
Differential Equations
Note!
Where
T is the Time constant
The Solution can be proved to be (will not be
shown here):
T = 5;
a = -1/T;
x0 = 1;
t = [0:1:25];
x = exp(a*t)*x0;
plot(t,x);
grid
Students: Try this example
Differential Equations
Problem with this method:
We need to solve the ODE before we can plot it!!
T = 5;
a = -1/T;
x0 = 1;
t = [0:1:25];
x = exp(a*t)*x0;
plot(t,x);
grid
function dx = mydiff(t,x)
T = 5;
a = -1/T;
dx = a*x;
clear
clc
x0
tspan = [0 25];
x0 = 1;
tspan
[t,x] = ode23(@mydiff,tspan,x0);
plot(t,x)
Mass-Spring-Damper System
This gives:
Finally:
=
=
=
=
1;
5;
1;
1;
dx = zeros(2,1); %Initialization
dx(1) = x(2);
dx(2) = -(k/m)*x(1)-(c/m)*x(2)+(1/m)*F;
clear
clc
tspan = [0 50];
x0 = [0;0];
[t,x] = ode23(@mass_spring_damper_diff,tspan,x0);
plot(t,x)
...
[t,x] = ode23(@mass_spring_damper_diff,tspan,x0);
plot(t,x)
...
[t,x] = ode23(@mass_spring_damper_diff,tspan,x0);
plot(t,x(:,2))
=
=
=
=
param(1);
param(2);
param(3);
param(4);
dx = zeros(2,1);
dx(1) = x(2);
dx(2) = -(k/m)*x(1) - (c/m)*x(2) + (1/m)*F;
Whats next?
Learning by Doing!
Lesson 2
Interpolation
Curve Fitting
Example
Interpolation
Given the following Data Points:
x
15
10
(Logged
Data from
a given
Process)
x=0:5;
y=[15, 10, 9, 6, 2, 0];
plot(x,y ,'o')
grid
Interpolation
We can use one of the built-in Interpolation functions in MATLAB:
x=0:5;
y=[15, 10, 9, 6, 2, 0];
plot(x,y ,'-o')
grid on
new_x=3.5;
new_y = interp1(x,y,new_x)
new_y =
4
Curve Fitting
In the previous section we found interpolated points,
i.e., we found values between the measured points
using the interpolation technique.
It would be more convenient to model the data as a
mathematical function y=f(x).
Then we can easily calculate any data we want based
on this model.
Data
Mathematical Model
Example:
Curve Fitting
Linear Regression
15
10
Based on the
Data Points we
create a Plot in
MATLAB
x=0:5;
y=[15, 10, 9, 6, 2, 0];
plot(x,y ,'o')
grid
Students: Try this example
Example
Curve Fitting
Linear Regression
clear
clc
x=[0, 1, 2, 3, 4 ,5];
y=[15, 10, 9, 6, 2 ,0];
n=1; % 1.order polynomial
p = polyfit(x,y,n)
p =
-2.9143
14.2857
Example
Curve Fitting
Linear Regression
We will plot and validate
the results in MATLAB
clear
clc
close all
x
0
15
10
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)
15
10
Curve Fitting
Linear Regression
3 ways to do this:
Use the interp1 function (shown
earlier)
... (see previus examples)
Implement y=-2.9+14.3
new_x=3.5;
and calculate y(3.5)
Use the polyval function new_y = interp1(x,y,new_x)
new_y = a*new_x + b
Students: Try this example
Curve Fitting
Polynomial Regression
1.order:
p = polyfit(x,y,1)
2.order:
p = polyfit(x,y,2)
3.order:
p = polyfit(x,y,3)
etc.
15
10
Curve Fitting
clear
clc
close all
x=[0, 1, 2, 3, 4 ,5];
y=[15, 10, 9, 6, 2 ,0];
for n=1:6 % n = model order
p = polyfit(x,y,n)
As expected, the higher order
ymodel = polyval(p,x);
models match the data better
and better.
subplot(3,2,n)
Note! The fifth order model
plot(x,y,'o',x,ymodel)
matches exactly because there
title(sprintf('Model order %d', n));
were only six data points
available.
end
n > 5 makes no sense because
we have only 6 data points
Whats next?
Learning by Doing!
Lesson 3
Numerical Differentiation
Numerical Integration
Numerical Differentiation
Note! We will use MATLAB in order to find the numeric solution not the analytic solution
Numerical Differentiation
Example:
We know for this simple
example that the exact
analytical solution is:
Given the following values:
x
-2
-1
0
1
2
y
4
1
0
1
4
Numerical Differentiation
Example:
MATLAB Code:
x=-2:2;
y=x.^2;
numeric
% Exact Solution
dydx_exact = 2*x;
exact
% Numerical Solution
dydx_num = diff(y)./diff(x);
Numerical
Solution
dydx =
-3
-1
1
3
NaN
Exact
Solution
-4
-2
0
2
4
Numerical Differentiation
x=-2:0.1:2
x=-2:2
Numerical Integration
An integral can be seen as the area under a curve.
Given y=f(x) 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 (trapezoid rule ):
Numerical Integration
Example:
A = 0.3350
Students: Try this example
Numerical Integration
Example:
In MATLAB we have several built-in functions we can use for numerical integration:
clear
clc
close all
x=0:0.1:1;
y=x.^2;
plot(x,y)
% Calculate the Integral (Trapezoid method):
avg_y = y(1:length(x)-1) + diff(y)/2;
A = sum(diff(x).*avg_y)
% Calculate the Integral (Simpson method):
A = quad('x.^2', 0,1)
% Calculate the Integral (Lobatto method):
A = quadl('x.^2', 0,1)
Whats next?
Learning by Doing!
Lesson 4
Optimization
Optimization
Optimization is important in modelling, control and simulation applications.
Optimization is based on finding the minimum of a given criteria function.
Example:
We want to find for what value of x the function has its minimum value
clear
clc
x = -20:0.1:20;
y = 2.*x.^2 + 20.*x - 22;
plot(x,y)
grid
i=1;
while ( y(i) > y(i+1) )
i = i + 1;
end
x(i)
y(i)
(-5,72)
The minimum of the function
Optimization
Example:
function f = mysimplefunc(x)
f = 2*x.^2 + 20.*x -22;
x_min =
-5
y =
-72
We got the same results as
previous slide
clear
clc
close all
x = -20:1:20;
f = mysimplefunc(x);
plot(x, f)
grid
x_min = fminbnd(@mysimplefunc, -20, 20)
y = mysimplefunc(x_min)
Whats next?
Learning by Doing!
Lesson 5
Transfer Functions
State-space models
Transfer functions
Differential
Equations
Laplace
H(s)
Input
Example:
Output
Transfer
Functions
Numerator
Denumerator
Transfer functions
1.order Transfer function:
Step Response:
Example:
Transfer functions
MATLAB:
clear
clc
close all
% Transfer Function
num = [4];
den = [2, 1];
H = tf(num, den)
% Step Response
step(H)
Transfer functions
Example:
MATLAB:
clear
clc
close all
% Transfer Function
num = [2];
den = [1, 4, 3];
H = tf(num, den)
% Step Response
step(H)
State-space models
A set with linear differential equations:
State-space models
Example:
MATLAB:
clear
clc
close all
Note! The
system is
unstable
% State-space model
A = [1, 2; 3, 4];
B = [0; 1];
C = [1, 0];
D = [0];
ssmodel = ss(A, B, C, D)
% Step Response
step(ssmodel)
% Transfer function
H = tf(ssmodel)
Students: Try this example
State-space models
Mass-Spring-Damper System
Example:
Mass-Spring-Damper System
State-space models
This gives:
We set:
Finally:
k = 5;
c = 1;
m = 1;
A =
B =
C =
D =
sys
[0 1; -k/m -c/m];
[0; 1/m];
[0 1];
[0];
= ss(A, B, C, D)
step(sys)
This gives:
Whats next?
Learning by Doing!
Lesson 6
Discrete Systems
Discrete Systems
When dealing with computer simulation, we need
to create a dicrete version of our system.
This means we need to make a discrete version of
our continous differential equations.
Actually, the built-in ODE solvers in MATLAB use
different discretization methods
The Interpolation, Curve Fitting, etc. is alos based on
a set of discrete values (data points or
measurements)
The same with Numerical Differentiation and
Numerical Integration
Discrete values
etc.
x
15
10
Discrete Systems
Discrete Approximation of the time derivative
Euler backward method:
Discrete Systems
Discretization Methods
Euler backward method:
More Accurate!
Other methods are Zero Order Hold (ZOH), Tustins method, etc.
Discrete Systems
Different Discrete Symbols and meanings
Previous Value:
Present Value:
Example:
Discrete Systems
Solution:
Discrete Systems
Solution:
Discrete Systems
MATLAB Code:
% Simulation of discrete model
clear, clc, close all
% Model Parameters
a = 0.25;b = 2;
% Simulation Parameters
Ts = 0.1; %s
Tstop = 20; %s
uk = 1; % Step in u
x(1) = 0; % Initial value
% Simulation
for k=1:(Tstop/Ts)
x(k+1) = (1-a*Ts).*x(k) + Ts*b*uk;
end
% Plot the Simulation Results
k=0:Ts:Tstop;
plot(k, x)
grid on
Solution:
Discrete Systems
MATLAB Code:
% Find Discrete model
clear, clc, close all
% Model Parameters
a = 0.25;
b = 2;
Ts = 0.1; %s
%
A
B
C
D
State-space model
= [-a];
= [b];
= [1];
= [0];
model = ss(A,B,C,D)
model_discrete = c2d(model, Ts, 'forward')
step(model_discrete)
grid on
Whats next?
Learning by Doing!
Lesson 7
Frequency Response
Frequency Response
Example:
Air Heater
Imput Signal
Output Signal
Dynamic
System
Amplitude
Frequency
Gain
Phase Lag
Example:
Frequency Response
clear
clc
close all
% Define Transfer function
num=[1];
den=[1, 1];
H = tf(num, den)
% Frequency Response
bode(H);
grid on
Whats next?
Learning by Doing!