Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Basic Fractal Tree Matlab

  1. Apr 14, 2010 #1
    So I'm creating a very basic fractal tree function in matlab. The function is supposed to create a fractal tree where each branch comes out at 90 degrees so it looks like a bunch of T's just put together. Currentley I am running into the problem where I can not get the branches to plot in the correct spot. I was hoping someone would be able to point me in the right direction or give me and idea of where I can fix my code to change this program.

    t/here are a couple errors in the code, but overall it still works. I am not looking for those errors to be fixed, i can do that on my own. I am just looking for a way to plot my points right.

    Code (Text):
    %To use this there are 7 input arguments, the original points Xo, Yo. The
    %new points X and Y. The theta angle (given in radians), the length_ratio
    %of how much you want the branch to decrease and then how many branch pairs
    %you want.
    function fractree_b2(Xo,Yo,X,Y,theta,length_ratio,branches)
    plot([Xo X],[Yo Y],'r')
    hold on
        if branches <= 0
        else
            draw_right_branch(Xo,Yo,X,Y,theta,length_ratio,branches);
            draw_left_branch(Xo,Yo,X,Y,theta,length_ratio,branches);
        end  
    % Above coding creates the first line, the trunk and the point matrix to
    % create further lines below is what will rotate the lines and create
    % branches. It also calls the two imbeded functions that actually create
    % the left and right branches. It also calls in a loop that will end the
    % program and only draw the trunk if the value entered into branches is
    % equal to or less than 0

       
        function draw_right_branch(Xo,Yo,X,Y,theta,length_ratio,branches)
            rotational_matrix_r = [cos(theta) sin(theta); -sin(theta) cos(theta)];
            endpoint_1 = [Xo;Yo];
            endpoint_2 = [X;Y];
            endpoint_1_final = rotational_matrix_r*endpoint_1*length_ratio;
            endpoint_2_final = rotational_matrix_r*endpoint_2*length_ratio;
            plot([endpoint_1_final(1)+X endpoint_2_final(1)+X],[endpoint_1_final(2)+Y endpoint_2_final(2)+Y],'g')
            hold on        
                if branches >1    
                    draw_left_branch(Xo,Yo,X,Y,theta,length_ratio,branches)
                    Xo = X;
                    Yo = Y;
                    X = X*length_ratio;
                    Y = Y*length_ratio;
                    matrix_Po = [Xo;Yo];
                    matrix_P = [X;Y];
                    branch_o = rotational_matrix_r*matrix_Po;
                    branch = rotational_matrix_r*matrix_P;
                    draw_right_branch(branch_o(1),branch_o(2),branch(1),branch(2),theta,length_ratio,branches-1)
                else
                end
            %This coding is what will draw the right branches of a the fractal
            %tree. It takes the original point and multiplies it with the
            %rotational matrix and length_ratio to get the proper point. Then
            %an if loop is called in so the left branch function can be called
            %again as well as the right branch function. Before calling the
            %right branch function the Xo and X are redifined and rotated for
            %when the function is called again.    
        end
       
        function draw_left_branch(Xo,Yo,X,Y,theta,length_ratio,branches)
            rotational_matrix_l = [cos(theta) -sin(theta); sin(theta) cos(theta)];
            endpoint_1 = [Xo;Yo];
            endpoint_2 = [X;Y];
            endpoint_1_final = rotational_matrix_l*endpoint_1*length_ratio;
            endpoint_2_final = rotational_matrix_l*endpoint_2*length_ratio;
            plot([endpoint_1_final(1)+X endpoint_2_final(1)+X],[endpoint_1_final(2)+Y endpoint_2_final(2)+Y],'b')
            hold on
                 if branches >1    
                    draw_right_branch(Xo,Yo,X,Y,theta,length_ratio,branches-1)
                    Xo = X;
                    Yo = Y;
                    X = X*length_ratio;
                    Y = Y*length_ratio;
                    matrix_Po = [Xo;Yo];
                    matrix_P = [X;Y];
                    branch_o = rotational_matrix_l*matrix_Po;
                    branch = rotational_matrix_l*matrix_P;
                    draw_left_branch(branch_o(1),branch_o(2),branch(1),branch(2),theta,length_ratio,branches-1)                
                 else
                end
            %This coding is what will draw the left branches of a the fractal
            %tree. It takes the original point and multiplies it with the
            %rotational matrix and length_ratio to get the proper point.
        end
    end  
     

    Attached Files:

  2. jcsd
  3. May 17, 2010 #2
    Hi,

    I have some similar problem, but in this case my problem may be solution for Yours or vice-versa.

    For simple example run this code and You'll see what does it look like:

    m = 1;
    T=zeros(4,16);
    for t1=1:2
    for t2=1:2
    for t3=1:2
    for t4=1:2
    T(1,m)=t1;
    T(2,m)=t2;
    T(3,m)=t3;
    T(4,m)=t4;
    m=m+1;
    end;
    end;
    end;
    end;
    disp(T);


    and in my opinion it is familiar which basic fractal tree, there in this case each branch replicates it to full spectrum of elements (consider reapeating elements as single).

    You could generate this kind of matrix if You are not in need of big tree and it takes like 10 to 14 ms for 4x810'000 elements (4*30^4) to generate. in this way, that would be a big bush and Your distances may be negative power depending on level of branch as well as degrees of angle could be expressed by absolute measures as well.

    My question: how to optimise creation of this matrix? (I have way heavear dimensions like 30^8 to 50^17) and this generations takes considerable even though second largest period.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Basic Fractal Tree Matlab
  1. Matlab basics (Replies: 2)

  2. Basic MATLAB (Replies: 10)

  3. Matlab - basic script (Replies: 1)

Loading...