Advertisement
StreckerCM

MatLab - Mean Suqared Difference

Feb 14th, 2020
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.66 KB | None | 0 0
  1. %The goal of the as described by the author of the MatLab code
  2.    
  3.     %The FdFd2F function evaluates the functional F, which is the mean squared difference between the corrected measured magnetic
  4.     %field and the corrected reference field, and the first and second derivatives of F.  The updating or correction is done when
  5.     %FdFd2F calls function CalcF.  CalcF applies the current values of the unknowns to the measurements and then evaluates F.
  6.  
  7.     %The first and second derivatives are found by brute force, applying small perturbations to the unknowns and re-evaluating F.
  8.     %These perturbations form the vector pert in function FdFd2F.
  9.  
  10.     %The overall objective of the program is to find those values of the unknowns which minimize the value of F.
  11.  
  12. %Main Function Code
  13.     % Set initial values for unknowns
  14.     bBx = 0;
  15.     bBy = 0;
  16.     bBz = 0;
  17.     sBx = 0;
  18.     sBy = 0;
  19.     sBz = 0;
  20.     Torsion = 0;
  21.     dBref = 0;
  22.     dDref = 0;
  23.  
  24.     unknowns = [bBx bBy bBz sBx sBy sBz Torsion dBref dDref];
  25.     unknown_header = 'Iter_num Bx_bias By_bias Bz_bias   Bx_sf   By_sf   Bz_sf Torsion   dBref   dDref  rmsdBD\n';
  26.     unknown_header2 = 'Iter_num,Bx_bias,By_bias,Bz_bias,Bx_sf,By_sf,Bz_sf,Torsion,dBref,dDref,rmsdBD\n';
  27.     fprintf(unknown_header);
  28.     fprintf(fileID, unknown_header2);
  29.     [F, dF, d2F, Bxcorr, Bycorr, Bzcorr, Brefcorr, Drefcorr] = FdFd2F_1_2(solve, Gx, ...
  30.         Gy, Gz, Bxmeas, Bymeas, Bzmeas, Gref, Bref, Dref, active, unknowns, delta);
  31.  
  32.     rmsdBD = sqrt(F);
  33.     unknown_format = '%8.0f%8.0f%8.0f%8.0f%8.5f%8.5f%8.5f%8.2f%8.0f%8.2f%8.0f\n';
  34.     unknown_format2 = '%8.0f%c%8.0f%c%8.0f%c%8.0f%c%8.5f%c%8.5f%c%8.5f%c%8.2f%c%8.0f%c%8.2f%c%8.0f\n';
  35.     fprintf(unknown_format, [iteration_count unknowns rmsdBD]);
  36.     fprintf(fileID, unknown_format2, iteration_count,',',unknowns(1),',',unknowns(2),',',unknowns(3),',', ...
  37.         unknowns(4),',',unknowns(5),',',unknowns(6),',',unknowns(7),',',unknowns(8),',',unknowns(9),',',rmsdBD);
  38.  
  39.     while iterate == true
  40.         iteration_count = iteration_count + 1;
  41.         adj = dF/d2F;
  42.         for i = 1:length(solve) % update unknowns vector
  43.             unknowns(solve(i)) = unknowns(solve(i)) - adj(i); % could shorten?
  44.         end
  45.         [F, dF, d2F, Bxcorr, Bycorr, Bzcorr, Brefcorr, Drefcorr] = FdFd2F_1_2(solve, Gx, ...
  46.             Gy, Gz, Bxmeas, Bymeas, Bzmeas, Gref, Bref, Dref, active, unknowns, delta);
  47.         rmsdBD = sqrt(F);
  48.         [sdB, sdD, sBD] = QC_1_3(Gx, Gy, Gz, Bxcorr, Bycorr, Bzcorr, Brefcorr, Drefcorr, ...
  49.             MBX, MBY, MBZ, MSX, MSY, MSZ, AMIL, MFI, MDI);
  50.         if strcmp(BD_dim, '2D')
  51.             [max_sBD, max_indexBD] = max(abs(sBD).*active'); % Find maximum MdBD among active stations
  52.             max_sd = max_sBD;
  53.             max_index = max_indexBD;
  54.         else
  55.             [max_sdB, max_indexB] = max(abs(sdB).*active'); % Find maximum MdB among active stations
  56.             [max_sdD, max_indexD] = max(abs(sdD).*active'); % Find maximum MdD among active stations
  57.             max_sd = max([max_sdB max_sdD]);
  58.             if max_sd == max_sdB
  59.                 max_index = max_indexB;
  60.             else
  61.                 max_index = max_indexD;
  62.             end
  63.         end
  64.         if iteration_count >= iteration_limit
  65.             iterate = false;
  66.             if max(abs(abs(adj) - delta(solve))) <= 1; % test for convergence
  67.                 if max_sd > BD_lim; % if there is a bad active station
  68.                     end_message = 'Bad station';
  69.                 else
  70.                     end_message = 'Converged\n';
  71.                 end
  72.             else
  73.                 end_message = 'Not converged\n';
  74.             end
  75.         else
  76.             if max_sd > BD_lim; % if there is a bad active station
  77.                 active(max_index) = 0; % deactivate failed active station
  78.             else
  79.                 if max(abs(abs(adj) - delta(solve))) <= 1; % test for convergence
  80.                     end_message = 'Converged\n';
  81.                     iterate = false;
  82.                 end
  83.             end
  84.         end
  85.         fprintf(unknown_format, [iteration_count unknowns rmsdBD]);    
  86.         fprintf(fileID, unknown_format2, iteration_count,',',unknowns(1),',',unknowns(2),',',unknowns(3),',', ...
  87.             unknowns(4),',',unknowns(5),',',unknowns(6),',',unknowns(7),',',unknowns(8),',',unknowns(9),',',rmsdBD);
  88.     end
  89.  
  90. %FdFd2F_1_2 function
  91.     function[F, dF, d2F, Bxcorr, Bycorr, Bzcorr, Brefcorr, Drefcorr] = FdFd2F_1_2(solve, Gx, Gy, Gz, Bxmeas, Bymeas, Bzmeas, ...
  92.     Gref, Bref, Dref, active, unknowns, delta)
  93.  
  94.     % Version 1.0, 11-3-17
  95.     % Version 1.1, 6-10-18 No changes
  96.     % Version 1.2, 7-15-18 Call to calcF_1_2 instead of calcF
  97.  
  98.     % compute the functional
  99.     [F, Bxcorr, Bycorr, Bzcorr, Brefcorr, Drefcorr] ...
  100.         = calcF_1_2(Gx, Gy, Gz, Bxmeas, Bymeas, Bzmeas, Bref, Dref, active, unknowns);
  101.  
  102.     % Compute the vector of first derivatives and the diagonals of the Jacobian
  103.     N = length(solve);
  104.     dF(N) = zeros;
  105.     d2F(N, N) = zeros;
  106.     pert(9) = zeros;
  107.     for i = 1:N
  108.         pert(1:9) = zeros;
  109.         pert(solve(i)) = delta(solve(i));
  110.         plus = calcF_1_2(Gx, Gy, Gz, Bxmeas, Bymeas, Bzmeas, Bref, Dref, active, unknowns+pert);
  111.         pert(solve(i)) = - delta(solve(i));
  112.         minus = calcF_1_2(Gx, Gy, Gz, Bxmeas, Bymeas, Bzmeas, Bref, Dref, active, unknowns+pert);
  113.         dF(i) = (plus - minus)/(2*delta(solve(i)));
  114.         d2F(i, i) = (plus + minus - 2*F)/(delta(solve(i)))^2;
  115.     end
  116.  
  117.     % Complete the Jacobian matrix of second derivatives
  118.     N = length(solve);
  119.     for i = 1:N-1
  120.         for j = i+1:N
  121.             pert(1:9) = zeros;
  122.             pert(solve(i)) = delta(solve(i));
  123.             pert(solve(j)) = delta(solve(j));
  124.             pp = calcF_1_2(Gx, Gy, Gz, Bxmeas, Bymeas, Bzmeas, Bref, Dref, active, unknowns+pert);
  125.             pert(solve(j)) = - delta(solve(j));
  126.             pm = calcF_1_2(Gx, Gy, Gz, Bxmeas, Bymeas, Bzmeas, Bref, Dref, active, unknowns+pert);
  127.             pert(solve(i)) = - delta(solve(i));
  128.             mm = calcF_1_2(Gx, Gy, Gz, Bxmeas, Bymeas, Bzmeas, Bref, Dref, active, unknowns+pert);
  129.             pert(solve(j)) = delta(solve(j));
  130.             mp = calcF_1_2(Gx, Gy, Gz, Bxmeas, Bymeas, Bzmeas, Bref, Dref, active, unknowns+pert);
  131.             d2F(i,j) = (pp + mm - pm - mp)/(4*delta(solve(i))*delta(solve(j)));
  132.             d2F(j,i) = d2F(i,j);
  133.         end
  134.     end
  135.  
  136. %calcF_1_2 function
  137.     function[F, Bxcorr, Bycorr, Bzcorr, Brefcorr, Drefcorr] ...
  138.         = calcF_1_2(Gx, Gy, Gz, Bxmeas, Bymeas, Bzmeas, Bref, Dref, active, unknowns)
  139.  
  140.     % Version 1.0, 11-4-17
  141.     % Version 1.1, 6-10-18 No changes
  142.     % Version 1.2, 7-15-18 No changes
  143.  
  144.     bBx = unknowns(1);
  145.     bBy = unknowns(2);
  146.     bBz = unknowns(3);
  147.     sBx = unknowns(4);
  148.     sBy = unknowns(5);
  149.     sBz = unknowns(6);
  150.     Torsion = unknowns(7);
  151.     dBref = unknowns(8);
  152.     dDref = unknowns(9);
  153.  
  154.     Bxcorr = (Bxmeas - bBx)*cosd(Torsion)/(1 + sBx) + (Bymeas - bBy)*sind(Torsion)/(1 + sBy);
  155.     Bycorr = (Bymeas - bBy)*cosd(Torsion)/(1 + sBy) - (Bxmeas - bBx)*sind(Torsion)/(1 + sBx);
  156.     Bzcorr = (Bzmeas - bBz)/(1 + sBz);
  157.     G = sqrt(Gx.^2 + Gy.^2 + Gz.^2);
  158.     Bcorr = sqrt(Bxcorr.^2 + Bycorr.^2 + Bzcorr.^2);
  159.     Dcorr = asind((Gx.*Bxcorr + Gy.*Bycorr + Gz.*Bzcorr)./(G.*Bcorr));
  160.     Brefcorr = Bref - dBref;
  161.     Drefcorr = Dref - dDref;
  162.     dBD = sqrt(Bcorr.^2 + Brefcorr.^2 - 2*Bcorr.*Brefcorr.*cosd(Dcorr - Drefcorr));
  163.     F = sum((dBD.*active.').^2)/sum(active); % this is the functional to be minimized (mean square dBD)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement