Thursday, March 6, 2014

The Sierpinski triangle implemented in MATLAB
Here is the code used to generate the triangle above:

n = 1000 %input('number of iterations? ')
m=2; xi=zeros(n);yi=zeros(n);
for m = 2:n
    r = randi([1 3]);
    
    switch r
        case 1
            xi(m)=.5*xi(m-1);
            yi(m)=.5*yi(m-1);
        case 2
            xi(m)=.5*xi(m-1)+.25;
            yi(m)=.5*yi(m-1)+sqrt(3)/4;
        case 3
            xi(m)=.5*xi(m-1)+.5;
            yi(m)=.5*yi(m-1);
    end
end
plot(xi,yi,'^')


No comments:

Post a Comment