functionret =mandel(x,y,steps,init_value)% mandel - a function to generate the Mandelbrot Set.% Written by Shlomi Fish, 2001%% This file is under the public domain.% Assign some default values to the parameters% nargin is an internal Matlab variable that specifies how many% parameters were passed to the function.ifnargin<1x=100;endifnargin<2y=100;endifnargin<3steps=10;endifnargin<4init_value=0;end% Generate the coordinates in the complex plane[X,Y]=meshgrid(-2:(4/x):2,-2:(4/y):2);% Combine them into a matrix of complex numbersZ=X+j*Y;% Retrieve the dimensions of Zdims=size(Z);% The length in the x directionx_len=dims(1);% The length in the y directiony_len=dims(2);% value is initialized to init_value in every point of the planevalue=ones(x_len,y_len)*init_value;% In the beginning all points are considered as part of the Mandelbrot% set. Thus, they are initialized to zero.ret=zeros(x_len,y_len);% The mask which indicates which points have already overflowed, is set% to zero, to indicate that none have so far.mask=zeros(x_len,y_len);% Perform the check "steps" timesforstep=1:steps% For every point with a mandel value of "v" and a coordinate of "z"% perform v <- (v ^ 2) + z%% .* is an element-by-element multiplication of two matrixes of the same size.% (regular * indicates matrix multiplication)value=(value.*value)+Z;% Retrieve the points that overflowed in this iteration% An overflowed point has a mandel value with an absolute value greater% than 2.current_mask=(abs(value)>2);% Update the mask. We use "or" in order to avoid a situation where% 1 and 1 become two or so.mask=or(mask,current_mask);% Upgrade the points in the mask to a greater value in the returned% Mandelbrot-map. ret=ret+mask;% Zero the points that have overflowed, so they will not propagate% to infinity.value=value.*not(current_mask);end% Now ret is ready for prime time so we return it.