Vector Fields and Solutions to Ordinary Differential Equations using Octave

Similar documents
Vector Fields and Solutions to Ordinary Differential Equations using MATLAB/Octave

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

Solution: (a) Before opening the parachute, the differential equation is given by: dv dt. = v. v(0) = 0

Chapter 1: Introduction

Numerical Integration of Ordinary Differential Equations for Initial Value Problems

Numerical Methods for the Solution of Differential Equations

Chapter 9b: Numerical Methods for Calculus and Differential Equations. Initial-Value Problems Euler Method Time-Step Independence MATLAB ODE Solvers

MECH : a Primer for Matlab s ode suite of functions

MTH 452/552 Homework 3

2 Solving Ordinary Differential Equations Using MATLAB

Syntax. Arguments. Solve m oderately stifo DEsand DAEs;trapezoidalrule. 1 of :34

Experiment 1: Linear Regression

MAT 275 Laboratory 6 Forced Equations and Resonance

Lesson 14: Van der Pol Circuit and ode23s

Ordinary differential equations - Initial value problems

Laboratory 10 Forced Equations and Resonance

Mathematics 22: Lecture 11

Numerical Analysis II. Problem Sheet 12

Ordinary Differential Equations (ODE)

Solving ODEs and PDEs in MATLAB. Sören Boettcher

Computer lab for MAN460

Manifesto on Numerical Integration of Equations of Motion Using Matlab

Ordinary Differential Equations II

Initial Value Problems

Ordinary Differential Equations II

Solution: (a) Before opening the parachute, the differential equation is given by: dv dt. = v. v(0) = 0

Ordinary Differential Equations: Initial Value problems (IVP)

AMS 27L LAB #8 Winter 2009

AM205: Assignment 3 (due 5 PM, October 20)

4.4 Computing π, ln 2 and e

. For each initial condition y(0) = y 0, there exists a. unique solution. In fact, given any point (x, y), there is a unique curve through this point,

Chap. 20: Initial-Value Problems

Lecture 17: Ordinary Differential Equation II. First Order (continued)

24, B = 59 24, A = 55

Delay Differential Equations Part I: Constant Lags

Ordinary Differential Equations (ode)

Solving systems of ODEs with Matlab

Physics 584 Computational Methods

Using Matlab to integrate Ordinary Differential Equations (ODEs)

Section 7.4 Runge-Kutta Methods

The family of Runge Kutta methods with two intermediate evaluations is defined by

1 Introduction to MATLAB

Logistic Map, Euler & Runge-Kutta Method and Lotka-Volterra Equations

MAT 275 Laboratory 6 Forced Equations and Resonance

[ ] is a vector of size p.

Modeling and Experimentation: Compound Pendulum

13 Numerical Solution of ODE s

ES205 Analysis and Design of Engineering Systems: Lab 1: An Introductory Tutorial: Getting Started with SIMULINK

Simple ODE Solvers - Derivation

Octave at BFH-TI Biel. Andreas Stahel, Bern University of Applied Sciences, Switzerland

Numerical method for approximating the solution of an IVP. Euler Algorithm (the simplest approximation method)

ODE Runge-Kutta methods

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

Mechanics of Structures (CE130N) Lab 3

Graded Project #1. Part 1. Explicit Runge Kutta methods. Goals Differential Equations FMN130 Gustaf Söderlind and Carmen Arévalo

Differential Equations (Mathematics) Evaluate the numerical solution using output of ODE solvers.

Appendix 3B MATLAB Functions for Modeling and Time-domain analysis

Section 2.1 Differential Equation and Solutions

Memristor Based Chaotic Circuits

PSTricks. pst-ode. A PSTricks package for solving initial value problems for sets of Ordinary Differential Equations(ODE), v0.12.

Introduction to ODEs in Matlab. Jeremy L. Marzuola Charles Talbot Benjamin Wilson

Solving Ordinary Differential Equations

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

Fourth Order RK-Method

Lecture 5b: Starting Matlab

Ordinary Differential equations

Computational Methods of Scientific Programming. Lecturers Thomas A Herring Chris Hill

An Introduction to Xcos

Computational Methods in Plasma Physics

2 Ordinary Differential Equations: Initial Value Problems

NUMERICAL SOLUTION OF ODE IVPs. Overview

Ordinary differential equation II

Temperature measurement

Two applications of macros in PSTricks

Chapter 6 - Ordinary Differential Equations

Laboratory 1. Solving differential equations with nonzero initial conditions

Chapter 1 Fundamental Concepts

Lecture 1. Scott Pauls 1 3/28/07. Dartmouth College. Math 23, Spring Scott Pauls. Administrivia. Today s material.

FORTRAN 77 Lesson 7. Reese Haywood Ayan Paul

The Spring Pendulum. Nick Whitman. May 16, 2000

University of Alberta ENGM 541: Modeling and Simulation of Engineering Systems Laboratory #5

Numerical Solution of Differential Equations

4. Numerical analysis II

Matlab Section. November 8, 2005

FIRST-ORDER ORDINARY DIFFERENTIAL EQUATIONS II: Graphical and Numerical Methods David Levermore Department of Mathematics University of Maryland

AMS 27L LAB #6 Winter 2009

Solution. Your sketch should show both x and y axes marked from 5 to 5 and circles of radius 1, 3, and 5 centered at the origin.

INTRODUCTION TO COMPUTER METHODS FOR O.D.E.

CPS 5310: Parameter Estimation. Natasha Sharma, Ph.D.

APPLICATIONS OF FD APPROXIMATIONS FOR SOLVING ORDINARY DIFFERENTIAL EQUATIONS

Solutions of the Sample Problems for the First In-Class Exam Math 246, Fall 2017, Professor David Levermore

MATH2071: LAB 3: Implicit ODE methods

Numerical Analysis II. Problem Sheet 9

Lab 13: Ordinary Differential Equations

Research Computing with Python, Lecture 7, Numerical Integration and Solving Ordinary Differential Equations

10.34: Numerical Methods Applied to Chemical Engineering. Lecture 19: Differential Algebraic Equations

Lesson 9: Predator-Prey and ode45

A Glimpse at Scipy FOSSEE. June Abstract This document shows a glimpse of the features of Scipy that will be explored during this course.

Transcription:

Vector Fields and Solutions to Ordinary Differential Equations using Andreas Stahel 6th December 29 Contents Vector fields. Vector field for the logistic equation...............................2 Solutions of ordinary differential equations with lsode.................. 2.3 Vector field for the equation of a damped pendulum...................... 3.4 Solution to the pendulum equation............................... 5 2 The ODE Package on -Forge 6 2. Examples for ode23, ode45 and ode78...................... 7 3 Codes from Lecture Notes 8 3. Using RK45, Runge-Kutta adaptiv............................. 9 3.2 Using ode Runge, Runge-Kutta with fixed step...................... 9 3.3 Using lsode........................................ 9 In these notes you find the methods to generate vector fields with the help of. In addition one of the methods to solve ordinary differential equations is illustrated. The notes assume that you have a recent version of installed. Vector fields A vector field in the plane R 2 is determined by a vector function F x, x 2 F x = F 2 x, x 2 At each point x R 2 the vector F x is attached. One of the possible applications of vectors fields is the visualization of solution of ordinary differential equations.. Vector field for the logistic equation The vector field for the logistic differential equation d dt xt = xt x2 t

VECTOR FIELDS 2 is given by Ft, x = x x 2 To visualize this field we have to choose the domain to be used define the functions for the vector field choose how many vectors to draw compute the vectors at the chosen points rescale the length of the vectors generate the graphic on screen save the result in a file The above tasks can be performed with, as shown in the code below with the resulting Figure. t = :. 5 : 8 ; %% choose the time values x = :. 2 : 2 ; %% choose the x values %% define the function function dx = l o g i s t i c x, t dx = x x ˆ 2 ; %% c r e a t e the zero vector f i e l d nt = length t ; nx = length x ; V = zeros nt, nx ; V2 = zeros nt, nx ; %% compute the vector f i e l d for i = : nt for j = : nx V i, j = ; V2 i, j = l o g i s t i c x j, t i ; endfor endfor %% choose the s c a l e f a c t o r s c a l e f a c t o r =. 2 ; f i g u r e ; quiver t, x,v,v2, s c a l e f a c t o r axis [, 8,, 2 ] ; xlabel time t ; ylabel population x grid on %% p r i n t Logistic. png %% p r i n t Logistic. eps.2 Solutions of ordinary differential equations with lsode provides a selection of commands to solve ordinary differential equations, including systems. Here we only use lsode to generate solutions for the differential equations arising from the above vector fields.

VECTOR FIELDS 3 2.5 population x.5 2 3 4 5 6 7 8 time t Figure : Vector field for the logistic equation To solve the initial value problem we proceed as follows. d dt xt = xt x2 t with x =.2 Choose the values of time t for which the solution is to be computed. Compute the solution with the command lsode. We may compute more solution by using different initial conditions, e.g. x =.4 and x = 2.. Plot the solutions. The solutions and the vector field can be shown in one graphic. Find the result in Figure 2. T = :. : 8 ; X = lsode l o g i s t i c,. 2,T ; X2 = lsode l o g i s t i c,. 4,T ; X3 = lsode l o g i s t i c, 2.,T ; f i g u r e 3 ; hold off p l o t T, [ X,X2,X3 ] ; grid on ; axis [, 8,, 2 ] ; hold on quiver t, x,v,v2,. 4.3 Vector field for the equation of a damped pendulum For atonomous differential equations the first component of the vector field equals. For general problems this is not the case. As an example we convert the differential equation for a damped pemdulum ÿt = k yt α ẏt

VECTOR FIELDS 4 2.5.5 2 3 4 5 6 7 8 Figure 2: Solutions and vector field for the logistic equation into a system of diffeential equations of order one d dt yt vt = vt k yt α vt and examine the resulting vector field. With the numerical values k = and α =. we have to examine the vector field Fy, v = y. v The code to be used is very similar to the logistic equations and thus shown without any further explanation. y = :.:; %% choose the y values v = :.:; %% choose the v values %% define the function function dy = Fy, v dy = v ; function dv = F2y, v k = ; alpha =. ; dv = k y alpha v ; %% c r e a t e the zero vector f i e l d ny = length y ; nv = length v ; V = zeros ny, nv ; V2 = zeros ny, nv ; %% compute the vector f i e l d for i = : ny for j = : nv V i, j = Fy i, v j ; V2 i, j = F2y i, v j ; endfor endfor

VECTOR FIELDS 5 %% choose the s c a l e f a c t o r s c a l e f a c t o r =. 5 ; f i g u r e ; quiver y, v,v, V2, s c a l e f a c t o r grid on ; axis [ min y,max y, min v,max v ] xlabel p o s i t i o n x ; ylabel speed v The only additional feature is the explicit choice of the domain shown in the graphic by the command axis. As can be seen in the left of Figure 3 the vectors at the origin have a small length and thus it is difficult to read of the direction. This can be improved by normalizing all the vectors to have length and then rescale them to have a good length for visalisation. This is done in the code below witrh the result on the right in Figure 3. f i g u r e 2 ; Vlength = s q r t V.ˆ2+V2. ˆ 2 ; Vn = V. / Vlength ; V2n = V2. / Vlength ; quiver y, v, Vn,V2n, s c a l e f a c t o r grid on ; xlabel p o s i t i o n x ; ylabel speed v axis [ min y,max y, min v,max v ].5.5 speed v speed v -.5 -.5 - - -.5.5 position x - - -.5.5 position x Figure 3: Standard and normalized vector field for the pendulum equation.4 Solution to the pendulum equation To determine solutions of the system of differential equations d dt yy vt = vt yt. vt with y v =.9 we have to rewrite the function to be used by lsode. T = :. 5 : 2 5 ; function dy = ODEPendy, t

2 THE ODE PACKAGE ON OCTAVE-FORGE 6 k = ; alpha =. ; dy = [ y 2 ; k y alpha y 2 ] ; YV = lsode ODEPend, [ ;. 9 ],T ; f i g u r e 3 ; p l o t T,YV ; grid on xlabel time ; ylabel p o s i t i o n and velocity ; f i g u r e 4 hold off quiver y, v, Vn,V2n, s c a l e f a c t o r grid on axis [ min y,max y, min v,max v ] hold on p l o t YV :,,YV :, 2, r xlabel position ; ylabel velocity ; hold off position and velocity.5 -.5 velocity.5 -.5-5 5 2 25 time - - -.5.5 position Figure 4: One solution and the vector field for the pendulum equation 2 The ODE Package on -Forge The standard command in to solve ordinary differential equations is lsode, as used in the previous section. On Forge at http://octave.sourceforge.net/ you can find an package with additional commands to solve ODEs. Dowload the package odepkg in a local directory Launch and install the package with the command pkg install odepkg-.6.4.tar.gz Depending on your local configuration you might have to load the package with pkg load odepkg

2 THE ODE PACKAGE ON OCTAVE-FORGE 7 With this package many commands from MATLAB are now available in, and some additional commands. Table shows some of the commands. Solver lsode ode23 Description efficient, addaptive solver based on Hindmarsh s work [Hind93] addaptive, explicit,solver, based on Heun s method ode45 addaptive, explicit solver, based on Runge-Kutta method of order 4, resp. 5 ode54 Runge Kutta solver ode78 addaptive, explicit solver, based on Runge-Kutta method of order 7, resp. 8 ode2r ode5r solver for stiff problems, based on Hairer and Wanner s code solver for stiff problems, based on Hairer and Wanner s code Table : commands to solve ordinary differential equations The package provides many additional commands, some of them shown in Table 2. Find a description of the algorithms, their advantages and disadvantages in the file odepkg.pdf, to be found in the subdirectory with the package. Command odeexamples ode23d ode45d ode78d Description launch demos for ordinary differential equations solver for delay differential equations solver for delay differential equations solver for delay differential equations Table 2: Additional commands in the ODE package 2. Examples for ode23, ode45 and ode78 As an example we consider again the pendulum equation d yy vt = dt vt yt. vt with y v = Thus a function file with this function may be generated and then used by all examples below. function dy = ODEPend t, y k = ; alpha =. ; dy = [ y2; k y alpha y 2 ] ; ODEPend.m As a general rule we first choose the parameters for the solver. As a first example we choose a relative tolerance of 3 and an absolute tolerance of 3. We want a graph to be generated while the differential equation is solved. vopt = odeset RelTol, e 3, AbsTol, e 3, NormControl, on,\ OutputFcn, @odeplot ; Then we use ode78 to solve the system of equations..9

3 CODES FROM LECTURE NOTES 8 ode78 @ODEPend, [ 25], [. 9 ], vopt ; The resulting animation and final solution will look rather ragged. Since the Runge Kutta algorithm of order 7 is very efficient, only very few points have to be computed. Thus the graphic does not look nice, but the numerical results are reliable. The command ode23 @ODEPend, [ 25], [. 9 ], vopt ; will generate a nice looking graph, but require more computation time. With all of the above codes the numerical values will not be returned and are thus not available for further computation. Use the code below if further computations have to be performed. You will find a plot of position and velocity as function of time and a phase plot. vopt = odeset RelTol, e, AbsTol, e, NormControl, on ; [ t, y ] = ode78 @ODEPend, [ 25], [. 9 ], vopt ; f i g u r e ; p l o t t, y ; xlabel time ; ylabel p o s i t i o n and velocity ; grid on f i g u r e 2 ; p l o t y :,, y :, 2 ; xlabel position ; ylabel velocity ; grid on 3 Codes from Lecture Notes To illustrate the codes presented in the lecture notes we use the example of a diode circuit examined in the notes. The behavior of the diode is given by a function { i = D u = for u u s R D u + u s for u < u s Based on Kirchhoff s law we find the differential equations u h = D u h u in + D u out u h C u out = u in D u out u h C 2 We define the initial conditions and the two function in a script file. u in t = cost. Tend = 3; u = [ ; ] ; We examine an input voltage of function curr = Diode u Rd = ; us =. 7 ; i f u>= us curr =; e l s e curr=rd u+us ; endif function y = c i r c u i t u, t C = ; C2 = ; y = [ /C Diode u sin t Diode u2 u ; cos t /C2 Diode u2 u ] ;

3 CODES FROM LECTURE NOTES 9 3. Using RK45, Runge-Kutta adaptiv We can use the adaptive Runge-Kutta algorithm with relative and absolute tolerance of 5 and generate the plot by t = cputime ; [ t, u ] = rk45 c i r c u i t,, Tend, u, e 5,e 5; % Runge Kutta adaptiv timer = cputime t f i g u r e ; p l o t t, u :, 2. grid on ; xlabel time ; ylabel tension ; The result in Figure 5 seems reasonable, but it took 8 seconds of CPU time to compute. 3.2 Using ode Runge, Runge-Kutta with fixed step We can compare the result with a Runge-Kutta calculation with steps, resulting in Figure 5. tfix = linspace, Tend, ; t = cputime ; [ tfix, ufix ] = ode Runge c i r c u i t, tfix, u, ; % Runge Kutta timer = cputime t p l o t tfix, ufix :, 2, t, u :, 2 grid on ; xlabel time ; ylabel tension ; legend u fix, u adapt It took only.5 seconds, but the solution is ragged at the turning points. This can be improved by using intermediate steps between the output times. Use [ tfix, ufix ] = ode Runge c i r c u i t, tfix, u, ; % Runge Kutta to find a competitive solution in.3 seconds. This is one of the rare occasion where a fixed size algorithm outperforms an adaptive algorithm. 3.3 Using lsode We can compare the above result with the performance of lsode. Unfortunately the arguments t and u have to be given in reverse order for lsode and the above codes and thus the header of the function circuit has to be modified slightly. We have to specify the tolerances. With a computation time of.48 seconds we find a good solution. This illustrates the quality of the algorithm in lsode.m. Tend = 3; u = [ ; ] ; DoubleTensionLSODE.m function curr = Diode u Rd = ; us =. 7 ; i f u>= us curr =; e l s e curr=rd u+us ; endif function y = c i r c u i t u, t C = ; C2 = ; y = [ /C Diode u sin t Diode u2 u ; cos t /C2 Diode u2 u ] ; t = linspace, Tend, ;

REFERENCES 3 25 u fix u adapt tension 2 5 5-5 5 5 2 25 3 time Figure 5: Solution for the diode circuit with Runge-Kutta, fixed step and adaptive lsode options absolute lsode options r e l a t i v e t o l e r a n c e, e 5; t o l e r a n c e, e 5; t = cputime ; u = lsode c i r c u i t, u, t ; timer = cputime t p l o t t, u :, 2 grid on ; xlabel time ; ylabel tension ; In the previous section the codes and data files in Table 3 were used. filename ode Euler.m ode Heun.m ode Runge.m rk45.m DoubleTension.m DoubleTensionLSODE.m function algorithm of Euler, fixed step size algorithm of Heun, fixed step size algorithm of Runge-Kutta, fixed step size adaptive Runge Kutta algorithm sample code for the diode circuit using lsode Table 3: Codes and data files References [Hind93] A. C. Hindmarsh and K. Radhakrishnan. Description and Use of LSODE, the Livermore Solver for Ordinary Differential Equations. NASA, 993.