Mandelbrot set on python using matplotlib + need some advices -


this first post here, i'm sorry if didn't follow rules

i learned python, know basics , writing famous sets , plot them, i've wrote codes hofstadter sequence, logistic sequence , succeeded in both

now i've tried writing mandelbrot's sequence without complex parameters, doing "by hand"

for exemple if z(n) complexe(x+iy) variable , c(n) complexe number (c+ik)

i write sequence {x(n)=x(n-1)^2-y(n-1)^2+c ; y(n)=2.x(n-1).y(n-1)+c}

from math import * import matplotlib.pyplot plt  def mandel(p,u):     c=5     k=5     in range(p):         c=5         k=k-10/p         n in range(p):             c=c-10/p             x=0             y=0             m in range (u):                 x=x*x-y*y + c                 y=2*x*y + k                 if sqrt(x*x+y*y)>2:                     break             if sqrt(x*x+y*y)<2:                 x=x+[c]                 y=y+[k]         print (round((i/p)*100),"%")     return (plt.plot(x,y,'.')),(plt.show()) 

p width , number of complexe parameters want, u number of iterations

this result :

http://i.stack.imgur.com/apq1k.png

i think it's bit close want.

now questions, how can make function faster? , how can make better ?

thanks lot !

a place start profile code.

https://docs.python.org/2/library/profile.html

using cprofile module or command line profiler, can find inefficient parts of code , try optimize them. if had guess without profiling it, array appending inefficient.

you can either use numpy array premade @ appropriate size, or in pure python can make array given size (like 50) , work through entire array. when fills up, append array main array. reduces number of times array has rebuilt. same done numpy array.

quick things though

if sqrt(x*x+y*y)>2: 

should become this

if x*x+y*y>4: 

remove calls sqrt if can, faster exponentiate other side 2. multiplication cheaper finding roots.

another thing this.

print (round((i/p)*100),"%") 

should become this

# print (round((i/p)*100),"%") 

you want faster code?...remove things not related plotting it.

also, break loop after comparison make same comparison...do want after comparison , break it...no need compute twice.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -