Finding azimuthal and polar angles

  • Thread starter Thread starter OneObstacle
  • Start date Start date
  • Tags Tags
    Angles Polar
AI Thread Summary
The discussion focuses on creating a MATLAB program to calculate azimuthal and polar angles for various cities based on their latitude and longitude. The program successfully prints the city data and attempts to compute the angles using specified equations, but the user struggles with correctly implementing the logic to differentiate between northern and southern latitudes, as well as eastern and western longitudes. The user also seeks to identify the largest surface separation between cities, but encounters issues with the loop limits in their distance calculation. Overall, the thread highlights the challenges of programming angle calculations and distance metrics in MATLAB.
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?
 
Back
Top