import numpy as np
import matplotlib.pyplot as plt
def u_periodic(x):
"""
Defines the periodic function u(x) = x with period 1, based on the interval [0, 1].
"""
# The modulo operator handles the periodicity. x % 1 gives the remainder of x divided by 1.
# For positive x, this is the fractional part. For negative x, this gives a value
# in the range (0, 1] or [-1, 0) depending on the implementation, but adding 1
# and taking modulo 1 again ensures it's in [0, 1).
return (x % 1 + 1) % 1
# Generate x values from -2 to 2
# We use a large number of points to get a smooth-looking plot
= np.linspace(-2, 2, 400)
x_values
# Calculate the corresponding u(x) values
= u_periodic(x_values)
y_values
# Plot the function
=(8, 4))
plt.figure(figsize
plt.plot(x_values, y_values)'Plot of u(x) = x, Periodic from 0 to 1')
plt.title('x')
plt.xlabel('u(x)')
plt.ylabel(True)
plt.grid( plt.show()
Test
Test
i