有没有在Matlab中构造Mandelbrot集的更简单方法? [英] Is there a simpler way to construct Mandelbrot set in Matlab?

查看:187
本文介绍了有没有在Matlab中构造Mandelbrot集的更简单方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面显示的用于绘制 Mandelbrot集的代码,我认为我的代码有点多余构建 Matrix M.在 Python 中,我知道有一种干净的方法可以做到这一点,

The code shown below for drawing a Mandelbrot set, I think my code a bit redundancy to construction the Matrix M. In Python I know there is a clean way do this,

M = [[mandel(complex(r, i)) for r in np.arange(-2, 0.5,0.005) ] for i in np.range(-1,1,0.005)]

在Matlab中有类似的方法吗?

Is there a similar way do this in Matlab?

function M=mandelPerf()
rr=-2:0.005:0.5;
ii=-1:0.005:1;
M = zeros(length(ii), length(rr));
id1 = 1;
for i =ii
    id2 = 1;
    for r = rr
        M(id1, id2) = mandel(complex(r,i));
        id2 = id2 + 1;
    end
    id1 = id1 + 1;
end
end

function n = mandel(z)
n = 0;
c = z;
for n=0:100
    if abs(z)>2
        break
    end
    z = z^2+c;
end
end

推荐答案

您至少可以避免for循环:

You could at least avoid the for-loop:

function M=mandelPerf()

rr = -2:0.005:0.5;
ii = -1:0.005:1;

[R,I] = meshgrid(rr,ii);
M = arrayfun(@(x) mandel(x), R+1i*I);

end

function n = mandel(z)
n = 0;
c = z;
for n=0:100
    if abs(z)>2
        break
    end
    z = z^2+c;
end
end

这篇关于有没有在Matlab中构造Mandelbrot集的更简单方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆