reinman.py (3472B)
1 ########################################## 2 # Name: Kris Yotam # 3 # Program: Reinman # 4 # Date: 10/11/2024 # 5 # Description: This program visualizes # 6 # the Riemann surface for the square # 7 # root function and allows dynamic # 8 # interaction, multiple surfaces, # 9 # critical points analysis, and a basic # 10 # GUI interface. # 11 ########################################## 12 13 import numpy as np 14 import matplotlib.pyplot as plt 15 from mpl_toolkits.mplot3d import Axes3D 16 from matplotlib.widgets import Slider 17 import tkinter as tk 18 19 # Create a mesh grid for complex numbers (z = x + iy) 20 theta = np.linspace(0, 2 * np.pi, 100) 21 r = np.linspace(0.01, 2, 50) # Start from 0.01 to avoid singularity at 0 22 theta, r = np.meshgrid(theta, r) 23 x = r * np.cos(theta) 24 y = r * np.sin(theta) 25 26 # Define the function to plot the surface 27 def plot_surface(exponent): 28 # Compute the square root of complex numbers z = x + iy 29 z = x + 1j * y 30 w = z ** exponent 31 32 # Real and imaginary parts of the surface 33 X1 = np.real(w) 34 Y1 = np.imag(w) 35 Z1 = np.log(r) # Using log(r) to make visualization more informative 36 37 ax.clear() # Clear previous plots 38 # Color map based on height (Z1) 39 cmap = plt.get_cmap('coolwarm') # Blue to light blue gradient 40 41 # Plot the first branch of the square root with the updated color scheme 42 ax.plot_surface(x, y, X1, rstride=1, cstride=1, 43 facecolors=cmap((Z1 - Z1.min()) / (Z1.max() - Z1.min())), 44 alpha=0.9, edgecolor='k') 45 46 # Plot the second branch (negative square root) 47 ax.plot_surface(x, y, -X1, rstride=1, cstride=1, 48 facecolors=cmap((Z1 - Z1.min()) / (Z1.max() - Z1.min())), 49 alpha=0.9, edgecolor='k') 50 51 # Labels and title 52 ax.set_xlabel('Re(z)') 53 ax.set_ylabel('Im(z)') 54 ax.set_zlabel('Re(z^n)') 55 ax.set_title(f'Riemann Surface for z^{exponent} with Custom Colors') 56 plt.draw() 57 58 # Function to calculate critical points 59 def critical_points(exponent): 60 # Calculate critical points based on the derivative 61 # For z^n, critical points occur at z=0 for n > 1 62 if exponent > 1: 63 return [0] # Critical point at origin 64 return [] 65 66 # Create sliders for interactive control 67 fig, ax = plt.subplots(figsize=(10, 8), subplot_kw={'projection': '3d'}) 68 plt.subplots_adjust(left=0.1, bottom=0.25) 69 70 # Create slider for exponent 71 ax_exponent = plt.axes([0.1, 0.1, 0.65, 0.03]) # [left, bottom, width, height] 72 slider_exponent = Slider(ax_exponent, 'Exponent', 1, 5, valinit=2) 73 74 # Update function for slider 75 def update(val): 76 plot_surface(slider_exponent.val) 77 cp = critical_points(slider_exponent.val) 78 print("Critical Points:", cp) 79 80 slider_exponent.on_changed(update) 81 plot_surface(slider_exponent.val) 82 83 # Basic GUI setup using tkinter 84 def create_gui(): 85 root = tk.Tk() 86 root.title("Riemann Surface Visualizer") 87 88 label = tk.Label(root, text="Welcome to the Riemann Surface Visualizer!") 89 label.pack() 90 91 instruction = tk.Label(root, text="Use the slider to change the exponent.") 92 instruction.pack() 93 94 # Add a button to close the GUI 95 close_button = tk.Button(root, text="Close", command=root.quit) 96 close_button.pack() 97 98 root.mainloop() 99 100 # Show GUI in a separate thread 101 import threading 102 103 gui_thread = threading.Thread(target=create_gui) 104 gui_thread.start() 105 106 plt.show() # Show the plot