誤差関数¶
In [1]:
import matplotlib.pyplot as plt
import numpy as np
import scipy as sp
from scipy.special import erf, erfc # 誤差関数,余誤差関数
from scipy.integrate import quad
import pandas as pd
In [2]:
import scienceplots
#Warning : As of version 2.0.0, you need to add import scienceplots before setting the style (plt.style.use('science')).
plt.style.use(['science', 'notebook'])
# rcPramsを使ってTeXのフォントをデフォルトにする
plt.rcParams['font.family'] = 'Times New Roman' # font familyの設定
plt.rcParams['font.family'] = 'serif' # font familyの設定
plt.rcParams['mathtext.fontset'] = 'cm' # math fontの設定
In [3]:
np.set_printoptions(precision=5)
誤差関数 $\mathrm{erf} (x)$ ,余誤差関数 $\mathrm{erfc} (x)$ は, \begin{gather} \mathrm{erf} (x) = \frac{2}{\sqrt{\pi}} \int_0^x e^{-t^2} dt\\ \mathrm{erfc} (x) = \frac{2}{\sqrt{\pi}} \int_x^{\infty} e^{-t^2} dt \end{gather}
In [4]:
uu = np.linspace(0.0,4.0,9)
f_erf = erf(uu) # 誤差関数
f_erfc = erfc(uu) # 余誤差関数
print(f"{'u':>3s} {'erf(u)':>17s} {'erfc(u)':>17s} {'erf(u)+erfc(u)':>17s}")
for j,u in enumerate(uu):
print(f"{u:>.1f} {f_erf[j]:>.15f} {f_erfc[j]:>.15f} {f_erf[j]+f_erfc[j]:>.15f}")
u erf(u) erfc(u) erf(u)+erfc(u) 0.0 0.000000000000000 1.000000000000000 1.000000000000000 0.5 0.520499877813047 0.479500122186953 1.000000000000000 1.0 0.842700792949715 0.157299207050285 1.000000000000000 1.5 0.966105146475311 0.033894853524689 1.000000000000000 2.0 0.995322265018953 0.004677734981047 1.000000000000000 2.5 0.999593047982555 0.000406952017445 1.000000000000000 3.0 0.999977909503001 0.000022090496999 1.000000000000000 3.5 0.999999256901628 0.000000743098372 1.000000000000000 4.0 0.999999984582742 0.000000015417258 1.000000000000000
In [5]:
df = pd.DataFrame({
'$u$':uu,
'erf$(u)$':f_erf,
'erfc$(u)$':f_erfc,
'erf$(u)+$erfc$(u)$':f_erf+f_erfc,
})
df.style.format({"$u$":"{:.2f}"})
Out[5]:
$u$ | erf$(u)$ | erfc$(u)$ | erf$(u)+$erfc$(u)$ | |
---|---|---|---|---|
0 | 0.00 | 0.000000 | 1.000000 | 1.000000 |
1 | 0.50 | 0.520500 | 0.479500 | 1.000000 |
2 | 1.00 | 0.842701 | 0.157299 | 1.000000 |
3 | 1.50 | 0.966105 | 0.033895 | 1.000000 |
4 | 2.00 | 0.995322 | 0.004678 | 1.000000 |
5 | 2.50 | 0.999593 | 0.000407 | 1.000000 |
6 | 3.00 | 0.999978 | 0.000022 | 1.000000 |
7 | 3.50 | 0.999999 | 0.000001 | 1.000000 |
8 | 4.00 | 1.000000 | 0.000000 | 1.000000 |
In [6]:
def fe(x):
return np.exp(-x**2)
In [7]:
uu = np.linspace(0.0,4.0,9)
print(f"{'u':>3s} {'quad:erf(u)':>17s} {'quad:erfc(u)':>17s} {'erf(u)+erfc(u)':>17s}")
for j,u in enumerate(uu):
q_erf = quad(fe,0,u)[0]*2/np.sqrt(np.pi) # 誤差関数
q_erfc = quad(fe,u,np.inf)[0]*2/np.sqrt(np.pi) # 余誤差関数
print(f"{u:>.1f} {q_erf:>.15f} {q_erfc:>.15f} {q_erf+q_erfc:>.15f}")
u quad:erf(u) quad:erfc(u) erf(u)+erfc(u) 0.0 0.000000000000000 1.000000000000000 1.000000000000000 0.5 0.520499877813047 0.479500122186954 1.000000000000000 1.0 0.842700792949715 0.157299207050285 1.000000000000000 1.5 0.966105146475311 0.033894853524689 1.000000000000000 2.0 0.995322265018953 0.004677734981047 1.000000000000000 2.5 0.999593047982555 0.000406952017438 0.999999999999993 3.0 0.999977909503002 0.000022090496999 1.000000000000000 3.5 0.999999256901628 0.000000743098362 0.999999999999990 4.0 0.999999984582742 0.000000015417258 1.000000000000000
積分範囲の上限が無限大の場合, \begin{gather} \int_0^{\infty} e^{-t^2} dt = \frac{\sqrt{\pi}}{2} \end{gather} あるいは, \begin{gather} \frac{2}{\sqrt{\pi}} \int_0^{\infty} e^{-t^2} dt = 1 \end{gather}
In [8]:
quad(fe, 0, np.inf)[0]*2/np.sqrt(np.pi)
Out[8]:
1.0
被積分関数¶
In [9]:
x = np.linspace(-4, 4, 201)
y = fe(x)
fig = plt.figure() # グラフ領域
plt.grid()
plt.plot(x, y)
fig.savefig('erf_integrand.pdf')
plt.show()