Olivia
Olivia

Reputation: 37

Creating fractal image in a plot MATLAB but the plot is empty

I am writing a function to create a fractal image in a plot. When I run my code, a plot pops up but it is empty. I think the issues lies somewhere in my if/elseif statements, but I am having a hard time finding it. My code is:

function [] = fractal(x, y, N)
close all
start = [x, y];
X = zeros(N+1,1);
Y = zeros(N+1,1);
X = start;
hold on
for i = 1:N
prob = rand;
if prob >= 0.01
    X(i+1,:) = 0;
    Y(i+1,:) = 0.16*y(1);
elseif prob == 0.02:0.86
    X(i+1,:) = (0.85*x(i))-(0.04*y(i));
    Y(i+1,:) = (-0.04*x(i))+(0.85*y(i))+1.6;
elseif prob == 0.87:0.94
    X(i+1,:) = (0.2*x(i))-(0.26*y(i));
    Y(i+1,:) = (0.23*x(i))+(0.22*y(i))+1.6;
elseif prob == 0.95:1.0
    X(i+1,:) = (-0.15*x(i))+(0.28*y(i));
    Y(i+1,:) = (0.26*x(i))+(0.24*y(i))+0.44;
plot(X(i,:),Y(i,:),'.','Markersize',1)
axis equal 
    end
end
end

When I run my code with

>> fractal(1,1,1000)

... a plot comes up but it is empty.

Upvotes: 2

Views: 268

Answers (1)

rayryeng
rayryeng

Reputation: 104555

Yup... it's your if statements, but there are more issues with your code though but we will tackle those later. Let's first address your if statements. If you want to compare in a range of values for examples, you need use the AND (&&) statement. In addition, you should place your plot code outside of any if/elseif/else statement. You currently have it inside your last elseif statement so plot will only run if the last condition is satisfied.

To be explicit, if you wish to compare if a value is in between a certain range, do something like:

if (prob >= a && prob < b)

and for elseif:

elseif (prob >= a && prob < b)

a and b are the lower and upper limits of what you want to compare. This includes a but excludes b in the comparison.

I also have a several comments and recommendations with your current code in order to get this to work:

  1. You run your function with a single x and y value, but you are trying to access this x and y in your for loop as if these were arrays. I'm assuming this is recursive in nature so you need to actually use X and Y in your if/else conditions instead of x and y.
  2. Since you are using single values, it is superfluous to use : to access the second dimension. Just leave that out.
  3. You create X and Y but then overwrite X to be the starting location as a 2D array... I think you meant to replace X and Y's first element with the starting location instead.
  4. Your first if statement I think is incorrect. You'd want to access Y(i) not Y(1) no?... given the behaviour of your code thus far.
  5. Your first condition will definitely mess things up for you. This is saying that as long as the value is greater than or equal to 0.01, execute that statement. Otherwise, try and execute the other conditions which may in fact never work because you are looking for values that are greater than 0.01 where the first condition already handles that for you. I assume you meant to check if it was less than 0.01 instead.
  6. Doing value comparecondition array in MATLAB means that this statement is true provided that any one of the values in array matches the condition provided by value. This will have unintended side effects with your current code.
  7. Make sure that your ranges covered for each if statement are continuous (i.e. no gaps or disconnects between ranges). Right now, you are checking for values in 0.01 intervals. rand generates random values between 0 and 1 exclusive. What if you had a value of 0.15? None of your if conditions handle this so you need to use what I talked about above.
  8. You are most likely getting a blank plot because your MarkerSize attribute is very small.... you set it to 1 pixel. Unless you have super human vision, you can't really visualize this. Make the MarkerSize larger.
  9. Use drawnow; after you plot to immediately update the results to screen.

Therefore, with refactoring your code, you should make it look something like this:

function [] = fractal(x, y, N)
close all
start = [x, y];
X = zeros(N+1,1);
Y = zeros(N+1,1);
%// Change - Initialize first elements of X and Y to be the starting positions
X(1) = start(1);
Y(1) = start(2);
hold on
for i = 1:N
    prob = rand;
    if prob <= 0.01 %// Change
        X(i+1) = 0;
        Y(i+1) = 0.16*Y(i); %// Change
    elseif (prob > 0.01 && prob <= 0.86) %// Change
        X(i+1) = (0.85*X(i))-(0.04*Y(i)); %// Change
        Y(i+1) = (-0.04*X(i))+(0.85*Y(i))+1.6; %// Change
    elseif (prob > 0.86 && prob <= 0.94) %// Change
        X(i+1) = (0.2*X(i))-(0.26*Y(i)); %// Change
        Y(i+1) = (0.23*X(i))+(0.22*Y(i))+1.6; %// Change
    elseif (prob > 0.94 && prob <= 1.0) %// Change
        X(i+1) = (-0.15*X(i))+(0.28*Y(i)); %// Change
        Y(i+1) = (0.26*X(i))+(0.24*Y(i))+0.44; %// Change
    end

    %// Change - move outside of if/else blocks
    %// Also make marker size larger
    plot(X(i),Y(i),'.','Markersize',18); %// Change
    axis equal 

    %// Add just for kicks
    drawnow;
end

end

I now get this figure when I do fractal(1,1,1000):

enter image description here

.... cool fractal btw!

Upvotes: 3

Related Questions