Mandelbrot09

py

School

Temple University *

*We aren’t endorsed by this school

Course

1102

Subject

Computer Science

Date

Dec 6, 2023

Type

py

Pages

4

Uploaded by ProfTigerPerson972

Report
# create a command-line function # command line arguments: # sys.argv[0] = "Mandelbrot09.py" # sys.argv[1] = nx # sys.argv[2] = ny # sys.argv[3] = N # sys.argv[4] = xmin # sys.argv[5] = xmax # sys.argv[6] = ymin # sys.argv[7] = ymax # Mandelbrot Algorithm # # Pseudocode: # Controls: # Set circle radius (r=4) (lets not deal with sqrt) # Set max iterations (N=100) # Set window: (xmin, xmax, ymin, ymax) # set resolution: nx, ny # Initialize variables: # Create empty Z array (size of image) # create array of x values # create array of y values # Main Code: # For each pixel # Determine Initial Position (X_0, Y_0) # Let X_old = 0 # Let Y_old = 0 # Let z = 0 (iteration count) # Let r_new = x_0^2 + y_0^2 # Mandelbrot Algorithm: # While (r_new < r and z<N) # Calculate X_new = X_old^2 - Y_old^2 + X_0 # Calculate Y_new = 2*X_old*Y_old + Y_0 # Let X_old = X_new # Let Y_old = Y_new # z = z + 1 # store z in Z array # visualize the colorZ array # Note you need to install numpy first - IDLE command line: # >>> import pip # >>> pip.main(["install","numpy"]) import numpy as np # Note you need to install matplotlib first - IDLE command line: # >>> import pip # >>> pip.main(["install","matplotlib"]) import matplotlib.pyplot as plt
# pyplot is a sub library of matplotlib import sys #for arg in sys.argv: # print arg ###################### ###################### # Function Definitions ###################### ###################### # Distance to Origin (from last class) def distance_to_origin(x,y): # old way: return (x**2 + y**2)**.5 return np.sqrt(x**2 + y**2) def mandelbrotAlgorithm(x0,y0,N,r): # Let X_old = 0 # Let Y_old = 0 xOld = 0 yOld = 0 # Let z = 0 (iteration count) z = 0 # Let r_new = x_0^2 + y_0^2 rNew = x0**2 + y0**2 # Mandelbrot Algorithm: # While (r_new < r and z<N) while rNew < r and z<N: # Calculate X_new = X_old^2 - Y_old^2 + X_0 xNew = xOld**2 - yOld**2 + x0 # Calculate Y_new = 2*X_old*Y_old + Y_0 yNew = 2*xOld*yOld + y0 # Let X_old = X_new xOld = xNew # Let Y_old = Y_new yOld = yNew #calculate range rNew = xNew**2 + yNew**2 # z = z + 1 z = z + 1 return z ##################### ##################### # MAIN ##################### ##################### ######################
# 0. Solution Controls ###################### # Set Circle Radius (lets use r^2 to avoid unneeded sqrts r = 4.0 # Set max iterations (the more iterations, the more color detail, but longer run time) if len(sys.argv)>1: # sys.argv[3] = N N = int(sys.argv[3]) else: N = 100 #################### # 1. Define the Grid # (xmin, xmax, ymin, ymax, nx, ny) #################### # Set window limits: (xmin, xmax, ymin, ymax) if len(sys.argv)>4: xmin = float(sys.argv[4]) xmax = float(sys.argv[5]) ymin = float(sys.argv[6]) ymax = float(sys.argv[7]) else: xmin = -2 xmax = 1 ymin = -1 ymax = 1 # set resolution: nx, ny if len(sys.argv)>1: # sys.argv[1] = nx # sys.argv[2] = ny nx = int(sys.argv[1]) ny = int(sys.argv[2]) else: nx = 11 ny = 11 # xgrid (comments show the old way) #dx = (xmax - xmin)/(nx - 1) #x = [xmin + i*dx for i in range(nx)] x = np.linspace(xmin,xmax, nx) # ygrid (comments show the old way) #dy = (ymax - ymin)/(ny - 1) #y = [ymin + j*dy for j in range(ny)] y = np.linspace(ymin,ymax, ny) ############################ # 2. Initialize the Solution # (create an empty array the same size as the grid) ############################ # solution grid (2D array) (comments show the old way):
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
# Z = [[0 for i in range(nx)] for j in range(ny)] Z = np.zeros([nx, ny]) ########################### # 3. Calculate the Solution # (loop through the grid, apply the equation at each point) ########################### # For each pixel for i in range(nx): for j in range(ny): ####### # Progress Monitor ####### print(i, j) ###### # Perform the calculation at point (i,j): ###### #z = distance_to_origin(x[i],y[j]) z = mandelbrotAlgorithm(x[i],y[j],N,r) ###### # store the solution for point (i,j) in the solution array ###### Z[i,j] = z #note indexing change ########################### # 4. Visualize the solution array ########################### print(Z) #plt.plot(x,y) #plt.show() plt.pcolor(Z) plt.show()