Project 5

388 days ago by dwhite2

#First I apologize for the essay that follows. In English class I am an absolute stickler when it comes to citations and #documentation. I feel that this is no different. What follows is my best attempt to be as rigorous and careful as possible #in my citations and documentation of my sources for code, equations, and inspiration for ideas. 
       
#The first drawing is my proto-fractal. I know it is somewhat crude. I included it in here to show the metamorphosis of #Dr. Angulo's mandelbrot set code and others' explanations of the Julia set into a Julia set. I will include all sources and #exactly what is their work and what is my original progress down below. #Much of the code used to create the drawing below is based on Dr. Angulo's code used to create the mandelbrot set. Skip #ahead to 2:34 in the video <http://www.dailymotion.com/video/xfjge0_cython-from-the-sage-notebook-mandelbrot-set_tech> to #see the code I used. I also feel is the simplest code along with a line by line explanation available that creates a #mandelbrot set. #In 2.1 of <http://warp.povusers.org/Mandelbrot> the author claims that if the modulus of z>2, then z will become #infinite. Hence I will choose, as Dr. Angulo did, a ball of radius 2 that will contain all points in the mandelbrot set. #The author chose 50 iterations (Dr. Angulo also used 50 iterations) to see if z leaves a ball of radius 2 so I will also #use 50 iterations. The reason for this is that the author claims that the resolution of the image is too low to have the #extra iterations reveal extra information. 
       
#This drawing is my first attempt at fractal drawing. If you went ahead to 2:34 in the link provided above you can see the #first 6 lines are an exact copy of Dr. Angulo's. I was not very comfortable with the code so in my first attempt I relied #extremely heavily on Dr. Angulo's code. At first I had no idea of why Dr. Angulo coded for a matrix so that part remained #unchanged. As always we begin iterating at n=0 so no change could be made here from Dr. Angulo's code. I found the next line #of code (the while statement) to be particularly succinct so I left that unchanged as well. As we discussed on Tuesday in #office hours I split z up into two equations and iterated them simultaneously using temp. The main achievement here was #successfully using temp to iterate two equations simultaneously. I used the equations for z that you derived for us in #class. Since I was unsure why Dr. Angulo coded for a matrix I left the last two lines of code unchanged so as not to cause #errors. 
       
def fractal(x0,y0,side,N=200,L=50,R=float(2)): m=matrix(N,N) delta=side/N for j in range(N): for k in range (N): c=complex(x0+j*delta,y0+k*delta) pp=0 qq=0 n=0 while (n<L) and (abs(pp+qq)<R): pp=(pp - qq)*(pp - qq) + c temp=2*pp*qq + c qq=temp n+=1 m[j,k]=n return m 
       
#I also used the same numbers below as Dr. Angulo did because Dr. Angulo used the optimal radius and iterations and because #the x0 and y0 he provided center the image of the mandelbrot set in the plane below. The lack of changes is due to my if it #is not broke then do not fix it mentality. #As an aside I am unsure why there are two gigantic nodes growing out of my mandelbrot set. I think it would actually be a #good mandelbrot set if they were not there. #After trying for hours to correct that flaw I did some more research online and discovered the julia set is very closely #related to the mandelbrot set so I abandoned my efforts to draw the mandelbrot set and instead concentrated on the julia #set. This is what caused me to try to draw the julia set. 
       
matrix_plot(fractal(-2,-1.5,3,400,50)) 
       
#In the block of code below I tried to combine Dr. Angulo's code, code from project 4, and code you provided me with on #Tuesday in your office. The result was a haphazard mess that I gave up on trying to figure out in several hours. I honestly #did not have a full grasp of what to do and trying to express what I understood in python was nearly an impossible task for #me. If you hit evaluate you will get an error. 
       
def mandelbrot(x,y,s,N,R=float(2)): llx=x-s/2 lly=y-s/2 for i in range(n): for j in range(n): a=llx+ih b=lly+jh c=complex(a,b*imag_I) while (n<N) and (abs(pp+qq)<R): pp=(pp - qq)*(pp - qq) + c temp=2*pp*qq + c qq=temp n+=1 P[] if bounded at (a,b): P+=green else: P+=red 
       
Traceback (click to the left of this block for traceback)
...
SyntaxError: invalid syntax
Traceback (most recent call last):                b=lly+jh
  File "", line 1, in <module>
    
  File "/tmp/tmpUh4LAV/___code___.py", line 16
    P[]
      ^
SyntaxError: invalid syntax
#After the unsuccessful attempt to use different code I decided it would be best to use work on one person's code because #combining different code results in a mess that is too difficult to untangle. #At this point I resolved to draw a julia set by altering Dr. Angulo's code to show some originality, ambition, and #creativity on my part. Again the basic code I used here can be found at 2:34 in #<http://www.dailymotion.com/video/xfjge0_cython-from-the-sage-notebook-mandelbrot-set_tech>. On #<http://www.javaview.de/vgp/tutor/fractal/PaFractalImage.html> and <http://warp.povusers.org/Mandelbrot>, I learned about #the connection between Mandelbrot and Julia sets. These explanations were not very helpful beyond demonstrating a #connection because they were not detialed enough for me. After doing some research I came across #<http://www.egregium.us/complex-julia-set-fractals/> which show that different Juila sets could be made by altering the #equation to be iterated. It also explained that c was held constant while z was the parameter in the Julia set. One Julia #set that I found particularly bedazzling was the set created by z=c*sin(z) where c=1+I*3/10. It can be found on #<http://paulbourke.net/fractals/sinjulia/>. #In order to create this set I switched z and c in the code and played around with the intial points that define z to create #an interesting plot. I changed the code to make a rectangle instead of a square in order to create more flexibility with my picture. I also split the rectangle into m*n subrectangles proportional with m and n proportional to the length and width of #the original rectangle so the resolution would be the same throughout. #I named the function after the fractal I was trying to create for simplicity's sake. Since I wanted a rectangle split into #subrectangles instead of a square split into subsquares I needed to add several extra inputs into the mandelbrot function #to create the julia function. I changed the side input into length and width inputs. I also added an M input which was the #number of sublengths I would divide the length into. N would now become the number of subwidths I divided the width into. #At this point I was still unsure about why a matrix was necessary so that portion of the code remains unchanged. Now m was #no longer an NxN matrix because I wanted subrectangles. So I changed m to an MxN matrix (this is the reason I chose M). Then #I needed to split delta up into deltax and deltay because we had rectangles and subrectangles instead of squares. The next #two lines are unchanged from Dr. Angulo's code because any substitution I could make would just be changing j,k,M, or N to #another letter. In the julia set I made c a constant and varied z as a parameter. The equation to be iterated was c*sinz and #as usual we begin at h=0. I left the next two lines unchanged because they are necessary but I do not understand what they #mean. 
       
def julia(x0,y0,length,width,M=400,N=200,L=75,R=float(50)): m=matrix(M,N) deltax=length/M deltay=width/N for j in range(M): for k in range (N): z=complex(x0+j*deltax,y0+k*deltay) c=1+.3*I h=0 while (h<L) and (abs(z)<50): z=c*sin(z) h+=1 m[j,k]=h return m 
       
#I kept the color of the plot balck and white because the design reminds me of photos of the milky way, andromeda, #triangulum, and whirlpool galaxies. #The x0 and y0 coordinates were chosen after much guesswork. The length and width are also products of guesswork with M and #N being scaled accordingly from Dr. Angulo's code. 
       
matrix_plot(julia(-2.25,-2.75,9,6,400,300,75)) 
       
#Just to show a little ambition I tried plotting the julia set using different initial points and colors to see what extra #information we could learn form the differnt plots. In particular I tried to simulate "zooming" in on a piece of it. #From here on out the code is exactly the same so I will not bore you with another explanation of the same thing. I just #changed the initial point, the length, the width, the number of sublengths, and the number of subwidths. I hope you enjoy #the following pictures. 
       
def juliaset(x0,y0,length,width,M=400,N=200,L=75,R=float(50)): m=matrix(M,N) deltax=length/M deltay=width/N for j in range(M): for k in range (N): z=complex(x0+j*deltax,y0+k*deltay) c=1+.3*I h=0 while (h<L) and (abs(z)<50): z=c*sin(z) h+=1 m[j,k]=h return m 
       
matrix_plot(juliaset(0,0,9,6,400,150,75),cmap='RdBu_r') 
       
#In the next picture I moved x0 a little to the left so we could see the galactic core 
       
def juliasetfractal(x0,y0,length,width,M=400,N=200,L=75,R=float(50)): m=matrix(M,N) deltax=length/M deltay=width/N for j in range(M): for k in range (N): z=complex(x0+j*deltax,y0+k*deltay) c=1+.3*I h=0 while (h<L) and (abs(z)<50): z=c*sin(z) h+=1 m[j,k]=h return m 
       
matrix_plot(juliasetfractal(-1,-1,10,7,400,150,75),cmap='RdBu_r') 
       
#In the next picture I tried to get a look at the galaxy itself in more detail. 
       
def whirlpool(x0,y0,length,width,M=400,N=200,L=75,R=float(50)): m=matrix(M,N) deltax=length/M deltay=width/N for j in range(M): for k in range (N): z=complex(x0+j*deltax,y0+k*deltay) c=1+.3*I h=0 while (h<L) and (abs(z)<50): z=c*sin(z) h+=1 m[j,k]=h return m 
       
matrix_plot(whirlpool(-1,-1,2.5,3.5,400,150,75),cmap='RdBu_r')