-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp1.py
38 lines (31 loc) · 962 Bytes
/
p1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""p1.py - convergence of fourth-order finite differences"""
import numpy as np
import scipy.sparse as sp
import matplotlib.pyplot as plt
n_vals = 2 ** np.arange(3, 13)
times = np.zeros(len(n_vals))
for i, n in enumerate(n_vals):
h = 2 * np.pi / n
x = np.arange(1, n + 1) * h - np.pi
u = np.exp(np.sin(x))
du = np.cos(x) * u
# Construct the matrix
e = np.ones(n)
D = sp.csr_matrix(
(2 * e / 3, (np.arange(n), np.concatenate((np.arange(1, n), [0])))),
shape=(n, n),
)
D = D - sp.csr_matrix(
(e / 12, (np.arange(n), np.concatenate((np.arange(2, n), [0], [1])))),
shape=(n, n),
)
D = (D - D.T) / h
# Compute errors
error = np.linalg.norm(D @ u - du, np.inf)
plt.loglog(n, error, ".", markersize=8)
plt.xlabel("N")
plt.ylabel("error")
plt.title("Convergence of fourth-order finite differences")
plt.grid(which="both")
plt.plot(n_vals, n_vals ** (-4.0), "c--")
plt.show()