# Truncation Error Analysis via sympy¶

MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
In [42]:
import sympy as s
s.init_printing()


Establish some variables that we'll need:

In [43]:
u = s.Function("u")
a, x, t, h_x, h_t = s.symbols("a, x, t, h_x, h_t")
xi_1, xi_2, tau = s.symbols("xi1, xi2, tau")


taylor is a utility function that spits out a taylor expansion for $f(x+h)$, optionally including a remainder term, with all variables under our control.

In [44]:
def taylor(f, x, h, n, remainder_variable=None):
result = sum(f.diff(x, i)*h**i/s.factorial(i) for i in range(n))
if remainder_variable:
result += f.diff(x, n).subs(x, remainder_variable)*h**n/s.factorial(n)
return result

• Try it out by expanding $u(x+h_x,t)$
• Vary the order
• Expand $u(x,t+h_t)$ instead
In [48]:
taylor(u(x,t), x, h_x, 3, xi)

Out[48]:
$\displaystyle \frac{h_{x}^{3} \frac{\partial^{3}}{\partial \xi^{3}} u{\left(\xi,t \right)}}{6} + \frac{h_{x}^{2} \frac{\partial^{2}}{\partial x^{2}} u{\left(x,t \right)}}{2} + h_{x} \frac{\partial}{\partial x} u{\left(x,t \right)} + u{\left(x,t \right)}$

Assign the PDE we're solving to pde:

In [49]:
pde = u(x, t).diff(t) + a * u(x, t).diff(x)
pde

Out[49]:
$\displaystyle a \frac{\partial}{\partial x} u{\left(x,t \right)} + \frac{\partial}{\partial t} u{\left(x,t \right)}$

Write out the scheme we're analyzing, in this case ETCS:

In [37]:
etcs = (
(u(x, t+h_t) - u(x, t))/h_t
+
a*(u(x+h_x, t) - u(x-h_x, t))/(2*h_x))
etcs

Out[37]:
$\displaystyle \frac{a \left(- u{\left(- h_{x} + x,t \right)} + u{\left(h_{x} + x,t \right)}\right)}{2 h_{x}} + \frac{- u{\left(x,t \right)} + u{\left(x,h_{t} + t \right)}}{h_{t}}$

etcs
.subs(u(x, t+h_t), taylor(u(x,t), t, h_t, 2, tau))

to arrive at the truncation error.

⚠️ Make sure to keep the two $x$ remainder terms separate.

In [50]:
etcs_taylor = (
etcs
.subs(u(x, t+h_t), taylor(u(x,t), t, h_t, 2, tau))
.subs(u(x+h_x, t), taylor(u(x,t), x, h_x, 3, xi_1))
.subs(u(x-h_x, t), taylor(u(x,t), x, -h_x, 3, xi_2))
)
sp.simplify(etcs_taylor - pde)

Out[50]:
$\displaystyle \frac{a h_{x}^{2} \frac{\partial^{3}}{\partial \xi_{1}^{3}} u{\left(\xi_{1},t \right)}}{12} + \frac{a h_{x}^{2} \frac{\partial^{3}}{\partial \xi_{2}^{3}} u{\left(\xi_{2},t \right)}}{12} + \frac{h_{t} \frac{\partial^{2}}{\partial \tau^{2}} u{\left(x,\tau \right)}}{2}$
In [ ]: