Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 8.41 KB | None | 0 0
  1. %==========================================================================
  2. % SSY280 Model Predictive Control 2012
  3. %
  4. % Homework Assignment 2:
  5. % MPC Control of a Linearized MIMO Well Stirred Chemical Reactor
  6. % Revised 2013-02-10
  7. %==========================================================================
  8.  
  9. %******************* Initialization block *********************************
  10.  
  11. clear;
  12. clc
  13. close all
  14. tf=50;                  % number of simulation steps
  15.  
  16. %==========================================================================
  17. % Process model
  18. %==========================================================================
  19.  
  20. h = 1; % sampling time in minutes
  21.  
  22. A = [ 0.2681   -0.00338   -0.00728;
  23.       9.7032    0.3279   -25.44;
  24.          0         0       1   ];
  25. B = [ -0.00537  0.1655;
  26.        1.297   97.91 ;
  27.        0       -6.637];
  28. C = [ 1 0 0;
  29.       0 1 0;
  30.       0 0 1];
  31. Bp = [-0.1175;
  32.       69.74;
  33.        6.637 ];
  34.    
  35. n = size(A,1); % n is the dimension of the state
  36. m = size(B,2); % m is the dimension of the control signal
  37. p = size(C,1); % p is the dimension of the measured output
  38.  
  39. d=0.01*[zeros(1*tf/5,1);ones(4*tf/5,1)]; %unmeasured disturbance trajectory
  40.  
  41. x0 = [0.01;1;0.1]; % initial condition of system's state
  42.  
  43. %==========================================================================
  44. % Set up observer model
  45. %==========================================================================
  46.  
  47. % Three cases to be investigated
  48.  
  49. example = 'c';
  50. switch example
  51.     case 'a'
  52.         nd = 2;
  53.         Bd = zeros(n,nd);
  54.         Cd = [1 0;0 0; 0 1];
  55.     case 'b'
  56.         nd = 3;
  57.         Bd = zeros(n,nd);
  58.         Cd = [1 0 0;0 0 1;0 1 0];
  59.     case 'c'
  60.         nd=3;
  61.         Bd = [zeros(3,2) Bp];
  62.         Cd = [1 0 0;0 0 0;0 1 0];
  63. end
  64.  
  65. % Verify detectability
  66. detect=rank([eye(n)-A, -Bd;C,Cd]);
  67. if detect==n+nd
  68.     fprintf('System Detectable\n')
  69. else
  70.     fprintf('System Not Detectable\n')
  71. end
  72.  
  73. % Augment the model with constant disturbances
  74.  
  75. Ae = [A,Bd;zeros(nd,n),eye(nd)];
  76. Be = [B;zeros(nd,2)];
  77. Ce = [C,Cd];
  78.  
  79. % Calculate observer gain
  80. switch example
  81.     case 'a'
  82.         Le = place(Ae',Ce',[-0.01, -0.03, -0.02, -0.015, -0.025])';
  83.     case 'b'
  84.         Le=[-0.106146678301203 -0.00296211521208997 -0.0364757336637961;9.56640530077989 1.37652321422317 -25.5979722091198;-0.540036420958648 -0.0403551551790192 2.44451009284336;1.40982966694229 -0.000478200306218713 0.0297436133689198;0.534553573163769 0.0408494810943643 -0.418716295708000;-0.000839197290552221 7.57279394927729e-05 0.154576370720167];
  85.     case 'c'
  86.         Le = place(Ae',Ce',[-0.01, -0.03, -0.02, -0.015, -0.025,-0.01])';
  87. end
  88. %==========================================================================
  89. % Prepare for computing steady state targets
  90. %==========================================================================
  91.  
  92. % Select 1st and 3rd outputs as controlled outputs  
  93. H = [1 0 0;0 0 1];
  94.  
  95. % Matrices for steady state target calculation to be used later
  96.  
  97. As=[eye(n)-A, -B; H*C, zeros(2,2)];
  98. Bs = @(dh) [Bd*dh;[0;0]-H*Cd*dh];
  99.  
  100. %==========================================================================
  101. % Set up MPC controller
  102. %==========================================================================
  103.  
  104. N=10;                   % prediction horizon
  105. M=3;                    % control horizon
  106.  
  107. Q = diag([1 0.001 1]);  % state penalty
  108. Pf = Q;                 % terminal state penalty
  109. R = 0.01*eye(m);        % control penalty
  110.    
  111.     %=================================
  112.     % Build Hessian Matrix
  113.     %=================================
  114.  
  115.     Hess = zeros(n*N+M*m);
  116.     Hess(1:n*N,1:n*N) = kron(eye(N),Q);
  117.     Hess(n*N+1:n*N+M*m,n*N+1:n*N+M*m) = kron(eye(M),R);
  118.     f = zeros(length(Hess),1);
  119.    
  120.     %==========================================
  121.     % Equality Constraints
  122.     %==========================================
  123.     leftAeq=zeros(N);
  124.     rightAeq=zeros(N,M);
  125.     for i=2:N
  126.         leftAeq(i,i-1)=-1;
  127.     end
  128.     for i = 1:N
  129.         if i > M
  130.             rightAeq(i,M)=1;
  131.         else
  132.             rightAeq(i,i)=1;
  133.         end
  134.     end
  135.     leftAeq=kron(leftAeq,A)+eye(n*N);
  136.     rightAeq=kron(rightAeq,-B);
  137.     Aeq=[leftAeq,rightAeq];
  138.     AA=[A;zeros(n*(N-1),n)];
  139.    
  140.     %==========================================
  141.     % Inequality Constraints
  142.     %==========================================
  143.    
  144.     Ain = [];
  145.     Bin = [];
  146.    
  147.     %==============================================
  148.     % Choose QP solver
  149.     %==============================================
  150.    
  151.     solver = 'int';
  152.     switch solver
  153.         case 'int'
  154.             options = optimset('Algorithm','interior-point-convex','Display','off');
  155.         case 'set'
  156.             options = optimset('Algorithm','active-set','Display','off');
  157.     end
  158.  
  159. %******************* End of initialization ********************************    
  160.  
  161. %==========================================================================
  162. % Simulation
  163. %==========================================================================
  164.    
  165. % Initialization
  166.    
  167. xk=x0;
  168. xhat = zeros(n,1);
  169. d0 = zeros(nd,1);
  170. xi = [xhat; d0];
  171. Output=[];
  172. Input=[];
  173. Disturbance=[];
  174. time_vec=[];
  175. uk=[0;0];
  176.  
  177. % Simulate closed-loop system
  178.    
  179.     for k = 1:tf
  180.  
  181.         %======================================
  182.         % Update the observer state xhat(k|k-1)
  183.         %======================================
  184.        
  185.         % YOUR CODE GOES HERE
  186.         xi(:, k+1) = Ae * xi(:, k) + Be * uk + Le * (C* xk - [C, Cd] * xi(:, k));
  187.        
  188.         %==============================================
  189.         % Update the process state x(k) and output y(k)
  190.         %==============================================
  191.        
  192.         xk = A*xk + B*uk + Bp*d(k);
  193.         yk = C*xk;        
  194.        
  195.         %=========================================
  196.         % Calculate steady state targets xs and us
  197.         %=========================================
  198.        
  199.         dhat=xi(n+1:n+nd,k+1);
  200.         %dhat=C*xk-C*xi(:,k+1);
  201.         Bsk=Bs(dhat);
  202.         xs_us=As\Bs(dhat);
  203.         xs=xs_us(1:n);
  204.         us=xs_us(n+1:n+m);
  205.        
  206.         %============================================
  207.         % Solve the QP (for the deviation variables!)
  208.         %============================================
  209.        
  210.         beq=AA*(xk-xs);
  211.         xk-xs
  212.        
  213.         % NOTE THAT Hess IS USED FOR THE HESSIAN, NOT TO BE CONFUSED
  214.         %   WITH H THAT IS USED FOR SELECTING CONTROLLED VARIABLES
  215.         z = quadprog(Hess,f,Ain,Bin,Aeq,beq,[],[],[],options);
  216.        
  217.        
  218.         % NOTE THAT YOU NEED TO GO FROM DEVIATION VARIABLES TO 'REAL' ONES!
  219.         uk=z(N*n+1:N*n+m)+us;
  220.         %===============================        
  221.         % Store current variables in log
  222.         %===============================
  223.        
  224.         Input=[Input,uk];
  225.         Output=[Output,yk];
  226.         Disturbance=[Disturbance,dhat];
  227.         time_vec=[time_vec,k];
  228.        
  229.    end % simulation loop
  230.  
  231. %==========================================================================
  232. % Plot results
  233. %==========================================================================
  234.  
  235.  
  236. xHat = xi(1:3, 2:end);
  237.  
  238. figure(1)
  239. outputBoundaries = [min([Output xHat], [], 2) max([Output xHat], [], 2)];
  240. margin = abs(outputBoundaries(:, 1) - outputBoundaries(:, 2)) * 0.1;
  241. ylims = [outputBoundaries(:, 1) - margin outputBoundaries(:, 2) + margin];
  242.  
  243.  
  244. subplot(3, 1, 1); hold on;
  245. plot(time_vec,Output(1,:));
  246. plot(time_vec, xHat(1, :));
  247. grid minor
  248. ylim(ylims(1, :))
  249. ylabel('Concentration')
  250. legend({'x', '$\hat{x}$'}, 'Interpreter', 'Latex');
  251.  
  252. subplot(3, 1, 2); hold on;
  253. plot(time_vec,Output(2,:));
  254. plot(time_vec,xHat(2,:));
  255. grid minor
  256. ylim(ylims(2, :))
  257. ylabel('Temperature')
  258.  
  259. subplot(3, 1, 3); hold on;
  260. plot(time_vec,Output(3,:));
  261. plot(time_vec,xHat(3,:));
  262. grid minor
  263. ylim(ylims(3, :))
  264. ylabel('Tank level')
  265.  
  266. xlabel('time (m)')
  267. hold off
  268.  
  269. figure(2)
  270. subplot(2, 1, 1);
  271. plot(time_vec,Input(1,:));
  272. ylabel('Coolant Temperature')
  273. grid minor
  274. subplot(2, 1, 2);
  275. plot(time_vec,Input(2,:));
  276. ylabel('Outlet flowrate')
  277. grid minor
  278. xlabel('time (m)')
  279. hold off
  280.  
  281. figure(3)
  282. hold on
  283. plot(time_vec,d)
  284. plot(time_vec,Disturbance(1,:));
  285. plot(time_vec,Disturbance(2,:));
  286.  
  287.  
  288. if nd == 3
  289.     plot(time_vec,Disturbance(3,:));
  290.     legend({'$d$', '$\hat{d_1}$','$\hat{d_2}$','$\hat{d_3}$'}, 'Interpreter', 'Latex')
  291. else
  292.     legend({'$d$', '$\hat{d_1}$','$\hat{d_2}$', 'Interpreter'}, 'Interpreter', 'Latex')
  293. end
  294.  
  295. xlabel('time (m)')
  296. hold off
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement