import numpy as np
import matplotlib.pyplot as plt
from numpy.polynomial import Polynomial

# Define the continuous function we want to approximate
def f(x):
    return np.sin(3*np.pi * x)

# Generate points in the interval [0, 1]
x = np.linspace(0, 1, 400)

# Define the degree of polynomial approximation
degrees = [1, 3, 5, 9]

# Create a figure and axis for plotting
plt.figure(figsize=(18, 8))

# Create polynomial approximations
for degree in degrees:
    # Polynomial fitting (least-squares fit)
    p = Polynomial.fit(x, f(x), degree)
    
    # Plot the polynomial approximation
    plt.plot(x, p(x), label=f'Degree {degree}')

# Plot the original function
plt.plot(x, f(x), label=r'$f(x) = \sin(3\pi x)$', color='black', linewidth=2, linestyle='--')

# Labels and legend
plt.title("Polynomial Approximation", fontsize=24)
plt.xlabel("$x$", fontsize=20)
plt.ylabel("$f(x)$", fontsize=20)
plt.grid(True)
plt.legend(loc="best", fontsize=16)

# Increase tick parameters
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)

if True:
    plt.savefig("weierstrass-theorem.png", dpi=300)

# Show the plot
plt.show()
