加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MandelbrotSet.py 1.07 KB
一键复制 编辑 原始数据 按行查看 历史
Nick Crump 提交于 2015-11-27 10:31 . computational physics
"""
this generates a Mandelbrot set using a simple slow method
"""
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
# input parameters
# ------------------------------
# set xy-domain size
xmin = -1.8
xmax = 0.8
ymin = -1.0
ymax = 1.0
# set xy-grid size
xpts = 200
ypts = 200
# set max iterations
maxn = 20
# ------------------------------
# iterate over Mandelbrot equation
# z = z^2 + c for complex numbers c and z
# start timer
t0 = datetime.now()
# initialize variables
im = np.complex(0,1)
z0 = np.complex(0,0)
x = []
y = []
# get grid
xgrid = np.linspace(xmin,xmax,xpts)
ygrid = np.linspace(ymin,ymax,ypts)
# evaluate grid points
for i in xgrid:
for j in ygrid:
c = i + im*j
z = z0
k = 0
# if abs(z) stays less than about 2 then point is in set
while abs(z) < 2 and k < maxn:
z = z*z + c
k += 1
if k >= maxn:
x.append(i)
y.append(j)
# make plot
plt.plot(x,y,'.k')
# print elapsed time
t1 = datetime.now()
print '\nelapsed time:'
print t1-t0
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化