水准网平差程序Matlab实现 全部代码,详细教程

一、程序结构:

输入文件格式如下所示:

 

1 读取文件:主要用到fopen, str2num函数

fid = fopen('input_leveling.txt','r');
line1 =  fgetl(fid); 
vec1 = str2num(line1);
%read the first line and change it to number vector (the number of known points, unknown points, and obervations)
num_known = vec1(1);
num_unknown = vec1(2);
num_observ = vec1(3);
line2 = fgetl(fid); %read the second line(the index of known point)
idx_known = str2num(line2);
line3 = fgetl(fid);% read the third line(the height of known point)
h_known = str2num(line3);
line4 = fgetl(fid)
flag = str2num(line4);
for iii = 1:num_observ %  read the obersvation data
        line = fgetl(fid);
        observs(:, iii) = str2num(line);
end

2 初始化高程向量,若其为未知点,则高程设为1e8,若为已知点,则为其高程。

start_pt = observs(1,:); % start point
end_pt = observs(2,:);% end point
h = observs(3,:);% oberved height
dist = observs(4,:);% distances of each baseline
num_total = num_known+num_unknown;
ttt = 1;
%create the origin height vector 
%for unknown points -> 1e6 
%for known points -> height
for kkk = 1:num_total
        if(sum(kkk == idx_known) == 1)
                h0(kkk) = h_known(ttt);
                ttt = ttt+1;
        else
                h0(kkk) = 1e6;
        end
end

3 利用while函数和for函数求其初始高程:

idx_unknown =  find(h0 == 1e6);
%calculate the origin height of each points
while(sum(h0 == 1e6)>=1)
        for ttt = 1:num_observ
                starts= start_pt(ttt);
                ends = end_pt(ttt);
                if((h0(starts)>= 1e6)&&(h0(ends)<1e6))
                        h0(starts) = h0(ends) - h(ttt);
                elseif((h0(starts)< 1e6)&&(h0(ends)>=1e6))
                        h0(ends) = h0(starts) + h(ttt);
                end
        end
end

4 求解B矩阵, l 矩阵以及权重矩阵:

p = diag(1./dist);% p is the weight matrix 
B = zeros(num_observ, num_unknown);% B 
for hhh = 1:num_observ;
        starts_ = start_pt(hhh);
        ends_ = end_pt(hhh);
        if(sum(find( idx_known == starts_ )) == 0)%if the start point is unknown set it to -1
                starts_index = find(idx_unknown == starts_) ;      
                B(hhh, starts_index) = -1;
        end
        if(sum(find( idx_known == ends_ )) == 0)%if the end point is unknown set it to 1
                ends_index = find(idx_unknown == ends_) ;
                B(hhh, ends_index) = 1;
        end
        l(hhh) = -h0(starts_) + h0(ends_) - h(hhh);
end

5 利用得到的B, p, l进行平差:

%adjustment
l = -l;

Nbb = B'*p*B;
w = B'*p*l';
x_ = inv(Nbb)*w;
v = B*x_ - l';
num_r = num_observ-(num_unknown);
sigma0 = sqrt(v'*p*v/num_r);
Qxx = inv(B'*p*B);
ttt = 1;
for sss = 1: num_total
        if(sum(find( idx_known == sss )) == 0 )
        sigma(sss) = sigma0*sqrt(Qxx(ttt,ttt));
        dh(sss) = x_(ttt);
        ttt  = ttt+1;
        
        else
                sigma(sss) = 0;
                dh(sss) = 0;
        end
end

6 输出

%outputs the results
fid2 =  fopen('adjustment_results.txt','wt')
fprintf(fid2, '%4s   %4s        %4s       %4s         %4s\n', 'idx','h0','dh','h','theta_h')
for ppp = 1: num_total
       fid3 =  fopen('adjustment_results.txt','a') 
        fprintf(fid3, '%2d   %4.4f   %4.4f   %4.4f   %4.4f\n', ppp, h0(ppp),dh(ppp),h0(ppp)+dh(ppp),sigma(ppp))
end

  • 18
    点赞
  • 150
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
很抱歉,由于水准网平差程序的复杂性和庞大数据量,我们无法在此提供完整的Matlab程序和数据,但是我们可以提供一些基础的Matlab代码和用于练习的数据,帮助你理解程序实现的基本思路。 1. 基础代码 以下是一些基础的Matlab代码,用于读取数据和计算平差结果: % 读取数据 obs_data = load('obs_data.txt'); sta_data = load('sta_data.txt'); % 构建观测矩阵 num_obs = size(obs_data,1); num_sta = size(sta_data,1); A = zeros(num_obs,num_sta); for i=1:num_obs sta1 = obs_data(i,1); sta2 = obs_data(i,2); A(i,sta1) = -1; A(i,sta2) = 1; end % 构建权矩阵 P = diag(obs_data(:,3).^2); % 构建常数项 L = obs_data(:,4); % 构建坐标矩阵 X = zeros(num_sta,2); X(:,1) = sta_data(:,2); X(:,2) = sta_data(:,3); % 求解平差结果 x_hat = (A'*P*A)\(A'*P*L); v = A*x_hat - L; sigma_0 = sqrt(v'*P*v/(num_obs-num_sta)); Q_xx = sigma_0^2*(A'*P*A)^-1; % 输出平差结果 disp(['平差结果:']); for i=1:num_sta disp(['站点' num2str(i) ':']); disp(['X = ' num2str(x_hat(i,1)) 'm']); disp(['Y = ' num2str(x_hat(i,2)) 'm']); end 2. 练习数据 以下是一个简单的水准网数据,用于练习水准网平差程序实现: % 观测数据(起点编号、终点编号、观测值、观测精度) obs_data = [ 1 2 1.1 0.01; 2 3 1.2 0.01; 3 4 1.3 0.01; 4 5 1.4 0.01; 5 6 1.5 0.01; ]; % 站点数据(站点编号、X坐标、Y坐标) sta_data = [ 1 0 0; 2 1 0; 3 2 0; 4 3 0; 5 4 0; 6 5 0; ]; 请将以上数据保存为文本文件obs_data.txt和sta_data.txt,并将代码保存为m文件,可以在Matlab中运行该程序进行练习。
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值