MATLAB GPS坐标算距离
时间: 2023-05-31 15:06:44 浏览: 107
使用MATLAB可以通过以下步骤计算GPS坐标之间的距离:
1. 定义两个GPS坐标点的经纬度,可以使用以下格式:
point1 = [latitude1, longitude1];
point2 = [latitude2, longitude2];
2. 将经纬度转换为弧度:
point1_rad = deg2rad(point1);
point2_rad = deg2rad(point2);
3. 计算两个点之间的距离,可以使用Haversine公式:
delta_lat = point2_rad(1) - point1_rad(1);
delta_lon = point2_rad(2) - point1_rad(2);
a = sin(delta_lat/2)^2 + cos(point1_rad(1)) * cos(point2_rad(1)) * sin(delta_lon/2)^2;
c = 2 * atan2(sqrt(a), sqrt(1-a));
distance = 6371 * c; % 6371是地球半径
4. 输出计算结果:
disp(['The distance between the two points is ', num2str(distance), 'km.']);
完整代码如下:
point1 = [39.9042, 116.4074]; % 北京市的经纬度
point2 = [31.2304, 121.4737]; % 上海市的经纬度
point1_rad = deg2rad(point1);
point2_rad = deg2rad(point2);
delta_lat = point2_rad(1) - point1_rad(1);
delta_lon = point2_rad(2) - point1_rad(2);
a = sin(delta_lat/2)^2 + cos(point1_rad(1)) * cos(point2_rad(1)) * sin(delta_lon/2)^2;
c = 2 * atan2(sqrt(a), sqrt(1-a));
distance = 6371 * c;
disp(['The distance between the two points is ', num2str(distance), 'km.']);
阅读全文