Modelling and Simulation with MATLAB: a case study in … · Modelling and Simulation with MATLAB:...
Transcript of Modelling and Simulation with MATLAB: a case study in … · Modelling and Simulation with MATLAB:...
School of Electrical, Electronic and
Communications Engineering,
University College Dublin, Ireland
Dr Paul Curran
MATLAB Virtual Conference, March 26, 2014 1
a case study in problem-based learning
Modelling and Simulation with
MATLAB:
Layout of presentation
• Basics and background
• Initial module design decisions
• Introductory material: lectures and
laboratories
• Student work from the class of 2013
• Conclusion
MATLAB Virtual Conference, March 26, 2014 2
The basics
• 167 students registered to module in 2013
• Mostly year three (junior year)
• 5 credits
• One semester
• 2 hours in lecture theatre and 4 hours in
laboratory per week
• The module is a first course on numerical
methods
MATLAB Virtual Conference, March 26, 2014 3
... there is evidence of an ongoing decline in the
candidates’ willingness to struggle for success.
MATLAB Virtual Conference, March 26, 2014 4
Leaving Certificate Examination 2005. Mathematics. Higher Level.
Chief Examiner’s Report.
Background: Perseverance
MATLAB Virtual Conference, March 26, 2014 5
“Have patience. All things are difficult before they
become easy”. Sa’dī (circa 1210 – 1292)
quotes taken from BrainyQuote.
Perseverance
MATLAB Virtual Conference, March 26, 2014 6
“Have patience. All things are difficult before they
become easy”. Sa’dī (circa 1210 – 1292)
The perceived wisdom is that a failure to
persevere can be equated to a failure to learn.
Perseverance
Thinking inside the box
MATLAB Virtual Conference, March 26, 2014 7
... a noticeable decline in the capacity of candidates
to engage with problems that are not of a routine and
well-rehearsed type.
Leaving Certificate Examination 2005. Mathematics. Higher Level. Chief
Examiner’s Report.
Thinking inside the box
MATLAB Virtual Conference, March 26, 2014 8
... a noticeable decline in the capacity of candidates
to engage with problems that are not of a routine and
well-rehearsed type.
Leaving Certificate Examination 2005. Mathematics. Higher Level. Chief
Examiner’s Report.
There is a strong tendency among the
student body to compartmentalise their
learning.
“Experience is the teacher of all things”. Gaius Julius Caesar (100 – 44 BC)
learn to do by doing
MATLAB Virtual Conference, March 26, 2014 9
Practice:
“Experience is the teacher of all things”. Gaius Julius Caesar (100 – 44 BC)
learn to do by doing
MATLAB Virtual Conference, March 26, 2014 10
The students will best learn how to apply numerical
methods to the solution of engineering problems by
actually applying numerical methods to the solution
of engineering problems.
Practice:
learn to do by doing
MATLAB Virtual Conference, March 26, 2014 11
The student body is varied. The engineering
disciplines of Biosystems, Civil, Electrical,
Electronic, Mechanical, Materials and
Structural are all represented.
Practice:
learn to do by doing
MATLAB Virtual Conference, March 26, 2014 12
The students will best learn how to apply numerical
methods to the solution of engineering problems by
actually applying numerical methods to the solution
of engineering problems of some interest to them.
Practice:
MATLAB Virtual Conference, March 26, 2014 13
“A man who carries a cat by the tail learns
something he can learn in no other way”.
Samuel Clemens “Mark Twain” (1835 – 1910)
The fundamental dichotomy
MATLAB Virtual Conference, March 26, 2014 14
“A man who carries a cat by the tail learns
something he can learn in no other way”.
Samuel Clemens “Mark Twain” (1835 – 1910)
We learn from doing it wrong, not from doing
it right.
The fundamental dichotomy
MATLAB Virtual Conference, March 26, 2014 15
“Learning too soon our limitations, we never learn
our powers”. Mignon McLaughlin(1913 – 1983)
The fundamental dichotomy
MATLAB Virtual Conference, March 26, 2014 16
We gain confidence from doing it right, not
from doing it wrong.
“Learning too soon our limitations, we never learn
our powers”. Mignon McLaughlin(1913 – 1983)
The fundamental dichotomy
MATLAB Virtual Conference, March 26, 2014 17
We learn from doing it wrong, not from doing it
right.
We gain confidence from doing it right, not from
doing it wrong.
The fundamental dichotomy
MATLAB Virtual Conference, March 26, 2014 18
We learn from doing it wrong, not from doing it
right.
We gain confidence from doing it right, not from
doing it wrong.
Practice but receive rewards
Why Matlab ?
MATLAB Virtual Conference, March 26, 2014 19
Computation
Visualization
Programming
Why Matlab ?
MATLAB Virtual Conference, March 26, 2014 20
Computation
Visualization
Programming
Computation: simplicity of initial use instils confidence.
Programming: significantly extends problem solving
power and efficiency.
Visualisation: renders results very accessible.
Why Matlab ?
MATLAB Virtual Conference, March 26, 2014 21
MATLAB permits an expert to do
amazing things.
Why Matlab ?
MATLAB Virtual Conference, March 26, 2014 22
MATLAB also permits a non-expert to do
amazing things.
MATLAB permits an expert to do
amazing things.
An Engineering Motto
MATLAB Virtual Conference, March 26, 2014 23
“In theory, there is no difference between theory and
practice. But in practice, there is”.
Lawrence “Yogi” Berra (1925 – )
An Engineering Motto
MATLAB Virtual Conference, March 26, 2014 24
Problems should be as real as possible.
“In theory, there is no difference between theory and
practice. But in practice, there is”.
Lawrence “Yogi” Berra (1925 – )
An Engineering Motto
MATLAB Virtual Conference, March 26, 2014 25
Embrace the term “modelling” in the title of the
module. The students would have to
mathematically model as their first step in solving
many of the problems.
Modelling
MATLAB Virtual Conference, March 26, 2014 26
The students found this to be the most
challenging part of the module. It
challenges compartmentalisation and
demands assimilation of previous learning.
It is neither rote nor routine.
Lecture notes will adopt a large
number of standardised styles for
the presentation of different types
of material.
Lectures:
MATLAB Virtual Conference, March 26, 2014 27
>> a = 1; % initialise a
>> b = 2; % initialise b
>> fa = (a^2) – 2 % determine value of f(a)
>> fb = (b^2) – 2 % determine value of f(b)
If f(a) and f(b) have opposite sign:
>> c = a – (fa*(b – a))/(fb – fa) % update c using old a and b
>> fc = (c^2) – 2 % determine value of f(c)
If f(c) = 0 stop. If f(c) and f(a) have same sign:
>> a = c; % update a
else:
>> b = c; % update b
22 xxfRegula Falsi
Style for command
window MATLAB code
Lecture notes will adopt a large
number of standardised styles for
the presentation of different types
of material.
Lectures:
MATLAB Virtual Conference, March 26, 2014 28
>> a = 1; % initialise a
>> b = 2; % initialise b
>> fa = (a^2) – 2 % determine value of f(a)
>> fb = (b^2) – 2 % determine value of f(b)
If f(a) and f(b) have opposite sign:
>> c = a – (fa*(b – a))/(fb – fa) % update c using old a and b
>> fc = (c^2) – 2 % determine value of f(c)
If f(c) = 0 stop. If f(c) and f(a) have same sign:
>> a = c; % update a
else:
>> b = c; % update b
a0 = 1.0000 , b0 = 2
a1 = 1.3333 , b1 = 2
a2 = 1.4000 , b2 = 2
a3 = 1.4118 , b3 = 2
a4 = 1.4138 , b4 = 2
a5 = 1.4141 , b5 = 2
a6 = 1.4142 , b6 = 2
22 xxfRegula Falsi
Style for command
window MATLAB code
Style for presentation
of numerical results
Introductory Laboratories: basics
MATLAB Virtual Conference, March 26, 2014 29
Test Problem:
Using both the matrix inverse command, inv, and the
left matrix divide command, \ , solve this system of
equations.
3423
02
12
zyx
zyx
yx
Introductory Laboratories: basics
MATLAB Virtual Conference, March 26, 2014 30
Test Problem:
“Wow! That was
easy”.
Using both the matrix inverse command, inv, and the
left matrix divide command, \ , solve this system of
equations.
3423
02
12
zyx
zyx
yx
Introductory Laboratories:
MATLAB files
MATLAB Virtual Conference, March 26, 2014 31
Test Problem: Write a MATLAB file to
calculate and output N+1 successive states of the
logistic map having parameter μ and initial condition
x0 . ...
nnn xxx 11
Introductory Laboratories:
MATLAB files
MATLAB Virtual Conference, March 26, 2014 32
A typical output
might be as shown.
0 20 40 60 80 100 1200.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
n
x
Logistic map for = 3.5 , x0 = 0.1
Introductory Laboratories:
MATLAB files
MATLAB Virtual Conference, March 26, 2014 33
A typical output
might be as shown.
0 20 40 60 80 100 1200.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
n
x
Logistic map for = 3.5 , x0 = 0.1
Introductory Laboratories:
MATLAB files
MATLAB Virtual Conference, March 26, 2014 34
A typical output
might be as shown.
Explore!
0 20 40 60 80 100 1200.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
n
x
Logistic map for = 3.5 , x0 = 0.1
Introductory Laboratories: MATLAB files
MATLAB Virtual Conference, March 26, 2014 35
function y = my_logistic_map(mu,x0,N)
% y = my_logistic_map(mu,x0,N): iterate map x(n+1) = mu x(n)(1-x(n)) for parameter mu lying between 0
% and 4 and for initial condition x(0) = x0 lying between 0 and 1. Under these conditions the logistic
% map maps the interval [0,1] into itself. Iterate N times and produce output as vector y of N+1
% elements being the initial value x0 and N subsequent iterations in order of iteration.
% Version 1: 2014 Paul Curran
% Revision: not applicable
if nargin ~= 3 error('Not enough input arguments: my_logistic_map requires 3 inut arguments mu, x0 and
N.'); end
if nargout ~= 1 error('Not enough output arguments: my_logistic_map requires 1 output argument y being
a vector of N+1 element'); end
if mu < 0 || mu > 4 error('Invalid range: input argument mu must lie between 0 and 4'); end
if x0 < 0 || x0 > 1 error('Invalid range: input argument x0 must lie between 0 and 1'); end
if N ~= fix(N) || N <= 0 error('Invalid range or type: input argument N must be a positive integer');
end
y = zeros(N+1,1); % Set aside memory to store required N+1 values of the first N+1 successive states
y(1) = x0; % Store initial state as first element of y
x = x0; % initialise state variable x
for count = 1:N
x = mu*x*(1-x); % employ logistic map to determine next state, i.e. iterate
y(count+1) = x; % Store current state in appropriate location in vector y
end
end
Introductory Laboratories: MATLAB files
MATLAB Virtual Conference, March 26, 2014 36
function y = my_logistic_map(mu,x0,N)
% y = my_logistic_map(mu,x0,N): iterate map x(n+1) = mu x(n)(1-x(n)) for parameter mu lying between 0
% and 4 and for initial condition x(0) = x0 lying between 0 and 1. Under these conditions the logistic
% map maps the interval [0,1] into itself. Iterate N times and produce output as vector y of N+1
% elements being the initial value x0 and N subsequent iterations in order of iteration.
% Version 1: 2014 Paul Curran
% Revision: not applicable
if nargin ~= 3 error('Not enough input arguments: my_logistic_map requires 3 inut arguments mu, x0 and
N.'); end
if nargout ~= 1 error('Not enough output arguments: my_logistic_map requires 1 output argument y being
a vector of N+1 element'); end
if mu < 0 || mu > 4 error('Invalid range: input argument mu must lie between 0 and 4'); end
if x0 < 0 || x0 > 1 error('Invalid range: input argument x0 must lie between 0 and 1'); end
if N ~= fix(N) || N <= 0 error('Invalid range or type: input argument N must be a positive integer');
end
y = zeros(N+1,1); % Set aside memory to store required N+1 values of the first N+1 successive states
y(1) = x0; % Store initial state as first element of y
x = x0; % initialise state variable x
for count = 1:N
x = mu*x*(1-x); % employ logistic map to determine next state, i.e. iterate
y(count+1) = x; % Store current state in appropriate location in vector y
end
end
Descriptive function name
Introductory Laboratories: MATLAB files
MATLAB Virtual Conference, March 26, 2014 37
function y = my_logistic_map(mu,x0,N)
% y = my_logistic_map(mu,x0,N): iterate map x(n+1) = mu x(n)(1-x(n)) for parameter mu lying between 0
% and 4 and for initial condition x(0) = x0 lying between 0 and 1. Under these conditions the logistic
% map maps the interval [0,1] into itself. Iterate N times and produce output as vector y of N+1
% elements being the initial value x0 and N subsequent iterations in order of iteration.
% Version 1: 2014 Paul Curran
% Revision: not applicable
if nargin ~= 3 error('Not enough input arguments: my_logistic_map requires 3 inut arguments mu, x0 and
N.'); end
if nargout ~= 1 error('Not enough output arguments: my_logistic_map requires 1 output argument y being
a vector of N+1 element'); end
if mu < 0 || mu > 4 error('Invalid range: input argument mu must lie between 0 and 4'); end
if x0 < 0 || x0 > 1 error('Invalid range: input argument x0 must lie between 0 and 1'); end
if N ~= fix(N) || N <= 0 error('Invalid range or type: input argument N must be a positive integer');
end
y = zeros(N+1,1); % Set aside memory to store required N+1 values of the first N+1 successive states
y(1) = x0; % Store initial state as first element of y
x = x0; % initialise state variable x
for count = 1:N
x = mu*x*(1-x); % employ logistic map to determine next state, i.e. iterate
y(count+1) = x; % Store current state in appropriate location in vector y
end
end
Extensive comments
Version information
Help file
Descriptive function name
Introductory Laboratories: MATLAB files
MATLAB Virtual Conference, March 26, 2014 38
function y = my_logistic_map(mu,x0,N)
% y = my_logistic_map(mu,x0,N): iterate map x(n+1) = mu x(n)(1-x(n)) for parameter mu lying between 0
% and 4 and for initial condition x(0) = x0 lying between 0 and 1. Under these conditions the logistic
% map maps the interval [0,1] into itself. Iterate N times and produce output as vector y of N+1
% elements being the initial value x0 and N subsequent iterations in order of iteration.
% Version 1: 2014 Paul Curran
% Revision: not applicable
if nargin ~= 3 error('Not enough input arguments: my_logistic_map requires 3 inut arguments mu, x0 and
N.'); end
if nargout ~= 1 error('Not enough output arguments: my_logistic_map requires 1 output argument y being
a vector of N+1 element'); end
if mu < 0 || mu > 4 error('Invalid range: input argument mu must lie between 0 and 4'); end
if x0 < 0 || x0 > 1 error('Invalid range: input argument x0 must lie between 0 and 1'); end
if N ~= fix(N) || N <= 0 error('Invalid range or type: input argument N must be a positive integer');
end
y = zeros(N+1,1); % Set aside memory to store required N+1 values of the first N+1 successive states
y(1) = x0; % Store initial state as first element of y
x = x0; % initialise state variable x
for count = 1:N
x = mu*x*(1-x); % employ logistic map to determine next state, i.e. iterate
y(count+1) = x; % Store current state in appropriate location in vector y
end
end
Extensive comments
No bad code, such as assignment
of memory inside loop
Error checking with fully
descriptive error messages
Version information
Help file
Descriptive function name
Test Problem: Using ode45 determine an
approximate solution to the ordinary differential
equation ...
Introductory Laboratories: ode45
MATLAB Virtual Conference, March 26, 2014 39
012
2
2
ytd
ydy
td
yd
Introductory Laboratories: ode45
MATLAB Virtual Conference, March 26, 2014 40
ode45 will permit the students to test the
performance of their own first principles
ordinary differential equation solvers. Indeed
numerous MATLAB commands will permit
testing in general and this is another reason
for adopting the package.
Introductory Laboratories: ode45
MATLAB Virtual Conference, March 26, 2014 41
A typical output
might be as shown.
Students can
investigate dynamic
plots.
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5-2.5
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
2.5
y
dy/d
t
Van der Pol equation for = 0.1
Explore!
Introductory Laboratories: ode45
MATLAB Virtual Conference, March 26, 2014 42
For example in
exploring the
Lorenz equation
and sensitive
dependence on
initial conditions
one student
received bonus
grades for this:
Introductory Laboratories:
MATLAB Virtual Conference, March 26, 2014 43
1
11
6
8 910
121314
15
16
7
1718 19
20
-5
0
5
10
15
20
-15
-10
-5
0
5
10
-5
0
5
10
15
20
Code given to draw
shaft. Problem is to
draw crossbar.
patch
Introductory Laboratories:
MATLAB Virtual Conference, March 26, 2014 44
1
11
6
8 910
121314
15
16
7
1718 19
20
-5
0
5
10
15
20
-15
-10
-5
0
5
10
-5
0
5
10
15
20
Code given to draw
shaft. Problem is to
draw crossbar.
Reuse, don’t
rewrite!
patch
One minor project was concerned with a
10 bar linkage from 1991 Ford Mustang
convertible roof mechanism. The
mechanism was animated on the website
of J. Michael McCarthy (University of
California at Irvine) although the link
has since ceased to work.
Minor Project: Static Equations
MATLAB Virtual Conference, March 26, 2014 45
gggggggggggggggggggg
0
1
2
3
4
5
6
7
8
9 1011
40o
20o
20o
40o
12
20o
45o
Minor Project: Static Equations
MATLAB Virtual Conference, March 26, 2014 46
The crank drives the mechanism.
The problem is treated as pseudo
static, with all angles determined
for a given crank angle.
gggggggggggggggggggg
0
1
2
3
4
5
6
7
8
9 1011
40o
20o
20o
40o
12
20o
45o
Minor Project: Static Equations
MATLAB Virtual Conference, March 26, 2014 47
The first rule of perseverance:
ignore what you cannot do and find
something that you can do. There
are no big problems, only large
collections of small problems.
0
1
2
3
4
5
6
7
8
9 1011
40o
20o
20o
40o
12
20o
45o
The student must derive a number
of closure equations.
Minor Project: Static Equations
MATLAB Virtual Conference, March 26, 2014 48
0
1
2
3
4
5
6
7
8
9 1011
40o
20o
20o
40o
12
20o
45o
For example we may obtain two
expressions for the vector from
node 1 to node 4.
Employing Cartesian coordinates we
obtain the closure equation:
Minor Project: Static Equations
MATLAB Virtual Conference, March 26, 2014 49
0
1
2
3
4
5
6
7
8
9 1011
40o
20o
20o
40o
12
20o
45o
sin5.21
cos5.2
sin2sin2
cos2cos2
4,33,1
4,33,1
This equation can be solved by
employing the Newton-Raphson
algorithm.
Reuse, don’t rewrite!
As a solution I had in mind something like this:
Minor Project: Static Equations
MATLAB Virtual Conference, March 26, 2014 50
Minor Project: Static Equations
MATLAB Virtual Conference, March 26, 2014 51
In general the
solution which the
students actually
obtained looked
more like this,
without the
animation having
been completed.
Problem 2.5: Space-shuttle
... model mission STS-30, the 29th Space Shuttle
mission, ... produce an animation of the flight of the
vehicle from launch to the point where the external
tank is jettisoned, ...
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference, March 26, 2014 52
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference, March 26, 2014 53
Sensible models for numerous effects
such as reduction with altitude of air
density and force of gravity, changing
drag, decrease in mass, changing throttle
setting, staging etc. had to be chosen.
Team Minor Project:
Dynamic Equations
54
MATLAB Virtual
Conference, March 26, 2014
Donal Bourke,
Sean Cleary and
Deborah Hayden.
Snapshots from
report of Sean
Cleary.
Team Minor Project:
Dynamic Equations
55
Nose cones
modelled using
ogives. Indicative
of quality of
reporting required
and received.
MATLAB Virtual
Conference, March 26, 2014
Team Minor Project: Dynamic
Equations
56
MATLAB Virtual
Conference, March 26, 2014
Team Minor Project: Dynamic
Equations
57 MATLAB Virtual Conference, March 26, 2014
https://www.dropbox.com/sh/pe8y5axenp2u5he/fvWCFR4jxT
Problem 2.4: 2-body problem
Set up the equations of motion for the planet earth
taking into account the gravitational effects of the sun
and of the planet Jupiter. ... Visualise by creating an
animation, ...
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference, March 26, 2014 58
Anton Katayev, Peadar Keegan and Carl Kelly.
Carl solves a number of similar problems on his website:
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference, March 26, 2014 59
http://carl-kelly.site44.com/projects/cele/cele.html#top
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference, March 26, 2014 60
Problem 2.2: Vehicle Suspension
... describe the performance of the half vehicle ...
Choose your own model for the road surface but be
sure to include a seam, i.e. a sharp step, some
undulation and a stochastic component.
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference, March 26, 2014 61
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference,
March 26, 2014
Report of Ciaran Devenney:
Start with
mathematical model
of vehicle.
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference, March 26, 2014 63
Report of Ciaran Devenney:
Start with
mathematical model
of vehicle.
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference, March 26, 2014 64
Next model
road.
Ciaran Devenney, Dominic Dolan and Ronan Keane.
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference, March 26, 2014 65
Team Minor Project:
Dynamic Equations
MATLAB Virtual Conference, March 26, 2014 66
This project shows what the non-expert can
achieve with MATLAB, producing an
animation which is physically meaningful and
totally accessible. On seeing it my two year old
son announced “I love that”.
Conclusion
MATLAB Virtual Conference, March 26, 2014 67
Thanks to Dr Barry Brophy, UCD
for video production.