绘制黑白 mandelbrot 集

Plot black and white mandelbrot set

以下代码摘自here

我正在尝试对下面的代码进行以下更改

If c is not part of the set, plot a white pixel, and if it is part of the set, plot a black pixel.

我得到这个输出:

但我想像这样绘制黑白 mandelbrot 图像:

Use the imshow command to plot your image

有人能帮忙吗?

    m=601; 
    n=401;
    count_max = 200;

  %Change the range of x's and y's here 
  x_max =   1;
  x_min = - 2;
  y_max =   1;
  y_min = - 1;

%
%  Create an array of complex sample points in [x_min,x_max] + [y_min,y_max]*i.
%
  I = ( 1 : m );
  J = ( 1 : n );
  X = ( ( I - 1 ) * x_max + ( m - I ) * x_min ) / ( m - 1 );
  Y = ( ( J - 1 ) * y_max + ( n - J ) * y_min ) / ( n - 1 );
  [ Zr, Zi ] = meshgrid ( X, Y );
  C = complex ( Zr, Zi );
%
%  Carry out the iteration.
%
  epsilon = 0.001;

  Z = C;
  ieps = 1 : numel ( C );

  %Iterate through 1 to maximum no. of iterations
  for i = 1 : count_max
    Z(ieps) = Z(ieps) .* Z(ieps) + C(ieps); 
    W(ieps) = exp ( - abs ( Z(ieps) ) );
    ieps = ieps ( find ( epsilon < W(ieps) ) );
  end
%
%  Display the data.
%
  d = log ( abs ( Z ) );
  t = delaunay ( Zr, Zi );
%
%  To see individual pixels, use 'flat' color.
%
 h = trisurf ( t, Zr, Zi, d, 'FaceColor', 'flat', 'EdgeColor', 'flat' );
%  h = trisurf ( t, Zr, Zi, d, 'FaceColor', 'interp', 'EdgeColor', 'interp' );

  view ( 2 )
  axis equal
  axis on
  title_string = sprintf ( 'Mandelbrot set, %d x %d pixels, %d iterations', ...
    m, n, count_max );
  title ( title_string )
  xlabel ('Real Axis');
  ylabel ('Imaginary Axis');
%  Terminate.

属于集合和不属于集合的区别无非就是 epsilon < W(ieps) 并且您已经在代码中使用了它。图中的颜色信息来自 trisurf ( t, Zr, Zi, d,...).

的第四个参数

所以我建议在 trisurf 命令之前执行以下操作:

d(1 : numel ( C )) = 1;
d(ieps) = 0;

这首先将所有元素设置为 1,然后根据收敛测试的最后一次迭代中已经计算出的条件,将属于该集合的那些元素设置为零。

要绘制黑白图,只需在 trisurf 之后使用 colormap gray

希望对您有所帮助。