Ordinary Differential Equations (ODE) in MATLAB Ordinary Differential Equations (ODE) in MATLAB What will we learn from the next 5 lectures Ordinary Differential Equations (ODE) in MATLAB Shan He School for Computational Science University of Birmingham ◮ How to solve ODEs using MATLAB. ◮ How to model biological systems using ODEs in MATLAB. ◮ How to analyse ODEs using MATLAB. ◮ Understand bifurcation and chaos using MATLAB. ◮ Applications of bifurcation and chaos to biological problems. Module 06-23836: Computational Modelling with MATLAB Ordinary Differential Equations (ODE) in MATLAB Outline Ordinary Differential Equations (ODE) in MATLAB Concepts about ODE Outline of Topics What is ODE Concepts about ODE Solving ODE in MATLAB ODE Solvers in MATLAB Solving linear ODEs in MATLAB Solving high order ODEs in MATLAB Solving ODEs in MATLAB: Advanced topics Ordinary Differential Equations (ODE) in MATLAB Concepts about ODE An Ordinary Differential Equation (ODE) is an equation involving a function and its derivatives. Ordinary Differential Equations (ODE) in MATLAB Concepts about ODE Definition of ODE Linear ODE and Homogeneous Linear ODE ◮ ◮ Let y be an unknown function of x. ◮ Let y ′ = ◮ Let y (n) = ◮ Let F be a given function ◮ Then an ODE of order n is an equation of the form: dy dx A ODE is said to be linear if F can be written as a linear combination of the derivatives of y together with a constant term, all possibly depending on x: be the first derivative with respect to x. dny dx n an (x)y n + an−1 (x)y n−1 + · · · + a1 (x)y ′ + a0 y = r (x) be the nth derivative with respect to x. or more concisely, yn = F (x, y , y ′ , . . . , y (n) ) = 0 n−1 X ai (x)y (i) + r (x) i=0 ◮ where ai (x) and r (x) are continuous functions in x and the function r (x) is called the source term. A linear ODE is said to be homogeneous if r (x) = 0, otherwise it is called non-homogeneous or inhomogeneous. Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB ODE Solvers in MATLAB Solution to ODE Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB ODE Solvers in MATLAB ODE Solvers in MATLAB ◮ ◮ If an ODE is linear, it can be solved by analytical methods. ◮ ◮ ◮ In general, an nth-order ODE has n linearly independent solutions. Any linear combination of linearly independent functions solutions is also a solution. If an ODE is not linear, most of the case it cannot be solved exactly: we will use MATLAB to obtain approximate solutions Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB ODE Solvers in MATLAB How to use MATLAB ODE Solvers ◮ The MATLAB ODE solvers can be called as a function: [T,Y] = ode**(@odefun,tspan,y0,options) ◮ @odefun: Function handle of the ODE function ◮ tspan: Interval of integration, [t0 ,tfinal ]. ◮ y0: Initial conditions. ◮ options: Optional parameters. ◮ The ODE function odefun define the ODEs: function [dy] = odefun(t, y, parameters) Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving linear ODEs in MATLAB Solving linear ODE in MATLAB Steps for solving this equation numerically in MATLAB: ◮ 1 2 3 4 5 ◮ ◮ ode45: based on an explicit Runge-Kutta (4, 5) formula and the Dormand-Prince method. ode23: based on an explicit Runge-Kutta (2, 3) formula and the Bogacki and Shampine method. ◮ We choose according to order of accuracy and the type of systems (stiff or nonstiff). ◮ Rule of thumb: Always try ode45 first. Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving linear ODEs in MATLAB Solving linear ODE in MATLAB One simple example: ( u′ = v′ = du(x) dx dv (x) dx = u(x) + v (x) = u(x) Analytical solution can be obtained, but how to solve it in MATLAB numerically? Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving linear ODEs in MATLAB Solving linear ODE in MATLAB ◮ Step 1: Create a MATLAB function that defines the ODEs function dy = simple_ode(t,y) % function to be integrated dy = zeros(2,1); dy(1) = y(1) + y(2); dy(2) = y(1); Matlab has several different ODE solvers for the numerical solution of ODEs: 1 2 3 Step 2: Call a numerical solver provided in MATLAB, e.g., [T,Y] = ode45(odefun,tspan,y0) tspan = [0 10]; y0 = [1 0]; [X,Y] = ode45(@simple_ode,tspan,y0); Ordinary Differential Equations (ODE) in MATLAB Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving ODE in MATLAB Solving high order ODEs in MATLAB Solving high order ODEs in MATLAB Reduction of ODE order Reduction of ODE order Methods: ◮ Recall an ODE of the general form: ′ F (x, y , y , . . . , y (n) ◮ We will use a second order ODE as an example: ( x ′ = −ye (−t/5) + y ′ e (−t/5) + 1 y ′′ = −2 sin(t) ◮ Step 1: Introduce a new variable that equals the first derivative of the free variable in the second order equation: z = y′ ◮ Step 2: Taking the derivative of each side yields the following: z ′ = y ′′ )=0 ◮ We said this system is an ODE of order n. ◮ Any differential equation of order n can be reduced to a system of n first-order (n = 1) differential equations. ◮ We do so because high order ODE (n > 1) is difficult to solve. ◮ MATLAB ODE solvers cannot handle higher order ODE! Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving high order ODEs in MATLAB Reduction of ODE order Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving high order ODEs in MATLAB Solve high order ODE in MATLAB Methods: ◮ Methods: ◮ Step 3: Substituting the second order ODE with z and z ′ : ′ (−t/5) + ze (−t/5) + 1 x = −ye ′ z = −2 sin(t) ′ y =z 1 2 3 4 5 6 7 8 9 Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving high order ODEs in MATLAB Solve high order ODE in MATLAB Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving ODEs in MATLAB: Advanced topics Stiffness of ODE equations ◮ Methods: ◮ 1 2 3 4 5 6 7 8 Step 5: evaluate the system of equations using ODE45: t0 = 5; % Start time tf = 20; % Stop time x0 = [1 -1 3] % Initial conditions [t,s] = ode45(@ho_ode,[t0,tf],x0); x = s(:,1); y = s(:,2); z = s(:,3); plot(t,s); Step 4: Write a MATLAB function ho ode.m to define the ODE: function dy = high_order_ode_example(t,x) % x(1) = x % x(2) = y % x(3) = z dy = [-x(2) * exp(-t/5) + ... x(3) * exp(-t/5) + 1; x(3); -2*sin(t)] end ◮ ◮ Stiffness is a subtle, difficult, and important concept in the numerical solution of ordinary differential equations. “A problem is stiff if the solution being sought varies slowly, but there are nearby solutions that vary rapidly, so the numerical method must take small steps to obtain satisfactory results.” Example: y′ = y2 − y3 ◮ y (0) = τ 2 0≤t≤ τ If we weren’t concerned with efficiency of ODE solver, we wouldn’t be concerned about stiffness. Ordinary Differential Equations (ODE) in MATLAB Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving ODE in MATLAB Solving ODEs in MATLAB: Advanced topics Solving ODEs in MATLAB: Advanced topics Passing parameters to ODEs Passing parameters to ODEs ◮ ◮ By passing the value to ODE function, we can change the coefficients in ODEs each time we call the ODE solver. ◮ We need to define the parameters in the ODE function header: function dy = ode_pv(t,y,A) % function to be integrated dy = zeros(2,1); dy = A*y 1 2 3 4 1 2 3 4 5 6 7 8 9 10 Ordinary Differential Equations (ODE) in MATLAB Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving ODE in MATLAB Solving ODEs in MATLAB: Advanced topics Solving ODEs in MATLAB: Advanced topics Events ◮ ◮ Define events Recall syntax of the ODE solvers: [T,Y] = ode**(@odefun,tspan,y0,options) We generally assume tspan is known, e.g., ◮ Define the ODE function: function dydt = ode_ball(t,y) dydt = [y(2); -9.8]; ◮ Define event function for this problem: function [value,isterminal,direction] ... = events(t,y) % Locate the time when height passes through % zero and stop integration. value = y(1); % Detect height = 0 isterminal = 1; % Stop the integration direction = -1; % Desired directionality of % the zero crossings: 1 2 t0 ≤ t ≤ tfinal ◮ ◮ ◮ But sometimes it is also important to determine tfinal . Example: a ball is falling because of gravity, when does it hit the ground? Equations: y ′′ = g = −9.8 ◮ where y (t) is the height of the object at time t The question: for what t does y (t) = 0? We can use events. Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving ODEs in MATLAB: Advanced topics Run ODE Solver with events 3 4 5 6 1 2 3 4 5 6 7 8 Ordinary Differential Equations (ODE) in MATLAB Solving ODE in MATLAB Solving ODEs in MATLAB: Advanced topics Other ODE solvers ◮ Define the event function as an option for ODE solver: opts = odeset(’events’,@ball_events); ◮ What we have introduced are all for Initial Value Problems for ODEs. ◮ Solve it using MATLAB ode45: y0 = [20; 0]; [t,y,tfinal] = ode45(@ode_ball,[0 Inf]... ,y0,opts); tfinal plot(t,y(:,1),’-’,[0 tfinal],[1 0],’o’) ◮ To solve Boundary Value Problems: bvp4c. Tutorial. ◮ To solve Delay ODEs: dde23. Tutorial. ◮ To solve Stochastic ODEs: MATLAB SDE Toolbox. Tutorial. 1 2 Call the ODE solver and pass the parameter to the ODE function: tspan = [0 10]; initvalue = [1 0]; option = []; for ii=-10:4 A = [1 ii; 2 -1]; [t,y] = ode45(@ode_pv,... tspan,initvalue,option,A); hold on; plot(t, y); end