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')