Using Matlab. I have almost everything figured out I need help with section %% R
ID: 3860881 • Letter: U
Question
Using Matlab. I have almost everything figured out I need help with section
%% Recording time it takes for each competitor to complete the races
What I want to do in this section is to save the last value of ind_rab_t and ind_tort_t into vectors rab_best_t and tort_best_t respectively.
%% Each animal's fastest and slowest run
For this, my goal is if the current ind_rab_t(end) and ind_tort_t(end) is less than the previous one in the while statement, then it will save the lesser vector.
Please don't rewrite anything prior to that unless you have to...
Here's the problem:
Here's a link if the image doesn't show: https://puu.sh/uGJkE/a8e3a8d258.png
Here's what I have:
clc
clear
run_count=0;
win_tort=0;
win_rab=0;
tie=0;
t_tort_100=0;
t_rab_100=0;
%% 100 Trials of race
while run_count<100
%% Conversion mph to mpm
% Tortoise stuff (mpm)
FP=2/60;
SP=0.5/60;
S=-1/60;
% Rabbit stuff (mpm)
Sleep=0;
BH=10/60;
SH=3/60;
BS=-8/60;
SS=-3/60;
%% Tortoise
ind = 1;
distance_tort(ind) = 0;
ind_tort_t(ind) = 0;
t_tort = 0;
ind = 2;
while distance_tort(ind-1)<4.2
for action_type_tort=randi([0 100])
if action_type_tort<=20 % if 20% fast plod
ind_tort_t(ind)=ind_tort_t(ind-1)+2;
t_tort = t_tort + 2;
distance_tort(ind) = distance_tort(ind-1) + (FP*2);
ind = ind + 1;
elseif action_type_tort>20 && action_type_tort<=80 % if 60% slow plod
ind_tort_t(ind)=ind_tort_t(ind-1)+4;
t_tort = t_tort + 4;
distance_tort(ind) = distance_tort(ind-1) + (SP*4);
ind = ind + 1;
elseif action_type_tort<=100 && action_type_tort>80 % if 20% slip
ind_tort_t(ind)=ind_tort_t(ind-1)+1;
t_tort = t_tort + 1;
distance_tort(ind) = distance_tort(ind-1) + (S*1);
ind = ind + 1;
end
end
end
%% Rabbit
ind = 1;
distance_rab(ind) = 0;
ind_rab_t(ind) = 0;
t_rab = 0;
ind = 2;
while distance_rab(ind-1)<4.2
for action_type_rab=randi([0 100])
if action_type_rab<=20 % if 20% sleep
ind_rab_t(ind)=ind_rab_t(ind-1)+45;
t_rab = t_rab + 45;
distance_rab(ind) = distance_rab(ind-1);
ind = ind + 1;
elseif action_type_rab>20 && action_type_rab<=30 % if 10% big hops
ind_rab_t(ind)=ind_rab_t(ind-1)+1;
t_rab =t_rab + 1;
distance_rab(ind) = distance_rab(ind-1) + (BH*1);
ind = ind + 1;
elseif action_type_rab>30 && action_type_rab<=70 % if 40% slow hops
ind_rab_t(ind)=ind_rab_t(ind-1)+5;
t_rab = t_rab + 5;
distance_rab(ind) = distance_rab(ind-1) + (SH*5);
ind = ind + 1;
elseif action_type_rab>70 && action_type_rab<=80 % if 10% big slip
ind_rab_t(ind)=ind_rab_t(ind-1)+1;
t_rab = t_rab + 1;
distance_rab(ind) = distance_rab(ind-1) + (BS*1);
ind = ind + 1;
elseif action_type_rab<=100 && action_type_rab>80 % if 20% small slip
ind_rab_t(ind)=ind_rab_t(ind-1)+1;
t_rab = t_rab + 1;
distance_rab(ind) = distance_rab(ind-1) + (SS*1);
ind = ind + 1;
end
end
end
%% Recording the number of races won by each animal
if ind_tort_t(end) fprintf('Tortoise has won!')
win_tort=win_tort+1;
elseif ind_tort_t(end)>ind_rab_t(end)
fprintf('Rabbit has won!')
win_rab=win_rab+1;
else
fprintf('It''s a tie!')
tie=tie+1;
end
%% Recording time it takes for each competitor to complete the races
t_tort_100(ind)=ind_tort_t(end);
t_rab_100(ind)=ind_rab_t(end);
%% Each animal's fastest and slowest run
if ind_tort_t(end) tort_best_t=ind_tort_t;
else
tort_best_t=ind_tort_t;
end
if ind_rab_t(end) rab_best_t=ind_rab_t;
else
rab_best_t=ind_rab_t;
end
run_count=run_count+1
end
The racetrack is 4.2 miles long. The first contender to travel that distance wins. The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground. The competitors can move in a variety of actions, which move them forward or backward at a certain velocity. After doing one action for a specific time, they immediately do another one. Each action has a certain frequency of occurrence (assumed to be independent of previous actions. The action type, velocity, time to complete, and frequency for each competitor is shown in the following table. Animal Action Type Frequency Time to complete Velocity 20Ro Tortoise Fast Plod 2 minutes Forward at 2 mph Slow plod 4 minutes Forward at mph minute Backward at 1 mph Seep 20Ro Hare 45 minutes No movement 10% Forward at 10 mph Small hops 40% 5 minutes Forward at 3 mph Big Slip 10% minute Backward at 8 mph Backward at 3 mph Small slip Model a series of races using MATLAB and do the following: 1. Perform 100 trials of the race. 2. Record the number of races won by each animal. Present this result in your report. 3. Record how long it takes for each competitor to complete each race. Create a histogram. for each. 4. Keep track of the race progress (time and position) for each animal's fastest and slowest run. Make a plot of position vs. time that features four lines: one for each competitor's fastest and slowest nunsExplanation / Answer
Try this... Will work good...
tortoiseMove = [2, 0.5, -1];
tortoiseTime = [2, 4, 1];
tortoiseCFreq= [0.2, 0.8, 1];
rabbitMove = [0, 10, 3, -8, -3];
rabbitTime = [45, 1, 5, 1, 1];
rabbitCFreq= [0.2, 0.3, 0.7, 0.8, 1];
tortoiseTotalTimeArray = rand (1, 100);
rabbitTotalTimeArray = rand (1, 100);
for i= 1:100
tortoiseDist = 0;
rabbitDist = 0;
tortoiseTotalTime = 0;
rabbitTotalTime = 0;
while tortoiseDist < 4.2
tortoiseChoise = V1 = find(tortoiseCFreq > rand(1), 1, 'first');
maxDist = tortoiseMove(tortoiseChoise)*tortoiseTime(tortoiseChoise)/60;
if maxDist + tortoiseDist < 4.2
tortoiseDist = maxDist + tortoiseDist;
tortoiseTotalTime = tortoiseTotalTime + tortoiseTime(tortoiseChoise);
else
tortoiseTotalTime = tortoiseTotalTime + tortoiseTime(tortoiseChoise)* (4.2 - tortoiseDist)/maxDist;
tortoiseDist = 4.2;
end
end
while rabbitDist < 4.2
rabbitChoise = V1 = find(rabbitCFreq > rand(1), 1, 'first');
maxDist = rabbitMove(rabbitChoise)*rabbitTime(rabbitChoise)/60;
if maxDist + rabbitDist < 4.2
rabbitDist = maxDist + rabbitDist;
rabbitTotalTime = rabbitTotalTime + rabbitTime(rabbitChoise);
else
rabbitTotalTime = rabbitTotalTime + rabbitTime(rabbitChoise)* (4.2 - rabbitDist)/maxDist;
rabbitDist = 4.2;
end
end
rabbitTotalTime
tortoiseTotalTime
if (rabbitTotalTime > tortoiseTotalTime)
printf("Tortoise wins ");
else
printf("Rabbit wins ");
end
tortoiseTotalTimeArray(i) = tortoiseTotalTime;
rabbitTotalTimeArray(i) = rabbitTotalTime;
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.