Reader small image

You're reading from  Mastering SciPy

Product typeBook
Published inNov 2015
Reading LevelIntermediate
Publisher
ISBN-139781783984749
Edition1st Edition
Languages
Right arrow
Authors (2):
Francisco Javier Blanco-Silva
Francisco Javier Blanco-Silva
author image
Francisco Javier Blanco-Silva

I will always be indebted to Bradley J. Lucier and Rodrigo Bañuelos, for being a constant inspiration, for their guidance and teachings. Special thanks to my editors, Sriram Neelakantam, Bharat Patil, Nikhil Potdukhe, and Mohammad Rizvi. Many colleagues have contributed with encouragement and fruitful discussions. In particular, I would like to mention Parsa Bakhtary, Aaron Dutle, Edsel Peña, Pablo Sprechmann, Adam Taylor, and Holly Watson. But the most special thanks go without a doubt to my wife and daughter. Grace's love and smiles alone provided all the motivation, enthusiasm and skills to overcome any difficulties encountered during the pursuit of this book, and everything life threw at me ever since she was born.
Read more about Francisco Javier Blanco-Silva

View More author details
Right arrow

Chapter 5. Initial Value Problems for Ordinary Differential Equations

Initial value problems for ordinary differential equations (or systems) hardly need any motivation. They arise naturally in almost all sciences. In this chapter, we will focus on mastering numerical methods to solve these equations.

Throughout the chapter, we will explore all the techniques implemented in the SciPy stack through three common examples:

  • The trivial differential equation of the first order y'(t) = y(t) with the initial condition as y(0) = 1. The actual solution is y(t) = et.

  • A more complex differential equation of first order: a Bernoulli equation ty'(t) + 6y(t) = 3ty(t)3/4, with the initial condition y(1) = 1. The actual solution is y(t) = (3t5/2 + 7)4/(10000t6).

  • To illustrate the behavior with autonomous systems (where the derivatives do not depend on the time variable), we use a Lotka-Volterra model for one predator and one prey, y0'(t) = y0(t) – 0.1 y0(t) y1(t) and y1'(t) = –1.5 y1(t) + 0.075 y0(t) y1(t...

Symbolic solution of differential equations


Symbolic treatment of a few types of differential equations is coded in the SciPy stack through the module sympy.solvers.ode. At this point, only the following equations are accessible with this method:

  • First order separable

  • First order homogeneous

  • First order exact

  • First order linear

  • First order Bernoulli

  • Second order Liouville

  • Any order linear equations with constant coefficients

In addition to these, other equations might be solvable with the following techniques:

  • A power series solution for the first or second order equations (the latter only at ordinary and regular singular points)

  • The lie group method for the first order equations

Let's see these techniques in action with our one-dimensional examples, y' = y and the Bernoulli equation. Note the method of inputting a differential equation. We write it in the form F(t,y,y') = 0, and we feed the expression F(t,y,y') to the solver (see line 3 that follows). Also, notice how we code derivatives of a function...

Analytic approximation methods


Analytic approximation methods try to compute approximations to the exact solutions on suitable domains, in the form of truncated series expansions over a system of basis functions. In the SciPy stack, we have an implementation based on the Taylor series, through the routine odefun in the module sympy.mpmath.

Note

mpmath is a Python library for arbitrary-precision floating-point arithmetic, hosted inside the sympy module. Although it is independent of the numpy machinery, they both work well together.

For more information about this library, read the official documentation at http://mpmath.org/doc/current/.

Let's see it in action, first with our trivial example y'(t) = y(t), y(0) = 1. The key here is to assess the speed and the accuracy of the approximation, as compared to the actual solution in the interval [0, 1]. Its syntax is very simple, we assume the equation is always in the form of y' = F, and we provide the routine odefun with this functional F and the...

Discrete-variable methods


In discrete-variable methods, we are concerned with finding approximations to the solutions, but only at a discrete set of points in the domain. These points could be predetermined before solving, or we could generate them dynamically as part of the integration, to better suit the properties of the functions involved. This is especially useful when the solutions present singularities, for example, once a discrete set of points have been generated, we can compute a nice analytic approximation to the solutions, by a simple process of interpolation.

We have two schema for discrete-variable methods:

  • One-step methods: The value of the solution at one point is computed solely from information on the previous point. Classical exponents of this scheme are, for instance, Euler's method, improved Euler's method, any second-order two-stage method, or any Runge-Kutta method.

  • Multistep methods: The value of the solution at one point depends on the knowledge of several previous...

Summary


In this short chapter, we have mastered all the symbolic and numerical techniques to solve differential equations/systems and related initial value problems.

In the next chapter, we will explore what resources we have in the SciPy stack, to address problems in computational geometry.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering SciPy
Published in: Nov 2015Publisher: ISBN-13: 9781783984749
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Francisco Javier Blanco-Silva

I will always be indebted to Bradley J. Lucier and Rodrigo Bañuelos, for being a constant inspiration, for their guidance and teachings. Special thanks to my editors, Sriram Neelakantam, Bharat Patil, Nikhil Potdukhe, and Mohammad Rizvi. Many colleagues have contributed with encouragement and fruitful discussions. In particular, I would like to mention Parsa Bakhtary, Aaron Dutle, Edsel Peña, Pablo Sprechmann, Adam Taylor, and Holly Watson. But the most special thanks go without a doubt to my wife and daughter. Grace's love and smiles alone provided all the motivation, enthusiasm and skills to overcome any difficulties encountered during the pursuit of this book, and everything life threw at me ever since she was born.
Read more about Francisco Javier Blanco-Silva