Finding azimuthal and polar angles

  • Thread starter Thread starter OneObstacle
  • Start date Start date
  • Tags Tags
    Angles Polar
Click For Summary
SUMMARY

This discussion focuses on calculating azimuthal and polar angles using MATLAB for a set of cities with given latitude and longitude coordinates. The participants detail a complete MATLAB program that prints city data and computes the angles based on directional indicators (N/S for latitude and E/W for longitude). The program also aims to find the largest surface separation between the cities, utilizing the Haversine formula for distance calculation. The final output includes the maximum distance and the corresponding city names.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Knowledge of geographic coordinate systems (latitude and longitude)
  • Familiarity with trigonometric functions in MATLAB
  • Basic concepts of distance calculation on a sphere (Haversine formula)
NEXT STEPS
  • Implement error handling for invalid latitude and longitude inputs in MATLAB
  • Explore MATLAB's built-in functions for geographic calculations
  • Learn about the Haversine formula for accurate distance measurement
  • Investigate optimization techniques for improving the efficiency of nested loops in MATLAB
USEFUL FOR

Students in geography or computer science, MATLAB programmers, and anyone interested in geographic data analysis and distance calculations.

OneObstacle
Messages
6
Reaction score
0

Homework Statement



City Latitude(degrees) Longitude(degrees)
LOS ANGELES 35.20 N 118.03 W
CHICAGO 41.82 N 87.62 W
CORVALLIS 44.53 N 123.30 W
MONTREAL 45.50 N 73.58 W
LONDON 51.51 N 0.12 E
BEIJING 40.08 N 116.33 W
RIO DE JANEIRO 22.83 S 43.33 W
MELBOURNE 35.87 S 145.14 E
VLADIVOSTOK 43.10 N 131.78 E
JOHANNESBURG 26.10 S 27.95 E
PUNTA ARENA 53.15 S 70.92 W
SCAMMON BAY 61.83 N 165.57 W

Write a complete MATLAB program that does the following:
a) Print the data as shown. The directions N/S or E/W should be stored as character variables and used to calculate the polar and azimuthal angles α and β respectively.
b) Find the largest surface separation in miles and print the result to 2 decimal places. Also print the corresponding names of the two cities. Your algorithm should be efficient!



Homework Equations


α= 90 - lat (if N)
=90 + lat (if S)
β=long (if W)
= 360-long (if E)

The Attempt at a Solution


clear
clc
fprintf('City Latitude(degrees) Longitude(degrees)\n')
C = char('LOS ANGELES','CHICAGO','CORVALLIS','MONTREAL','LONDON','BEIJING','RIO DE JANEIRO','MELBOURNE','VLADIVOSTOK','JOHANNESBURG','PUNTA ARENA','SCAMMON BAY');
LATDIR = ['N','S','E','W'];
Lat=char('35.20 N','41.82 N','44.53 N','45.50 N','51.51 N','40.08 N','22.83 S','35.87 S','43.10 N','26.10 S','53.15 S','61.83 N');
Long = char('118.03 W','87.62 W','123.30 W','73.58 W','0.12 E','116.33 W','43.33 W','145.14 E','131.78 E','27.95 E','70.92 W','165.57 W');
[nr nc]=size(Long);
for i=1:nr
fprintf('%s %s %s\n',C(i,:),Lat(i,:),Long(i,:))
end
%Find alpha
[nr nc]=size(Lat);


As you can see, I got the first part of a done and printed the data. However, I have no idea how to call the data and make it recognize that, say, the latitude of Los Angeles is to the north, and as such should use the first equation. Once I get that, I think I can get the rest. Any help would be much appreciated. Thanks!

EDIT: Part of me suspects I should write the matrices for lat and long differently.
 
Physics news on Phys.org
Okay, rewrote the matrices so that instead of 3, I have 5, two of which are numerical and 3 of which are alpha-numerical. Here is my new code(so far):clear
clc
fprintf('City Latitude(degrees) Longitude(degrees)\n')
C = char('LOS ANGELES','CHICAGO','CORVALLIS','MONTREAL','LONDON','BEIJING','RIO DE JANEIRO','MELBOURNE','VLADIVOSTOK','JOHANNESBURG','PUNTA ARENA','SCAMMON BAY');
Lat=[35.20;41.82;44.53;45.50;51.51;40.08;22.83;35.87;43.10;26.10;53.15;61.83];
Latdir=char('N','N','N','N','N','N','S','S','N','S','S','N');
Long=[118.03;87.62;123.30;73.58;0.12;116.33;43.33;145.14;131.78;27.95;70.92;165.57];
Longdir=char('W','W','W','W','W','E','W','E','E','E','W','W');
[nr nc]=size(Long);
for i=1:nr
fprintf('%s %5.2f%s %5.2f%s\n',C(i,:),Lat(i,:),Latdir(i),Long(i,:),Longdir(i))
end
%Find alpha
[nr nc]=size(Lat);
 
If anybody could help, I'm up to:


clear
clc
fprintf('City Latitude(degrees) Longitude(degrees)\n')
C = char('LOS ANGELES','CHICAGO','CORVALLIS','MONTREAL','LONDON','BEIJING','RIO DE JANEIRO','MELBOURNE','VLADIVOSTOK','JOHANNESBURG','PUNTA ARENA','SCAMMON BAY');
Lat=[35.20,41.82,44.53,45.50,51.51,40.08,22.83,35.87,43.10,26.10,53.15,61.83];
Latdir=['N';'N';'N';'N';'N';'N';'S';'S';'N';'S';'S';'N'];
Long=[118.03,87.62,123.30,73.58,0.12,116.33,43.33,145.14,131.78,27.95,70.92,165.57];
Longdir=['W';'W';'W';'W';'W';'E';'W';'E';'E';'E';'W';'W'];
[nr nc]=size(Long);
for i=1:nc
fprintf('%s %5.2f%s %5.2f%s\n',C(i,:),Lat(i),Latdir(i),Long(i),Longdir(i))
end
%Find alpha and beta
n=length(Latdir);
for i=1:n
if Latdir(i)=='N'
alpha(i)=Lat(i)-90;
else
alpha(i)=Lat(i)+90;
end
if Longdir(i)=='W'
beta(i) = Long(i);
else
beta(i) = 360-Long(i);
end
end
R=3958.9;
n=length(Lat);
s=0;
for i=1:n-1
for j=1:n

x(i)=cosd(alpha(i)).*cosd(alpha(j))+sind(alpha(i)).*sind(alpha(j)).*cosd(beta(j)-beta(i));
d(i)=x(i)*R;
end
if abs(d(i))>s
s=d(i);
end
end
fprintf('The farthest distance is %6.2f between %sand %s\n',s,C(i,:),C(j,:))



I think there is a problem with my "limits" on i and j, the bolded part. Any ideas?