Advertisement
sashachca

Untitled

Nov 13th, 2018
601
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 8.52 KB | None | 0 0
  1. function SP_LR_2_SALNIKOVA
  2.   % Variant 13
  3.  
  4.   p = [0, 0.353, 0.647];
  5.   q = [0.59, 0.41];
  6.  
  7.  
  8.   %______________________________________task 1:
  9.   states = [ 'GGGRR', 'GGRGR', 'GGRRG', 'GRGGR', 'GRGRG', 'GRRGG', 'RGGGR', 'RGGRG', 'RGRGG', 'RRGGG' ];
  10.   possibleStates = [];
  11.   P = A = zeros(10, 10);
  12.  
  13.   % togda k n-mu sostoyaniyu mojno obraschat'sya kak  states(1+5*(n-1):5+5*(n-1))
  14.   for N = 1:10
  15.     numbers = [];
  16.     mainState = states(1+5*(N-1):5+5*(N-1));
  17.    
  18.     for n = 1:10
  19.       state = states(1+5*(n-1):5+5*(n-1));
  20.       flag = g = r = 0;
  21.       pq = 1;
  22.      
  23.       for m = 1:5
  24.         mS = mainState(m);
  25.        
  26.         g += mS == 'G';
  27.         r += mS == 'R';
  28.         if mS ~= state(m);
  29.           flag += 1;
  30.   %______________________________________dlya perehodnoy matricy P:
  31.  
  32.           switch mS
  33.             case 'G'
  34.               pq *= p(g);
  35.             case 'R'
  36.               pq *= q(r);
  37.           endswitch
  38.   %______________________________________
  39.  
  40.         endif
  41.       endfor
  42.      
  43.       if flag == 2 && pq ~= 0 % sostoyaniya doljny otlichat'sya na 2 simvola
  44.         numbers = [numbers, n];
  45.         P(N, n) = pq; % stroim perehodnuyu matricu P
  46.         A(N, n) = 1;
  47.       endif
  48.     endfor
  49.    
  50.     possibleStates = [possibleStates; numbers];
  51.   endfor
  52.  
  53.   printf('\n\n-------------\n  TASK NUMBER 1\n\n\n');
  54.   possibleStates
  55.  
  56.  
  57.   %______________________________________task 2:
  58.   printf('\n\n-------------\n  TASK NUMBER 2\n\n\n');
  59.   P
  60.  
  61.  
  62.   %______________________________________task 3:
  63.   xy = [0.9, 3.5; 1.4, 2; 2.5, 3; 2.2, 4; 2.1, 4.8; 1.5, 5; 0.5, 2.25; 0.6, 2.75; 0, 1.75; -0.5, 3.25];
  64.     figure
  65.   graph(triu(P), xy, num2cell(1:10));
  66.   title('i to j');
  67.     figure
  68.   graph(tril(P), xy, num2cell(1:10));
  69.   title('j to i');
  70.  
  71.  
  72.   %______________________________________task 4:
  73.   printf('\n\n-------------\n  TASK NUMBER 4\n\n\n');
  74.   incs = incommun(A) % nesuschestvennye sostoyaniya
  75.   if ~isempty(incs)
  76.     fprintf("the chain isn't ergodic\n\n");
  77.   endif
  78.  
  79.  
  80.   %______________________________________task 5:
  81.   printf('\n\n-------------\n  TASK NUMBER 5\n\n\n');
  82.   p100 = P^100
  83.  
  84.  
  85.   %______________________________________task 6:
  86.   printf('\n\n-------------\n  TASK NUMBER 6\n\n\n');
  87.   r = stationDist(P)
  88.  
  89.  
  90.   %______________________________________task 7:
  91.   deltas = [];
  92.   for i = 1:10
  93.     for j = 1:10
  94.       deltas(i, j) = abs( p100(i, j) - r(i) );
  95.     endfor
  96.   endfor
  97.  
  98.   most_max_delta = max( max(deltas) );
  99.   for j = 1:10
  100.     MAX_DELTAS(j) = max( deltas(:, j) );
  101.   endfor
  102.  
  103.   printf('\n\n-------------\n  TASK NUMBER 7\n\n\n');
  104.   deltas
  105.   MAX_DELTAS
  106.   most_max_delta
  107.  
  108.   %______________________________________task 8:
  109.   V = N = [];
  110.   J = 1:10;
  111.   for j = J
  112.     NV = gen(j, 10000);
  113.     N(j) = NV(1);
  114.     V(j, 1:3) = NV(2:4);
  115.   endfor
  116.  
  117.   printf('\n\n-------------\n  TASK NUMBER 8\n\n\n');
  118.   TABLE = [J', N', r', V(:, 1:3)]
  119.   %print_(TABLE, J);
  120.  
  121.  
  122.  
  123.   %______________________________________used functions:
  124.  
  125.   function incs = incommun(A) % task 4
  126.     incs = [];
  127.    
  128.     for k = 1:10
  129.       for i = 1:10
  130.         for j = 1:10
  131.        
  132.           if i ~= j && A(i,k) + A(k,j) == 2
  133.             A(i,j) = 1; % vnosim v nashu matricu smejnosti informatsiyu o tom, mojno li hot' kak-to iz odnoy vershiny proyti v druguyu
  134.           endif
  135.        
  136.         endfor
  137.       endfor
  138.     endfor
  139.    
  140.     for j = 1:10
  141.       indices_of_zeros = find(A(:, j) == 0);
  142.       if length(indices_of_zeros) > 1
  143.         incs(end+1) = j;
  144.       endif
  145.     endfor
  146.  
  147.   endfunction
  148.  
  149.  
  150.   function q = stationDist(P) % task 6
  151.       % q - stolbets
  152.       % qP = q => qP - q = 0 => q(P - E) = 0, gdE E - edinichnaya matrica, prichem v nashem kodE eto eye(n)
  153.       % q = (r1, r2, r3)
  154.       % eye(n) - edinichnaya matrica n x n
  155.  
  156.       A = P' - eye(10); % vychitaem edinichnuyu matricu
  157.       A = [A, zeros(10, 1)]; % pripisyvaem 4-y stolbets s nulevymi svobodnymi chlenami
  158.       A(10, :) = ones(1, 11); % t.k. vectory lineyno zavisimye, to vycherkivaem poslednyuyu stroku
  159.       % pri etom u nas est' usloviE normirovki: r1 + r2 + r3 = 1 -- stavim ego na mesto vycherknutoy stroki
  160.  
  161.       A = gauss(A); % reshaem SLAU
  162.  
  163.       printf('\n\n');
  164.       q = A(:, 11)' % q - naydennoE stacionarnoE raspredelenie
  165.       qP = q*P(:, :, 1) % Prover 04ka:
  166.  
  167.       if myround(q, 5) == myround(qP, 5)
  168.         disp(' => q = qP')
  169.       else
  170.         disp(' => q =/= qP')
  171.       endif
  172.      
  173.   endfunction
  174.  
  175.   function NV = gen(j, Nj) % task 8
  176.       v = I = [];
  177.       i = j; n = 1;
  178.      
  179.       while n <= Nj
  180.            I(end+1) = i;
  181.            
  182.            amount_of_k = 0;
  183.            for k = 1:n
  184.              if I(k) == j
  185.                 amount_of_k += 1;
  186.              endif
  187.            endfor
  188.            R = amount_of_k;
  189.            v(n) = R/n;
  190.  
  191.            [value, min_n] = min( abs(v .- r(j)) );
  192.            if value < 0.001
  193.               Nj = min_n;
  194.            endif
  195.            
  196.            E = rand(1);
  197.            if  0 <= E && E < P(i,1)
  198.              i = 1;
  199.            elseif E < (P(i,1)+P(i,2))
  200.              i = 2;
  201.            elseif E < (P(i,1)+P(i,2)+P(i,3))
  202.              i = 3;
  203.            elseif E < (P(i,1)+P(i,2)+P(i,3)+P(i,4))
  204.              i = 4;
  205.            elseif E < (P(i,1)+P(i,2)+P(i,3)+P(i,4)+P(i,5))
  206.              i = 5;
  207.            elseif E < (P(i,1)+P(i,2)+P(i,3)+P(i,4)+P(i,5)+P(i,6))
  208.              i = 6;
  209.            elseif E < (P(i,1)+P(i,2)+P(i,3)+P(i,4)+P(i,5)+P(i,6)+P(i,7))
  210.              i = 7;
  211.            elseif E < (P(i,1)+P(i,2)+P(i,3)+P(i,4)+P(i,5)+P(i,6)+P(i,7)+P(i,8))
  212.              i = 8;
  213.            elseif E < (P(i,1)+P(i,2)+P(i,3)+P(i,4)+P(i,5)+P(i,6)+P(i,7)+P(i,8)+P(i,9))
  214.              i = 9;
  215.            else
  216.              i = 10;
  217.            endif
  218.          
  219.            n += 1;
  220.        endwhile
  221.  
  222.        NV = [Nj, v(Nj), v(Nj-1), v(Nj-2)];
  223.     endfunction
  224.    
  225.   %______________________________________konec osnovnoy funkcii  
  226. end
  227.  
  228.  
  229.  
  230. %______________________________________other functions:
  231.  
  232. function A = gauss(A) % task 6 (uf: stationDist)
  233.   len = length(A)-1;
  234.  
  235.   for j = 1:len-1
  236.     A = noDivOnZero(A, len, j);
  237.     for i = (j+1):len
  238.       d = -A(i, j) / A(j, j);
  239.       A(i, :) += A(j, :) .* d;
  240.     endfor
  241.   endfor
  242.  
  243.   for i = len:-1:1
  244.     a = A(i, i);
  245.     A(i, :) = A(i, :) ./ a;
  246.     xsum = 0;
  247.     j = len-1;
  248.    
  249.     while j >= i
  250.       xsum += A(i, j+1) * A(j+1, len+1);
  251.       j -= 1;
  252.     endwhile
  253.    
  254.     A(i, len+1) -= xsum;
  255.   endfor
  256.  
  257. endfunction
  258.  
  259.  
  260. function A = noDivOnZero(A, len, ij) % task 6 (of: gauss)
  261.  
  262.   C = [];
  263.   flag = 0;
  264.   k = 0;
  265.   for i = ij:len
  266.  
  267.     if A(i, ij) == 0
  268.       flag = 1;
  269.     endif
  270.    
  271.     if flag == 0
  272.       break
  273.     elseif flag == 1
  274.       flag = 2;
  275.     else
  276.       k = i;
  277.       break
  278.     endif
  279.    
  280.   endfor
  281.  
  282.   if flag ~= 0
  283.     A([ij, k], :) = A([k, ij], :); % menyayem stroki mestami
  284.   endif
  285.  
  286. endfunction
  287.  
  288.  
  289. function graph(A, xy, labels) % task 3
  290.  
  291.   [i,j,w] = find(A);
  292.   [ignore, p] = sort(max(i,j));
  293.   i = i(p);
  294.   j = j(p);
  295.   w = w(p);
  296.  
  297.   X = [ xy(i,1) xy(j,1) repmat(NaN,size(i))]';
  298.   Y = [ xy(i,2) xy(j,2) repmat(NaN,size(i))]';
  299.  
  300.   plot(X, Y, 'Color', 'c')
  301.     hold on;
  302.   plot(xy(:, 1), xy(:, 2), 'ro');
  303.  
  304.   if (max(w) - min(w) > 0) % vychislyayem serediny otrezkov:
  305.       cxs = (xy(i, 1)  + xy(j, 1)) ./ 2;
  306.       cys = (xy(i, 2)  + xy(j, 2)) ./ 2;
  307.       weights = cell(size(cxs));
  308.      
  309.       for iw=1:length(w)
  310.           weights{iw} = num2str(w(iw)); % prevrashayem tsifry v strochnye simvoly
  311.       endfor
  312.       text(cxs, cys, weights, 'FontSize', 14); % pishem vesa u seredin otrezkov
  313.   endif
  314.  
  315.   if (nargin < 3)
  316.       labels = [];
  317.   endif
  318.   if (~isempty(labels))
  319.       text(xy(:, 1), xy(:, 2), labels, 'Color', 'red', 'FontSize', 24, 'FontWeight', 'bold'); % nanosim nomera vershin
  320.   endif
  321.  
  322. endfunction
  323.  
  324. %function print_(TABLE, J) % task 8
  325. %  printf('\nJ\n')
  326. %  for j = J
  327. %   printf('\n%i\n', myround(TABLE(j, 1), 5))
  328. %  endfor
  329. %  printf('\nN\n')
  330. %  for j = J
  331. %   printf('\n%i\n', myround(TABLE(j, 2), 5))
  332. %  endfor
  333. %  printf('\nr\n')
  334. %  for j = J
  335. %   printf('\n%i\n', myround(TABLE(j, 3), 5))
  336. %  endfor
  337. %  printf('\nV\n')
  338. %  for j = J
  339. %   printf('\n%i\n', myround(TABLE(j, 4), 5))
  340. %  endfor
  341. %  printf('\nV1\n')
  342. %  for j = J
  343. %   printf('\n%i\n', myround(TABLE(j, 5), 5))
  344. %  endfor
  345. %  printf('\nV2\n')
  346. %  for j = J
  347. %   printf('\n%i\n', myround(TABLE(j, 6), 5))
  348. %  endfor
  349. %endfunction
  350.  
  351. function R = myround(x, k) % okrugleniye do 5 znakov posle zapyatoy
  352.   R = 0;
  353.  
  354.   R = round(x*10^k)/10^k;
  355.  
  356. endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement