Creating a Basic Fractal Tree Function in Matlab | Fixing Branch Placement

  • Context: MATLAB 
  • Thread starter Thread starter hunter.hoopes
  • Start date Start date
  • Tags Tags
    Fractal Matlab Tree
Click For Summary
SUMMARY

The forum discussion centers on creating a basic fractal tree function in MATLAB, specifically addressing issues with branch placement. The user seeks assistance in plotting branches correctly at 90-degree angles using a function that requires seven input arguments, including initial coordinates and branch parameters. The code includes embedded functions for drawing right and left branches, utilizing rotational matrices for accurate placement. The discussion also touches on optimizing matrix creation for larger fractal trees, highlighting performance concerns with extensive data sets.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of rotational matrices
  • Knowledge of fractal geometry
  • Experience with function creation and recursion in MATLAB
NEXT STEPS
  • Explore MATLAB's plotting functions for advanced visualizations
  • Learn about optimizing recursive functions in MATLAB
  • Investigate performance improvements for large matrix operations in MATLAB
  • Study fractal generation algorithms and their mathematical foundations
USEFUL FOR

Mathematics enthusiasts, MATLAB programmers, and developers interested in fractal generation and optimization techniques.

hunter.hoopes
Messages
1
Reaction score
0
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:
%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
 

Attachments

Physics news on Phys.org
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K