import numpy as np import matplotlib.pyplot as plt from scipy.optimize import brentq # Parameters corresponds to the given theta and b but trying different t. b=1.0 theta = np.pi/4 t=np.linspace(0, 2*np.pi, 100) # Plotting the ball B2(0) of radius 2 fig, ax = plt.subplots(figsize=(10, 8)) circle = plt.Circle((0, 0), 2, color='blue', fill=False, label='Ball B2(0)') ax.add_artist(circle) # Parametrization of p for different values of k k_values = [0.1, 0.5, 1, 2] # Plot parametrization of p for different values of k on the ball for k in k_values: p_x = (1/k) * (np.sin(theta) + np.cos(t)) p_y = b + (1/k) * (-np.cos(theta) + np.sin(t)) ax.plot(p_x, p_y, label=f'k={k}') # Plot the points (±2, 0) ax.plot([2, -2], [0, 0], 'go', label='Points (±2, 0)') # Setting the same scale for both axes and other plot settings ax.set_xlim([-3, 3]) ax.set_ylim([-3, 3]) ax.set_aspect('equal', 'box') ax.legend() ax.grid(True) ax.set_title('Parametrization of p for different k values and Arc through (±2, 0) on Ball B2(0)') plt.show() ##################Plot the dynamical version of the parametrization of p for different k values on the ball B2(0) and the arc through (±2, 0)############################################ import os import numpy as np import matplotlib.pyplot as plt import imageio from scipy.optimize import brentq os.makedirs('tmp', exist_ok=True) # Define parametrization functions def p_x(t, k, theta): return (1/k) * (np.sin(theta) + np.cos(t)) def p_y(t, k, theta, b): return b + (1/k) * (-np.cos(theta) + np.sin(t)) # Define the distance function from the origin def distance_from_origin(t, k, theta, b): return np.sqrt(p_x(t, k, theta)**2 + p_y(t, k, theta, b)**2) - 2 # Check for sign changes in the distance function across the interval [0, 2pi] def find_intersections(k, theta, b): t_values = np.linspace(0, 2 * np.pi, 1000) distances = distance_from_origin(t_values, k, theta, b) sign_changes = np.where(np.diff(np.sign(distances)))[0] # Indices where sign changes intersections = [] for idx in sign_changes: try: t_intersection = brentq(distance_from_origin, t_values[idx], t_values[idx + 1], args=(k, theta, b)) intersections.append(t_intersection) except ValueError: pass return intersections # Function to plot parametrization of p for a given k on the ball B2(0) def plot_for_k(k, filename, b, theta): intersections = find_intersections(k, theta, b) fig, ax = plt.subplots(figsize=(10, 8)) circle = plt.Circle((0, 0), 2, color='blue', fill=False) ax.add_artist(circle) t = np.linspace(0, 2 * np.pi, 100) ax.plot(p_x(t, k, theta), p_y(t, k, theta, b), label=f'k={k}') # Points (±2, 0) ax.plot([2, -2], [0, 0], 'go') for t_int in intersections: P_int = (p_x(t_int, k, theta), p_y(t_int, k, theta, b)) ax.plot(P_int[0], P_int[1], 'ro') # Line for k=0.05 line_x = np.linspace(-3, 3, 100) line_y = np.tan(theta) * (line_x - 0) + b ax.plot(line_x, line_y, 'r--', label='Line for k=0.05') # Calculate and plot tangent at (0,b) for k # Placeholder tangent, replace with actual calculation tangent_y = np.tan(theta) * (line_x - 0) + b ax.plot(line_x, tangent_y, 'g--', label=f'Tangent at (0,{b}) for k={k}') ax.set_xlim([-3, 3]) ax.set_ylim([-3, 3]) ax.set_aspect('equal', 'box') ax.legend() plt.close(fig) fig.savefig(filename) return filename def arc_equation(t, k, theta, b): p_x = (1/k) * (np.sin(theta) + np.cos(t)) p_y = b + (1/k) * (-np.cos(theta) + np.sin(t)) return p_x, p_y # Generate plots for a series of k values and create a GIF theta = np.pi/4 #0 b=1 k_values = list(np.linspace(0.05, 0.86, 20) )+ list(np.linspace(0.86, 0.87, 50) )+list(np.linspace(0.87, 4, 30)) filenames = [] for k in k_values: filename = f'tmp/plot_k_{k:.4f}.png' plot_for_k(k, filename,b, theta) filenames.append(filename) # Create a GIF from the saved images with imageio.get_writer('parameterized_theta=0.gif', mode='I', duration=0.5) as writer: for filename in filenames: image = imageio.imread(filename) writer.append_data(image)