| 89 |  | xn(:, 1, n) = x0' - [0 0 OMEGAt 0]' + neznalost*sqrtm(P)*randn(4,1); | 
                        | 90 |  | for k = 1:K-1, | 
                        | 91 |  | tu =  L(:, :, k)*(xn(:, k, n)); | 
                        | 92 |  | xn(1, k+1, n) = a*xn(1, k, n) + b*xn(3, k, n)*sin(xn(4, k, n)) + c*tu(1) + sum*sqrt(Q(1, 1))*randn(); | 
                        | 93 |  | xn(2, k+1, n) = a*xn(2, k, n) - b*xn(3, k, n)*cos(xn(4, k, n)) + c*tu(2) + sum*sqrt(Q(2, 2))*randn(); | 
                        | 94 |  | xn(3, k+1, n) = d*xn(3, k, n) + e*(xn(2, k, n)*cos(xn(4, k, n)) - xn(1, k, n)*sin(xn(4, k, n))) + sum*sqrt(Q(3, 3))*randn(); | 
                        | 95 |  | xn(4, k+1, n) = xn(4, k, n) + xn(3, k, n)*DELTAt + sum*sqrt(Q(4, 4))*randn(); | 
                      
                        |  | 90 | xn(:, 1, n) = x0' + neznalost*sqrtm(P)*randn(4,1); | 
                        |  | 91 | for k = 1:Kt-1, | 
                        |  | 92 | tu =  L(:, :, k)*(xn(:, k, n) - [0 0 OMEGAt 0]'); | 
                        |  | 93 | xn(1, k+1, n) = a*xn(1, k, n) + b*xn(3, k, n)*sin(xn(4, k, n)) + c*tu(1) + sumsim*sqrt(Q(1, 1))*randn(); | 
                        |  | 94 | xn(2, k+1, n) = a*xn(2, k, n) - b*xn(3, k, n)*cos(xn(4, k, n)) + c*tu(2) + sumsim*sqrt(Q(2, 2))*randn(); | 
                        |  | 95 | xn(3, k+1, n) = d*xn(3, k, n) + e*(xn(2, k, n)*cos(xn(4, k, n)) - xn(1, k, n)*sin(xn(4, k, n))) + sumsim*sqrt(Q(3, 3))*randn(); | 
                        |  | 96 | xn(4, k+1, n) = xn(4, k, n) + xn(3, k, n)*DELTAt + sumsim*sqrt(Q(4, 4))*randn(); | 
                        |  | 97 | %            xn(1, k+1, n) = a*xn(1, k, n) + b*(xn(3, k, n)+OMEGAt)*sin(xn(4, k, n)) + c*tu(1) + sum*sqrt(Q(1, 1))*randn(); | 
                        |  | 98 | %            xn(2, k+1, n) = a*xn(2, k, n) - b*(xn(3, k, n)+OMEGAt)*cos(xn(4, k, n)) + c*tu(2) + sum*sqrt(Q(2, 2))*randn(); | 
                        |  | 99 | %            xn(3, k+1, n) = -OMEGAt + d*(xn(3, k, n)+OMEGAt) + e*(xn(2, k, n)*cos(xn(4, k, n)) - xn(1, k, n)*sin(xn(4, k, n))) + sum*sqrt(Q(3, 3))*randn(); | 
                        |  | 100 | %            xn(4, k+1, n) = xn(4, k, n) + (xn(3, k, n)+OMEGAt)*DELTAt + sum*sqrt(Q(4, 4))*randn(); | 
            
                      
                        | 103 |  | S(:, :, K) = X; | 
                        | 104 |  | for k = K-1:-1:1, | 
                        | 105 |  | A(3, 1) = -e*sin(xs(4, k)); | 
                        | 106 |  | A(3, 2) = e*cos(xs(4, k)); | 
                        | 107 |  | A(1, 3) = b*sin(xs(4, k)); | 
                        | 108 |  | A(2, 3) = -b*cos(xs(4, k)); | 
                        | 109 |  | A(1, 4) = b*xs(3, k)*cos(xs(4, k)); | 
                        | 110 |  | A(2, 4) = b*xs(3, k)*sin(xs(4, k)); | 
                        | 111 |  | A(3, 4) = -e*(xs(2, k)*sin(xs(4, k) + xs(1,k)*cos(xs(4, k)))); | 
                        | 112 |  | S(:, :, k) = A'*(S(:, :, k+1) - S(:, :, k+1)*B*inv(B'*S(:, :, k+1)*B + Y)*B'*S(:, :, k+1))*A + X; | 
                        | 113 |  | L(:, :, k) = -inv(B'*S(:, :, k+1)*B + Y)*B'*S(:, :, k+1)*A; | 
                        | 114 |  | end | 
                      
                        |  | 108 | for kt = 1:Kt-1, | 
                        |  | 109 | %     receding horizon | 
                        |  | 110 | if((K-1+kt)<Kt) | 
                        |  | 111 | S(:, :, K) = X; | 
                        |  | 112 | for k = K-1+kt-1:-1:1+kt-1, | 
                        |  | 113 | A(3, 1) = -e*sin(xs(4, k)); | 
                        |  | 114 | A(3, 2) = e*cos(xs(4, k)); | 
                        |  | 115 | A(1, 3) = b*sin(xs(4, k)); | 
                        |  | 116 | A(2, 3) = -b*cos(xs(4, k)); | 
                        |  | 117 | A(1, 4) = b*(xs(3, k))*cos(xs(4, k)); | 
                        |  | 118 | A(2, 4) = b*(xs(3, k))*sin(xs(4, k)); | 
                        |  | 119 | %             A(1, 4) = b*(xs(3, k)+OMEGAt)*cos(xs(4, k)); | 
                        |  | 120 | %             A(2, 4) = b*(xs(3, k)+OMEGAt)*sin(xs(4, k)); | 
                        |  | 121 | A(3, 4) = -e*(xs(2, k)*sin(xs(4, k) + xs(1,k)*cos(xs(4, k)))); | 
                        |  | 122 | S(:, :, k-kt+1) = A'*(S(:, :, k-kt+2) - S(:, :, k-kt+2)*B*inv(B'*S(:, :, k-kt+2)*B + Y)*B'*S(:, :, k-kt+2))*A + X; | 
                        |  | 123 | end | 
                        |  | 124 | L(:, :, kt) = -inv(B'*S(:, :, 1)*B + Y)*B'*S(:, :, 1)*A; | 
                        |  | 125 | else | 
                        |  | 126 | L(:, :, kt) = L(:, :, kt-1); %kopiruje poslednich K kroku z Kt kde to nejde na K predpocitat | 
                        |  | 127 | end | 
                        |  | 128 |  | 
                        |  | 129 | end | 
            
                  
                          | 128 | 143 | xn(1, k+1, n) = a*xn(1, k, n) + b*xn(3, k, n)*sin(xn(4, k, n)) + c*tu(1) + sumsim*sqrt(Q(1, 1))*randn(); | 
                          | 129 | 144 | xn(2, k+1, n) = a*xn(2, k, n) - b*xn(3, k, n)*cos(xn(4, k, n)) + c*tu(2) + sumsim*sqrt(Q(2, 2))*randn(); | 
                          | 130 | 145 | xn(3, k+1, n) = d*xn(3, k, n) + e*(xn(2, k, n)*cos(xn(4, k, n)) - xn(1, k, n)*sin(xn(4, k, n))) + sumsim*sqrt(Q(3, 3))*randn(); | 
                          | 131 | 146 | xn(4, k+1, n) = xn(4, k, n) + xn(3, k, n)*DELTAt + sumsim*sqrt(Q(4, 4))*randn(); | 
            
                  
                          |  | 147 | %            xn(1, k+1, n) = a*xn(1, k, n) + b*(xn(3, k, n)+OMEGAt)*sin(xn(4, k, n)) + c*tu(1) + sum*sqrt(Q(1, 1))*randn(); | 
                          |  | 148 | %            xn(2, k+1, n) = a*xn(2, k, n) - b*(xn(3, k, n)+OMEGAt)*cos(xn(4, k, n)) + c*tu(2) + sum*sqrt(Q(2, 2))*randn(); | 
                          |  | 149 | %            xn(3, k+1, n) = -OMEGAt + d*(xn(3, k, n)+OMEGAt) + e*(xn(2, k, n)*cos(xn(4, k, n)) - xn(1, k, n)*sin(xn(4, k, n))) + sum*sqrt(Q(3, 3))*randn(); | 
                          |  | 150 | %            xn(4, k+1, n) = xn(4, k, n) + (xn(3, k, n)+OMEGAt)*DELTAt + sum*sqrt(Q(4, 4))*randn(); |