Created
April 27, 2022 07:39
-
-
Save louity/736e42992abe09c43fce51cefdc917f6 to your computer and use it in GitHub Desktop.
Finite difference of order 2 and order 4 on staggered grid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
x = 1.3 | |
f = np.exp | |
fp = np.exp | |
errs2, errs4 = [], [] | |
dxs = np.linspace(1e-4, 1e-1, 200) | |
for dx in dxs: | |
fpx = fp(x) | |
fpx_2 = (f(x+dx/2) - f(x-dx/2))/dx | |
fpx_4 = (-1./24*f(x+dx*3/2) + 9/8*f(x+dx/2) - 9/8*f(x-dx/2) + 1./24*f(x-3*dx/2))/dx | |
errs2.append(np.abs(fpx_2 - fpx)) | |
errs4.append(np.abs(fpx_4 - fpx)) | |
plt.plot(dxs, errs2, label='order2') | |
plt.plot(dxs, errs4, label='order4') | |
plt.loglog() | |
plt.legend() | |
plt.grid(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment