You are on page 1of 3

% Particle in a box

% Request user data


disp 'Enter the Velocity "unit m/s" ';
prompt = 'Velocity in X direction = ';
Vx = input(prompt);
prompt = 'Velocity in Y direction = ';
Vy = input(prompt);
prompt = 'Enter the Time to find the position of the particle "unit s" = ';
t = input(prompt);
disp 'Enter the postion of the particle "unit m" between 0 to 10 = ';
prompt = 'Position X = ';
posX = input(prompt);
while(posX >10 || posX < 0)
prompt = 'ERROR - Position X between 0 to 10 = ';
posX = input(prompt);
if(posX <= 1 && posX >= 0)
break;
end
end
prompt = 'Position Y = ';
posY = input(prompt);
while(posY >10 || posY < 0)
prompt = 'ERROR - Position X between 0 to 10 = ';
posY = input(prompt);
if(posY <= 1 && posY >= 0)
break;
end
end
pOsXY

%pOsXY
%Calculate the total distance along X and Y -- dX and dY
dX = Vx * t;
dY = Vy * t;
%Make adjustments for the postion so that the particle starts from origin
adjusting the time accordingly.
% Horizontal side
if (Vx < 0)
dX = -1*Vx * t;
if (posX > dX)
fposX = posX - dX;
else
calposX
%call function to cal position of X
end
else
if ((posX+dX)<= 10)

fposX = posX + dX;

else

end

calposX
%call function to cal position of X
end
dX = dX + posX;

% Vertical side
if (Vy < 0)
dY = -1*Vy * t;
if (posY > dY)
fposY = posY - dY;
else
calposY
%call function to cal position of X
end
else
if ((posY+dY)<= 10)
fposY = posY + dY;
else
calposY
%call function to cal position of X
end
dY = dY + posY;
end
disp( sprintf( 'The final position of the particle is in X is %d, and Y is
%d', fposX, fposY ));

%Calculate the position of Y Make adjustments for the postion so that the
particle starts from origin adjusting the time accordingly.
if (Vy < 0)
dY = dY - posY;
else
dY = dY + posY;
end
%Calculate the final position and the number of time it bounces along the
%walls
bY = dY/20; %bouces on Y
disp( sprintf( 'It bounced %d times on the horizontal side', floor(bY*2) ));
if ((bY - floor(bY))<= 0.5)
fposY = dY - (floor(bY)*20); % final position of the particle along the x
direction
else
fposY = 20 - dY + (floor(bY)*20);
end

%Calculate the position of X Make adjustments for the postion so that the
particle starts from origin adjusting the time accordingly.
if (Vx < 0)
dX = dX - posX;
else
dX = dX + posX;
end
%Calculate the final position and the number of time it bounces along the
%walls
bX = dX/20; %bouces on X

disp( sprintf( 'It bounced %d times on the verticle side', floor(bX*2) ));
if ((bX - floor(bX))<= 0.5)
fposX = dX - (floor(bX)*20); % final position of the particle along the x
direction
else
fposX = 20 - dX + (floor(bX)*20);
end

You might also like