clear
clc
L = 100;
width = L;
height = L;
length = L;
num = 1000;
range = 10;
ax = zeros(1,num);
ay = zeros(1,num);
az = zeros(1,num);
% where to start in the box
ax(1) = width/2;
ay(1) = height/2;
az(1) = length/2;
figure(1);
plot3([0 0 L L 0 0 0 L L 0],[0 L L 0 0 0 L L 0 0],[0 0 0 0 0 L L L L L],'r') % forms a box
saveas(gcf,['brownian3d_0',num2str(99),'_'], 'png')
close(gcf)
% note: output of rand is uniform from 0 to 1
for i = 1:num
moveX = (rand - .5)*range;
moveY = (rand - .5)*range;
moveZ = (rand - .5)*range;
if ( (ax(i)+moveX) > L | (ax(i)+moveX) < 0 )
ax(i+1) = ax(i) - moveX;
else
ax(i+1) = ax(i) + moveX;
end
if ( (ax(i)-moveX) > L | (ax(i)-moveX) < 0 )
ax(i+1) = ax(i) + moveX;
else
ax(i+1) = ax(i) - moveX;
end
if ( (ay(i)+moveY) > L | (ay(i)+moveY) < 0)
ay(i+1) = ay(i) - moveY;
else
ay(i+1) = ay(i) + moveY;
end
if ( (ay(i)-moveY) > L | (ay(i)-moveY) < 0)
ay(i+1) = ay(i) + moveY;
else
ay(i+1) = ay(i) - moveY;
end
if ( (az(i)+moveZ) > L | (az(i)+moveY) < 0)
az(i+1) = az(i) - moveZ;
else
az(i+1) = az(i) + moveZ;
end
if ( (az(i)-moveZ) > L | (az(i)-moveZ) < 0)
az(i+1) = az(i) + moveZ;
else
az(i+1) = az(i) - moveZ;
end
if (ax(i+1) == 0) | (ay(i+1) ==0) | (az(i+1) == 0)
disp(['not incremented at',num2str(i)])
end
% if you've reached here, there's an instance of being in one of the
% 8 unallowed octants
%if ((ax(i) <= 0) | (ay(i) <= 0) | (az(i) <= 0) | (ax(i) > L) | (ay(i) > L) | (az(i) > L))
if ( (ax(i+1) <= 0) | (ay(i+1) <= 0) | (az(i+1) <= 0) | (ax(i+1) > L) | (ay(i+1) > L) | (az(i+1) > L) )
% should stay in the positive quadrant. If it's not that's an error
%plot3(ax,ay,az)
az(i)
az(i+1)
ax(i)
ax(i+1)
ay(i)
ay(i+1)
stop
end
% {
% for animation
figure(1)
plot3(ax(1:i),ay(1:i),az(1:i))
hold on;
plot3([0 0 L L 0 0 0 L L 0],[0 L L 0 0 0 L L 0 0],[0 0 0 0 0 L L L L L],'r') % forms a box
saveas(gcf,['brownian3d_',num2str(i+99),'_'], 'png')
close(gcf)
% }
end
%{
plot3(ax,ay,az)
xlabel('this is x');xlabel('heres y');xlabel('z');
hold on;
%plot3([L L 0],[L 0 0],[0 0 0],'r') % forms a 2D bend
%plot3([L 0 0],[L L 0],[0 0 0],'r')
%plot([0 0 L L 0],[0 L L 0 0],[0 0 0 0 0],'r') % forms a 2D box
plot3([0 0 L L 0 0 0 L L 0],[0 L L 0 0 0 L L 0 0],[0 0 0 0 0 L L L L L],'r') % forms a box
%}