Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ordinary Differential Equations (ODEs) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Michael Sokolov ETH Zurich, Institut.

Similar presentations


Presentation on theme: "Ordinary Differential Equations (ODEs) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Michael Sokolov ETH Zurich, Institut."— Presentation transcript:

1 Ordinary Differential Equations (ODEs) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften ETH Hönggerberg / HCI F135 – Zürich E-Mail: michael.sokolov@chem.ethz.ch http://www.morbidelli-group.ethz.ch/education/index

2 Problem Definition  We are going to look at initial value problems of explicit ODE systems, which means that they can be cast in the following form 2Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

3 Higher Order ODEs  Higher order explicit ODEs can be cast into the first order form by using the following «trick»  Therefore, a system of ODEs of order n can be reduced to first order by adding n-1 variables and equations  Example: 3Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

4 Example: A 1-D First Order ODE  Consider the following initial value problem  This ODE and its solution follow a general form (Johann Bernoulli, Basel, 17 th – 18 th century) 4Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

5 Some Nomenclature  On the next slides, the following notation will be used 5Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

6 Numerical Solution of a 1-D First Order ODE  Since we know the first derivative, Taylor expansion suggests itself as a first approach  This is known as the forward / explicite Euler method, named after Leonhard Euler (Basel, 18 th century) 6Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

7 Definition of an implicit algorithm  In an implicit algorithm, the function value at the next step appears on the right hand side of the step equation:  A simple example is the backward / implicit Euler method:  A system of equations has to be solved at every iteration step; Why even use implicit algorithms? 7Daniel Baur / Numerical Methods for Chemical Engineers / Implicit ODE Solvers

8 How does Matlab do it? Non-Stiff Solvers  ode45  ode45 : Most general solver, best to try first; Uses an explicit Runge-Kutta pair (Dormand-Prince, p = 4 and 5)  ode23ode45  ode23 : More efficient than ode45 at crude tolerances, better with moderate stiffness; Uses an explicit Runge- Kutta pair (Bogacki-Shampine, p = 2 and 3).  ode113  ode113 : Better at stringent tolerances and when the ODE function file is very expensive to evaluate; Uses a variable order Adams-Bashforth-Moulton method 8Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

9 How does Matlab do it? Stiff Solvers  ode15sode45  ode15s : Use this if ode45 fails or is very inefficient and you suspect that the problem is stiff; Uses multi-step numerical differentiation formulas (approximates the derivative in the next point by extrapolation from the previous points)  ode23sode15s  ode23s may be more efficient than ode15s at crude tolerances and for some problems (especially if there are largely different time-scales / dynamics involved); Uses a modified Rosenbrock formula of order 2 (one-step method) 9Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

10 Matlab Syntax Hints [T, Y] = ode45(ode_fun, tspan, y0, …);  All ODE-solvers use the syntax [T, Y] = ode45(ode_fun, tspan, y0, …);  ode_fun is a function handle taking two inputs, a scalar t (current time point) and a vector y (current function values), and returning as output a vector of the derivatives dy/dt in these points  tspan is either a two component vector [tstart, tend] or a vector of time points where the solution is needed [tstart, t1, t2,...]  y0 are the values of y at tstart f_new = @(t,y)ode_fun(t, y, k, p);  Use parametrizing functions to pass more arguments to ode_fun if needed f_new = @(t,y)ode_fun(t, y, k, p);  This can be done directly in the solver call  [T, Y] = ode45(@(t,y)ode_fun(t, y, k, p), tspan, y0); 10Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

11 Assigment 1: Radioactive decay  A decaying radioactive element changes its concentration according to the ODE where k is the decay constant  The analytical solution to this problem reads 11Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

12 Assignment 1 1.Plot the behavior of the radioactive decay problem as a vector field in the y vs t plane  Find online the function vector_field.m. It plots the solutions and derivatives of first order IVPs for different initial conditions  Plot the vector field for different initial values between 0 and 1, time between 0 and 2 and k = 1. Can you see what a solver has to do?  Is it possible to switch from one trajectory in the vector field to another? What follows for the uniqueness of the solutions? 2.The forward Euler method reads for this problem ynp = Forward_stepper(k, yn, h)  First define a function defining the successor of y n with a header like function ynp = Forward_stepper(k, yn, h) function [T,Y] = stepper_integrate(@stepper,k,t0,tEnd,y0,h)  Use the conditions y0 = 1, k = 1 and h = 0.1 to solve the radioactive decay problem from t0 = 0 to tEnd = 10 by defining an integrating function of the form function [T,Y] = stepper_integrate(@stepper,k,t0,tEnd,y0,h) 12Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

13 13Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers 3.The backward Euler algorithm uses the following step formula  Rearrange this equation so that you can define a function defining the successor of y n with a header like function ynp = Backward_stepper(k, yn, h) ynp = Backward_stepper(k, yn, h)  Use the prior defined integration function to solve the problem with the backward method. 4.Plot the obtained solutions comparing them to the analytical solution. Use the subplot method to produce two subplots in a single figure. What happens if you increase the step size h? What happens if you increase the step size above 2? 5.Plot the global error (error at tEnd) for both methods as a function of the stepsize with h = logspace(-4,0,10) in a double logarithmic plot ( loglog ). Assignment 1 (cont’d)

14 Assigment 2: System of ODEs Problem: The analytical solution to this problem reads 14Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers

15 Assigment 2: System of ODEs 15Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers 1.Solve the problem numerically using  x = 0:1/N:1 with a reasonably large N  y0 = [4;13;1]  A function using the matrix A with a header like function dy = func(x,y,A)  A forward Euler method suitable for vectors y with a header like function [t,y] = eulerForward(f, x0, xEnd, y0, N) 2.Determine and plot the global error using  N = 2.^(0:12)  The built-in function expm in Matlab to determine the analytical solution  The function norm to calculate the error and a double logarithmic plot ( loglog )

16 Assigment 3: Dampened harmonic oscillator  A mass on a spring is subject to a force when out of equilibrium  According to Newton’s second law (including friction), the resulting movement follows the solution to an ODE: 16Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers m y F Spring force Friction

17 Assignment 3 1.Rewrite the motion equation for the dampened harmonic oscillator as an explicit, first order ODE system 2.Use ode45 to solve the problem  Set up a function file to calculate the derivatives, its header should read something like function dy = harm_osc(t, y, m, k, a)  Use m = 1; k = 10; a = 0.5; y0 = 1; dy/dt0 = 0; tSpan = [0, 20];  Plot against time : (1) the position of the mass y, (2) the speed of the mass, (3) the kinetic energy of the mass, (4) the potential energy of the mass 17Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers


Download ppt "Ordinary Differential Equations (ODEs) 1Michael Sokolov / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Michael Sokolov ETH Zurich, Institut."

Similar presentations


Ads by Google