Problem with Generating Barnsley Fern Fractal in MATLAB

In summary, the code is as follows:The code is as follows:function fern()AI = [0 0 ; 0 0.16]; AII = [0.85 0.04 ; -0.04 -0.85 ] ; AIII = [0.2 -0.26 ; 0.23 0.22 ] ; AIV = [-0.15 0.28 ; 0.26 0.24 ];BI = [ 0 ; 0];BII = [ 0 ; 1.6];BIII = [ 0 ; 1.6];BIV = [
  • #1
Euler2718
90
3
The code is as followed:

Matlab:
function fern()

AI = [0  0 ; 0 0.16];
  
AII = [ 0.85  0.04 ; -0.04 -0.85 ] ;
  
AIII = [ 0.2  -0.26 ; 0.23 0.22 ] ;
  
AIV = [-0.15  0.28 ; 0.26 0.24 ];BI = [ 0 ; 0];
BII = [ 0 ; 1.6];
BIII = [ 0 ; 1.6];
BIV = [0 ; 0.44];

N = 10000;
I = 50;

H = zeros(N,2);

for n=1 : N
  
    x = rand;
    y = rand;
    T = [x;y];
 
  
    for i=1 : I
      
        p = rand;
      
        if p < 0.01
            %disp('Scheme 1')
            S1x = AI*T + BI;
            S1y = AI*T + BI;
            x = S1x(1);
            y = S1y(2);
        elseif p < 0.08
            %disp('Scheme 2')
            S2x = AII*T + BII;
            S2y = AII*T + BII;
            x = S2x(1);
            y = S2y(2);
        elseif p < 0.15
            %disp('Scheme 3')
            S3x = AIII*T + BII;
            S3y = AIII*T + BII;
            x = S3x(1);
            y = S3y(2);
        else
            %disp('Scheme 4')
            S4x = AIV*T + BIV;
            S4y = AIV*T + BIV;
            x = S4x(1);
            y = S4y(2);
        end
        H(n,1) = x;
        H(n,2) = y;
    end
end

X = H(:,1);
Y = H(:,2);
plot(X,Y,'.')

I have no idea why it isn't working. Is not the iterative process correct? I ran it once and got the picture below. I'm at the end of the road here, and on a deadline, any help would be appreciated.

Screenshot_2016-09-21_20-49-37.png
 
Last edited by a moderator:
Physics news on Phys.org
  • #3
How can I copy your code to MATLAB? When I simply copied and pasted, it put your whole program as one single line in MATLAB, then added a URL reference link below it.
 
  • #4
Image Analyst said:
How can I copy your code to MATLAB? When I simply copied and pasted, it put your whole program as one single line in MATLAB, then added a URL reference link below it.

Try copying to another editor that recognizes newline chars as line separators and will then save to the windows crlf format (carriage return/linefeed line terminator).

This is a common problem when moving text from unix to windows and vice versa (in which case you'll see every other line as a blank line)
 

1. How do I generate a Barnsley Fern Fractal in MATLAB?

To generate a Barnsley Fern Fractal in MATLAB, you can use the "barnsley" function, which takes in the number of iterations as an input and outputs the x and y coordinates of the points in the fractal.

2. Why is my Barnsley Fern Fractal not displaying correctly in MATLAB?

There could be several reasons for this issue. It could be due to incorrect input parameters, such as the number of iterations or the scaling factor. It could also be because of a mistake in the code or the use of incorrect functions. Make sure to double-check your code and input parameters to ensure they are correct.

3. Can I customize the colors and size of my Barnsley Fern Fractal in MATLAB?

Yes, you can customize the colors and size of your Barnsley Fern Fractal in MATLAB by using the "scatter" function to plot the points with different colors and sizes. You can also use the "xlim" and "ylim" functions to adjust the axes limits and resize the fractal.

4. How can I save my Barnsley Fern Fractal in MATLAB as an image?

To save your Barnsley Fern Fractal as an image in MATLAB, you can use the "saveas" function and specify the desired file format, such as PNG or JPEG. You can also adjust the resolution and size of the image using the "print" function.

5. Is there a way to speed up the generation of a Barnsley Fern Fractal in MATLAB?

Yes, there are a few ways to speed up the generation of a Barnsley Fern Fractal in MATLAB. One way is to preallocate the arrays that store the x and y coordinates of the points. Another way is to use vectorization instead of loops in your code. You can also decrease the number of iterations or use the "scatter" function with a smaller point size.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
941
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
958
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
829
  • Engineering and Comp Sci Homework Help
Replies
1
Views
890
Back
Top