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

Input restrictions The function should halt and return a red error message for t

ID: 3837025 • Letter: I

Question

Input restrictions The function should halt and return a red error message for the following conditions The input n is not a positive integer bigger than/equal to 2 The input N is not a positive integer greater than or equal to n+1 4. Create the travel plan The family starts out from Lubbock and visits some randomly picked cities and comes back to Lubbock. However, they do not know in which order they want to travel to the cities to ensure fastest travel. The sections help them with deciding the order of visit. a) Write a function M-file Travel plan. m that uses four inputs s, an array of n distinct numbers (cities to be visited) P, an (Nx2) matrix (containing the city locations, the first row being the start and end of trip) border, an (M x 2) matrix (containing the border points) speed, an (N x N) matrix (containing information about speed of travel between any two cities) The journey always starts from P (1, and ends in P (1, i.e., the city whose location is given by the first row of P. The function will generate all possible different orders of cities in input s (hint: use built-in function perms).

Explanation / Answer

Answer: See the code below:

-------------------------------------------

function [time_min sequence border_cross]=Travel_plan(S,P,border,speed)
if (size(P,1) ~= size(speed,1)
    error("Error in input");
    return;
end
#number of cities
N=length(S);
#number of border points
M=size(border,1);

start_city=P(1,:); #start city
end_city=P(1,:); #end city
#possible routes
possible_routes=perms(S);
times_required=zeros(1,N);
sequences=[];
border_cross=zeros(1,M);
i=1;  
for route in possible_routes  
    current_city=start_city;
    current_city_loc=1;
    for city in route
      city_loc=P(city,:);
      dist=sqrt((current_city(1)-city_loc(1))^2+(current_city(2)-city_loc(2))^2);
      time_taken=dist/speed(current_city_loc,city);
      times_required(i)=times_required(i)+time_taken;    
      sequences(i)=city;
      current_city=city_loc;
      current_city_loc=city;
      i=i+1;
    end
end
[min,index]=min(times_required);
#minimum time
time_min=min;
#sequence of cities with minimum time
sequence=sequences(index);
end

-----------------------------------------

Note: Calculation for border cross points is not shown, as function intersection mentioned in problem statment and to be used in border point calculation was not given.