maxCarVelocity=5;
nStarts=2;
nCarsPerStart=100;
deltaT=1;
t=300;
carPosition=carCoords(nStarts,nCarsPerStart);
function [carPosition] = carCoords(nStarts,nCarsPerStart)
carPosition=NaN(nStarts*nCarsPerStart,nStarts);
for i=1:nStarts
carPosition((1:nCarsPerStart)+(i-1)*nCarsPerStart,i)=cumsum([0; randi([-10,-2],nCarsPerStart-1 ,1)]);
end
end
carProperties=intitalCarProperties(nStarts,nCarsPerStart,maxCarVelocity);
function [carProperties] = intitalCarProperties(nStarts,nCarsPerStart,maxCarVelocity)
driverBehaviour=1;
typeOfVehicle=1;
carProperties=zeros(nStarts*nCarsPerStart,3);
for i=1:nStarts*nCarsPerStart
carProperties(:,1)=maxCarVelocity;
carProperties(:,2)=driverBehaviour;
carProperties(:,3)=typeOfVehicle;
end
end
carPosWithTimeStep=repmat(carPosition,1,t+1);
timeSteps = repmat(0:deltaT:t,nStarts,1);
timeSteps = timeSteps(1:end);
for i=1:1:nStarts*nCarsPerStart
for k=nStarts+1:1:nStarts*(t+1)
carPosWithTimeStep(i,k)=carPosWithTimeStep(i,k)+carProperties(i,1)*timeSteps(1,k);
end
end
roadLength=1000;
figure('units','normalized','position',[0 0 1 1])
road=plot ([0,roadLength],[7.3,7.3],'k',[0,roadLength],[3.65,3.65],'k--',...
[0,roadLength],[0,0],'k','linewidth', 1.5);
axis equal
startingXPos1 = [0, 4.5, 4.5, 0];
startingYPos1 = [0.9, 0.9, 2.7, 2.7];
g = hgtransform;
patch('XData',startingXPos1,'YData',startingYPos1,'FaceColor','Red','Parent',g)
for b=1:1:nStarts*nCarsPerStart
for h=nStarts+1:nStarts:nStarts*(t+1)
g.Matrix = makehgtform('translate',carPosWithTimeStep(b,h)-carPosWithTimeStep(b,h-nStarts),0,0);
drawnow
end
end