Introduction to Numerical Methods/Ordinary Differential Equations

From testwiki
Revision as of 21:03, 26 November 2018 by imported>Lubaochuan (Example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Resources:

A differential equation is a equation that relates some function with its derivatives. Such relationship is commonly found in dynamic system modeling where the rate of change of a quantity is affect by another quantity, which can be a function of other quantities. Therefore differential equations are often used to mathematically represent such relations in many disciplines including engineering, physics, economics, and biology. We will focus on ordinary differential equations, which involves no partial derivative.

The solution to a differential equation is the function or a set of functions that satisfies the equation. Some simple differential equations with explicit formulas are solvable analytically, but we can always use numerical methods to estimate the answer using computers to a certain degree of accuracy.

http://martinfowler.com/bliki/TwoHardThings.html "Two hard things in computer science: cache invalidation, naming things, and off-by-one errors."

Initial Value Problem

The general form of first order differential equation: y=f(x,y) or y=f(x,y(x)). The solution contains an arbitrary constant (the constant of integration) because z(x)=y(x)+c satisfies all differential equations y(x) satisfies. With an auxiliary condition, e.g. y(a)=b, we can solve y'=F(x, y). This is known as the initial value problem.

An indefinite integral of a function f(x) is a class of functions F(x) so that F(x)=f(x). The indefinite integral is also known as the antiderivative. The definite integral of a function over a fixed interval is a number.

Connection with Integration

To calculate the integral F(x)=axf(t)dt is to find a F(x) that satisfies the condition dF(x)dx=f(x), F(a)=0. In other words, integration can be used to solve differential equations with a initial condition.

Euler's Method

Illustration of the Euler method. The unknown curve is in blue, and its polygonal approximation is in red.

Euler's method uses a step-wise approach to solve ordinary differential equations with an initial condition.

Given dydx=f(x,y) and y(x0)=y0 (x is an independent variable and y is a dependent variable) Euler's methods solves for yi for x=xi.

Euler's method can be derived in a number of ways. Lets look at a geographical description.

Given that dydx=f(x,y) at a particular point (xi,yi) we can approximate f(xi,yi) with yi+1yixi+1xi. The derivative represents the instantaneous (happening continuously) change in y at x:

limΔx0ΔyΔx=dydx=y(x)

The average change in y over x is

yi+1yixi+1xi=ΔyΔx,

which represents a discrete version of the change (changes over "steps" of time). If the change in x is small enough, either of the two can be used to approximate the other.

Assume that we know the derivative function of an unknown curve and wants to find the shape of the curve (a function that satisfies a differential equation). We can start from a known point, use the differential equation as as a formula to get the slope of the tangent line to the curve at any point on the curve, and take a small step along that tangent line up to the next point. If the step is small enough, we can expect the next point to be close to the curve. If we pretend that point is still on the curve, the same process can be used to find the next point and so on. Graphically we are using a polynomial to approximate the unknown curve. The gap between the two curves represents the error of the approximation, which can be reduced by shortening the step sizes.

We just derived the formula for Euler's method:

yi+1=yi+f(xi,yi)(xi+1xi)

Example

Given dydx=y, and y(0)=1, solve it for x at 1, i.e. y(1).

The Euler's method formula is a recursively defined function, which can be calculated iteratively. (xi+1xi) is called the step size. If we pick a step size of 1 the solution is as follows:

Illustration of numerical integration for the equation y=y,y(0)=1. Blue is the Euler method; green, the midpoint method; red, the exact solution, y=et. The step size is h = 1.0.
i xi yi dy/dx
0 0 1 1
1 1 2 2
2 2 4 4
3 3 8 8

Another way to derive Euler's method is to use taylor expansion around xi:

y(x0+h)=y(x0)+hy(x0)+12h2y(x0)+O(h3).

If we ignore higher order derivative terms we get the Euler's method formula:

y(x0+h)=y(x0)+hy(x0)

Runge-Kutta 2nd Order Method

One of the Runge-Kutta 2nd order method is the midpoint method, which is a modified Euler's method (one-step method) for numerically solving ordinary differential equations:

y(x)=f(x,y(x)),y(x0)=y0.

The midpoint method is given by the formula

yi+1=yi+hf(xi+h2,yi+h2f(xi,yi)).

Note that yi is the approximation of y(xi) using this method.

Illustration of the midpoint method assuming that yn equals the exact value y(tn). The midpoint method computes yn+1 so that the red chord is approximately parallel to the tangent line at the midpoint (the green line).

Runga-Kutta 4th Order Method

The Runga-Kutta 4th order method is often called "RK4", "classical Runge–Kutta method" or simply as "the Runge–Kutta method".

yi+1=yi+h6(k1+2k2+2k3+k4)xi+1=xi+h

for i = 0, 1, 2, 3, . . . , using

k1=f(xi,yi),k2=f(xi+h2,yi+12k1h),k3=f(xi+h2,yi+12k2h),k4=f(xi+h,yi+k3h).

This method calculate yi+1 by adding to yi) a weighted average of four increments, each of which is the step size h multiplied by an estimated slope:

  • k1 is the increment based on the estimated slope at the beginning of the interval (Euler's method) ;
  • k2 is the increment based on the slope at the midpoint of the interval;
  • k3 is again the increment based on the slope at the midpoint, but now using k2;
  • k4 is the increment based on the slope at the end of the interval, using k3.

In averaging the four increments, greater weight is given to the increments at the midpoint. The weights are chosen such that if f is independent of y, so that the differential equation is equivalent to a simple integral.

Case Study

source

solution in a iPython notebook

Template:BookCat