0

I have to calculate for a given Latitude (lat0) and Longitude (lon0) the new latitude (lat1) and longitude (lon1) if I move a distance x[m] and y[m] away from the initial position. For example:

clear all; clc;
%initial coordinates:
lat0=56;
lon0=5;

%moving away from lat0,lon0,
xcor=200; %[m]
ycor=100; %[m]

First I use this code to calculate lat1 and lon1: (source: Adding distance to a GPS coordinate)

lat1=lat0+rad2deg((xcor/6372800))
lon1=lon0+rad2deg((ycor/6372800)/(cos(lat0)))
distance=sqrt(xcor^2+ycor^2)

Now i want to check my answer with the haversine equation:

dlat = deg2rad(lat1-lat0);
dlon = deg2rad(lon1-lon0);
lat0 = deg2rad(lat0);
lat1 = deg2rad(lat1);
a = (sin(dlat./2)).^2 + cos(lat0) .* cos(lat1) .* (sin(dlon./2)).^2;
c = 2 .* asin(sqrt(a));
distance_check=6372800*c

However, most of the time the distance from haversine is different with 100+ meters compared to the calculated distance in the first 3 lines of code.

What is going wrong in this code?

Community
  • 1
  • 1
Jurriën
  • 199
  • 1
  • 2
  • 16
  • The site http://www8.nau.edu/cvm/latlongdist.html gives the same answer as the haversine equation... That means the error has to be in the first initial calculation? – Jurriën Jan 14 '16 at 13:25
  • I have found the error myself. In the first code, 2nd line, cos(lat0) has to be cos(deg2rad(lat0)). I hope this post will help some people in the future :) – Jurriën Jan 14 '16 at 13:29
  • 1
    Maybe use `cosd(lat0)` instead of `cos(lat0)`? Edit: 35 seconds late :P – dasdingonesin Jan 14 '16 at 13:30
  • @JurriënPlijter: When you found the answer yourself, please write an answer to your own question and accept it. – Daniel Jan 14 '16 at 13:45

1 Answers1

1

In the first code, 2nd line, cos(lat0) has to be cos(deg2rad(lat0)).

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Jurriën
  • 199
  • 1
  • 2
  • 16