import numpy as np
import matplotlib.pyplot as plt
# Set parameters
L = 2
r = L / 2
# Number of points and x values
npts = 51
x = np.linspace(0, L, npts)
# Function definitions
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)
# First derivative
df1 = (1/(4*r))*(20*(x/r)**3 - 15*(x/r)**4)
df2 = (1/(4*r))*(20*z**3 - 15*z**4)
df = np.where(x <= r, df1, df2)
# Second derivative
d2f1 = (1/(4*r**2))*(60*(x/r)**2 - 60*(x/r)**3)
d2f2 = -(1/(4*r**2))*(60*z**2 - 60*z**3)
d2f = np.where(x <= r, d2f1, d2f2)
# Third derivative
d3f1 = (1/(4*r**3))*(120*(x/r) - 180*(x/r)**2)
d3f2 = (1/(4*r**3))*(120*z - 180*z**2)
d3f = np.where(x <= r, d3f1, d3f2)
# Plotting
plt.figure()
plt.plot(x, f, '-o')
plt.title('Function f')
plt.figure()
plt.plot(x, df, '-o')
plt.title('First Derivative df')
plt.figure()
plt.plot(x, d2f, '-o')
plt.title('Second Derivative d2f')
plt.figure()
plt.plot(x, d3f, '-o')
plt.title('Third Derivative d3f')
plt.show(block=False)
# Save data to file
smooth = np.column_stack((x, f, df, d2f, d3f))
np.savetxt('indscaled.dat', smooth, fmt='%f')