# Finite difference operator notation
<div id="sec:form:fdop"></div>

<!-- Equation labels as ordinary links -->
<div id="Dop:fd1:center"></div>

$$
\begin{equation}
u'(t_n) \approx
\lbrack D_tu\rbrack^n = \frac{u^{n+\frac{1}{2}} - u^{n-\frac{1}{2}}}{\Delta t} \label{Dop:fd1:center} \tag{1}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="Dop:fd1:center2"></div>

$$
\begin{equation} 
u'(t_n) \approx
\lbrack D_{2t}u\rbrack^n = \frac{u^{n+1} - u^{n-1}}{2\Delta t}
\label{Dop:fd1:center2} \tag{2}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="Dop:fd1:bw"></div>

$$
\begin{equation} 
u'(t_n) =
\lbrack D_t^-u\rbrack^n = \frac{u^{n} - u^{n-1}}{\Delta t}
\label{Dop:fd1:bw} \tag{3}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="Dop:fd1:fw"></div>

$$
\begin{equation} 
u'(t_n) \approx
\lbrack D_t^+u\rbrack^n = \frac{u^{n+1} - u^{n}}{\Delta t}
\label{Dop:fd1:fw} \tag{4}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="Dop:fd1:theta"></div>

$$
\begin{equation} 
u'(t_{n+\theta}) =
\lbrack \bar D_tu\rbrack^{n+\theta} = \frac{u^{n+1} - u^{n}}{\Delta t}
\label{Dop:fd1:theta} \tag{5}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="Dop:fd1:bw2"></div>

$$
\begin{equation} 
u'(t_n) \approx
\lbrack D_t^{2-}u\rbrack^n = \frac{3u^{n} - 4u^{n-1} + u^{n-2}}{2\Delta t}
\label{Dop:fd1:bw2} \tag{6}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="Dop:fd2:center"></div>

$$
\begin{equation} 
u''(t_n) \approx
\lbrack D_tD_t u\rbrack^n = \frac{u^{n+1} - 2u^{n} + u^{n-1}}{\Delta t^2}
\label{Dop:fd2:center} \tag{7}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="Dop:avg:arith"></div>

$$
\begin{equation} 
u(t_{n+\frac{1}{2}}) \approx \lbrack \overline{u}^{t}\rbrack^{n+\frac{1}{2}}
= \frac{1}{2}(u^{n+1} + u^n)
\label{Dop:avg:arith} \tag{8}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="Dop:avg:geom"></div>

$$
\begin{equation} 
u(t_{n+\frac{1}{2}})^2 \approx \lbrack \overline{u^2}^{t,g}\rbrack^{n+\frac{1}{2}}
= u^{n+1}u^n
\label{Dop:avg:geom} \tag{9}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="Dop:avg:harm"></div>

$$
\begin{equation} 
u(t_{n+\frac{1}{2}}) \approx \lbrack \overline{u}^{t,h}\rbrack^{n+\frac{1}{2}}
= \frac{2}{\frac{1}{u^{n+1}} + \frac{1}{u^n}}
\label{Dop:avg:harm} \tag{10}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="Dop:avg:theta"></div>

$$
\begin{equation} 
u(t_{n+\theta}) \approx \lbrack \overline{u}^{t,\theta}\rbrack^{n+\theta}
= \theta u^{n+1} + (1-\theta)u^n ,
\label{Dop:avg:theta} \tag{11}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="_auto1"></div>

$$
\begin{equation} 
\qquad t_{n+\theta}=\theta t_{n+1} + (1-\theta)t_{n-1}
\label{_auto1} \tag{12}
\end{equation}
$$

Some may wonder why $\theta$ is absent on the right-hand side
of ([5](#Dop:fd1:theta)). The fraction is an approximation to the
derivative at the point $t_{n+\theta}=\theta t_{n+1} + (1-\theta) t_{n}$.

# Truncation errors of finite difference approximations
<div id="sec:form:truncerr"></div>

$$
u'(t_n) =
[D_tu]^n + R^n = \frac{u^{n+\frac{1}{2}} - u^{n-\frac{1}{2}}}{\Delta t} +R^n\nonumber,
$$

<!-- Equation labels as ordinary links -->
<div id="form:trunc:fd1:center"></div>

$$
\begin{equation} 
R^n = -\frac{1}{24}u'''(t_n)\Delta t^2 + {\cal O}(\Delta t^4)
\label{form:trunc:fd1:center} \tag{13}
\end{equation}
$$

$$
u'(t_n) =
[D_{2t}u]^n +R^n = \frac{u^{n+1} - u^{n-1}}{2\Delta t} +
R^n\nonumber,
$$

<!-- Equation labels as ordinary links -->
<div id="form:trunc:fd1:center2"></div>

$$
\begin{equation} 
R^n = -\frac{1}{6}u'''(t_n)\Delta t^2 + {\cal O}(\Delta t^4)
\label{form:trunc:fd1:center2} \tag{14}
\end{equation}
$$

$$
u'(t_n) =
[D_t^-u]^n +R^n = \frac{u^{n} - u^{n-1}}{\Delta t}
+R^n\nonumber,
$$

<!-- Equation labels as ordinary links -->
<div id="form:trunc:fd1:bw"></div>

$$
\begin{equation} 
R^n = -\frac{1}{2}u''(t_n)\Delta t + {\cal O}(\Delta t^2)
\label{form:trunc:fd1:bw} \tag{15}
\end{equation}
$$

$$
u'(t_n) =
[D_t^+u]^n +R^n = \frac{u^{n+1} - u^{n}}{\Delta t}
+R^n\nonumber,
$$

<!-- Equation labels as ordinary links -->
<div id="form:trunc:fd1:fw"></div>

$$
\begin{equation} 
R^n = \frac{1}{2}u''(t_n)\Delta t + {\cal O}(\Delta t^2)
\label{form:trunc:fd1:fw} \tag{16}
\end{equation}
$$

$$
u'(t_{n+\theta}) =
[\bar D_tu]^{n+\theta} +R^{n+\theta} = \frac{u^{n+1} - u^{n}}{\Delta t}
+R^{n+\theta}\nonumber,
$$

$$
R^{n+\theta} = -\frac{1}{2}(1-2\theta)u''(t_{n+\theta})\Delta t +
\frac{1}{6}((1 - \theta)^3 - \theta^3)u'''(t_{n+\theta})\Delta t^2 +
\nonumber
$$

<!-- Equation labels as ordinary links -->
<div id="form:trunc:fd1:theta"></div>

$$
\begin{equation} 
\quad {\cal O}(\Delta t^3)
\label{form:trunc:fd1:theta} \tag{17}
\end{equation}
$$

$$
u'(t_n) =
[D_t^{2-}u]^n +R^n = \frac{3u^{n} - 4u^{n-1} + u^{n-2}}{2\Delta t}
+R^n\nonumber,
$$

<!-- Equation labels as ordinary links -->
<div id="form:trunc:fd1:bw2"></div>

$$
\begin{equation} 
R^n = \frac{1}{3}u'''(t_n)\Delta t^2 + {\cal O}(\Delta t^3)
\label{form:trunc:fd1:bw2} \tag{18}
\end{equation}
$$

$$
u''(t_n) =
[D_tD_t u]^n +R^n = \frac{u^{n+1} - 2u^{n} + u^{n-1}}{\Delta t^2}
+R^n\nonumber,
$$

<!-- Equation labels as ordinary links -->
<div id="form:trunc:fd2:center"></div>

$$
\begin{equation} 
R^n = -\frac{1}{12}u''''(t_n)\Delta t^2 + {\cal O}(\Delta t^4)
\label{form:trunc:fd2:center} \tag{19}
\end{equation}
$$

$$
u(t_{n+\theta}) = [\overline{u}^{t,\theta}]^{n+\theta} +R^{n+\theta}
= \theta u^{n+1} + (1-\theta)u^n +R^{n+\theta},\nonumber
$$

<!-- Equation labels as ordinary links -->
<div id="form:trunc:avg:theta"></div>

$$
\begin{equation} 
R^{n+\theta} = -\frac{1}{2}u''(t_{n+\theta})\Delta t^2\theta (1-\theta) +
{\cal O}(\Delta t^3)
\thinspace .
\label{form:trunc:avg:theta} \tag{20}
\end{equation}
$$

# Finite differences of exponential functions
<div id="sec:form:fdexp"></div>

### Complex exponentials

Let $u^n = \exp{(i\omega n\Delta t)} = e^{i\omega t_n}$.

<!-- Equation labels as ordinary links -->
<div id="form:exp:fd2:center"></div>

$$
\begin{equation}
[D_tD_t u]^n = u^n \frac{2}{\Delta t}(\cos \omega\Delta t - 1) =
-\frac{4}{\Delta t}\sin^2\left(\frac{\omega\Delta t}{2}\right),
\label{form:exp:fd2:center} \tag{21}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:exp:fd1:fw"></div>

$$
\begin{equation} 
[D_t^+ u]^n = u^n\frac{1}{\Delta t}(\exp{(i\omega\Delta t)} - 1),
\label{form:exp:fd1:fw} \tag{22}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:exp:fd1:bw"></div>

$$
\begin{equation} 
[D_t^- u]^n = u^n\frac{1}{\Delta t}(1 - \exp{(-i\omega\Delta t)}),
\label{form:exp:fd1:bw} \tag{23}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:exp:fd1:center"></div>

$$
\begin{equation} 
[D_t u]^n = u^n\frac{2}{\Delta t}i\sin{\left(\frac{\omega\Delta t}{2}\right)},
\label{form:exp:fd1:center} \tag{24}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:exp:fd1c:center"></div>

$$
\begin{equation} 
[D_{2t} u]^n = u^n\frac{1}{\Delta t}i\sin{(\omega\Delta t)}
\label{form:exp:fd1c:center} \tag{25}
\thinspace .
\end{equation}
$$

### Real exponentials

Let $u^n = \exp{(\omega n\Delta t)} = e^{\omega t_n}$.

<!-- Equation labels as ordinary links -->
<div id="form:rexp:fd2:center"></div>

$$
\begin{equation}
[D_tD_t u]^n = u^n \frac{2}{\Delta t}(\cos \omega\Delta t - 1) =
-\frac{4}{\Delta t}\sin^2\left(\frac{\omega\Delta t}{2}\right),
\label{form:rexp:fd2:center} \tag{26}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:rexp:fd1:fw"></div>

$$
\begin{equation} 
[D_t^+ u]^n = u^n\frac{1}{\Delta t}(\exp{(i\omega\Delta t)} - 1),
\label{form:rexp:fd1:fw} \tag{27}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:rexp:fd1:bw"></div>

$$
\begin{equation} 
[D_t^- u]^n = u^n\frac{1}{\Delta t}(1 - \exp{(-i\omega\Delta t)}),
\label{form:rexp:fd1:bw} \tag{28}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:rexp:fd1:center"></div>

$$
\begin{equation} 
[D_t u]^n = u^n\frac{2}{\Delta t}i\sin{\left(\frac{\omega\Delta t}{2}\right)},
\label{form:rexp:fd1:center} \tag{29}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:rexp:fd1c:center"></div>

$$
\begin{equation} 
[D_{2t} u]^n = u^n\frac{1}{\Delta t}i\sin{(\omega\Delta t)}
\label{form:rexp:fd1c:center} \tag{30}
\thinspace .
\end{equation}
$$

# Finite differences of $t^n$
<div id="sec:form:fdtn"></div>

The following results are useful when checking if a polynomial term in a
solution fulfills the discrete equation for the numerical method.

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn:fw"></div>

$$
\begin{equation}
\lbrack D_t^+ t\rbrack^n = 1,
\label{form:Dop:tn:fw} \tag{31}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn:bw"></div>

$$
\begin{equation} 
\lbrack D_t^- t\rbrack^n = 1,
\label{form:Dop:tn:bw} \tag{32}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn:cn"></div>

$$
\begin{equation} 
\lbrack D_t t\rbrack^n = 1,
\label{form:Dop:tn:cn} \tag{33}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn:2cn"></div>

$$
\begin{equation} 
\lbrack D_{2t} t\rbrack^n = 1,
\label{form:Dop:tn:2cn} \tag{34}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop2:tn:cn"></div>

$$
\begin{equation} 
\lbrack D_{t}D_t t\rbrack^n = 0
\label{form:Dop2:tn:cn} \tag{35}
\thinspace .
\end{equation}
$$

The next formulas concern the action of difference operators on a $t^2$ term.

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn2:fw"></div>

$$
\begin{equation}
\lbrack D_t^+ t^2\rbrack^n = (2n+1)\Delta t,
\label{form:Dop:tn2:fw} \tag{36}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn2:bw"></div>

$$
\begin{equation} 
\lbrack D_t^- t^2\rbrack^n = (2n-1)\Delta t,
\label{form:Dop:tn2:bw} \tag{37}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn2:cn"></div>

$$
\begin{equation} 
\lbrack D_t t^2\rbrack^n = 2n\Delta t,
\label{form:Dop:tn2:cn} \tag{38}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn2:2cn"></div>

$$
\begin{equation} 
\lbrack D_{2t} t^2\rbrack^n = 2n\Delta t,
\label{form:Dop:tn2:2cn} \tag{39}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop2:tn2:cn"></div>

$$
\begin{equation} 
\lbrack D_{t}D_t t^2\rbrack^n = 2,
\label{form:Dop2:tn2:cn} \tag{40}
\end{equation}
$$

Finally, we present formulas for a $t^3$ term:

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn3:fw"></div>

$$
\begin{equation}
\lbrack D_t^+ t^3\rbrack^n = 3(n\Delta t)^2 + 3n\Delta t^2 + \Delta t^2,
\label{form:Dop:tn3:fw} \tag{41}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn3:bw"></div>

$$
\begin{equation} 
\lbrack D_t^- t^3\rbrack^n = 3(n\Delta t)^2 - 3n\Delta t^2 + \Delta t^2,
\label{form:Dop:tn3:bw} \tag{42}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn3:cn"></div>

$$
\begin{equation} 
\lbrack D_t t^3\rbrack^n = 3(n\Delta t)^2 + \frac{1}{4}\Delta t^2,
\label{form:Dop:tn3:cn} \tag{43}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop:tn3:2cn"></div>

$$
\begin{equation} 
\lbrack D_{2t} t^3\rbrack^n = 3(n\Delta t)^2 + \Delta t^2,
\label{form:Dop:tn3:2cn} \tag{44}
\end{equation}
$$

<!-- Equation labels as ordinary links -->
<div id="form:Dop2:tn3:cn"></div>

$$
\begin{equation} 
\lbrack D_{t}D_t t^3\rbrack^n = 6n\Delta t,
\label{form:Dop2:tn3:cn} \tag{45}
\end{equation}
$$

## Software


Application of finite difference operators to polynomials and exponential
functions, resulting in the formulas above, can easily be computed by
some `sympy` code (from the file [`lib.py`](${src_formulas}/lib.py)):


```python
from sympy import *
t, dt, n, w = symbols('t dt n w', real=True)

# Finite difference operators

def D_t_forward(u):
    return (u(t + dt) - u(t))/dt

def D_t_backward(u):
    return (u(t) - u(t-dt))/dt

def D_t_centered(u):
    return (u(t + dt/2) - u(t-dt/2))/dt

def D_2t_centered(u):
    return (u(t + dt) - u(t-dt))/(2*dt)

def D_t_D_t(u):
    return (u(t + dt) - 2*u(t) + u(t-dt))/(dt**2)


op_list = [D_t_forward, D_t_backward,
           D_t_centered, D_2t_centered, D_t_D_t]

def ft1(t):
    return t

def ft2(t):
    return t**2

def ft3(t):
    return t**3

def f_expiwt(t):
    return exp(I*w*t)

def f_expwt(t):
    return exp(w*t)

func_list = [ft1, ft2, ft3, f_expiwt, f_expwt]
```

To see the results, one can now make a simple loop over the different
types of functions and the various operators associated with them:


```python
for func in func_list:
    for op in op_list:
        f = func
        e = op(f)
        e = simplify(expand(e))
        print(e)
        if func in [f_expiwt, f_expwt]:
            e = e/f(t)
        e = e.subs(t, n*dt)
        print(expand(e))
        print(factor(simplify(expand(e))))
```

    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    0
    0
    0
    dt + 2*t
    2*dt*n + dt
    dt*(2*n + 1)
    -dt + 2*t
    2*dt*n - dt
    dt*(2*n - 1)
    2*t
    2*dt*n
    2*dt*n
    2*t
    2*dt*n
    2*dt*n
    2
    2
    2
    dt**2 + 3*dt*t + 3*t**2
    3*dt**2*n**2 + 3*dt**2*n + dt**2
    dt**2*(3*n**2 + 3*n + 1)
    dt**2 - 3*dt*t + 3*t**2
    3*dt**2*n**2 - 3*dt**2*n + dt**2
    dt**2*(3*n**2 - 3*n + 1)
    dt**2/4 + 3*t**2
    3*dt**2*n**2 + dt**2/4
    dt**2*(12*n**2 + 1)/4
    dt**2 + 3*t**2
    3*dt**2*n**2 + dt**2
    dt**2*(3*n**2 + 1)
    6*t
    6*dt*n
    6*dt*n
    (-exp(I*t*w) + exp(I*w*(dt + t)))/dt
    exp(I*dt*w)/dt - 1/dt
    (exp(I*dt*w) - 1)/dt
    (-exp(I*t*w) + exp(I*w*(dt + t)))*exp(-I*dt*w)/dt
    1/dt - exp(-I*dt*w)/dt
    (exp(I*dt*w) - 1)*exp(-I*dt*w)/dt
    (-exp(I*t*w) + exp(I*w*(dt + t)))*exp(-I*dt*w/2)/dt
    exp(I*dt*w/2)/dt - exp(-I*dt*w/2)/dt
    2*I*sin(dt*w/2)/dt
    (-exp(I*t*w) + exp(I*w*(2*dt + t)))*exp(-I*dt*w)/(2*dt)
    exp(I*dt*w)/(2*dt) - exp(-I*dt*w)/(2*dt)
    I*sin(dt*w)/dt
    (exp(2*I*dt*w) - 2*exp(I*dt*w) + 1)*exp(I*w*(-dt + t))/dt**2
    exp(I*dt*w)/dt**2 - 2/dt**2 + exp(-I*dt*w)/dt**2
    (exp(I*dt*w) - 1)**2*exp(-I*dt*w)/dt**2
    (-exp(t*w) + exp(w*(dt + t)))/dt
    exp(dt*w)/dt - 1/dt
    (exp(dt*w) - 1)/dt
    (-exp(t*w) + exp(w*(dt + t)))*exp(-dt*w)/dt
    1/dt - exp(-dt*w)/dt
    (exp(dt*w) - 1)*exp(-dt*w)/dt
    (-exp(t*w) + exp(w*(dt + t)))*exp(-dt*w/2)/dt
    exp(dt*w/2)/dt - exp(-dt*w/2)/dt
    2*sinh(dt*w/2)/dt
    (-exp(t*w) + exp(w*(2*dt + t)))*exp(-dt*w)/(2*dt)
    exp(dt*w)/(2*dt) - exp(-dt*w)/(2*dt)
    sinh(dt*w)/dt
    (exp(2*dt*w) - 2*exp(dt*w) + 1)*exp(w*(-dt + t))/dt**2
    exp(dt*w)/dt**2 - 2/dt**2 + exp(-dt*w)/dt**2
    (exp(dt*w) - 1)**2*exp(-dt*w)/dt**2


670a975c4668f94c1e71ae349bfc9ec1f4aa5aec
ipynb
Jupyter Notebook
fdm-devito-notebooks/A_formulas/formulas.ipynb
devitocodes/devito_book
30405c3d440a1f89df69594fd0704f69650c1ded
CC-BY-4.0
2020-07-17T13:19:15.000Z
2021-03-27T05:21:09.000Z
fdm-devito-notebooks/A_formulas/formulas.ipynb
devitocodes/devito_book
30405c3d440a1f89df69594fd0704f69650c1ded
CC-BY-4.0
2020-07-14T15:38:52.000Z
2020-09-25T11:54:59.000Z
fdm-devito-notebooks/A_formulas/formulas.ipynb
devitocodes/devito_book
30405c3d440a1f89df69594fd0704f69650c1ded
CC-BY-4.0
2021-03-27T05:21:14.000Z
2021-03-27T05:21:14.000Z
Qwen/Qwen-72B
1. YES
2. YES