MATLAB Answers

0

Invalid expression error with function!

Asked by Olivia Colombo on 28 Feb 2019
Latest activity Answered by Star Strider
on 28 Feb 2019
I am new to using a function, and I'm trying to make a function mydistance that will take user input of coordinates and then calculate the great circle distance on the surface of the earth between the coordinates. The equation is all in there, but I am getting an error on the part that asks for input. Any ideas? Thank you!
(I've been trying it using the input: 37N, 76W 37N, 9W)
function d = mydistance(a)
prompt = 'Input coordinates between which you want to find the great circle distance (XN, XW XN, XW): ';
getridof = ["N","W",","];
x = input(prompt)
x = split(replace(x,getridof," "));
a = acos(sin(x(1))*sin(x(3))+cos(x(1))*cos(x(3))*cos(abs(x(2))-x(4)));
d = a*.6371;
disp(['The great circle distance in km is: ',num2str(d)])
end

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Star Strider
on 28 Feb 2019

Read the coordinates as a string, then do the conversions:
prompt = 'Input coordinates between which you want to find the great circle distance (XN, XW XN, XW): ';
getridof = ["N","W",","];
xc = input(prompt, 's')
xc = split(replace(xc,getridof," "));
x = str2double(xc)
a = acos(sin(x(1))*sin(x(3))+cos(x(1))*cos(x(3))*cos(abs(x(2))-x(4)));
d = a*.6371;
disp(['The great circle distance in km is: ',num2str(d)])
This works, and with your desired inputds, produces:
The great circle distance in km is: 0.93002
when I run it.

  0 Comments

Sign in to comment.