Differential Equation Basics
Initial Value Problems
Differential equations describe the relation between an unknown function and its derivatives. To solve a differential equation is to find a function that satisfies the relation, typically while satisfying some additional conditions as well
In a canonical initial value problem, the behavior of the system is described by an ordinary differential equation (ODE) of the form
where:
is a known function
is the state of the system
is ’s time derivative
In an initial value problem we are given at some starting time , and wish to follow over time thereafter.
2D visualization:
The trajectory swept out by through forms an integral curve of the vector field
may or may not depend directly on time . If it does, then not only the point but the the vector field itself moves. So that point's velocity depends not only on where it is, but on when it arrives there.
Numerical Solutions
Numerical solutions take discrete time steps starting with the initial value .
To take a step:
use the derivative function to calculate an approximate change in : , over a time interval
increment by to obtain the new value
In calculating a numerical solution, the derivative function is regarded as a black box: we provide numerical values for and , receiving in return a numerical value for
Numerical methods operate by performing one or more of these derivative evaluations at each time step.
Euler’s Method
The simplest numerical method is called Euler’s method.
let is the stepsize parameter
Euler’s method simply computes the next state by taking a step in the derivative direction:
Instead of the true integral curve, the approximate solution follows a polygonal path, obtained by evaluating the derivative at the beginning of each leg:
Though simple, Euler’s method is not accurate:
Consider the case of a 2D function whose integral curves are concentric circles. A point governed by is supposed to orbit forever on whichever circle it started on. Instead, with each Euler step, will move on a straight line to a circle of larger radius, so that its path will follow an outward spiral. Shrinking the stepsize will slow the rate of this outward drift, but never eliminate it.
Moreover, Euler’s method can be unstable:
Consider a 1D function (is satisfied by ), which should make the point decay exponentially to zero. For sufficiently small step sizes we get reasonable behavior, but when:
, we have , so the solution oscillates about zero
Beyond , the oscillation diverges, and the system blows up
Finally, Euler’s method isn’t even efficient:
Most numerical solution methods spend nearly all their time performing derivative evaluations, so the computational cost per step is determined by the number of evaluations per step.
Though Euler’s method only requires one evaluation per step, the real efficiency of a method depends on the size of the steps it lets you take—while preserving accuracy and stability—as well as on the cost per step.
More sophisticated methods, even some requiring as many as four or five evaluations per step, can greatly outperform Euler’s method because their higher cost per step is more than offset by the larger stepsizes they allow.
To understand how we go about improving on Euler’s method, we need to look more closely at the error that the method produces. The key to understanding what’s going on is the Taylor series: Assuming is smooth, we can express its value at the end of the step as an infinite sum involving the the value and derivatives at the beginning:
We get the Euler update formula by truncating the series, which means that Euler’s method would be correct only if all derivatives beyond the first were zero, i.e. if were linear.
The error term, the difference between the Euler step and the full, untruncated Taylor series, is dominated by the leading term . Consequently, we can describe the error as (read “Order h squared”.)
Suppose that we chop our stepsize in half. Although this produces only about one fourth the error we got with a stepsize of , we have to take twice as many steps over any given interval. That means that the error we accumulate over an interval depends linearly upon . Theoretically, using Euler’s method we can numerically compute with as little error as we want, by choosing a suitably small stepsize. In practice, a great many timesteps might be required, depending on the error and the function .
The Midpoint Method
If we were able to evaluate as well as , we could acheive accuracy instead of :
Recall that the time derivative is given by a function . For simplicity in what follows, we will assume that the derivative function does depends on time only indirectly through , so that . The chain rule then gives:
详细推导
基于假设 不直接依赖于 ,仅直接依赖于
To avoid having to evaluate ,which would often be complicated and expensive, we can approximate the second-order term just in terms of , and substitute the approximation into the truncated Taylor series of , leaving us with error. To do this, we perform another Taylor expansion of :
We first introduce into this expression by choosing non-fixed step at time , that is, varying with :
so that:
We can now multiply both sides by (turning the term into ) and rearrange, yielding:
Substituting the right hand side into the truncated Taylor series of gives the update formula (notice that ):
This formula:
evaluates an Euler step
performs a second derivative evaluation at the midpoint of the step
using the midpoint evaluation to update
Hence the name midpoint method. The midpoint method is correct to within , but requires two evaluations of .
We don’t have to stop with an error of . By evaluating a few more times, we can eliminate higher and higher orders of derivatives. The most popular procedure for doing this is a method called Runge-Kutta of order 4 and has an error per step of . (The Midpoint method could be called Runge-Kutta of order 2.) We won’t derive the fourth order Runge-Kutta method, but the formula for computing is listed below:
Adaptive Stepsizes
Whatever the underlying method, a major problem lies in determing a good stepsize.
Ideally, we want to choose as large as possible—but not so large as to give us an unreasonable amount of error, or worse still, to induce instability.
If we choose a fixed stepsize, we can only proceed as fast as the “worst” sections of will allow.
What we would like to do is to vary as we march forward in time. This is the idea of adaptive stepsizing: varying over the course of solving the ODE.
Here we’ll be present adaptive stepsizing for Euler’s method. The basic idea is as follows. Lets assume we have a given stepsize , and we want to know how much we can consider changing it.
Suppose we compute two estimates for . We compute an estimate , by taking an Euler step of size . We also compute an estimate by taking two Euler steps of size .
Both are differ from the true value of by . That means that they differ from each other by . As a result, we can write that a measure of the current error is:
This gives us a convenient estimate to the error in taking an Euler step of size .
Suppose that we are willing to have an error of as much as per step, and that the current error is only . Since the error goes up as , we can increase the stepsize to:
Conversely, if we currently had an error of , and could only tolerate an error of , we would have to decrease the stepsize to:
Implementation
The ODEs we will want to solve may represent many things—for instance, a collection of masses and springs, some rigid bodies, or a deformable object.
We want to implement ODE solvers and the models on which they operate in a way that isolates each from the internal details of the other. This will make it possible to change solvers easily, and also make the solver code reusable.
Fortunately, this kind of modularity is not difficult to acheive, since all solvers can be expressed in terms of a small, stereotyped set of operations. Presumably, the system of ODE-governed objects will be embodied in a structure of some kind. The approach is to write type-specific code that operates on this structure to perform the standard operations, then to implement solvers in terms of these generic operations.
From the solver’s viewpoint, the system on which it operates is a black-box function . The solver needs to be able to evaluate , as required, at any values of and , and then to install the updated and when a time step is taken. To support these operations, the object that represents the ODE being solved must be able to handle these requests from the solver:
Return
: Since and may be vectors, the solver must know their length, to allocate storage, perform vector arithmetic ops, etc.Get
set
and : The solver must be able to install new values at the end of a step. In addition, a multi-step method must set them to intermediate values in the course of performing derivative evaulations.Evaluate
at the current and .
In an object-oriented language, these operations would naturally be implemented as generic functions that are handled in a type-specific way.
In a non-object-oriented language generic functions would be faked by installing pointers to type-specific functions in structure slots, or simply by passing the function pointers as arguments to the solver.
Later on we will consider in detail how these operations are to be implemented for specific models such as particle-and-spring systems
Last updated