实验三坐标正反算一、实验目的编写坐标正反算程序,并对格式化文件数据进行计算,验证程序。二、实验内容及步骤:1、编写坐标正算程序要求:1)建立以xy_direct命名的函数,函数输入输出格式为[x2,y2]=xy_direct(x1,y1,distance,azimuth)2)对文件data1.txt中数据进行坐标正算,并将已知点和计算点坐标按照格式存贮在文件data2.txt中,data1.txt格式为:x1y1距离方位角(dd.mmss)data2.txt格式为:x1y1x2y2编写程序共三个模块,分三个m文件组成,各m文件内容如下:1)主运行程序clear;clc;[filename,pathname]=uigetfile('*.*');%文件查找窗口file=fullfile(pathname,filename);%合并路径文件名A=importdata(file);dms=A.data(:,4);%提取A.data中所有行和第4列azimuth=dms2rad(dms);%度分秒转化成弧度[x2,y2]=xy_direct(A.data(:,1),A.data(:,2),A.data(:,3),azimuth(:,1));B=[A.data(:,1),A.data(:,2),x2,y2];%输出得到B矩阵fid=fopen('fileout','wt');%写入文件路径fprintf(fid,'x(m)y(m)x2(m)y2(m)\n');fprintf(fid,'%f\t%f\t%f\t%f\n',B(:,1:4)');fclose(fid);2)xy_direct程序如下:function[x2,y2]=xy_direct(x1,y1,distance,azimuth)%求x2,y2函数x2=x1+distance.*cos(azimuth);-1-y2=y1+distance.*sin(azimuth);end3)度分秒转弧度函数functionazimuth=dms2rad(dms)%度分秒转弧度函数dms_abs=abs(dms);dgree=fix(dms_abs);min_tem=(dms_abs-dgree)*100;min=fix(min_tem);second=(min_tem-min)*100;azimuth=(dgree+min/60+second/3600)*pi/180;ifdms0azimuth=-azimuth;elseazimuth=azimuth;endreturn运行程序所得data2内容如下:2、编写坐标反算程序要求:1)建立以xy_inv命名的函数,函数输入输出格式为[distance,azimuth]=xy_inv(x1,y1,x2,y2)2)对文件data2.txt中数据进行坐标反算,并将计算结果按照格式存贮在文件data3.txt中,Data3.txt格式为:x1y1x2y2距离方位角(dd.mmss)坐标反算程序共三个模块,分三个m文件组成,各m文件内容如下:1)主运行程序:clear;clc;[filename,pathname]=uigetfile('*.*');%文件查找窗口file=fullfile(pathname,filename);%合并路径文件名A=importdata(file);-2-fori=1:4[distance(i),azimuth(i)]=xy_inv(A.data(i,1),A.data(i,2),A.data(i,3),A.data(i,4));%提取data数据;dms(i)=rad1dms(azimuth(i));%rad--dmsendB=[A.data(:,1),A.data(:,2),A.data(:,3),A.data(:,4),distance',dms'];%输出得到B矩fid=fopen('data3.txt','wt');%写入文件路径fprintf(fid,'x1y1x2y2距离方位角(dd.mmss)\n');[m,n]=size(B);%输入矩阵m,n取行列号fori=1:mforj=1:nifj==nfprintf(fid,'%f\n',B(i,j));elsefprintf(fid,'%f\t',B(i,j));endendendfclose(fid);2)xy_inv程序如下:function[distance,azimuth]=xy_inv(x1,y1,x2,y2)distance=sqrt((x2-x1)^2+(y2-y1)^2);if(x2-x1)~=0azimuth=atan(abs((y2-y1)/(x2-x1)));%求弧度值%判断象限if(x2-x1)0&(y2-y1)0azimuth=azimuth;elseif(x2-x1)0&(y2-y1)==0azimuth=0;elseif(x2-x1)0&(y2-y1)0azimuth=pi-azimuth;elseif(x2-x1)0&(y2-y1)==0azimuth=pi;elseif(x2-x1)0&(y2-y1)0azimuth=pi+azimuth;elseif(x2-x1)0&(y2-y1)0azimuth=2*pi-azimuth;end-3-elseif(y2-y1)0azimuth=pi/2;elseazimuth=3*pi/2;endend3)弧度转度分秒函数functiondms=rad1dms(azimuth)%弧度转度分秒dgree_tem=azimuth*180/pi;dgree=fix(dgree_tem);min_tem=((dgree_tem-dgree)*60);min=fix(min_tem);second=((min_tem-min)*60);dms=dgree+min/100+second/10000;end运行程序所得data3内容如下: