What am I doing wrong?
10 views (last 30 days)
Show older comments
I'm writing a program that will display an image of the mandelbrot set, but I seem to be stuck when it comes to creating the while loop that will evaluate the loop body whenever k is less than max_esctime AND not all of the entries in the done array are true. In addition, when I run the function as it is below, the while loop just seems to keep going on and on.I also use ~exist in the beginning to default the values in case the user left an empty matrix or input some other numbers for the inputs. The name of the function is mandelbrot. What seems to be the problem? Any suggestions/help is greatly appreciated. One other thing, I have to use vectorization instead of if statements. Thank you! Below is the function declaration and the code:
function [E,image]= mandelbrot(limits,nx,ny,max_esctime)
%Inputs:
%limits=vector of rectangular region limits, as in axis()
%nx=The number of points (pixels) in the x-direction
%ny=The number of points (pixels) in the y-direction
%max_esctime=The maximum number of iterations in the sequence allowed when
%calculating the escape times
%Outputs:
%E=An ny-by-nx array containing the real escape times for each pixel.
%image=shows an image of the mandelbrot set
if ~exist('limits','var') || isempty(limits)
limits=[-2.0 0.5 -1.2 1.2]
~exist('nx','var') || isempty(nx)
nx=1000
~exist('ny','var') || isempty(ny)
ny=1000
~exist('max_esctime','var') || isempty(max_esctime)
max_esctime=1000
end
x = linspace(limits(1), limits(2), nx);
y = linspace(limits(4), limits(3), ny);
[X,Y] = meshgrid(x,y);
c = X + 1i*Y;
E= zeros(size(c));
Z= zeros(size(c));
k=0;
done= false(size((c)))
%up to this point everything is fine
%done is supposed to be an ny-by-nx array of all logical false values. This array will flag as “done” any values of c for which we already know the escape time. In the beginning we don’t know anything, so done should contain all false values. However, I don't know if this is the right syntax for that.
%I am asked to create a while loop that will evaluate the loop body whenever k is less than max_esctime and not all of the entries in the done array are true. Note that all(done(:)) will return true if and only if all entries in the done array are true. Inside the loop body, I have to perform the following: i. Replace each element of Z with its square plus the corresponding element of C. In other words, replace the current z with z^2 + c for each different value of c. Do this using vectorization: do not loop over all values of Z, but instead use appropriate array operations to process the whole array Z at once. ii. Increment the sequence index variable k. iii. Create an ny-by-nx logical array called new whose elements are true when the corresponding element of abs(Z) is greater than 2 and the corresponding element of done is false. Again, I have to use use vectorization here instead of looping through all values of Z and done. The true elements of this array new will represent all “newly escaped” sequences, that is, all sequences whose integer escape times are equal to the current value of the loop variable k.Step(iv): Use logical indexing to assign the appropriate real escape times to those elements of E given by the true elements of new. This assignment is simply:E(new) =k-log(log2(abs(Z(new))));Step(v) asks to update the done array by setting each element to true if either it was already true or the corresponding element of new is true. What would the syntax be or how would I approach this? I get stuck on this step. I guess my major problem is knowing how to use logical operators and loops.
if k<1000 & ~all(done(:))
while k<1000 & ~all(done:))
Z= Z.^2 + c;
new = logical(abs(Z) > 2 & done == false);
k=k+1;
E(new) = k - log(log2(abs(Z(new))));
end
%Is the syntax for step (iii), or any of the steps for that matter, correct? If not, then what would the syntax be?
2 Comments
Jan
on 19 Oct 2016
Please format your code properly using the "{} Code" button. Currently it is not readable and you cannot assume that the readers are able to understand it.
Accepted Answer
Jan
on 19 Oct 2016
Edited: Jan
on 19 Oct 2016
The 2nd line of the function is:
Inputs:
without a comment character at the start. Therefore the code cannot run at all and you must get an error message. Please post the code, which you actually run. Otherwise trying to fix it will require wild guessing.
The loop looks strange:
done = false(size((c)))
while k<1000 & ~all(done(:))
Z= Z.^2 + c
new = logical(abs(Z) > 2 & done == false)
k=k+1
end
new is not used at all and done does not change its value, so there is no need to check its value in the loop condition.
Displaying the results in each iteration will take a lot of time. Better set semicolons behind the lines to suppress the output.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!