H-1111, Budapest, Műegyetem rkp. 3. D building. 3 rd floor
Tel: 00 36 1 463 16 80 Fax: 00 36 1 463 30 91 www.hds.bme.hu
Numerical solution of ODEs
Péter Nagy, Csaba Hős
2015.
Table of contents
Numerical solution of ODEs
Homework
Introduction to Matlab programming
Solution of an initial value problem (IVP)
Solution of a boundary value problem (BVP)
Homework
Numerical solution of ODEs
Homework:
The homework and these slides will be available on:
http://www.hds.bme.hu/mota/eng/denum/index.html
Submission:
Only the source code and the plots have to be archived and submitted to [email protected] as NC_Name.zip.
The subject has to be „DENUM NC Name”.
The deadline is the end of 12th week.
Programming language:
Matlab preferred, otherwise personal submission with own laptop is necessary
Free alternatives: Octave, Freemat, Scilab
Introduction to Matlab programming
Numerical solution of ODEs
MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment
File explorer
Menu Command
window
Workspace Text editor
Current folder ! Errors,
warnings
Useful commands 1.
Numerical solution of ODEs
Type stg. + Tab key -> autocomplete
F1 key over a function -> Help+description+syntax
; at the end of a command -> the output is not written to the command window
clc -> Clear command window
clear all -> Delete variables
close all -> close figures
Avoid overwriting predefined functions and variables (F1 or Tab)!!!
Predefined variables: pi, i-imaginary unit, exp(1) – Euler number
function output = name(var1, var2)
global var1 var2 -> define global variables, subfunctions can read them
[x1, x2, x3] -> generates a row vector
[x1; x2; x3] -> generates a column vector
A’ -> the transpose of A (A can be a matrix or a vector)
Useful commands 2
Numerical solution of ODEs
{.+ .- .* ./} -> calculate {+ - * /} element by element
Defining vectors • linspace(a, b, n) -> generates linearly spaced vectors between a and b, n is the
number of elements
• a:b generates linearly spaced vectors between a and b, increment is 1
• a:deltax:b generates linearly spaced vectors between a and b, increment is deltax
v(i) -> the ith element of the vector
A(i,j) -> the element of a matrix in the row i and column j
A(i,:) -> the ith row of a matrix ; A(:,j) -> the jth column of a matrix
Loops
for i=1:num_steps
%code %comment
end
while (statement)
%code %comment
end
Useful commands 3
Numerical solution of ODEs
Define functions f(x,y) = Expression can be defined in Matlab
f=@(x,y) Expression; -> for simple expression (eg. ODES)
or
function output = f (x,y)
output=Expression; -> for longer, more complex calculation (eg. solver)
end
Important functions [t, y]=ode45(@(t,y) f(y,t) , t0, x0) -> solve ODE (f(y,t)); initial time: t0 ;initial condition x0
fminsearch=(@(y) f(y, var1, var2) , y0) -> minimize f(y), the initial guess is y0
Measure computation time (t)
tic
%code
toc (or t=toc)
Useful commands 4
Numerical solution of ODEs
Visualization figure(i) -> generates an empty figure, (i is an integer)
subplot(n, m, i) -> generates sub figures, n: number of rows, m: num. of columns,
i: is the active subplot
plot(x,y) -> plot points; x and y are vectors, further possibilities-> F1
in the case of multiple plots plot(x1,y1, x2,y2, x3,y3), where xi,yi are vectors
title('Title') -> generates title
legend('Title') -> generates title
xlabel('label of x axis'), ylabel('label of y axis'), xlim([xmin xmax]), ylim([ymin ymax])
saveas(figure(i), 'name.png') -> save figure(i) as name.png
Numerical solution of ODEs
Solution of an IVP
Transcript the differential equation
Numerical solution of ODEs
The ODE is given
1. If the ODE is higher order transform to first order ODE System
Example:
In matrix form:
0 0x t cx t s x t g x t 0 0, ; x =x ; x =x
1 2 2x : y x : y x : y; ;
2 2 1
2 2 1
y t c y t sy t g x t
y t cy t sy t g x t
,
,
1 2y x y=
0t (0)= , ; y f y y y
0
0
2 0
00 1t (0)
g y ts c
x= , ;
, x
y y + f y y y
T
1 2y y= ;y
Example
Numerical solution of ODEs
In the case of Van der Pool oscillator
Implemented as „VdP_rhs(t,y)” in „ExpImplEu_VanDerPol_new.m”
2
0 0
2
2 1 2 1
2
2 1 2 1
1 2
x 1 x x x 0 0 0
y 1 y y y 0
y 1 y y y
y y
; x =x ; x =x
Explicit Euler method
Numerical solution of ODEs
2. Select a solver
– Explicit Euler method
Implemented as
n 1 n n ndt t, y y f y
Implicit Euler method
Numerical solution of ODEs
– Implicit Euler method
Try to minimize the expression
Implemented as
n 1 n n 1 n 1
n n 1 n 1 n 1
dt t
dt t 0
,
,
y y f y
y f y y
n 1 n n 1 n 1 n 1dt t 0r , y = y f y y
Numerical solution of ODEs
Break
Adaptive timestep
Numerical solution of ODEs
Problem: Large timestep -> accuracy, stability problems
Small timestep -> computationally expensive (=slow)
Idea:
1. Predict the local error -> 2. hold between given limits
1. Prediction of the local error
Calculate one step in two different ways with different accuracy
(but with the same scheme!)
The accuracy can be changed: – the order of the scheme is changed (ode45)
– Solve the problem with different stepsize. First, in one step with dt, then in two steps with dt/2
Adaptive timestep
Numerical solution of ODEs
The error:
Hold the error between limits
If the estimated error is too large do not store the results
decrease the timestep
Else store the results
If the est. error is too small increase the timestep
1
n 1
2
n 1
the less accurate result
the more accurate result
y
y
2 1
n 1 n 1e = y y
min max,
max
new olddt rdt r <1
min
max
Adaptive timestep
Numerical solution of ODEs
Implemented as:
Accuracy of solvers
Numerical solution of ODEs
The problem:
x t x t 3 2t 0 2 0 1sin ; x = ; x =
2 1y t y t 3 2tsin
1 2y x y=
0
2
1
y
Analytical solution:
x t t 2 t 2tsin cos sin
Accuracy of solvers
Numerical solution of ODEs
Accuracy of solvers
Numerical solution of ODEs
Boundary value problem
Numerical solution of ODEs
x t v
y t g
x t 0 0
y t 0 0
y x 1 0
1
x t vt 0
y t x v 0
y t 0
/
?
BVP-Shooting method
Numerical solution of ODEs
y t g
y t 0 0
y t 0
1D : y t x v/
D 0 Solve
Implemented as:
BVP-Finite differences
Numerical solution of ODEs
y t g
y t 0 0
y t 1 v 0/
i 1 i i 1
2
i 1 i i 1
2
2
i 1 i i 1
y 2y yy t
t
y 2y yg
t
y 2y y t g
2
3
2
n 2
n 1
y2 1 1
y1 2 1
t g
y1 2 1 1
y1 2 1
BVP-Finite differences
Numerical solution of ODEs
Numerical solution of ODEs
Thank you for your attention!