Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Matlab, perform 100 trials of this race while recording position and time

ID: 2080488 • Letter: U

Question

Using Matlab, perform 100 trials of this race while recording position and time into an array for each.

You are to write a program that recreates one of the truly great moments in history: the classic race of the tortoise and the hare. You will use random number generation to develop multiple simulations of this event, and estimate how likely each isto win a given race. 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 Forward at 2 mph Slow plod. 60% 4 minutes Slip minute Backward at 1 mph Hare Sleep 20Ro 45 minutes No movement 10% 1 mimute Forward at 10 mph Small hops 40% 5 minutes Forward at 3 mph Big Slip Backward at 8 mph 10% 1 mimute Backward at 3 mph Small slip 1 mimute

Explanation / Answer

function varargout=ndstest(TOL) %Performs numerous tests of ndSparse math operations, % % ndstest(TOL) % %TOL is a tolerance value on the percent error. Execution will pause in debug %mode for inspection if any one of the tests exhibits an error greater than %TOL. if narginTOL, Error, error 'Something wrong with sparse() and full() methods'; end %Test of logical, double Error(end+1)=err( class(logical(P)), class(logical(Pf))); Error(end+1)=err( class(double(P)), class(double(Pf))); %%Test isnumeric, islogical, isempty, issparse, isfloat, isreal,isinf, % isnan, isfinite, isequal, isequalwithequalnans Error(end+1)= err( isnumeric(P),1); Error(end+1)= err( ~isnumeric(P>0),1); Error(end+1)= err( isfloat(P),1); Error(end+1)= err( ~isfloat(P>0),1); Error(end+1)= err( ~islogical(P),1); Error(end+1)= err( islogical(P>0),1); Error(end+1)= err( ~isempty(P),1); Error(end+1)= err( isempty(ndSparse([])),1); Error(end+1)= err( issparse(P),1); Error(end+1)= err( issparse(A), 1 ); Error(end+1)= err( ~isreal(A), 1 ); Error(end+1)= err( isreal(B), 1 ); Z=P; Z(1)=nan; Z(2)=inf; Zf=full(Z); Error(end+1)= err( isnan(Z), isnan(Zf) ); Error(end+1)= err( isinf(Z), isinf(Zf) ); Error(end+1)= err( isfinite(Z), isfinite(Zf) ); Error(end+1)= err( ~isequal(Z,Zf), 1 ); Error(end+1)= err( ~isequal(Zf,Z), 1 ); Error(end+1)= err( ~isequal(Z,Zf,Z), 1 ); Error(end+1)= err( ~isequal(Zf,Z,Z), 1 ); Error(end+1)= err( isequalwithequalnans(Z,Zf), 1 ); Error(end+1)= err( isequalwithequalnans(Zf,Z), 1 ); Error(end+1)= err( isequalwithequalnans(Z,Zf,Z), 1 ); Error(end+1)= err( isequalwithequalnans(Zf,Z,Zf), 1 ); %Test of real, imag, conj, abs,sqrt Z=ndSparse(Bf+Af); %complex result Zf=full(Z); Error(end+1)= err( real(Z), real(Zf) ); Error(end+1)= err( imag(Z), imag(Zf) ); Error(end+1)= err( conj(Z), conj(Zf) ); Error(end+1)= err( abs(Z), abs(Zf) ); Error(end+1)= err( sqrt(Z), sqrt(Zf) ); %%Test of size Error(end+1)=err( size(P), size(Pf) ); Error(end+1)=err( size(P,2), size(Pf,2) ); [mm,nn] =size(Q); [mmm,nnn]=size(Qf); Error(end+1)=err( [mm,nn] , [mmm,nnn] ); %%Test of reshape Error(end+1)= err( reshape(P,size(P,1),[]) , reshape(Pf,size(Pf,1),[])); %%Test of permute, ipermute ord=randperm(ndims(Pf)); Z=permute(P,ord); Zf=permute(Pf,ord); Error(end+1)= err( Z , Zf ); Error(end+1)= err( ipermute(Z,ord) , Pf ); %%Test of transpose, ctranspose Error(end+1)= err( A.' , Af.' ); Error(end+1)= err( A' , Af' ); %%Test of uplus, uminus Error(end+1)= err( +P , +Pf ); Error(end+1)= err( -Q , -Qf ); %%Test of plus, minus Error(end+1)= err( P+Q , Pf+Qf ); Error(end+1)= err( P-Q , Pf-Qf ); %%Test of inv Error(end+1)= err( inv(S) , inv(Sf) ); %%Test of find [II,JJ,KK]=find(P); [IIf,JJf,KKf]=find(Pf); Error(end+1)= err( II , IIf ); Error(end+1)= err( JJ , JJf ); Error(end+1)= err( KK , KKf ); %%Test of mtimes Error(end+1)= err( A*three , Af*three ); %scalar with ndSparse Error(end+1)= err( three*P , three*Pf ); Error(end+1)= err( S*A , Sf*Af ); % 2 ndSparses x=Af(end,:).'; y=Af(:,end).'; Error(end+1)= err( A*[x,x] , Af*[x,x] ); %pre-mult with columnized data Error(end+1)= err( [y;y]*A , [y;y]*Af ); %post-mult with columnized data Error(end+1)= err( S*Af , Sf*Af ); %mixed op %%Test of mldivide Error(end+1)= err( threeP , threePf ); %scalar with ndSparse Error(end+1)= err( threeA , threeAf ); Error(end+1)= err( BA , BfAf ); % 2 ndSparses Error(end+1)= err( BAf , BfAf ); %%Test of mrdivide Bt=B.'; Btsp=Bf.'; Error(end+1)= err( Bt/three , Btsp/three );%scalar with ndSparse Error(end+1)= err( P/three , Pf/three );%scalar with ndSparse Error(end+1)= err( A.'/Bt , Af.'/Btsp ); % 2 ndSparses Error(end+1)= err( Af.'/Bt , Af.'/Btsp ); %%Test of times Error(end+1)= err( P.*three , Pf.*three ); %scalar with ndSparse Error(end+1)= err( three.*P , three.*Pf ); Error(end+1)= err( P.*Q , Pf.*Qf ); % 2 ndSparses Error(end+1)= err( Pf.*Q , Pf.*Qf ); %%Test of rdivide Error(end+1)= err( Q./three , Qf./three ); %scalar with ndSparse Error(end+1)= err( three./Q , three./Qf ); Error(end+1)= err( P./Q , Pf./Qf ); %2 ndSparses Error(end+1)= err( Q./P , Qf./Pf ); %2 ndSparses Error(end+1)= err( Qf./P , Qf./Pf ); %mixed %%Test of ldivide Error(end+1)= err( Q. hree , Qf. hree ); %scalar with ndSparse Error(end+1)= err( three.Q , three.Qf ); Error(end+1)= err( Q.P , Qf.Pf ); %2 ndSparses Error(end+1)= err( P.Q , Pf.Qf ); %2 ndSparses Error(end+1)= err( Pf.Q , Pf.Qf ); %%Test of power, mpower Error(end+1)= err( P.^three , Pf.^three ); Error(end+1)= err( three.^P , three.^Pf ); Error(end+1)= err( Q.^P , Qf.^Pf ); Error(end+1)= err( Qf.^P , Qf.^Pf ); %%Test of mpower Error(end+1)= err( S^three , Sf^three ); %%Test of relops Error(end+1)= err( P>P , Pf>Pf ); Error(end+1)= err( P>Pf , Pf>Pf ); Error(end+1)= err( P>three/6 , Pf>three/6 ); Error(end+1)= err( P>=P , Pf>=Pf ); Error(end+1)= err( P>=Pf , Pf>=Pf ); Error(end+1)= err( P>=three/6 , Pf>=three/6 ); Error(end+1)= err( P