-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff_matr.py
executable file
·64 lines (46 loc) · 1.07 KB
/
diff_matr.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import matplotlib.pyplot as plt
import numpy as np
# Function.
def f(x):
return np.cos(x) * x
# Exact first derivative.
def df_exact(x):
return np.cos(x) - np.sin(x) * x
# Exact second derivative.
def d2f_exact(x):
return -np.sin(x) - np.sin(x) - np.cos(x) * x
fig, axes = plt.subplots(4, 1, figsize=(4, 8))
# Grid.
n = 31
x = np.linspace(0, 4, n)
# Differentiation matrix.
h = x[1] - x[0]
D = np.diag(-np.ones(n) / h) + np.diag(np.ones(n - 1) / h, 1)
# Backward difference in the last row.
D[n - 1, n - 2] = -1 / h
D[n - 1, n - 1] = 1 / h
# Evaluate function and derivatives.
y = f(x)
dy = D @ y
dy_exact = df_exact(x)
# Plot sparsity pattern.
ax = axes[0]
ax.spy(D, marker='s', markersize=2)
# Plot function.
ax = axes[1]
ax.set_ylabel('f(x)')
ax.plot(x, y)
# Plot derivative.
ax = axes[2]
ax.plot(x, dy_exact, label='exact')
ax.plot(x, dy, label='approx')
ax.set_ylabel("derivative")
ax.legend()
# Plot error.
ax = axes[3]
ax.plot(x, dy - dy_exact)
ax.set_xlabel('x')
ax.set_ylabel('error')
fig.tight_layout()
fig.savefig('diff_matr.pdf')