sashachca

Untitled

Nov 16th, 2018
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 8.92 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(j) );
  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, 5000);
  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); % Ta je tablica otdel'nymi stolbcami, chtoby bylo udobnee pri oformlenii
  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 && n ~= 1
  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.            
  197.            E = rand(1);
  198.            if  0 <= E && E < P(i, 1)
  199.              i = 1;
  200.            elseif E < (P(i, 1)+P(i, 2))
  201.              i = 2;
  202.            elseif E < (P(i, 1)+P(i, 2)+P(i, 3))
  203.              i = 3;
  204.            elseif E < (P(i, 1)+P(i, 2)+P(i, 3)+P(i, 4))
  205.              i = 4;
  206.            elseif E < (P(i, 1)+P(i, 2)+P(i, 3)+P(i, 4)+P(i, 5))
  207.              i = 5;
  208.            elseif E < (P(i, 1)+P(i, 2)+P(i, 3)+P(i, 4)+P(i, 5)+P(i, 6))
  209.              i = 6;
  210.            elseif E < (P(i, 1)+P(i, 2)+P(i, 3)+P(i, 4)+P(i, 5)+P(i, 6)+P(i, 7))
  211.              i = 7;
  212.            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))
  213.              i = 8;
  214.            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))
  215.              i = 9;
  216.            else
  217.              i = 10;
  218.            endif
  219.          
  220.            n += 1;
  221.        endwhile      
  222.        
  223.        if Nj <= 1
  224.         v1 = -1;
  225.       else
  226.         v1 = v(Nj-1);
  227.       endif
  228.      
  229.       if Nj <= 3
  230.         v2 = -1;
  231.       else
  232.         v2 = v(Nj-2);
  233.       endif
  234.        
  235.  
  236.        NV = [Nj, v(Nj), v1, v2];
  237.     endfunction
  238.    
  239.   %______________________________________konec osnovnoy funkcii  
  240. end
  241.  
  242.  
  243.  
  244. %______________________________________other functions:
  245.  
  246. function A = gauss(A) % task 6 (uf: stationDist)
  247.   len = length(A)-1;
  248.  
  249.   for j = 1:len-1
  250.     A = noDivOnZero(A, len, j);
  251.     for i = (j+1):len
  252.       d = -A(i, j) / A(j, j);
  253.       A(i, :) += A(j, :) .* d;
  254.     endfor
  255.   endfor
  256.  
  257.   for i = len:-1:1
  258.     a = A(i, i);
  259.     A(i, :) = A(i, :) ./ a;
  260.     xsum = 0;
  261.     j = len-1;
  262.    
  263.     while j >= i
  264.       xsum += A(i, j+1) * A(j+1, len+1);
  265.       j -= 1;
  266.     endwhile
  267.    
  268.     A(i, len+1) -= xsum;
  269.   endfor
  270.  
  271. endfunction
  272.  
  273.  
  274. function A = noDivOnZero(A, len, ij) % task 6 (of: gauss)
  275.  
  276.   C = [];
  277.   flag = 0;
  278.   k = 0;
  279.   for i = ij:len
  280.  
  281.     if A(i, ij) == 0
  282.       flag = 1;
  283.     endif
  284.    
  285.     if flag == 0
  286.       break
  287.     elseif flag == 1
  288.       flag = 2;
  289.     else
  290.       k = i;
  291.       break
  292.     endif
  293.    
  294.   endfor
  295.  
  296.   if flag ~= 0
  297.     A([ij, k], :) = A([k, ij], :); % menyayem stroki mestami
  298.   endif
  299.  
  300. endfunction
  301.  
  302.  
  303. function graph(A, xy, labels) % task 3
  304.  
  305.   [i,j,w] = find(A);
  306.   [ignore, p] = sort(max(i,j));
  307.   i = i(p);
  308.   j = j(p);
  309.   w = w(p);
  310.  
  311.   X = [ xy(i,1) xy(j,1) repmat(NaN,size(i))]';
  312.   Y = [ xy(i,2) xy(j,2) repmat(NaN,size(i))]';
  313.  
  314.   plot(X, Y, 'Color', 'c')
  315.     hold on;
  316.   plot(xy(:, 1), xy(:, 2), 'ro');
  317.  
  318.   if (max(w) - min(w) > 0) % vychislyayem serediny otrezkov:
  319.       cxs = (xy(i, 1)  + xy(j, 1)) ./ 2;
  320.       cys = (xy(i, 2)  + xy(j, 2)) ./ 2;
  321.       weights = cell(size(cxs));
  322.      
  323.       for iw=1:length(w)
  324.           weights{iw} = num2str(w(iw)); % prevrashayem tsifry v strochnye simvoly
  325.       endfor
  326.       text(cxs, cys, weights, 'FontSize', 14); % pishem vesa u seredin otrezkov
  327.   endif
  328.  
  329.   if (nargin < 3)
  330.       labels = [];
  331.   endif
  332.   if (~isempty(labels))
  333.       text(xy(:, 1), xy(:, 2), labels, 'Color', 'red', 'FontSize', 24, 'FontWeight', 'bold'); % nanosim nomera vershin
  334.   endif
  335.  
  336. endfunction
  337.  
  338. function print_(TABLE, J) % task 8
  339.   printf('\nTa je tablica otdelnymi stolbcami:\n');
  340.   printf('\nj:\n')
  341.   for j = J
  342.    printf('\n%i\n', myround(TABLE(j, 1), 5));
  343.   endfor
  344.   printf('\nN(j):\n')
  345.   for j = J
  346.    printf('\n%i\n', myround(TABLE(j, 2), 5));
  347.   endfor
  348.   printf('\nrj:\n')
  349.   for j = J
  350.    printf('\n%i\n', myround(TABLE(j, 3), 5));
  351.   endfor
  352.   printf('\nVj(N(j)):\n')
  353.   for j = J
  354.    printf('\n%i\n', myround(TABLE(j, 4), 5));
  355.   endfor
  356.   printf('\nVj(N(j)-1):\n')
  357.   for j = J
  358.    printf('\n%i\n', myround(TABLE(j, 5), 5));
  359.   endfor
  360.   printf('\nVj(N(j)-2):\n')
  361.   for j = J
  362.    printf('\n%i\n', myround(TABLE(j, 6), 5));
  363.   endfor
  364. endfunction
  365.  
  366. function R = myround(x, k) % okrugleniye do 5 znakov posle zapyatoy
  367.   R = 0;
  368.  
  369.   R = round(x*10^k)/10^k;
  370.  
  371. endfunction
Advertisement
Add Comment
Please, Sign In to add comment