-
Notifications
You must be signed in to change notification settings - Fork 0
/
p30.py
50 lines (40 loc) · 1.24 KB
/
p30.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
"""p30.py - Spectral integration, ODE style (compare p12.py)"""
import numpy as np
from numpy.linalg import inv
from scipy.special import erf
from cheb import cheb
import matplotlib.pyplot as plt
# Computation: various values of N, four functions
n_max = 50
e = np.zeros((4, n_max))
for n in range(1, n_max + 1):
ii = np.arange(n)
D, x = cheb(n)
x = x[ii]
D_i = inv(D[np.ix_(ii, ii)])
w = D_i[0, :]
f = np.abs(x) ** 3
e[0, n - 1] = abs(np.dot(w, f) - 0.5)
f = np.exp(-(x ** (-2)))
e[1, n - 1] = abs(
np.dot(w, f) - 2 * (np.exp(-1) + np.sqrt(np.pi) * (erf(1) - 1))
)
f = 1 / (1 + x**2)
e[2, n - 1] = abs(np.dot(w, f) - np.pi / 2)
f = x**10
e[3, n - 1] = abs(np.dot(w, f) - 2 / 11)
# Plot results
fig = plt.figure(figsize=(8, 5))
labels = ["$|x|^3$", "$e^{-x^{-2}}$", "$1/(1+x^2)$", "$x^{10}$"]
for i in range(4):
plt.subplot(2, 2, i + 1)
plt.semilogy(e[i, :] + 1e-100, "k.")
plt.plot(e[i, :] + 1e-100, "k-")
plt.axis([0, n_max, 1e-18, 1e3])
plt.grid()
plt.xticks(np.arange(0, n_max + 1, 10))
plt.yticks(10.0 ** np.arange(-15, 1, 5))
plt.ylabel("error")
plt.text(0.75, 0.75, labels[i], fontsize=10, transform=plt.gca().transAxes)
plt.tight_layout()
plt.show()