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

In summary, the conversation discusses creating a basic fractal tree function in MATLAB. The function is supposed to create a tree with branches at 90 degree angles. The code currently has some errors but still works. The user is looking for help with plotting the points correctly and optimizing the creation of a large matrix for the tree.
  • #1
hunter.hoopes
1
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

  • fractree_b2.m
    3.7 KB · Views: 730
Physics news on Phys.org
  • #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.
 

What is a fractal tree and why is it important?

A fractal tree is a mathematical concept in which a tree-like structure is created using recursive patterns. It is important because it is a visual representation of complex mathematical concepts and can be applied to various fields such as computer graphics, biology, and economics.

What is the purpose of creating a basic fractal tree function in Matlab?

The purpose of creating a basic fractal tree function in Matlab is to have a tool that can generate fractal trees quickly and efficiently. This function can be used for various purposes such as generating visualizations, studying recursive patterns, and testing algorithms.

What are the steps to create a basic fractal tree function in Matlab?

The steps to create a basic fractal tree function in Matlab are as follows:

  • Define the initial parameters such as tree size, branch length, and angle.
  • Use a recursive function to draw the branches of the tree.
  • Set a stopping condition for the recursive function.
  • Use a loop to iterate through each branch and apply the recursive function.
  • Plot the final tree using the created function.

How can the branch placement in a fractal tree be fixed?

In order to fix the branch placement in a fractal tree, the angle at which the branches are drawn can be adjusted. This can be done by adding a random variation to the angle at each iteration, or by using a specific mathematical formula to determine the angle.

Can the fractal tree function in Matlab be customized?

Yes, the fractal tree function in Matlab can be customized according to individual preferences. The initial parameters such as tree size, branch length, and angle can be adjusted to create different types of fractal trees. Additionally, the recursive function can be modified to create unique patterns and shapes.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
116
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
553
  • Differential Geometry
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
616
  • MATLAB, Maple, Mathematica, LaTeX
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
937
Back
Top