the endless while loop how do i avoid it

1 view (last 30 days)
I have the code below for julia sets. it is supposed to run for less than 3 minutes. However it has kept the computer busy for over 30 mins, i had to stop it with task manager.
How do i stop this endless loop and get a result.
c=-0.835 - 1i*0.232;
x=-2:0.008:2;
y=-2:0.008:2;
[X,Y]=meshgrid(x,y);
f=z.^2+c;
n_max=256;
b=10000; %threshold. stop an iteration when |z|==b.
z=(X + Y*1i);
M=zeros(500,500);
for d = 1:500
for e=1:500
n=0;
while n<n_max & abs(z)<b
z=z.^2+c;
n=n+1;
end
if abs(z)<b
M(d,e)=n;
end
end
end
colormap prism(256)
image([-2 -2], [-2 -2],M);
colorbar;
axis xy

Accepted Answer

Walter Roberson
Walter Roberson on 8 Oct 2012
You construct your X and Y as a grid of data, so your z then becomes a grid of data. abs(z)<b is then testing the entire grid and is true only if all of the absolute values are < b. Then later you have if abs(z)<b which again will be only if all of the absolute values are < b. In which situation you record a single n in M(d,e). And then you resume your "for" loops without having changed z. In this construct, as soon as one abs(z) becomes >= b, the "while" will stop, the "if" after will fail, and you are going to be just looping on d and e.
Have you considered using the debugger or some "disp" statements to figure out where it is getting stuck?

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!