How do you store data created in a loop in a vector
이전 댓글 표시
The problem is calculating the speed of a car based off of the time it take for a sensor to read the front and back wheels, which in turn is based off of the distance between the 2. here's the code
%input number of cars driven over the cable
ncars=input('how many cars have driven over the sensor today, must be >2: ');
while ncars<=2 || round(ncars) ~= ncars
ncars=input('ERROR invalid number of cars, must be >2, try again : ');
end
%set up speed limit specification
speedlimit=input('\n what is the speed limit for the specified road, must be >10 : '); %mph
while speedlimit <= 10 || round(ncars) ~= ncars
speedlimit=input('\n ERROR invalid entry for speedlimit (must be >10), try again : ');
end
%generate random wheelbase between 104 and 106 inches
wheelbaseoriginal=rand(104*2);
%convert wheelbase to miles
wheelbase=(wheelbaseoriginal/12)/5280;
%initiate loop for number of cars
total=0;
carnumber=1
%initiate vector to be updated later
velocities=[ncars];
for ncars=1:1:ncars
%generate random time difference for front to back tires
tlow=wheelbase/speedlimit*1.2;
thigh=wheelbase/speedlimit*0.9;
wheeltime=tlow+thigh*rand;
%calculate the speed of each car
speed=wheeltime*3600;
velocities(carnumber)=speed;
total= speed+total;
carnumber=carnumber+1
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!