Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Numerical Integration of Ordinary Differential Equations: Mathematical Methods and Modeling Laboratory Class

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Mathematical Methods and Modeling

Laboratory class

Numerical Integration of
Ordinary Differential Equations
Exact Solutions of ODEs

• Cauchy’s Initial Value Problem in normal form:

• Recall:
• if f is locally Lipschitz-continuous
 there is an unique local solution
• if f is (uniformly) Lipschitz-continuous on all I
 there is an unique solution in I, i.e.
global solution
Numerical Analysis

• If solution can hardly be explicited  numerical

• Numerical Analysis is the branch of mathematics


studying approximation methods for solving
equations  applications on calculators
Explicit Euler Method

• For finding an approximate (or numerical)


solution of an ODE in normal form, first discretize
the domain I into n subintervals of width h:

s.t.
• Discretize the derivative y’(x) with the (forward)
finite difference:

• Denote and
• suppose to be able to calculate
Explicit Euler Method

• The ODE can then be approximated as

• Approximate solution:

• This formula for calculating the approximate


solution is called Explicit/Forward Euler Method
• Necessary:
• Initially:
• At each step:
• Decide discretization step h
• It can be shown consistent:
Implicit Euler Method

• Conversely, if we take the backward finite


difference

we have the approximation:

• This formula is called Implicit/Backward Euler


Method
• However, in this method we have to solve the
equation (with f appearing) in the unknown
• In practice, when f(x,y) behaves badly, Implicit
method is preferred if computationally feasible
Theta-Method

• Instead, fix in [0,1] and take the intermediate


value of f:

• Then it is easy to derive a generalization of EMs:

• This formula is called the -method


• Clearly, with it is Explicit/Implicit EM
• Is it explicit or implicit?
• It has better convergence properties
Example

• Find the numerical solution to Cauchy’s IVP

• Compare it with the exact solution

1. Define the M-file for function f(t,y) = 3t-ty


2. Fix h>0 and find numerical solution u by
EEM
3. Draw y, u and the error y-u
Example

• Let's try to numerically solve previous IVP with


IEM. Unluckly, this time it is not possible to use
MATLAB for inverting f(t,y) w.r.t. y. Hence, invert it
with paper & pencil

• That yields

• Compare IEM's to EEM's and exact solution y,


finding maximum absolute error and sum-of-
squares error
MATLAB ODE Solver

• Luckly, MATLAB already has algorithms for ODEs


• ode45 function finds approximate solutions for
most of simple non-stiff problems. Basic syntax:
[T,Y] = ode45(@fun,[x0 xF],initvals);
• @fun is handle to funcion fun defining f(x,y)
• x0,xF are initial and final values of interval I
• vector initvals contains Cauchy’s initial value(s)
of the solution(s): y(x0)
• odeset function can set some options of ODE
Solver specified as additional argument. Eg.:
odeopt = odeset('RelTol',1e-4,'AbsTol',…
[1e-4 1e-4 1e-5]);
Example

• Find the numerical solution to Cauchy’s IVP

• Compare it with the exact solution

1. Define the function f(t,y) = 3t-ty


2. Call ode45 function
3. Draw y, u and the error y-u
Example

• Find the numerical solution to Cauchy’s IVP

• Compare it with the exact solution

1. Define the function f(t,y)


2. Call ode45 function
3. Draw y, u and the error y-u
Higher order ODE

• A n-th order ODE y(n)=f(t,y,y',...,y(n-1)) can be


transformed into a system of n ODEs with n
variables
• Example: equation of Van der Pol Oscillator

after assigning y=x', it can be rewritten as system

• Try solving through MATLAB with

in the interval [0, 20]


Higher order ODE

• Create the M-file vaderpol.m defining the vector


function f(t,y) needed by ode45:
function out = vanderpol(t,x)
%as stated in the documentation this function has
%to take as arguments: time t and state-variable x.
mu = 2; %parameter of the Van der Pol oscillator

out = [0; 0];


out(1) = x(2);
out(2) = mu*(1-x(1)^2)*x(2) - x(1);
end
Higher order ODE

•Then in the command window run the instruction


[T,Y] = ode45(@vanderpol, [0 20], [2 0]);
• and draw the trajectory:
plot3(T,Y(:,1),Y(:,2));
xlabel('t'); ylabel('x'), zlabel('y');

You might also like