# 有限差分法

## 函數嘅有限差分表示格式

${\displaystyle u_{i,j,k}^{n}=u(x,y,z,t)}$

## 導數喺有限差分嘅表示法

${\displaystyle f'(x)=\displaystyle \lim _{\Delta x\rightarrow 0}{\frac {f(x+\Delta x)-f(x)}{\Delta x}}}$

${\displaystyle f'(x)\approx {\frac {f(x+\Delta x)-f(x)}{\Delta x}}}$

${\displaystyle f(x+\Delta x)\approx f(x)+f'(x)\Delta x}$

${\displaystyle {\frac {df}{dx}}\approx {\frac {f_{i+1}-f_{i}}{\Delta x}}}$

${\displaystyle f'(x)=\displaystyle \lim _{\Delta x\rightarrow 0}{\frac {f(x)-f(x-\Delta x)}{\Delta x}}}$

${\displaystyle f'(x)\approx {\frac {f(x)-f(x-\Delta x)}{\Delta x}}}$

${\displaystyle f(x)\approx f(x-\Delta x)+f'(x)\Delta x}$

${\displaystyle {\frac {df}{dx}}\approx {\frac {f_{i}-f_{i-1}}{\Delta x}}}$

${\displaystyle f'(x)\approx {\frac {f(x+\Delta x)-f(x)}{\Delta x}}\cdots (1)}$

${\displaystyle f'(x)\approx {\frac {f(x)-f(x-\Delta x)}{\Delta x}}\cdots (2)}$

${\displaystyle (1)+(2):}$

${\displaystyle 2f'(x)\approx {\frac {f(x+\Delta x)-f(x)}{\Delta x}}+{\frac {f(x)-f(x-\Delta x)}{\Delta x}}}$

${\displaystyle 2f'(x)\approx {\frac {f(x+\Delta x)-f(x)+f(x)-f(x-\Delta x)}{\Delta x}}}$

${\displaystyle 2f'(x)\approx {\frac {f(x+\Delta x)-f(x-\Delta x)}{\Delta x}}}$

${\displaystyle f'(x)\approx {\frac {f(x+\Delta x)-f(x-\Delta x)}{2\Delta x}}}$

${\displaystyle {\frac {df}{dx}}\approx {\frac {f_{i+1}-f_{i-1}}{2\Delta x}}}$

${\displaystyle f(x+\Delta x)=\displaystyle \sum _{n=0}^{\infty }{\frac {f^{(n)}(x)}{n!}}\Delta x^{n}=f(x)+f'(x)\Delta x+{\frac {f''(x)}{2!}}\Delta x^{2}+{\frac {f'''(x)}{3!}}\Delta x^{3}+{\frac {f''''(x)}{4!}}\Delta x^{4}+\cdots \,\,\,\,\,\cdots (3)}$

${\displaystyle f(x-\Delta x)=\displaystyle \sum _{n=0}^{\infty }{\frac {(-1)^{n}f^{(n)}(x)}{n!}}\Delta x^{n}=f(x)-f'(x)\Delta x+{\frac {f''(x)}{2!}}\Delta x^{2}-{\frac {f'''(x)}{3!}}\Delta x^{3}+{\frac {f''''(x)}{4!}}\Delta x^{4}-\cdots \,\,\,\,\,\cdots (4)}$

${\displaystyle (3)}$ 準確到一階，我哋可以重寫佢，變成：

${\displaystyle f(x+\Delta x)=f(x)+f'(x)\Delta x+O(\Delta x^{2})}$

${\displaystyle f(x-\Delta x)=f(x)-f'(x)\Delta x+O(\Delta x^{2})}$

${\displaystyle f(x+\Delta x)-f(x-\Delta x)=2f'(x)\Delta x+{\frac {2f'''(x)}{3!}}\Delta x^{3}+\cdots =2f'(x)\Delta x+O(\Delta x^{3})}$

${\displaystyle f''(x)=\displaystyle \lim _{\Delta x\rightarrow 0}{\frac {f'(x+\Delta x)-f'(x-\Delta x)}{2\Delta x}}}$

${\displaystyle f''(x)\approx {\frac {f'(x+{\frac {\Delta x}{2}})-f'(x-{\frac {\Delta x}{2}})}{\Delta x}}}$

${\displaystyle f'(x+{\frac {\Delta x}{2}})\approx {\frac {f(x+\Delta x)-f(x)}{\Delta x}}}$ （條式同歐拉法一樣）

${\displaystyle f'(x-{\frac {\Delta x}{2}})\approx {\frac {f(x)-f(x-\Delta x)}{\Delta x}}}$ （條式同反向歐拉法一樣）

${\displaystyle f''(x)\approx {\frac {{\frac {f(x+\Delta x)-f(x)}{\Delta x}}-{\frac {f(x)-f(x-\Delta x)}{\Delta x}}}{\Delta x}}}$

${\displaystyle f''(x)\approx {\frac {\frac {f(x+\Delta x)-f(x)-[f(x)-f(x-\Delta x)]}{\Delta x}}{\Delta x}}}$

${\displaystyle f''(x)\approx {\frac {f(x+\Delta x)-2f(x)+f(x-\Delta x)}{\Delta x^{2}}}}$

${\displaystyle {\frac {d^{2}f}{dx^{2}}}\approx {\frac {f_{i+1}-2f_{i}+f_{i-1}}{\Delta x^{2}}}}$

${\displaystyle f(x+\Delta x)+f(x-\Delta x)=2f(x)+2\left[{\frac {f''(x)}{2!}}\Delta x^{2}\right]+2\left[{\frac {f''''(x)}{4!}}\Delta x^{4}\right]+\cdots }$

${\displaystyle f(x+\Delta x)+f(x-\Delta x)=2f(x)+2\left[{\frac {f''(x)}{2!}}\Delta x^{2}\right]+O(\Delta x^{4})}$

${\displaystyle O(\Delta x^{4})\rightarrow 0}$，二階導數嘅差分公式就會出返嚟。

## 有限差分法嘅應用

${\displaystyle y_{i}\left({\frac {y_{i+1}-2y_{i}+y_{i-1}}{\Delta x^{2}}}\right)+{\frac {y_{i+1}-y_{i-1}}{2\Delta x}}-{\frac {1}{y_{i}}}=1}$

${\displaystyle {\frac {y_{i+1}-2y_{i}+y_{i-1}}{\Delta x^{2}}}={\frac {1}{y_{i}}}\left(1+{\frac {1}{y_{i}}}-{\frac {y_{i+1}-y_{i-1}}{2\Delta x}}\right)}$

${\displaystyle {\frac {y_{i+1}-2y_{i}+y_{i-1}}{\Delta x^{2}}}={\frac {1}{y_{i}}}+{\frac {1}{y_{i}^{2}}}-{\frac {y_{i+1}-y_{i-1}}{2y_{i}\Delta x}}}$

${\displaystyle y_{i+1}\left({\frac {1}{\Delta x^{2}}}+{\frac {1}{2y_{i}\Delta x}}\right)={\frac {2y_{i}-y_{i-1}}{\Delta x^{2}}}+{\frac {1}{y_{i}}}+{\frac {1}{y_{i}^{2}}}+{\frac {y_{i-1}}{2y_{i}\Delta x}}}$

${\displaystyle {\frac {y_{i+1}}{\Delta x}}\left({\frac {1}{\Delta x}}+{\frac {1}{2y_{i}}}\right)={\frac {2y_{i}-y_{i-1}}{\Delta x^{2}}}+{\frac {1}{y_{i}}}+{\frac {1}{y_{i}^{2}}}+{\frac {y_{i-1}}{2y_{i}\Delta x}}}$

${\displaystyle y_{i+1}={\frac {2y_{i}\Delta x^{2}}{2y_{i}+\Delta x}}\left({\frac {2y_{i}-y_{i-1}}{\Delta x^{2}}}+{\frac {1}{y_{i}}}+{\frac {1}{y_{i}^{2}}}+{\frac {y_{i-1}}{2y_{i}\Delta x}}\right)}$

${\displaystyle y_{i+1}={\frac {2y_{i}\Delta x^{2}}{2y_{i}+\Delta x}}\left[{\frac {2y_{i}-y_{i-1}}{\Delta x^{2}}}+{\frac {1}{y_{i}}}\left(1+{\frac {1}{y_{i}}}+{\frac {y_{i-1}}{2\Delta x}}\right)\right]}$

${\displaystyle {\frac {\partial u}{\partial t}}+c{\frac {\partial u}{\partial x}}=0}$

${\displaystyle {\frac {u_{i}^{n+1}-u_{i}^{n-1}}{2\Delta t}}+c{\frac {u_{i+1}^{n}-u_{i-1}^{n}}{2\Delta x}}=0}$

${\displaystyle u_{i}^{n+1}=u_{i}^{n-1}-{\frac {c\Delta t}{\Delta x}}(u_{i+1}^{n}-u_{i-1}^{n})}$