Modeling Project #5

390 days ago by vsbhatia10

# The following equations came form Introduction to Mathematical Modeling Using Discrete Dynamical Systems by Frederick Marotto. # P was generated randomly. def BarnsleyX(x,y,rand): p=rand if(p<=.01): return .5 elif(p<=.11): return -.15*x+.28*y+.575 elif(p<=.23): return .2*x-.26*y+.4 return .85*x+.04*y+.075 def BarnsleyY(x,y,rand): p=rand if(p<=.01): return .16*y elif(p<=.11): return .26*x+.24*y-.086 elif(p<=.23): return .23*x+.22*y+.045 return -.04*x+.85*y+.18 Array=[] pointx=1 pointy=1 Array.append([pointx, pointy]) for x in range(50000): r=random() x0=pointx pointx=Barnsleyx(pointx, pointy, r) pointy=Barnsleyy(x0, pointy, r) Array.append([pointx, pointy]) show (scatter_plot(Array, markersize=.0028)) 
       
# The following code adjusts the y-values from above, thereby allowing for the leaf to curve differently. def BarnsleyX(x,y,rand): p=rand if(p<=.01): return .5 elif(p<=.11): return -.15*x+.17*y+.575 elif(p<=.23): return .2*x-.25*y+.4 return .85*x+.04*y+.075 def BarnsleyY(x,y,rand): p=rand if(p<=.01): return .16*y elif(p<=.11): return .26*x+.24*y-.086 elif(p<=.23): return .23*x+.09*y+.045 return -.04*x+.85*y+.18 Array=[] pointx=1 pointy=1 Array.append([pointx, pointy]) for x in range(50000): r=random() x0=pointx pointx=BarnsleyX(pointx, pointy, r) pointy=BarnsleyY(x0, pointy, r) Array.append([pointx, pointy]) show (scatter_plot(Array, markersize=.0028)) 
       
# This fractal is an example of a Mandelbrot Set. def mandelbrot(imaginary,real): w=complex(real,imaginary) z=complex(0,0) for r in range(1,99): z=(z*z)+w if(abs(z)>2): return r return 0 Array=[] for s in range(100): Array.append([]) realMin=-1 realMax=.5 imaginaryMin=-1 imaginaryMax=1 for y in range(200): w_imaginary=imaginaryMin+y*(imaginaryMax-imaginaryMin)/200 for x in range(300): w_real=realMin+x*(realMax-realMin)/300 mand=mandelbrot(w_imaginary,w_real) if(mand>0): Array[mand].append([x,y]) mandPlot=scatter_plot(Array[0],markersize=.395,facecolor='green',edgecolor='green') for k in range(1,99): colors=10*k/100 from sage.plot.colors import green if(len(Array[k])>0): mandPlot=mandPlot+scatter_plot(Array[k],markersize=.35,facecolor=(green.lighter(fraction=colors)).html_color(),edgecolor=(green.lighter(fraction=colors)).html_color()) show(mandPlot) 
       
# Here, the parameters on the graph and the values have been adjusted to provide a wider view of the above graph. def mandelbrot(imaginary,real): w=complex(real,imaginary) z=complex(0,0) for r in range(1,99): z=(z*z)+w if(abs(z)>2): return r return 0 Array=[] for s in range(100): Array.append([]) realMin=-2 realMax=1 imaginaryMin=-1.5 imaginaryMax=1.5 for y in range(250): w_imaginary=imaginaryMin+y*(imaginaryMax-imaginaryMin)/250 for x in range(250): w_real=realMin+x*(realMax-realMin)/250 mand=mandelbrot(w_imaginary,w_real) if(mand>0): Array[mand].append([x,y]) mandPlot2=scatter_plot(Array[0],markersize=.71,facecolor='gray',edgecolor='gray') for t in range(1,99): colors=4*t/100 from sage.plot.colors import gray if(len(Array[t])>0): mandPlot2=mandPlot2+scatter_plot(Array[t],markersize=.75,facecolor=(gray.lighter(fraction=colors)).html_color(),edgecolor=(gray.lighter(fraction=colors)).html_color()) show(mandPlot2) 
       
# The following is a seirpeinksi triangle which was introduced in class. def midFormula(Coord1, Coord2): return [(Coord1[0]+Coord2[0])/2,(Coord1[1]+Coord2[1])/2] def Triangle(tri1,tri2,tri3,x0,iterations): seirpinski=[tri1,tri2,tri3] points=[x0] x=x0 for i in range(iterations): rand=floor(3*random()) x=midFormula(seirpinski[rand],x) points.append(x) return points T=Triangle([-.5,0],[3,2],[0,5],[.2,.4],100000) show(list_plot(T,size=1)) 
       
# The following was derived from a seirpinski triangle. The midpoint formula subtracted the coordinates as opposed to adding them as in the previous example. This resulted in the following shape. def midFormula(Coord1, Coord2): return [(Coord1[0]-Coord2[0])/2,(Coord1[1]-Coord2[1])/2] def Snowflake(snow1,snow2,snow3,x0,iterate): InverseSeirpinski=[snow1,snow2,snow3] startPoint=[x0] x=x0 for i in range(iterate): rand=floor(3*random()) x=midFormula(InverseSeirpinski[rand],x) startPoint.append(x) return startPoint S=Snowflake([-.5,0],[3,2],[0,5],[.2,.4],100000) show(list_plot(S,size=1))