DiffEQ Basics Notesb
DiffEQ Basics Notesb
Andrew Witkin and David Baraff Robotics Institute Carnegie Mellon University
Please note: This document is 1997 by Andrew Witkin and David Baraff. This chapter may be freely duplicated and distributed so long as no consideration is received in return, and this copyright notice remains intact.
Differential equations describe the relation between an unknown function and its derivatives. To solve a differential equation is to nd a function that satises the relation, typically while satisfying some additional conditions as well. In this course we will be concerned primarily with a particular class of problems, called initial value problems. In a canonical initial value problem, the behavior of the system is described by an ordinary differential equation (ODE) of the form x = f (x, t), where f is a known function (i.e. something we can evaluate given x and t,) x is the state of the system, and x is xs time derivative. Typically, x and x are vectors. As the name suggests, in an initial value problem we are given x(t0 ) = x0 at some starting time t0 , and wish to follow x over time thereafter. The generic initial value problem is easy to visualize. In 2D, x(t) sweeps out a curve that describes the motion of a point p in the plane. At any point x the function f can be evaluated to provide a 2-vector, so f denes a vector eld on the plane (see gure 1.) The vector at x is the velocity that the moving point p must have if it ever moves through x (which it may or may not.) Think of f as driving p from point to point, like an ocean current. Wherever we initially deposit p, the current at that point will seize it. Where p is carried depends on where we initially drop it, but once dropped, all future motion is determined by f . The trajectory swept out by p through f forms an integral curve of the vector eld. See gure 2. We wrote f as a function of both x and t, but the derivative function may or may not depend directly on time. If it does, then not only the point p but the the vector eld itself moves, so that ps velocity depends not only on where it is, but on when it arrives there. In that case, the derivative x depends on time in two ways: rst, the derivative vectors themselves wiggle, and second, the point p, because it moves on a trajectory x(t), sees different derivative vectors at different times. This dual time dependence shouldnt lead to confusion if you maintain the picture of a particle oating through an undulating vector eld.
Numerical Solutions
Standard introductory differential equation courses focus on symbolic solutions, in which the functional form for the unknown function is to be guessed. For example, the differential equation x = kx, where x denotes the time derivative of x, is satised by x = ekt .
B1
Vector Field
Figure 1: The derivative function f (x, t). denes a vector eld.
Start Here
B2
Simplest numerical solution method Discrete time steps Bigger steps, bigger errors.
2.1
Eulers Method
The simplest numerical method is called Eulers method. Let our initial value for x be denoted by x0 = x(t0 ) and our estimate of x at a later time t0 + h by x(t0 + h), where h is a stepsize parameter. Eulers method simply computes x(t0 + h) by taking a step in the derivative direction, x(t0 + h) = x0 + h x(t0 ). You can use the mental picture of a 2D vector eld to visualize Eulers method. Instead of the real integral curve, p follows a polygonal path, each leg of which is determined by evaluating the vector f at the beginning, and scaling by h. See gure 3. Though simple, Eulers method is not accurate. Consider the case of a 2D function f whose integral curves are concentric circles. A point p governed by f is supposed to orbit forever on whichever circle it started on. Instead, with each Euler step, p 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.
B3
Inaccuracy: Error turns x(t) from a circle into the spiral of your choice.
Two Problems
Figure 4: Above: the real integral curves form concentric circles, but Eulers method always spirals outward, because each step on the current circles tangent leads to a circle of larger radius. Shrinking the stepsize doesnt cure the problem, but only reduces the rate at which the error accumulates. Below: too large a stepsize can make Eulers method diverge. Moreover, Eulers method can be unstable. Consider a 1D function f = kx, which should make the point p decay exponentially to zero. For sufciently small step sizes we get reasonable behavior, but when h > 1/k, we have | x| > |x|, so the solution oscillates about zero. Beyond h = 2/k, the oscillation diverges, and the system blows up. See gure 4. Finally, Eulers method isnt even efcient. 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 Eulers method only requires one evaluation per step, the real efciency of a method depends on the size of the steps it lets you takewhile preserving accuracy and stabilityas well as on the cost per step. More sophisticated methods, even some requiring as many as four or ve evaluations per step, can greatly outperform Eulers 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 Eulers method, we need to look more closely at the error that the method produces. The key to understanding whats going on is the Taylor series: Assuming x(t) is smooth, we can express its value at the end of the step as an innite sum involving the the value and derivatives at the beginning: x(t0 + h) = x(t0 ) + h x(t0 ) + h2 h3 hn n x x(t0 ) + (t0 ) + . . . + x + ... 2! 3! n! t n
As you can see, we get the Euler update formula by truncating the series, discarding all but the rst two terms on the right hand side. This means that Eulers method would be correct only if all derivatives beyond the rst were zero, i.e. if x(t) were linear. The error term, the difference
B4
between the Euler step and the full, untruncated Taylor series, is dominated by the leading term, (h 2 /2) (t0 ). Consequently, we can describe the error as O(h 2 ) (read Order h squared.) Suppose x that we chop our stepsize in half; that is, we take steps of size h . Although this produces only about 2 one fourth the error we got with a stepsize of h, we have to take twice as many steps over any given interval. That means that the error we accumulate over an interval t0 to t1 depends linearly upon h. Theoretically, using Eulers method we can numerically compute x over an interval t0 to t1 with as little error as we want, by choosing a suitably small h. In practice, a great many timesteps might be required, depending on the error and the function f .
2.2
If we were able to evaluate x as well as x, we could acheive O(h 3 ) accuracy instead of O(h 2 ) simply by retaining one additional term in the truncated Taylor series: x(t0 + h) = x(t0 ) + h x(t0 ) + h2 x(t0 ) + O(h 3 ). 2 (1)
Recall that the time derivative x is given by a function f (x(t), t). For simplicity in what follows, we will assume that the derivative function f does depends on time only indirectly through x, so that x = f (x(t)). The chain rule then gives x= f x = f f. x
To avoid having to evaluate f ,which would often be complicated and expensive, we can approximate the second-order term just in terms of f , and substitute the approximation into equation 1, leaving us with O(h 3 ) error. To do this, we perform another Taylor expansion, this time of the function of f , f (x0 + x) = f (x0 ) + x f (x0 ) + O( x2 ). (2) We rst introduce x into this expression by choosing x= so that f (x0 + h h h f (x0 )) = f (x0 ) + f (x0 ) f (x0 ) + O(h 2 ) = f (x0 ) + x(t0 ) + O(h 2 ), 2 2 2 h f (x0 ) 2
where x0 = x(t0 ). We can now multiply both sides by h (turning the O(h 2 ) term into O(h 3 )) and rearrange, yielding h2 h x + O(h 3 ) = h( f (x0 + f (x0 )) f (x0 ). 2 2 Substituting the right hand side into equation 1 gives the update formula x(t0 + h) = x(t0 ) + h( f (x0 + h f (x0 )). 2
This formula rst evaluates an Euler step, then performs a second derivative evaluation at the midpoint of the step, using the midpoint evaluation to update x. Hence the name midpoint method. The
B5
a b
Adaptive Stepsizes
Whatever the underlying method, a major problem lies in determing a good stepsize. Ideally, we want to choose h as large as possiblebut not so large as to give us an unreasonable amount of error, or worse still, to induce instability. If we choose a xed stepsize, we can only proceed as fast as the worst sections of x(t) will allow. What we would like to do is to vary h as we march
B6
forward in time. Whenever we can make h large without incurring too much error, we should do so. When h has to be reduced to avoid excessive error, we want to do that also. This is the idea of adaptive stepsizing: varying h over the course of solving the ODE. Here well be present adaptive stepsizing for Eulers method. The basic idea is as follows. Lets assume we have a given stepsize h, and we want to know how much we can consider changing it. Suppose we compute two estimates for x(t0 + h). We compute an estimate xa , by taking an Euler step of size h from t0 to t0 + h. We also compute an estimate xb by taking two Euler steps of size h/2, from t0 to t0 + h. Both xa and xb differ from the true value of x(t0 + h) by O(h 2 ). That means that xa and xb differ from each other by O(h 2 ). As a result, we can write that a measure of the current error e is e = |xa xb | This gives us a convenient estimate to the error in taking an Euler step of size h. Suppose that we are willing to have an error of as much as 104 per step, and that the current error is only 108 . Since the error goes up as h 2 , we can increase the stepsize to 104 108
1 2
h = 100h.
Conversely, if we currently had an error of 103 , and could only tolerate an error of 104 , we would have to decrease the stepsize to 104 103
1 2
h .316h.
Implementation
The ODEs we will want to solve may represent many thingsfor 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 difcult 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-specic code that operates on this structure to perform the standard operations, then to implement solvers in terms of these generic operations. From the solvers viewpoint, the system on which it operates is a black-box function f (x, t). The solver needs to be able to evaluate f , as required, at any values of x and t, and then to install the updated x and t 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 dim (x). Since x and x may be vectors, the solver must know their length, to allocate storage, perform vector arithmetic ops, etc. Get/set x and t. The solver must be able to install new values at the end of a step. In addition, a multi-step method must set x and t to intermediate values in the course of performing derivative evaulations.
B7
Evaluate f at the current x and t. In an object-oriented language, these operations would naturally be implemented as generic functions that are handled in a type-specic way. In a non-object-oriented language generic functions would be faked by installing pointers to type-specic 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 specic models such as particle-and-spring systems.
References
[1] W.H. Press, B.P. Flannery, S. A. Teukolsky, and W. T. Vetterling. Numerical Recipes in C. Cambridge University Press, Cambridge, England, 1988.
B8