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

a) Write a function M-file Travel_plan.m that uses four inputs: S, an array of n

ID: 3110502 • Letter: A

Question

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). For example, the following are the two possible travel routes with S=[3 9] [1 3 9 1] and [1 9 3 1] which means the family can travel from Lubbock to 3rd city to 9th city and back to Lubbock OR can travel from Lubbock to 9th city to 3rd city to back to Lubbock. The function then calculates the travel time needed for each of these possible sequences of visit and then finds which sequence needs the smallest time of travel. (hint: use [M,i] = min(A),where M is the minimum value of array A corresponding to i-th index of A) It also finds how many times the border was crossed for using the fastest route. (use: supplied function intersection for checking if two line segments cross or not. More information about how to use this function is include at the end of this document) The outputs are time_min, the time required for fastest route sequence, the sequence of cities in S for fastest route (exclude Lubbock) border_cross, the number of times the border was crossed for fastest route. Input restrictions: The function should halt and return a red error message if • the number of rows of P is not equal to the number of rows of speed.

Do this in MATLAB please

Explanation / Answer

First create these two functions

function [d] = degreeToRadians (r)
d= (r*pi)/180;

endfunction

function [dist] = distance (lat1,lon1,lat2,lon2)
earthRadiusKm = 6371;

dLat = degreeToRadians(lat2-lat1);
dLon = degreeToRadians(lon2-lon1);

lat1 = degreeToRadians(lat1);
lat2 = degreeToRadians(lat2);

a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) *sin(dLon/2) * cos(lat1) * cos(lat2);
c = 2 * atan2(sqrt(a),sqrt(1-a));
dist = earthRadiusKm * c;
endfunction

#S=[1, 2, 3,4 ,5];
#P= [20.60000038147,-100.38330078125;19.434200286865,-99.138603210449;25.683300018311,-100.25;32.533298492432,-117.01670074463;22.88330078125,-102.61669921875];
#b=[0,0];
#N=[90,90,90,90,90;90,90,90,90,90;90,90,90,90,90;90,90,90,90,90;90,90,90,90,90];

function [route, min_time]=Travel_plan (S,P,b,n)

#The inputs area
#S number of cities visited
#P location of the cities
#b border
#n matrix containing information about the speed between cities

#Fist we create an empty matrix for all the possible combinations

comb_mat = [];
comb_mat=perms(S(2:5));
[r c] =size(comb_mat);
mat_cities=[];
mat_cities =zeros(r,c+2);
for(i=1:c+2) #columns
for(j=1:r) #rows
if(i==1)
mat_cities(j,i)=1;
elseif (i==c+2)
mat_cities(j,i)=1;
elseif
mat_cities(j,i)=comb_mat(j,i-1);
endif   
endfor
endfor

#Then calculate the distance between cities and divide it by the speed to calculate time
#In the last column we sum the total time

[r2 c2]=size(mat_cities);
mat_dist=[];
mat_dist=zeros(r2,c2);

for(s=1:r2) #rows
for(t=1:c2-1) #columns
mat_dist(s,t)= distance(P(mat_cities(s,t),1),P(mat_cities(s,t),2),P(mat_cities(s,t+1),1),P(mat_cities(s,t+1),2))/90;
endfor
endfor

for(s=1:r2) #rows
aux_sum=0;
for(t=1:c2-1) #columns
aux_sum= aux_sum + mat_dist(s,t);
endfor
mat_dist(s,c2)=aux_sum;
endfor

#Finally we choose the sequence with lowest total time

aux_min= mat_dist(1,c2);
aux_p = 1;
for(k=1:r2)
if(mat_dist(k,c2)<aux_min)
aux_min= mat_dist(k,c2);
aux_p = k;
endif
endfor

#Shortest route
route = mat_cities(k,:);
min_time=aux_min
endfunction

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote