# The Leapfrog Method

Last time we saw that the naive implementation of the finite difference approximation is unstable. We maintain that the central difference approximation yields better results than the forward difference approximation at the cost of additional memory.

The central difference approximates the time derivative using the difference between the previous and the next timestep.

$$\frac{\partial}{\partial t}\Psi \left(x,t\right)\approx \frac{\Psi \left(x,t+\Delta t\right)-\Psi \left(x,t-\Delta t\right)}{2\Delta t}$$We can reuse the same logic we applied to the Schrödinger equation the first time, to lead us to the an implementation of this modified approach. This typifies how understanding general principles and concepts is far more valuable than focusing on specific recipes. While some of the details are different, the concepts and thought processes that we employ now are very much the same as the first time. Also seeing the process again, with a slightly different application, will strengthen your understanding.

Substituting this approximation into the Schrödinger equation and isolating the $t+\Delta t$ term on the left gives our guiding equation for evolving the wave function forward in time.

$$i\Psi \left(x,t+\Delta t\right)\approx i\Psi \left(x,t-\Delta t\right)-\frac{{\partial}^{2}}{\partial {x}^{2}}\Psi \left(x,t\right)\Delta t+V\left(x\right)\Psi \left(x,t\right)2\Delta t$$Remember that the terms on the right all became texture reads in the code. This equation shows that we will now need to read, and therefore keep, wavefunction values for $t-\Delta t$ as well as $t$ . The wave function for $t+\Delta t$ becomes the wavefunction from $t-\Delta t$ plus adjustments from $t$ . This gives raise to the name of the method, leapfrog.

Once again we have to cast the equation in a form that can be manipulated in JavaScript and GLSL shaders, which lack an explicit complex data type. Start by breaking the wave function into real and imaginary parts.

$$\Psi \left(x,t\right)=Re\left(\Psi \left(x,t\right)\right)+iIm\left(\Psi \left(x,t\right)\right)$$We put this into the Schrödinger equation, then gather the real and imaginary parts into their own equations.

$$\frac{\partial}{\partial t}Re\left(\Psi \left(x,t\right)\right)=-\frac{1}{2}\frac{{\partial}^{2}}{\partial {x}^{2}}Im\left(\Psi \left(x,t\right)\right)+V\left(x\right)Im\left(\Psi \left(x,t\right)\right)$$ $$\frac{\partial}{\partial t}Im\left(\Psi \left(x,t\right)\right)=\frac{1}{2}\frac{{\partial}^{2}}{\partial {x}^{2}}Re\left(\Psi \left(x,t\right)\right)-V\left(x\right)Re\left(\Psi \left(x,t\right)\right)$$We reuse our earlier substitution for $\frac{{\partial}^{2}}{\partial {x}^{2}}\Psi \left(x,t\right)$ , which the clever reader will note uses elements of the wave function from $x+\Delta x$ and $x-\Delta x$ , and is already a central difference expression.

$$\frac{{\partial}^{2}}{\partial {x}^{2}}\Psi \left(x,t\right)\approx \frac{\Psi \left(x+\Delta x,t\right)-2\Psi \left(x,t\right)+\Psi \left(x-\Delta x,t\right)}{{\left(\Delta x\right)}^{2}}$$Now we have approximations of both the time and space derivatives in terms of values on our grid. We use these in the Schrödinger and write out equations for the evolution of the real and imaginary parts of the wave function.

For the real part of the wave function we have

$$Re\left(\Psi \left(x,t+\Delta t\right)\right)\approx Re\left(\Psi \left(x,t-\Delta t\right)\right)-\frac{Im\left(\Psi \left(x+\Delta x,t\right)\right)-2Im\left(\Psi \left(x,t\right)\right)+Im\left(\Psi \left(x-\Delta x,t\right)\right)}{{\left(\Delta x\right)}^{2}}\Delta t+2V\left(x\right)Im\left(\Psi \left(x,t\right)\right)\Delta t$$and to evolve the imaginary component of the wave function in time

$$Im\left(\Psi \left(x,t+\Delta t\right)\right)\approx Im\left(\Psi \left(x,t-\Delta t\right)\right)+\frac{Re\left(\Psi \left(x+\Delta x,t\right)\right)-2Re\left(\Psi \left(x,t\right)\right)+Re\left(\Psi \left(x-\Delta x,t\right)\right)}{{\left(\Delta x\right)}^{2}}\Delta t-2V\left(x\right)Re\left(\Psi \left(x,t\right)\right)\Delta t$$```
```