Error Functions Example¶
Demonstrates error functions and Fresnel integrals using specialfunctions.erf(),
specialfunctions.erfc(), specialfunctions.fresnel_s(),
and specialfunctions.fresnel_c().
Example Code¶
"""Example: IMSL Special Functions — Error functions and Fresnel integrals."""
from __future__ import annotations
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from specialfunctions import erf, erfc, fresnel_s, fresnel_c
x_erf = np.linspace(-3, 3, 400)
x_fr = np.linspace(0, 5, 400)
print(f"erf(1) = {erf(1.0):.10f} (expected: 0.8427007929)")
print(f"erfc(1) = {erfc(1.0):.10f} (expected: 0.1572992071)")
print(f"S(1) = {fresnel_s(1.0):.7f}")
print(f"C(1) = {fresnel_c(1.0):.7f}")
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
fig.suptitle("Error Functions and Fresnel Integrals", fontsize=14)
axes[0].plot(x_erf, erf(x_erf), label="erf(x)", lw=1.5)
axes[0].plot(x_erf, erfc(x_erf), label="erfc(x)", lw=1.5)
axes[0].axhline(0, color="k", lw=0.5, ls="--")
axes[0].set_title("Error Functions")
axes[0].set_xlabel("x")
axes[0].legend()
axes[0].grid(True, alpha=0.3)
axes[1].plot(x_fr, fresnel_s(x_fr), label="S(x)", lw=1.5)
axes[1].plot(x_fr, fresnel_c(x_fr), label="C(x)", lw=1.5)
axes[1].axhline(0.5, color="k", lw=0.5, ls="--", alpha=0.5)
axes[1].set_title("Fresnel Integrals")
axes[1].set_xlabel("x")
axes[1].legend()
axes[1].grid(True, alpha=0.3)
plt.tight_layout()
plt.savefig("test_output/example_imsl_error_functions.svg", bbox_inches="tight")
print("Saved: test_output/example_imsl_error_functions.svg")
Plot Output¶
erf(x), erfc(x), Fresnel S(x), and Fresnel C(x) plotted over their domains.¶
Console Output¶
erf(1) = 0.8427007929 (expected: 0.8427007929)
erfc(1) = 0.1572992071 (expected: 0.1572992071)
S(1) = 0.4382591
C(1) = 0.7798934
Saved: test_output/example_imsl_error_functions.svg