This involves solving ODEs (ordinary differential equations) that are considered to be ‘stiff’. We will see why we qualify such ODEs as ‘stiff’ and how we should approach solving for these types of ODEs by using implicit numerical methods such as the Backwards Euler implicit method.
⚠️ Pixar’s resource advises us to avoid an stiff ODE by reformulating our dynamic problem. We’ll be focusing on the cases in which this is not possible.
Stiff EDOs
After having scoured the Internet for a definition of the term ‘stiff ordinary differential equation’, I ended up on Wikipedia’s entry which, admittedly for me, is not very sufficient but I will give it to you anyway:
A stiff differential equation is a differential equation whose sensitivity to parameters will make it difficult to solve by explicit numerical methods.
Definition aside, I find that Pixar Physics Simulation Animation Course gives us a fun example that we will work on to better visualize the Wikipedia definition… Let’s imagine that we want to simulate the dynamics of a particle whose change of position is described by the following differential equation:
X˙(t)=dtd(x(t)y(t))=(−x(t)−ky(t))
where k is a positive and large constant. If k is large enough, the particle will never stray too far from y(t)=0 since −ky(t) will constrain y(t) back to the origin. We also see that the x position of the particle is freer to move around, as described by the absence of this large and positive constant k.
In this example, this ODE would therefore be ‘stiff’ if the variable k is so large that the particle stays close to the line y=0. As we will see in the next part, explicit methods will have a hard time simulating this expected behavior.
The problem with explicit methods
Recall that the equation of the explicit Euler method is described as follows:
Xnew=X0+hX˙(t0)
and in this example, X0=(x0y0) and X˙(t)=dtd(x(t0)y(t0))=(−x0−ky0). Then, after the substitution, we have:
Xnew=(x0y0)+h(−x0−ky0)
Which produces the following:
Xnew=((1−h)x0(1−hk)y0)
We learned in Part 1 of Numerical Methods that in order to find a solution, we must take small steps in time to approach the solution as massive steps could very well make the system unstable. A quick rearrangement of terms will show us that these conditions must be met in order for the position of the particle described in our example to converge to (0,0):
xstep:∣1−h∣<1ystep:∣1−kh∣<1
🚨 If we take k equal to 1000: the maximum permitted step is calculated to be 1−1000h=−1=>h=0.002. If we surpass this limit, the particle’s y position will never be able to converge towards 0!
Solving for y by plugging in parameters h = 0.0015 and k = 1000.0into a forward Euler solver that I wrote, we can visualize the particle’s y position as it oscillates around zero:
Now, let’s take larger steps in time, where h > 0.002 (h = 0.003 and k = 1000.0). Notice how the solution diverges from 0:
OK, so we could use a very small step like h = 0.0015, right? Maybe… however, we would quickly realize that the x position converges to 0 very slowly, which will make our simulation slow, and expensive!
Can we do better? Could we use a more stable method to converge faster? Yes - with the Backwards Euler implicit method!
Backwards Euler (implicit)
Backwards Euler is the easiest of the implicit methods to understand and that’s why I’m going to focus on it.
Here is the equation:
Xnew=X0+hX˙new
If you had been an observant person, you would have seen that the difference between the implicit Euler equation and the explicit Euler equation is that the change in X is defined as hX˙∗new in the implicit Euler equation. We must therefore solve this change by calculating X˙∗new… to finally calculate X_new.
In fact, it’s like walking backwards.
X0=Xnew−hX˙new
In other words, it’s as if we were at position X_new and wanted to move backwards to get to position X0… Hence the name: ‘Backwards Euler’.
It’s a little awkward to solve X∗new, since we need to find X˙∗new… which is also unknown. Let’s see if we can rewrite this into something we can solve.
We start by providing the equation of the Backward Euler method:
X0+ΔX=X0+hf(X0+ΔX)
where ΔX=X_new−X0. We can write it as follows:
ΔX=hf(X0+ΔX)
💡 f(X∗0+ΔX) is unknown, and also ΔX (because ΔX=X∗new−X∗0 contains X∗new which is unknown). So we have to approximate f(X0+ΔX) using the Taylor series as follows:
f(X0+ΔX)=f(X0)+ΔXf′(X0)
See this resource to learn more about the Taylor Series and this exact derivation.
Using the last equation, we can rewrite our equation for ΔX
ΔX=h(f(X0)+ΔXf′(X0))h1ΔX−ΔXf′(X0))=f(X0)
💡 To continue, remember that f(X0) is a vector and f′(X0) is a matrix. This is the case thanks to vector calculus:
So X∗new=X0+−(X0)=(00) as _h* approaches infinity!
Now we can use the program I wrote to see this example. after navigating to the folder where you downloaded the code, run the following:
make
./out 0.2 1000.0 100.0 100.0
where h=0.2, k=1000.0, x0=100.0 and **y0 **=100.0. You should see two files implicit.txt and explicit.txt added to the folder. In implicit.txt we see that y converges to zero!
Comments for Numerical Methods (Backwards Euler) - Part 3
Written by Carmen Cincotti, computer graphics enthusiast, language learner, and improv actor currently living in San Francisco, CA. Follow @CarmenCincotti