0% found this document useful (0 votes)
55 views

4 - Matlab Mathemathical Modelling

This document discusses mathematical modeling and its applications. It provides examples of: 1) Formulating mathematical models using differential equations to simulate physical systems based on scientific principles. 2) Using numerical methods to generalize model solutions that can be implemented on computers. 3) Understanding different conservation laws underlying engineering models and the distinction between steady-state and dynamic solutions. It then provides a detailed example of modeling a bungee jumper's velocity over time using differential equations and numerical integration via Euler's method.

Uploaded by

Jerry Alejos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

4 - Matlab Mathemathical Modelling

This document discusses mathematical modeling and its applications. It provides examples of: 1) Formulating mathematical models using differential equations to simulate physical systems based on scientific principles. 2) Using numerical methods to generalize model solutions that can be implemented on computers. 3) Understanding different conservation laws underlying engineering models and the distinction between steady-state and dynamic solutions. It then provides a detailed example of modeling a bungee jumper's velocity over time using differential equations and numerical integration via Euler's method.

Uploaded by

Jerry Alejos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Mathematical modelling

•  Mathematical modelling
•  Differential equations
•  Numerical differentiation and integration

Applications

•  Mathematical methods

1
Applications

•  Mathematical methods
–  Learning how mathematical models can be formulated on the basis
of scientific principles to simulate the behavior of a simple physical
system.
•  Numerical methods
–  Understanding how numerical methods afford a means to
generalize solutions in a manner that can be implemented on a
digital computer.
•  Problem solving
–  Understanding the different types of conservation laws that lie
beneath the models used in the various engineering disciplines and
appreciating the difference between steady-state and dynamic
solutions of these models.

Mathematical modelling

The process of solving an engineering or physical problem


Engineering or Physical problems
(Description)

Mathematical Modeling
Approximation & Assumption

Common
Formulation or Governing features
Equations operation

Analytical & Numerical Methods


Computer
Solutions programming

Applications

2
Differential Equations

Mathematical model – Function

Dependent ⎛independent forcing ⎞


= f ⎜ , parameters, ⎟
variable ⎝ variables functions⎠
•  Dependent variable - a characteristic that usually reflects
the behavior or state of the system
€ •  Independent variables - dimensions, such as time and space,
along which the system’s behavior is being determined
•  Parameters - constants reflective of the system’s properties
or composition
•  Forcing functions - external influences acting upon the
system

3
Mathematical model – Function example

Mathematical model – Function example

Ø  You are asked to predict the velocity of a


bungee jumper as a function of time during
the free-fall part of the jump
Ø  Use the information to determine the length
and required strength of the bungee cord for
jumpers of different mass
Ø  The same analysis can be applied to a falling
parachutist or a rain drop

4
Exercise using .m files

1.  Make a MATLAB program to solve the problem with the


bungee jumper using the Euler’s method

2.  Plot the development of the velocity as a function of time


with different time steps and compare with the exact
solution

Mathematical model – Function example

Ø  Newton’s second law

F = ma = Fdown – Fup Fup


= mg - cdv2
(gravity minus air resistance)

Ø  We have now applied the fundamental


physical laws to establish a mathematical Fdown
model for the forces acting

5
Mathematical model – Solving the equation

Ø  Newton’s second law


dv
m = mg − c d v 2
dt Fup
dv c
= g − d v2
dt m
Ø  We have established an ordinary differential
equation (ODE) which has an analytical solution
Fdown
mg ⎛ gc d ⎞
v( t ) = tanh⎜⎜ t ⎟⎟
cd ⎝ m ⎠

Mathematical model – Analytical solution

Ø  In MATLAB, open the editor window and type

g = 9.81; m = 80 ; t = 20; cd = 0.25;


v = sqrt(g*m/cd) * tanh(sqrt(g*cd/m)*t) Fup

Ø  Save the file as bungee_jumper.m


Ø  Type bungee_jumper.m in the command
window
» bungee_jumper Type the name of the Fdown
v = script file
55.9268

6
Exercise using .m files

% Matlab program for solving the bungee


jumper problem
% using Eulers method
%
g=9.81;m=68.1;cd=0.25;
t=0:0.5:20;

% The analytic solution


v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t);

% Plotting of results
plot(t,v)
grid
title('Velocity for the bungee jumper')
legend('v (m/s)')

Mathematical model – Numerical solution

Ø  What if cd = cd (v) ≠ const?


Ø  Solve the ODE numerically!

dv Δv
= lim
dt Δt →0 Δ t

Δv v ( t i + 1 ) − v ( t i )
=
Δt ti+1 − ti
Assume constant slope (i.e,
constant drag force) over Δt

7
Mathematical model – Numerical (approximate) solution

Ø  Finite difference (Euler’s) method


dv Δv v ( t i + 1 ) − v ( t i )
≅ = Fup
dt Δt ti+1 − ti
v( ti+1 ) − v( ti ) c
= g − d v( ti ) 2
ti+1 − ti m

Ø  Numerical solution


Fdown
⎡ c ⎤
v( t i + 1 ) = v( t i ) + ⎢ g − d v( t i ) 2 ⎥( t i + 1 − t i )
⎣ m ⎦

Mathematical model – Example: Hand calculations

Ø  Mass of bungee jumper: m = 68.1 kg


Ø  Drag coefficient: cd = 0.25 kg/m
Fup
Ø  Gravity constant: = 9.81 m/s2
Ø  Use Euler’s method to compute the
first 12 s of free fall

⎡ c ⎤
v ( t i + 1 ) = v( t i ) + ⎢ g − d v( t i ) 2 ⎥( t i + 1 − t i )
⎣ m ⎦ Fdown
t0 = 0; v( t0 ) = 0

8
Mathematical model – Example: Euler’s method

Ø  Constant time increment of Δt = 2 s


0.25
Step 1 t = 2 s;
⎡
v = 0 + ⎢9.81 −
⎣ 68 .1
⎤
(0 ) 2 ⎥ ( 2 − 0 ) = 19.6200 m / s
⎦
0.25
Step 2 ⎡
t = 4 s; v = 19.6200 + ⎢9.81 −
⎣ 68 .1
⎤
( 19.6200 ) 2 ⎥ ( 4 − 2 ) = 36.4317 m / s
⎦
0.25
Step 3 t = 6 s;
⎡
v = 36.4137 + ⎢9.81 −
⎣ 68.1
⎤
( 36.4137 ) 2 ⎥ (6 − 4 ) = 46.2983 m / s
⎦
0.25
Step 4 t = 8 s;
⎡
v = 46.2983 + ⎢9.81 −
⎣ 68.1
⎤
( 46.2983 ) 2 ⎥ ( 8 − 6 ) = 50.1802 m / s
⎦
0.25
Step 5 ⎡
t = 10 s; v = 50.1802 + ⎢9.81 −
⎣ 68.1
⎤
( 50.1802 ) 2 ⎥ ( 10 − 8 ) = 51.3123 m / s
⎦
0.25
Step 6 ⎡
t = 12 s; v = 51.3123 + ⎢9.81 −
⎣ 68.1
⎤
( 51.3123 ) 2 ⎥ ( 12 − 10 ) = 51.6008 m / s
⎦

The solution accuracy depends on time increment

Mathematical model – Example: Bungee jumper

9
Exercise using .m files

1.  Make a MATLAB program to solve the problem with the


bungee jumper using the Euler’s method

2.  Plot the development of the velocity as a function of time


with different time steps and compare with the exact
solution

Exercise using .m files

% Matlab program for solving the for i = 1:n


% bungee jumper problem using dv = g-(cd/m)*v*abs(v);
% Eulers method v = v + dv*dt;
clear all V(i+1)=v;
g=9.81;m=80;cd=0.25; end
t0=0; tend=20; dt=0.5;vi=0; %% Plotting of results
t=t0:dt:tend; plot(t,vel,t,V,‘r.')
%% The analytic solution grid
vel=sqrt(g*m/cd)*… xlabel('time (s)')
tanh(sqrt(g*cd/m)*t); ylabel('velocity (m/s)')
%% The numerical solution title('Velocity for the bungee
n =(tend-t0)/dt; jumper')
ti=t0;v= vi; legend(‘analytical‘,…
V(1)=v; ’numerical’,2)

10
Exercise using .m files

% Matlab program for solving the for i = 1:n


% bungee jumper problem using dv = deriv(v,g,m,cd);
% Eulers method v = v + dv*dt;
clear all V(i+1)=v;
g=9.81;m=80;cd=0.25; end
t0=0; tend=20; dt=0.5;vi=0; %% Plotting of results
t=t0:dt:tend; plot(t,vel,t,V,‘r.')
%% The analytic solution grid
vel=sqrt(g*m/cd)*… xlabel('time (s)')
tanh(sqrt(g*cd/m)*t); ylabel('velocity (m/s)')
%% The numerical solution title('Velocity for the bungee
n =(tend-t0)/dt jumper')
ti=t0;v= vi; legend(‘analytical‘,…
V(1)=v; ‘numerical’,2)

Exercise using .m files

deriv.m function dv=deriv(v,g,m,cd)


dv = g – (cd/m)*v*abs(v);
end

11
Mathematical model – Effect of chord

Ø  Free-falling bungee jumper

dv c
= g − d v |v | Fup
dt m
Ø  At the end of the chord, additional forces appear
Gravitation Drag force

dv c k γ
= g − d v⋅ | v | − ( x − L) − v Fdown
dt m m m

Spring force Damping force

Mathematical model – Effect of chord

Ø  We must determine when the jumper


reaches the end of the chord
dx Fup
=v
dt
Ø  Hence, we have a system of two ODEs
dx
=v
dt Fdown
dv c k γ
= g − d v⋅ | v | − ( x − L ) − v
dt m m m

12
Mathematical model – System of two ODEs

Ø  We have a system of two ODEs

Fup

Ø  This can be written in the following form

Fdown

Mathematical model – System of two ODEs

Ø  In MATLAB syntax, we can write this as

dydt = [y(2);
g – sign(y(2))*cd/m*y(2)^2 – chord]

Ø  If we make a new variable for the the extra force from
the chord
chord = k/m*(y(1)-L) + gamma/m*y(2)

Ø  We can use one of the built-in ODE solvers in


MATLAB to solve the set of equations

13
Mathematical model – System of two ODEs

% Program for solving the bungee function


% jumper problem with dynamics dydt=bungee_dyn(t,y,L,cd,…
% m,k,gamma)
t0=0;tend=50; x0=0;v0=0; g=9.81; chord=0;
% determine if the chord
L=30; cd=0.25; m=80; k=40; gamma=8;
% exerts a force

% Built-in solver if y(1) > L


[t,y]=ode45(@bungee_dyn,[t0 tend],… chord = k/m*(y(1)L)
[x0 v0], [], L,cd,m,k,gamma); +gamma/m*y(2);
end

% Plot of results dydt=[y(2);


plot(t,-y(:,1),'-',t,y(:,2),':') g-sign(y(2))*cd/m*y(2)^2
legend('x (m)','v (m/s)') -chord];
% %

Eksempel fil – til hjelp med prosjektoppgåva

r=[0,20]; %Dette er startverdien for r=[x,z]


lagreX=[r(1)]; %Startverdien for x = r(1) lagres i lagreX
lagreZ=[r(2)];
deltat=0.01; %En ganske fornuftig verdi for deltat
v=[5,2]; %Dette er utgangshastigheten.
a=[0,-5]; %Dette er startverdien for akselerasjonen
ztopp = 0; %Denne skal lagre maksimal z
while (r(2)>0) %Vi kjorer helt til vi treffer bakken
r=r+v*deltat; %Her endrer vi r-verdien som tidligere forklart.
v=v+a*deltat; %Her endrer vi v likedan.
a=[a(1), a(2) - 0.07]; %Her endres kun z-verdien av akselerasjonen.
lagreX=[lagreX, r(1)]; %Den nye x-verdien legges til lagreX-vektoren.
lagreZ=[lagreZ, r(2)];
if (v(2)>0)
ztopp = r(2); %Mens farten i z-retning er positiv, oppdaterer vi ztopp.
end
end
plot(lagraX, lagraZ) %Plotter punktene vi har funnet, og viser grafen.
disp(r(1)) %Skriver ut x-verdien for punktet der objektet lander.

14
Differential equations

•  Question
–  How can we solve a first-order differential equation of the form

with the initial condition if we cannot solve it analytically


•  Example
–  We want to solve the ODE

with x(0) = 0, i.e. we need to find the right function x(t) which fulfils
the ODE and the initial conditions (IC).

Differential equations

•  Given the initial condition x(0) = 0, we want to know x(t) for


t>0. We will now find an approximate numerical solution of
the exact solution by computing values of the function only at
discrete values of t.
•  To do so, we define a discrete set of t-values, called grid
points by

•  The distance between two adjacent grid points is h. The largest


value is Depending on the problem, tN might be
given and h is then determined by how many grid points N we
choose

15
Differential equations

•  The key is now to approximate the derivative of x(t) at a point


tn by

•  We know that this relation is exact in the limit h à 0, since


x(t) is differentiable (according to the definition of the ODE).
For h>0, however, the approximation above only takes into
account the current value of x(t) and the value at the next
(forward) grid point. Hence, the method is called a forward
difference approximation.

Differential equations
•  In the expression on the previous page, we approximate the
slope of the tangent line at tn (“the derivative”) by the slope of
the chord that connects the point (tn,x(tn)) with the point (tn
+1,x(tn+1)). This is illustrated in the figure below

16
Differential equations
•  Substituting the approximation for the derivative into the
ODE, we obtain

•  We can rearrange this equation and use the simpler notation


xn = x(tn), we get

•  This describes an iterative method to compute the values of


the function successively at all grid points tn (with tn>0),
starting at t0=0 and x0=0 in our case.
This is called Euler’s method

Differential equations
•  For example, the value of x at the next grid point, t1=h, after
the starting point is

•  Similarly, we find at t2=2h

•  It is now a matter of what value to choose for h

17
Differential equations
•  In the corresponding Matlab code, we choose h = 0.001 and
N=10000, and so tN=10. Here is a plot of x(t), where the
discrete points have been connected by straight lines.

•  Run the code yourself!


What happens to xN when we decrease h by a factor of 10?
(Remember to increase N simultaneously by a factor of 10 in
order to obtain the same value for tN)

Differential equations
•  Accuracy
We see that the value of xN depends upon the step size h. In
theory a higher accuracy of the numerical solution in
comparison to the exact solution can be achieved by
decreasing h since our approximation of the derivative
more accurate.

However, we cannot decrease h infinitely since, eventually,


we are hitting the limits set by the machine precision. Also,
lowering h requires more time steps, hence, more
computational time.

18
Differential equations

•  For Euler’s method it turns out that the global error (error at a
given t) is proportional to the step size h while the local error
(error per step) is proportional to h2. This is called a first-
order method.

Differential equations
•  We can now summarize Euler’s method
Given the ODE

we can approximate the solution numerically in the


following way:

1.  Choose a step size h


2.  Define grid points: tn = t0+n*h, with n=0,1,2,3,…,N
3.  Compute iteratively the values of the function at these
grid points: xn+1=xn+h*g(xn,tn). Start with n=0.

19
Differential equations
•  Instability
Apart from its fairly poor accuracy, the main problem with
Euler’s method is that it can be unstable, i.e. the numerical
solution can start to deviate from the exact solution in
dramatic ways. Usually, this happens when the numerical
solution grows large in magnitude while the exact solution
remains small
•  A popular example to demonstrate this feature is the ODE

•  The exact solution is simply x(t) = e-t. It fulfils the ODE and
the initial condition.

Differential equations
•  On the other hand, our Euler methods reads

Clearly, if h>1, x(tn) will oscillate between negative and


positive numbers and grow without bounds in magnitude as tn
increases. We know that this is incorrect, since we know the
exact solution in this case.
•  On the other hand, when 0<h<1, the numerical solution
approaches zero as tn increases, reflecting the behaviour of the
exact solution.
•  Therefore, we need to make sure that the step size of the Euler
method is sufficiently small so as to avoid such instabilities.

20

You might also like