Question

use matlab: ) Computing a distance and travel time between two locations. Obtain user input for two variables, X and Y, which describe a coordinate location.

Assume this coordinate is in units of miles. Write a script that :

a) computes the distance D between the two points (0,0) and (X,Y) in kilometers.

b) if D is greater than 5 kilometers, assume we drive at a speed of 40 km per hour. Otherwise, assume we walk at a speed of 5 km per hour. Display the travel time to the command line.

Test Cases: If X=1 and Y=2, the distance should be 3.6 km, and travel time should be 0.72 hours. If X= -4 and Y=5, this distance should be 10.3 km, and travel time should be 0.26 hours.

*note: 1.609 km =1 mile *Hint: remember the Pythagorean theorem for the sides of right triangles is a2 + b2 =c2

Answer #1

SCRIPT:

clear all

clc

X=input('enter x coordinate of the location(in miles):')

Y=input('enter y coordinate of the location(in miles):')

D=sqrt(X^2+Y^2); %distance in mile

D=D*1.609;

if(D>5)

t=D/40; %t=time in hour

else

t=D/5;

end

disp(D) % for displaying distance

disp(t) % for displaying travel time

TEST CASE - 1

enter x coordinate of the location(in miles):1

X =

1

enter y coordinate of the location(in miles):2

Y =

2

3.5978

0.7196

TEST CASE - 2

enter x coordinate of the location(in miles):-4

X =

-4

enter y coordinate of the location(in miles):5

Y =

5

10.3026

0.2576

ADVERTISEMENT

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 11 minutes ago

asked 53 minutes ago

asked 56 minutes ago

asked 59 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago

asked 2 hours ago