Figure 4.15:

The indicator (step) function f_1(w;x) and its smooth approximation, f(w;x).

Code for Figure 4.15

Text of the GNU GPL.

main.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
import numpy as np
import matplotlib.pyplot as plt

# Constants
L = 1
r = L / 2

# Number of points
npts = 51
x = np.linspace(0, L, npts)

# Define the piecewise function
f1 = (1 / 4) * (5 * (x / r)**4 - 3 * (x / r)**5)
z = (2 * r - x) / r
f2 = -(1 / 4) * (5 * z**4 - 3 * z**5) + 1
f = np.where(x <= r, f1, f2)

# Points and lines for plotting
pts = np.array([[0, 1], [1, 0]])
lines = np.array([[-1, 1], [0, 1], [0, 0], [2, 0]])
z = np.flip(x)

# Plotting
plt.figure()
plt.plot(z, f, label='Smooth Function')
plt.plot(pts[:, 0], pts[:, 1], 'o', label='Points')
plt.plot(lines[:,0], lines[:,1])

plt.legend()
plt.show(block=False)

# Data preparation for saving
smooth = np.column_stack((z, f))

# Save data to file
with open("indicator.dat", "w") as f:
    np.savetxt(f, smooth, fmt='%f', header='z, f')
    f.write("\n\n")
    np.savetxt(f, pts, fmt='%f', header='x, y')
    f.write("\n\n")
    np.savetxt(f, lines, fmt='%f', header='x0, x1')