曼德托罗集

Mandelbrot集是二维复平面上的分形数集,1980前后发现,堪称人类认识数学存在的一个里程碑。它是由一个简单复函数f(Z)=Z2+C迭代运算而形成的收敛数集(Z是迭代复变数,C是点位复常数),谁做这样的迭代运算都能得到形态一样的数集,见下图,这便是Mandelbrot集。

  • Mandelbrot集1

import numpy as np
import matplotlib.pyplot as plt

def iterator(c,r,max_iter):#定义逃逸时间函数,c为初始值,r为收敛半径,max_iter为最大迭代次数,返回逃逸时间
    z=c #初始值
    for iter in range(0,max_iter,1):
        if abs(z)>r:break
        z=z**2+c
    return iter

def plot_mandelbrot(): #定义绘制mandelbrot图像
    X=np.linspace(-1.75,1.05,500) #实部范围,5000这个数要量力而行
    Y=np.linspace(-1.25,1.25,500) #虚部范围,5000这个数要量力而行
    real,image=np.meshgrid(X, Y) #生成网格点坐标矩阵。
    c=real+image*1j #构造复数
    mandelbrot_set = np.frompyfunc(iterator, 3, 1)(c, 1.5, 100).astype(np.float) #frompyfunc(func, nin, nout),其中func是需要转换的函数,nin是函数的输入参数的个数,nout是此函数的返回值的个数,frompyfunc把Python里的函数(可以是自写的)转化成ufunc
    plt.figure(dpi=1000) #dpi设置分辨率尽可能高,细节显示更炫
    plt.imshow(mandelbrot_set,extent=[-1.35, 1.35, -1.25, 1.25]) #extent用来调节显示框大小比例
    #plt.axis('off') #是否显示坐标轴
    plt.show()

if __name__=="__main__":
    plot_mandelbrot()

  • Mandelbrot集2

import numpy as np
import pylab as pl
import time
from matplotlib import cm

def iter_point(c):
    z = c
    for i in range(1, 100): # 最多迭代100次
        if abs(z)>2: break # 半径大于2则认为逃逸
        z = z*z+c
    return i # 返回迭代次数

def draw_mandelbrot(cx, cy, d):
    """
 绘制点(cx, cy)附近正负d的范围的Mandelbrot
 """
    x0, x1, y0, y1 = cx-d, cx+d, cy-d, cy+d
    y, x = np.ogrid[y0:y1:200j, x0:x1:200j]
    c = x + y*1j
    start = time.perf_counter()
    mandelbrot = np.frompyfunc(iter_point,1,1)(c).astype(np.float)
    print ("time=", time.perf_counter()-start)
    pl.imshow(mandelbrot, cmap=cm.Blues_r, extent=[x0,x1,y0,y1])
    pl.gca().set_axis_off()

x,y = 0.27322626, 0.595153338

pl.subplot(231)
draw_mandelbrot(-0.5,0,1.5)
for i in range(2,7):    
    pl.subplot(230+i)
    draw_mandelbrot(x, y, 0.2**(i-1))
pl.subplots_adjust(0.02, 0, 0.98, 1, 0.02, 0)
pl.show()

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一叶屋檐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值