Write a Matlab script that will execute the following requests • distance o a nu
ID: 3902137 • Letter: W
Question
Write a Matlab script that will execute the following requests
• distance
o a numerical variable representing the length of the trip, in miles
• time_desired
o the amount of time, in hours, the driver would like to complete the trip in
• speed_limit
o the speed limit of the road they will travel along, in miles per hour. We assume the speed limit is
the same for the duration of the trip for simplicity.
Your program should do the following:
Compute the required speed of the car in order to make the distance in the desired amount of time
Check to see whether or not this trip is feasible, assuming the driver will be obeying all posted speed limits
by using a conditional if/elseif/else statement
o Whenever it is possible, the program should print: “You can make it as long as you drive ____
miles per hour.” The blank space should be replaced with the required speed.
o Whenever it is not possible, the program should print: “You cannot make it since you would have
to drive _____ miles per hour, which exceeds the speed limit of _____ miles per hour.”
o Whenever an invalid number is put in (negatives or zero), the program should print an error
message: “ERROR: please check that your inputs are valid” (HINT: rather than to write three separate if/elseif statements, you can check to see whether any of three values is 0 or negative by using the “OR” operator, which is the | symbol on your keyboard. For example if x<=0 | y<=0 | z<=0 will execute if at least one of these values is less than or equal to zero.
Explanation / Answer
Please find the required code. The code is simple and self explanatory.
Hope this helps! If it works, please thumbs up!
%==============================================
while(1)
distance=input('Please enter the distance of the trip: ');
time_desired=input('Please enter the desired time to complete the trip: ');
speed_limit=input('Please enter the speed limit for the trip: ');
data=[distance,time_desired,speed_limit];
if(any(data<=0)) % Checking if any of the data inputs are invalid i.e. <=0
display('ERROR: please check that your inputs are valid !');
else
break;
end
end
required_speed=(distance/time_desired);
if(required_speed<=speed_limit)
display(strcat('You can make it as long as you drive->',num2str(required_speed),' miles per hour.'));
else
display(strcat('You cannot make it since you would have to drive->',num2str(required_speed),' miles per hour, which exceeds the speed limit of->',num2str(speed_limit),' miles per hour.'));
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.