H=0.5;
nmax=6;
length=2^nmax+1;
step=2^nmax;
halfstep=step/2;
x=linspace(0,1,length);
y=x;
[x,y]=meshgrid(x,y);
z=zeros(size(x));
for n=1 : nmax
    range=2^(-2*n*H);
    for i = 1 : step : length - step
        for j = 1 : step : length - step
            z(i+halfstep,j)=0.5*(z(i,j)+z(i+step,j))+(1-2*rand)*range; % (1+0,5 ; j)
            z(i,j+halfstep)=0.5*(z(i,j)+z(i,j+step))+(1-2*rand)*range; % (i ; j+0,5)
            z(i+step,j+halfstep)=0.5*(z(i+step,j)+z(i+step,j+step))+(1-2*rand)*range; % (i+1 ; j+0,5)
            z(i+halfstep,j+step)=0.5*(z(i,j+step)+z(i+step,j+step))+(1-2*rand)*range; % (i+0.5 ; j+1)
            z(i+halfstep,j+halfstep)=0.25*(z(i,j)+z(i+step,j)+z(i,j+step)+z(i+step,j+step))+(1-2*rand)*range;
        end
    end
    step=step/2;
    halfstep=halfstep/2;
    p=1;
    for i=1 : 2^n +1
        q=1;
        for j = 1 : 2^n + 1
            plotx(i,j) = x(p,q);
            ploty(i,j) = y(p,q);
            plotz(i,j) = z(p,q);
            q = q + step;
        end
        p = p + step;
    end
    surf(plotx,ploty,plotz,'facecolor','w')
    axis([0 1 0 1 -0.5 0.5])
    axis off
    shg
    pause(0.1)
end





