Project #5

390 days ago by ashmarie1016@aol.com

#Ashley Vega #Project #5 
       
def fern(x,y): r = random() if r<.01: return [0,.2*y] elif r < .08: return [.35*x-.19*y, .38*x+.44*y+2.3] elif r < .15: return [-.35*x+.54*y, .41*x+.36*y+.48] else: return [.57*x+.08*y,-.03*x+.79*y+2.6] def fern_orbit(n): traj = [[0,0]] for i in range(n): nt = fern(traj[-1][0],traj[-1][1]) traj.append(nt) return traj 
       
fern (0,4) 
       
[0.320000000000000, 5.76000000000000]
[0.320000000000000, 5.76000000000000]
show(points(fern_orbit(10000),pointsize=3),axes=False) 
       
#This fern looks like a maple leaf. 
       
# code used from http://trac.sagemath.org/sage_trac/ticket/8423 
       
def fern(x,y): r = random() if r<.01: return [0,.16*y] elif r < .08: return [.2*x-.26*y, .23*x+.22*y+1.6] elif r < .15: return [-.15*x+.28*y, .26*x+.24*y+.44] else: return [.85*x+.04*y,-.04*x+.85*y+1.6] def fern_orbit(n): traj = [[0,0]] for i in range(n): nt = fern(traj[-1][0],traj[-1][1]) traj.append(nt) return traj 
       
fern(1,1) 
       
[0.890000000000000, 2.41000000000000]
[0.890000000000000, 2.41000000000000]
show(points(fern_orbit(10000),pointsize=2),axes=False) 
       
#This fern looks like a Christmas tree tilted to the right. 
       
# code used from http://trac.sagemath.org/sage_trac/ticket/8423 
       
def butterfly2d(): """" EXAMPLE: sage: butterfly2d() """ g = Graphics() x1, y1 = 0, 0 from math import sin, cos, exp, pi for theta in srange( 0, 10*pi, 0.01 ): r = exp(cos(theta)) - 2*cos(4*theta) + sin(theta/12)^5 x = r * cos(theta) # Convert polar to rectangular coordinates y = r * sin(theta) xx = x*6 + 25 # Scale factors to enlarge and center the curve. yy = y*6 + 25 if theta != 0: l = line( [(x1, y1), (xx, yy)], rgbcolor=hue(theta/7 + 4) ) g = g + l x1, y1 = xx, yy g.show(dpi=100, axes=False) 
       
butterfly2d() 
       
#This is a butterfly. 
       
# code used from: http://wiki.sagemath.org/pics 
       
def butterfly2d(): g = Graphics() x1, y1 = 3, 6 from math import sin, cos, exp, pi for theta in srange( 0, 10*pi, 0.01 ): r = exp(cos(theta)) - 2*cos(4*theta) + sin(theta/12)^5 x = r * cos(theta) # Convert polar to rectangular coordinates y = r * sin(theta) xx = x*0.523 + 7 # Scale factors to enlarge and center the curve. yy = y*0.39 + 16 if theta != 0: l = line( [(x1, y1), (xx, yy)], rgbcolor=hue(theta/7 + 4) ) g = g + l x1, y1 = xx, yy g.show(dpi=100, axes=False) 
       
butterfly2d() 
       
#This is a smaller butterfly and it is farther away. 
       
# code used from: http://wiki.sagemath.org/pics 
       
def butterfly2d(): g = Graphics() x1, y1 = 0.65, 1.38 from math import sin, cos, exp, pi for theta in srange( 0, 21*pi, 0.03 ): r = exp(cos(theta)) - 2*cos(17*theta) + sin(theta/4)^2 x = r * cos(theta) # Convert polar to rectangular coordinates y = r * sin(theta) xx = x*3.6 + 0.83 # Scale factors to enlarge and center the curve. yy = y*0.49 + 7 if theta != 0: l = line( [(x1, y1), (xx, yy)], rgbcolor=hue(theta/7 + 4) ) g = g + l x1, y1 = xx, yy g.show(dpi=100, axes=False) 
       
butterfly2d() 
       
#This looks like a flower. 
       
# code used from: http://wiki.sagemath.org/pics 
       
%cython import numpy as np cimport numpy as np def mandelbrot_cython(float x0,float x1,float y0,float y1, int N=400, int L=25, float R=2): cdef double complex c, z, I cdef float deltax, deltay, R2 = R*R cdef int h, j, k cdef np.ndarray[np.uint16_t, ndim=2] m m = np.zeros((N,N), dtype=np.uint16) I = complex(0,1) deltax = (x1-x0)/N deltay = (y1-y0)/N for j in range(N): for k in range(N): c = (x0+j*deltax)+ I*(y0+k*deltay) z=0 h=0 while (h<L and z.real**2 + z.imag**2 < R2): z=z*z+c h+=1 m[j,k]=h return m 
import pylab x0_default = -2 y0_default = -1.5 side_default = 3.0 side = side_default x0 = x0_default y0 = y0_default options = ['Reset','Upper Left', 'Upper Right', 'Stay', 'Lower Left', 'Lower Right'] @interact def show_mandelbrot(option = selector(options, nrows = 2, width=8), N = slider(100, 1000,100, 300), L = slider(20, 300, 20, 60), plot_size = slider(2,10,1,6), auto_update = False): global x0, y0, side if option == 'Lower Right': x0 += side/2 y0 += side/2 elif option == 'Upper Right': y0 += side/2 elif option == 'Lower Left': x0 += side/2 if option=='Reset': side = side_default x0 = x0_default y0 = y0_default elif option != 'Stay': side = side/2 time m=mandelbrot_cython(x0 ,x0 + side ,y0 ,y0 + side , N, L ) pylab.clf() pylab.imshow(m, cmap = pylab.cm.gray) time pylab.savefig('mandelbrot.png') 
       

Click to the left again to hide and once more to show the dynamic interactive window

# code from http://wiki.sagemath.org/interact/fractal 
       
import pylab x0_default = -0.23 y0_default = -1.2 side_default = 3.0 side = side_default x0 = x0_default y0 = y0_default options = ['Reset','Upper Left', 'Upper Right', 'Stay', 'Lower Left', 'Lower Right'] @interact def show_mandelbrot(option = selector(options, nrows = 3, width=5), N = slider(100, 1000,100, 300), L = slider(20, 300, 20, 60), plot_size = slider(2,10,1,6), auto_update = False): global x0, y0, side if option == 'Lower Right': x0 += side/0.65 y0 += side/-0.56 elif option == 'Upper Right': y0 += side/0.65 elif option == 'Lower Left': x0 += side/-0.56 if option=='Reset': side = side_default x0 = x0_default y0 = y0_default elif option != 'Stay': side = side/-0.65 time m=mandelbrot_cython(x0 ,x0 / side ,y0 ,y0 / side , N, L ) pylab.clf() pylab.imshow(m, cmap = pylab.cm.gray) time pylab.savefig('mandelbrot.png') 
       

Click to the left again to hide and once more to show the dynamic interactive window

#This looks like a cascade of water. 
       
#code from http://wiki.sagemath.org/interact/fractal 
       
f(z) = z^5 + z - 1 + 1/z complex_plot(f, (-2, 2), (-2, 2)) 
       
#idea from: http://www.walkingrandomly.com/?cat=24 
       
f(z)=z^3-z+3+1/z complex_plot(f, (-1,1), (-2,2)) 
       
#idea from: http://www.walkingrandomly.com/?cat=24 
       
f(z)=(z^2)/(-z+3) complex_plot(f, (-4,7), (-4,4)) 
       
#idea from: http://www.walkingrandomly.com/?cat=24 
       
f(z)=(z^4)+(z^0.5)/(z^6) complex_plot(f, (4,16), (1,9)) 
       
#idea from: http://www.walkingrandomly.com/?cat=24