How can i calculate the distance between two cities using given longitude and latitudes
4 views (last 30 days)
Show older comments
Hi guys,
below is the question I am struggling with for my workshop. If anyone could answer it, would be much appreciated. the question is as follow:
Suppose you want to travel from Australia (specifically Brisbane, Darwin, Melbourne or Perth) to one of the following cities: Auckland, Bangkok, Johannesburg, Tokyo or Vancouver. The great circle distance (the distance that a direct flight would take) in radians between any two places on the Earth having latitude-longitude coordinates (lat1,lon1) and (lat2,lon2) is: angle = acos( sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2)*cos(lon1-lon2) ) A mathematically equivalent formula, less subject to rounding error for short distances, is: angle = 2*asin( sqrt( (sin((lat1-lat2)/2) )^2 + cos(lat1)*cos(lat2)*( sin((lon1-lon2)/2) )^2))
The angle can be converted to distance in either kilometres (km) or nautical miles (nm), given
that the circumference of the earth around the equator is about 40,000 km or 21600 nm.
The latitude and longitude coordinates of these cities are:
Brisbane 27° 28' S 153° 2' E
Darwin 12° 28' S 130° 51' E
Melbourne 37° 49' S 144° 58' E
Perth 31° 57' S 115° 51' E
Auckland 36° 51' S 174° 46' E
Bangkok 13° 44' N 100° 30' E
Johannesburg 26° 11' S 28° 3' E
Tokyo 35° 41' N 139° 46' E
Vancouver 49° 16' N 123° 7' W
These are given in degrees and minutes, where one minute is 1/60 of a degree. North and East
are typically taken to be positive, and South and West negative. For example, Vancouver’s
longitude of 123º7'W would become –(123+7/60) degrees, which means -123.12º.
Write a MATLAB script called distances.m that will ask you for your starting city, your
destination, and then tell you the distance between them. It should work exactly as follows:
(sample outputs – really a single line)
>> The distance between Perth and
Auckland is 5344 km or 2886 nm.
or
>> The distance between Brisbane and
Vancouver is 11856 km or 6402 nm.
or
>> The distance between Darwin and
Bangkok is 4432 km or 2393 nm.
or
>> The distance between Melbourne and
Johannesburg is 10321 km or 5573 nm.
Hints:
(a) Look at the menu command, test it out, and see how it works
(b) Consider what neat ways are available to represent the table of latitudes and longitudes.
Would you have a series of if-elseif statements? Or a switch? Or two 1-D vectors? Or a 2-D
matrix? Consider how each of these would work, then decide what you want to do and why.
(c) Implement your approach then check your outputs carefully against those above.
kind regards
0 Comments
Answers (0)
See Also
Categories
Find more on Language Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!