clear
clf

%aviobj = avifile('midpoint9','quality',100,'compression','Cinepak')

limit = 7;
random = rand((2^(limit+1)) - 2);
r = 0;

for c = .1:.005:.7
    x = [0,1,2];
    y = [0,1,0];
    xnew(1:(2^(limit+1) + 1)) = zeros;
    ynew(1:(2^(limit+1) + 1)) = zeros;
    for j=1:limit
        for i=1:(2^j)
            r = r + 1;
            k = 2*i - 1;
            xnew(k) = x(i);
            xnew(k+1) = .5*(x(i)+x(i+1));
            xnew(k+2) = x(i+1);
            ynew(k) = y(i);
            ynew(k+1) = c*(y(i)+y(i+1)) + .06*(random(r)-.5);
            ynew(k+2) = y(i+1);  
        end
        x = xnew;
        y = ynew;
    end
    plot(x,y)
    axis([0 2 0 3.5])
    axis off
    fill([x x(1) x(1)], [y y(end) y(1)], [.75 .40 0])
    whitebg([.55 1 1])
    axis([0 2 0 3.5])
    axis off
    %F(j) = getframe;
    %aviobj = addframe(aviobj,F(j));
end

%aviobj = close(aviobj);
