the endless while loop how do i avoid it

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

 采纳的回答

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?

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by