Stiff equations and implicit steps
The question. A tendon’s force settles in a millisecond and a forearm rises in a second. How can a stepping rule take steps sized for the forearm without being wrecked by the tendon?
Stage 2 ended on an awkward fact: the resting muscle, where nothing happens, was the hardest thing to simulate. Its equations are stiff, and a rule that works only from rates it already knows must creep through the whole run in steps short enough for the fastest thing in the model. This chapter explains the other kind of rule, which is stable at any step, what each of its steps costs, and how the simulator’s version chooses its own step.
By the end you will be able to step a fast decay by hand with an implicit rule, say what the simulator’s implicit rule does inside one step, and judge from a model’s stiffness and the accuracy you need which kind of rule will be cheaper.
One fast thing and one slow thing
Recall the argument of Stage 2. It named two kinds of stepping rule: an explicit rule works out the next state from rates it already knows, and an implicit rule has the rates at the end of the step in it, so that every step means solving an equation. A small error in the tendon force dies away at a rate times itself, with measured per second. The simplest explicit rule multiplies that error by in every step of length , in seconds, so it is safe only while . For the resting biceps is 1500 per second, and the step may not exceed 1.3 ms, although the forearm it is attached to could be followed well with steps many times as long.
To study this without a muscle in the way, keep only the fast thing. The test equation is the simplest equation with one rate in it:
In words: the quantity shrinks at a rate times its own size. Here stands for anything that settles, such as the mismatch between tendon force and fibre force, and is its rate of change with time . The exact solution is : after a time the quantity has fallen to 37 % of its starting value, after three such times to 5 %.
Over one step of length the exact solution is multiplied by , a number between 0 and 1. A stepping rule multiplies it by a number of its own. The amplification factor of a rule is the number by which one of its steps multiplies the solution of the test equation. Everything in this chapter can be read off that one number, and it depends on the step and the rate only through their product .
Why every explicit rule has a limit
For explicit Euler the new value is the old value plus the step times the rate at the start:
In words: explicit Euler’s amplification factor is . It equals when , and beyond that the computed value grows at every step while the true value shrinks (Fjordholm, section 3.1).
A better explicit rule moves the limit and cannot remove it. One step of Runge–Kutta 4 on the test equation multiplies by
which is listed among the proved properties. This factor stays within up to and then climbs without limit, because its last term grows as the fourth power of . The same happens to every explicit rule: its factor is built from sums and products of , and such an expression always grows in the end (Fjordholm, section 4.4).
Using the slope at the end of the step
Explicit Euler follows the slope it finds at the start of the step. Backward Euler follows the slope at the end of the step instead:
where is the rate of change of . In words: the new value is the old value plus the step times the rate at the new value (Fjordholm, equation 2.2). The new value appears on both sides, so this is an equation to be solved and not a formula to be evaluated. For the test equation the rate is , and ordinary algebra solves it:
In words: backward Euler’s amplification factor is 1 divided by . For every step, however long, that is a number between 0 and 1. The computed value can neither grow nor change sign, and the faster the decay or the longer the step, the more completely it is removed, which is what the true solution does (Fjordholm, example 2.1 and section 3.1).
The picture behind the algebra is this. Explicit Euler stands at the start, where the slope is steep, and slides down it for the whole step, far past zero. Backward Euler asks for the end point whose own slope, followed backward for the whole step, leads to the start. From a point beyond zero the slope leads back to a start that is also beyond zero, so such a point can never be the answer.
A worked example
Take per second, so that falls to 37 % in 10 ms, and a step of 25 ms, so that . Start from .
| Exact, per step | Explicit Euler, | Backward Euler, | |
|---|---|---|---|
| Start | 1 | 1 | 1 |
| After step 1 | 0.0821 | −1.5 | 0.2857 |
| After step 2 | 0.0067 | 2.25 | 0.0816 |
| After step 3 | 0.00055 | −3.375 | 0.0233 |
Explicit Euler is past its limit of ms and runs away, changing sign at every step. Backward Euler decays. It is also plainly inaccurate: after one step it says 0.29 where the truth is 0.08. Being stable and being accurate are different things. Backward Euler is a first-order rule like explicit Euler, and at a step this long it only promises not to blow up.
The price: an equation in every step
For the test equation, algebra turned backward Euler into a formula. For a muscle, is the whole chain of Stage 2, from tendon force to fibre speed to tendon stretch, and no algebra will bring out on its own. The equation has to be solved by trying values, and the method for that is Newton’s method, met in Finding the shape that balances: replace the curved equation by its tangent at the present guess, solve the tangent’s equation exactly, and take the answer as the next guess.
Write the mismatch of a guess as , which is zero at the answer. One Newton step moves the guess by
In words: the correction is the mismatch divided by the slope of the mismatch, and that slope is 1 minus the step times the rate at which itself changes with its argument. Try it on the worked example, where and s. Guess that nothing changes, . The mismatch is and its slope is , so and the new guess is 0.2857, the answer in the table. One step was enough because this is a straight line. A curved needs a few.
With many states the same thing happens in a table. The Jacobian is the table whose entry in row and column is the rate at which the rate of change of state changes when state is nudged (Driscoll and Braun, section 4.5). The slope of the mismatch becomes the matrix , where is the identity matrix, and the division becomes the solving of a set of equations, one for each state:
So one implicit step costs, at the least, the making of a Jacobian, the solving of a set of equations, and a handful of extra evaluations of the model. The simulator makes its Jacobian by nudging each state in turn and watching the rates change, in the way of Rates of change, which takes one evaluation of the model for every state. An implicit step is therefore many times dearer than an explicit one (Driscoll and Braun, section 6.7). It earns its keep only when it lets the step be many times longer.
The simulator’s rule: TR-BDF2
Backward Euler has two faults: every step needs an equation solved, and its error only halves when the step is halved. The first cannot be avoided. The second can. The simulator’s implicit rule is TR-BDF2, a rule of second order that takes each step in two stages (Hosea and Shampine 1996, who analyse the method that Bank and others introduced for simulating electronic circuits).
Stage one goes part of the way, from the time at which the step starts to the time with , using the average of the slope at the start and the slope at the point it reaches:
In words: move for a time at the average of two slopes, the known one at the start and the unknown one at the stopping point. Averaging the slopes at the two ends of an interval is the trapezoidal rule, the TR of the name.
Stage two goes to the end of the step with a weighted average of three slopes:
In words: move for the whole time at a blend of the slope at the start, the slope at the intermediate point and the unknown slope at the end. The three weights add up to one: . Hosea and Shampine describe this stage as a step of the second-order backward differentiation formula, which is the BDF2 of the name, and write the whole rule with these weights (Hosea and Shampine 1996, sections 1 and 2).
Each stage has the shape of backward Euler: an unknown value equals something known plus times the slope at the unknown value. Each is solved by Newton’s method as above, with the matrix . The number is chosen so that the same appears in both stages. Then one matrix serves both, and it has to be prepared only once in a step (Hosea and Shampine 1996, section 2).
Its amplification factor
Put the test equation, , into stage one and collect on the left:
Do the same with stage two:
Replace by the expression just found and put the right side over :
Multiplied out, the top line has the two terms and , which cancel. With , one step of TR-BDF2 multiplies by
In words: the top grows in proportion to and the bottom as its square (Hosea and Shampine 1996, section 6). Three things follow.
- Fast things are removed. When is very large the 1s no longer matter and the factor is close to . At it is , at it is . The stiffer the part, the less of it survives a step, and in the limit nothing does.
- Nothing ever grows. The factor is 1 at , falls to zero at , is most negative, , at , and then creeps back toward zero. Its size never exceeds 1. Both this and the limit above are among the proved properties of the simulator’s equations.
- Slow things are followed closely. At the exact factor is 0.904837. TR-BDF2 gives 0.904800, backward Euler 0.909091 and explicit Euler 0.9. The two Euler rules are wrong in the third decimal place and TR-BDF2 in the fifth.
Stage one alone would not do. Carried over a whole step, with in place of and so in place of , the trapezoidal rule has the factor , by the algebra of stage one. That never exceeds 1 in size either, but for large it tends to : a fast error is kept at full size and flipped at every step, for ever. The second stage is what makes fast errors die.
For the worked example, , the factor is . After one step TR-BDF2 holds where the truth is 0.0821: not accurate, since the step is far too long to follow a decay that is over in a fifth of it, but small, and after a second step 0.00014.
Choosing the step
A rule that is stable at any step still has to decide how long its steps should be. The simulator’s rules decide for themselves. An adaptive rule is one that estimates the error of each step it takes and uses the estimate to accept or repeat the step and to choose the length of the next.
TR-BDF2 gets its estimate almost for nothing. The three slopes of a step, blended with the weights , , , give the second-order answer above. Blended with other weights, 0.215, 0.687 and 0.098, the same three slopes give a third-order answer. The difference between the two answers is an estimate of the error of the step (Hosea and Shampine 1996, sections 2 and 6). The same authors found that this estimate grossly overstates the error of very stiff parts, and that the cure is to pass it once more through the matrix solve the step has already prepared; the simulator does so.
The tolerance is the error a rule is allowed in one step. In the simulator it is given per state as an absolute amount plus a fraction of the state’s size. The estimate is divided by it, state by state, and the results combined into one number : below 1 the step is good enough, above 1 it is not. Then
kept between a fifth of and five times . In words: if the error was a thousandth of what is allowed, the next step can be ten times longer, less a tenth for safety, and the limit of five then applies; if it was eight times too large, the step is repeated at just under half the length. The cube root is there because the estimate shrinks as the cube of the step, which is also proved for the test equation.
The simulator’s explicit rule is adaptive in the same way. It reads the rates seven times in a step and blends them into a fifth-order answer and a fourth-order one, whose difference is its estimate; the pair of formulas is that of Dormand and Prince 1980. Its amplification factor is of the kind that grows in the end, so stiffness limits its step whatever tolerance is asked.
Try it: one fast decay
The lab steps the test equation with per second. Each press of Step takes one step. The second plot shows the amplification factor of each rule against .
- Check the worked example. With explicit Euler and 25 ms, press Step three times: −1.5, 2.25, −3.375. Press Play: after twelve steps the value has passed 100 and the run stops.
- The slope at the end. Choose backward Euler and step three times: 0.286, 0.082, 0.023, against the exact 0.082, 0.007, 0.001.
- Predict, then check. Choose TR-BDF2. What will the first step give? −0.012: small, and on the wrong side of zero.
- Find the limit. Go back to explicit Euler. At 19 ms the value flips sign at every step and still dies away, by a factor −0.9. At 21 ms the factor is −1.1 and it grows. On the second plot the explicit line crosses −1 at .
- A very long step. Set the step to 200 ms, twenty times the time the decay takes. Backward Euler multiplies by and TR-BDF2 by −0.155. Neither grows.
- A short step. Set the step to 1 ms and take ten steps with each rule. The exact value at 10 ms is 0.3679. Explicit Euler gives 0.3487, backward Euler 0.3855 and TR-BDF2 0.3677.
A real case: lifting two kilograms
The test equation has only a fast thing in it. A muscle model has fast and slow together, and the question is what each rule then costs. Take the forearm of a six-muscle arm lifting a 2 kg load for half a second, with the three elbow flexors driven and the three extensors at rest. The resting extensors make the equations stiff, as in Stage 2. Both of the simulator’s adaptive rules were run on it with the same tolerance, one part in a million, and their answers compared with the same lift computed by another simulator, OpenSim, which ends at an elbow angle of 95.72°.
| Steps | Evaluations of the model | Elbow angle differs from OpenSim by | |
|---|---|---|---|
| Explicit rule | 1162 | 7316 | 0.000 003° |
| Implicit rule | 117 | 2642 | 0.003° |
The implicit rule takes a tenth of the steps. Each of its steps costs about 22 evaluations against 6, most of them spent on the Jacobian of the model’s 14 states, so it finishes with about a third of the work.
Look at the last column before calling that a win. At the same tolerance the explicit rule’s answer is a thousand times closer than the implicit rule’s, because its step was set by stability and not by the tolerance. Ask both rules for a hundred times less error and the explicit rule needs 1183 steps, hardly more than before, while the implicit rule needs 534 steps and 11 218 evaluations, more than the explicit rule’s 7226.
Make every tendon about five times as stiff, with a strain of 1 % at maximum isometric force in place of 4.9 %, and the balance tips the other way: at the first tolerance the explicit rule needs 5680 steps and 35 646 evaluations, the implicit rule 115 steps and 2645.
So the choice depends on two things, how stiff the model is and how accurate the answer must be. The implicit rule wins when the model is very stiff or a modest accuracy is enough. The explicit rule wins when the stiffness is mild and many digits are wanted.
Try it: a stiffer tendon
The lab repeats the lift of Stage 2 at an excitation of 0.5: one muscle, the forearm and 2 kg in the hand. Both rules compute the same second of motion. The plots show how many steps each has taken and how long each step was.
- The model’s own tendon. Press Play. With a tendon that stretches 4.9 % and a muscle that is switched on, the equations are barely stiff. The explicit rule finishes in 37 steps and 260 evaluations, the implicit rule in 42 steps and 625 evaluations. Here the explicit rule is the cheaper.
- Predict, then check. Make the tendon stiffer, 1 %. Which count will change? The explicit rule now needs 144 steps and the implicit rule 39.
- Stiffer still. At 0.2 % the explicit rule needs 722 steps and 4395 evaluations; the implicit rule 40 steps and 593 evaluations. On the third plot the explicit rule’s steps never grow beyond about 2 ms, while the implicit rule’s reach 76 ms.
- Ask for more accuracy. Keep 0.2 % and allow one part in a million. The explicit rule’s count hardly moves, from 722 steps to about 730: stability was already holding its step down. The implicit rule’s count rises from 40 to 164.
- Compare the answers. The last readout is the difference between the two rules’ elbow angles after one second: 0.045° at the looser tolerance and 0.003° at the tighter.
What this model leaves out
- Deformable tissue. A muscle cut into bricks has thousands of states, and nudging each in turn to build a Jacobian would cost thousands of evaluations a step. Its balance is found by the methods of Finding the shape that balances, and the linear equations inside each of its Newton steps are the subject of the next chapter.
- Saving work between steps. The simulator builds a new Jacobian and prepares a new matrix in every step. A rule can keep both for several steps and renew them only when Newton’s method starts to struggle (Hosea and Shampine 1996, section 7, describe a code that does). For the arm built of tissue bodies, solved without the tissue’s mass, this was tried and lost: keeping each factorisation for up to three steps made twelve steps of a curl cost 1.21 times as much, because near the balance the old factor brought the forces down too little for its steps to pay.
- Fast motion that should be kept. TR-BDF2 removes whatever is fast, and that includes a fast swing that ought to keep swinging. When energy must be kept over a long run, the semi-implicit rule of Stepping through time is the one to use. The tissue’s own rules, backward Euler, the trapezoidal rule and the Bossak rule that the simulator’s arm uses, which removes only what its step cannot follow, are in When tissue’s own inertia matters; how backward Euler’s own loss adds to the tissue’s damping is in Tissue that loses energy.
- Tissue and skeleton in one step. How a rigid skeleton and deformable tissue are advanced together is the subject of Tissue and skeleton together.
Summary
- A rule’s amplification factor is what one step multiplies a decaying quantity by. For explicit Euler it is , which passes −1 at ; every explicit rule has such a limit.
- Backward Euler uses the slope at the end of the step. Its factor, , lies between 0 and 1 for every step, but each step means solving an equation, by Newton’s method with a Jacobian.
- The simulator’s implicit rule, TR-BDF2, takes two such stages with one shared matrix. It is second order, its factor never exceeds 1 in size, and it tends to zero for infinitely stiff parts.
- Both of the simulator’s rules estimate their own error and set their own step. On a stiff model the explicit rule’s step is held down by stability whatever accuracy is asked.
- An implicit step costs several explicit ones, so the implicit rule pays when the model is very stiff or modest accuracy is enough.
Next: each Newton step for deformable tissue needs thousands of linear equations solved together: solving many equations at once.