Jurriën
Jurriën

Reputation: 309

Matlab GPS + distance in meters

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?

Upvotes: 0

Views: 1088

Answers (1)

Jurriën
Jurriën

Reputation: 309

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

Upvotes: 1

Related Questions