✬ ✫ ✩ ✪ yn+1= yn+ h 2(k1+ k2) , where k1= f (tn, yn), k2= f (tn+ h, yn+ hk1).
4.3
Computer implementation using Maple and MATLAB
Most good ODE solver packages use sophisticated methods of controlling errors, and being efficient most of the time they have widespread acceptance. Many schemes choose their own step sizes in order to optimise the results, and some vary this step size on an interval in order to provide an optimal result.
Note, once again, that there is a trade-off between decreasing the step size and increasing the round-off error, due to an increased number of computations. Another problem is that of instability over long time periods: sometimes, while providing an accurate approximation over an initial period of time, the solution may ‘become’ very inaccurate at later times.
Using Maple or MATLAB, the method of integration can be stipulated and the results compared. Maple or MATLAB also allow you to change and choose the initial step size of the default numerical scheme that, as we see below, is sometimes required for adequate resolution. Note, however, that with the standard DE solvers ode45 for MATLAB and DEplot for Maple, adaptive stepping is used. This means the step size is varied, becoming smaller when the function is changing fast, so as to maintain a specified error, and where the step size increases when the function is changing slowly, so generally, specifying the step size only really is useful if the initial default step size happens to be too large. In practice, the default values for MATLAB appear to be adequate for most problems, but for Maple, setting the stepsize parameter to 0.1, for example, is sometimes required, as discussed below.
In Chapter 2, in the discussion on finding a computer solution to a radioactive element decay cascade, we mentioned that it would be difficult to find a suitable numerical scheme because of the vastly different half-lives. (For Uranium-238, the half-life is billions of years, whereas for the next element in its decay series, Thorium-234, the half-life is in days.) This problem requires large time steps to resolve the Uranium decay process and (comparatively) very small time steps to resolve the Thorium decay: such a problem is known as a stiff problem. This discrepancy between rates can be seen clearly in the following example. Suppose a solution to some differential equation X′= f (x) is
X(t) = e−t+ e−1000t, t ≥ 0.
Clearly, the second component decays at a much faster rate than the first. When t is small, the value of X(t) is dominated by e−1000t, and small step sizes are required for
resolution of this behaviour. Alternatively, for t away from 0, the solution is dominated by e−t and large step sizes (in comparison) can be used for high accuracy. Attempting to
solve a stiff problem with a standard adaptive time-stepping method, such as the default in DEplotof Maple or ode45 in MATLAB, will result in the method taking smaller and smaller time steps, and usually being unable to complete the calculation. For some software this
may result in a prompt of an error message, triggered when a specified maximum number of function evaluations has been reached.
Alternative numerical methods, involving implicit solution methods, have been developed to deal specifically with stiff problems, but typically they increase the computation time incurred when the problem is not stiff. Thus, they are usually employed only once a problem is known to be stiff. Maple provides a stiff solver that can be used for the solution of such problems by stipulating the numerical method (always after numeric) in dsolve. Applying this to the example of lake pollution (Section 2.5), which is not a stiff problem, it is easy to establish a substantial increase in computation time. The code below gives an example of one way to specify a stiff solver in Maple.
> restart:with(plots): > cin:=3;V:=28;F:=4*12;threshold:=4;init_c:=10; > de1:=diff(c(t),t)=(F/V)*(cin-c(t)); > soln:=c0->dsolve({de1,c(0)=c0},c(t),numeric,method=lsode): > plot1:=c0->odeplot(soln(c0),[t,c(t)],0..8): > list1:=seq(plot1(i/2),i=1..12): > line1:=plot([[0,threshold],[8,threshold]]): > display({list1,line1});
With MATLAB, to use a stiff solver, simply replace the call to ode45 with one to ode15s. MATLABprovides a few different stiff solvers, of different accuracy, but the ode15s is a good first method of choice.
A problem that frequently arises when using Maple to sketch solutions in a plane is a very odd-shaped or angular ‘scribble’, rather than a smoothly varying solution. By now, if you have experimented with Maple, you will no doubt have encountered this problem in some implementation. Consider a pair of differential equations in time, de1=dX/dt and de2=dY /dt. Then the following code fragment below, without the step size parameter stipulated or set too large, could produce the ‘mess’ in the first diagram of Figure 4.3, while, with the appropriate step size, we obtain the second diagram. When the step size is specified, the problem ‘rights’ itself as displayed in the second diagram of Figure 4.3.
plot1:=DEplot([de1,de2],[X,Y],t=0..80,{inits},X=0..Xlimit,Y=0..Ylimit, scene=[P,H],linecolor=black,stepsize=0.1,arrows=none): display(plot1); 1 2 3 4 5 2 4 6 8 10 12 14 time t X (t ) 1 2 3 4 5 2 4 6 8 10 12 14 time t X (t )
Figure 4.3: Solution of a differential equation for different initial conditions. In the diagram on the left a large step size was used, and instability of the numerical scheme prevents convergence. In the diagram on the right the step size of the solver has been reduced.
4.4 Instability 91