Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.52 KB | None | 0 0
  1. %%%%%%%%%%%%
  2. clear all
  3. A=[0. ,1. ;-2. ,1.] ; B= [ 0. ; 1.] ;
  4. Q=[2. ,3. ;3. ,5.] ;
  5. F=[1. ,0.5;0.5,2.] ;
  6. R=[0.25] ;
  7. tspan=[O 5];
  8. xo=[2. ,-3.] ;
  9. [x,u,K]=lqrnss(A,B,F,Q,R,xO,tspan);
  10. %%%%%%%%%%%%%
  11.  
  12. %%%%%%%%%%%%%
  13. %% The following is lqrnss.m
  14. function [x,u,K]=lqrnss(As,Bs,Fs,Qs,Rs,xO,tspan)
  15. %Revision Date 11/14/01
  16. %%
  17. %This m-file calculates and plots the outputs for a %Linear Quadratic Regulator (LQR) system based on given % state space matrices A and B and performance index
  18. % matrices F, Q and R. This function takes these inputs, %and using the analytical solution to the
  19. %% matrix Riccati equation,
  20. %and then computing optimal states and controls.
  21. %
  22. %
  23. % SYNTAX: [x,u,K]=lqrnss(A,B,F,Q,R,xO,tspan) %
  24. % INPUTS (All numeric):
  25. Matrices from xdot=Ax+Bu Performance Index Parameters;
  26. State variable initial condition Vector containing time span [to tf]
  27. % A,B
  28. % F,Q,R
  29. % xO
  30. % tspan %
  31. % OUTPUTS:
  32. % x
  33. % u
  34. % K %
  35. % The system plots Riccati coefficients, x vector, %and u vector
  36. %
  37. %Define variables to use in external functions
  38. %
  39. global A E F Md tf W11 W12 W21 W22 n,
  40. %
  41. %Check for correct number of inputs
  42. %
  43. if nargin<7
  44.     error('Incorrect number of inputs specified')
  45.     return
  46. end
  47. %
  48. %Convert Variables to normal symbology to prevent %problems with global statement
  49. %
  50. A=As;
  51. B=Bs;
  52. F=Fs;
  53. Q=Qs;
  54. R=Rs;
  55. plotflag = O;
  56. %data on figures
  57. %
  58. %Define secondary variables for global passing to
  59. % ode-related functions %
  60. [n,m]=size(A);
  61. [nb,mb]=size(B);
  62. [nq,mq] =size (Q) ;
  63. [nr,mr]=size(R);
  64. [nf ,mf] =size (F) ;
  65. if n~=m
  66.     error('A must be square')
  67. else
  68.     [n, n] =size(A);
  69. end
  70. %
  71. %Data Checks for proper setup
  72. if length(A>rank(ctrb(A,B))
  73. %Check for controllability
  74.     error('System Not Controllable')
  75.     return
  76. end
  77. if(n~=nq)|(n~=mq)
  78. %Check that A and Q are the same size
  79.     error('A and Qmust be the same size');
  80.     return
  81. end
  82. if~(mf==l&nf==l)
  83.     if(nq ~= nf)|(mq ~= mf)
  84. %Check that Q and F are the same size
  85.         error('Q and F must be the same size');
  86.         return
  87.     end
  88. end
  89. if ~(mr==l & nr==l)
  90.     if(mr~=nr)|(mb~=nr)
  91.     error('R must be consistent with B');
  92.     return
  93.     end
  94. end
  95. mq = norm(Q,l);
  96. % Check if Q is positive semi-definite and symmetric
  97. if any(eig(Q) < -eps*mq) I (norm(Q'-Q,l)/mq > eps)
  98.     disp('Warning: Q is not symmetric and positive ... semi-definite');
  99. end
  100. mr = norm(R,1);
  101. % Check if R is positive definite and symmetric
  102. if any(eig(R) <= -eps*mr) I (norm(R'-R,l)/mr > eps)
  103.     disp('Warning: R is not symmetric and positive ... definite');
  104. end
  105. %
  106. %Define Initial Conditions for
  107. %numerical solution of x states
  108. %
  109. to=tspan (1) ;
  110. tf=tspan(2);
  111. tspan=[tf to];
  112. %
  113. %Define Calculated Matrices and Vectors
  114. %
  115. E=B*inv(R)*B' ; %E Matrix E=B*(l/R)*B'
  116. %
  117. %Find Hamiltonian matrix needed to use %analytical solution to
  118. %matrix Riccati differential equation %
  119. Z=[A,-E;-Q,-A'];
  120. %
  121. %Find Eigenvectors
  122. %
  123. [W,DJ] = eig(Z) ;
  124. %
  125. %Find the diagonals from D and pick the %negative diagonals to create
  126. %a new matrix M
  127. %
  128. j=n;
  129. [ml,indexlJ=sort(real(diag(D)));
  130.     for i=l:l:n
  131.     m2(i)=ml(j);
  132.     index2(i)=indexl(j);
  133.     index2(i+n)=indexl(i+n);
  134.     j=j-l;
  135.     end
  136. Md=-diag(m2);
  137. %
  138. %Rearrange W so that it corresponds to the sort %of the eigenvalues
  139. %
  140. for i=1:2*n
  141.     w2(:,i)=W(:,index2(i));
  142. end
  143. W=w2;
  144. %
  145. %Define the Modal Matrix for D and Split it into Parts %
  146. Wll=zeros(n);
  147. W12=zeros(n);
  148. W21=zeros(n);
  149. W22=zeros(n);
  150. j=l;
  151. for i=1:2*n:(2*n*n-2*n+l)
  152.     Wll(j:j+n-l)=W(i:i+n-l);
  153.     W21(j:j+n-l)=W(i+n:i+2*n-l);
  154.     W12(j:j+n-l)=W(2*n*n+i:2*n*n+i+n-l);
  155.     W22(j:j+n-l)=W(2*n*n+i+n:2*n*n+i+2*n-l);
  156.     j=j+n;
  157. end %
  158. %Define other initial conditions for % calculation of P, g, x and u
  159. %
  160. tl=O. ;
  161. tx=O. ;
  162. tu=O. ;
  163. x=O. ;
  164. %
  165. %Calculation of optimized x %
  166. %time array for x %time array for u %state vector
  167. [tx,x]=ode45('lqrnssf',fliplr(tspan),xO, ...
  168.     odeset('refine',2,'RelTol',le-4,'AbsTol',le-6));
  169. %
  170. %Find u vector
  171. %
  172. j=1;
  173. us=O.; %Initialize computational variable
  174. for i=l:l:mb
  175.     for tua=tO:.l:tf
  176.         Tt=-inv(W22-F*W12)*(W21-F*Wll);
  177.         P=(W21+W22*expm(-Md*(tf-tua))*Tt* ...
  178. expm(-Md*(tf-tua)))*inv(Wll+W12*expm(-Md*(tf-tua)) ...
  179.     *Tt*expm(-Md*(tf-tua)));
  180.     K=inv(R)*B'*P;
  181.     xs=interpl(tx,x,tua);
  182.     usl=real(-K*xs');
  183.     us(j) =usl(i);
  184.     tu(j)=tua;
  185.     j=j+l;
  186.     end
  187.     u(:,i)=us' ;
  188.     us=O;
  189.     j=1;
  190. end
  191. %
  192. %Provide final steady-state K
  193. %
  194. P = W21/W11;
  195. K=real(inv(R)*B'*P);
  196. %
  197. %Plotting Section, if desired
  198. %
  199. if plotflag~=l
  200. %
  201. %Plot diagonal Riccati coefficients using a
  202. %flag variable to hold and change colors %
  203. fig=1 ; %Figure number
  204. cflag=1; %Variable used to change plot color
  205. j=1;
  206. Ps=O. ;
  207. for i=1:1:n*n %Initialize P matrix plot variable
  208.     for tla=tO: .1:tf
  209.     Tt=-inv(W22-F*W12)*(W21-F*Wll);
  210.     P=real<W21+W22*expm(-Md*(tf-tla))*Tt*expm(-Md* ...
  211.     (tf-tla)))*inv(Wll+W12*expm(-Md*(tf-tla))*Tt ...
  212.     *expm(-Md*(tf-tla))));
  213.     Ps(j)=P(i);
  214.     tl(j)=t1a;
  215.     j=j+l ;
  216.     end
  217.     if cflag==l;
  218.     figure(fig)
  219.     plot(t1,Ps, 'b')
  220.     title('Plot of Riccati Coefficients')
  221.     xlabel('t')
  222.     ylabel ('P Matrix') .
  223.     hold
  224.     cflag=2;
  225.  
  226.     elseif cflag==2
  227.         plot( t1, Ps,' m: ' )
  228.         cflag=3;
  229.     elseif cflag==3
  230.         plot(t1,Ps,'g-.')
  231.         cflag=4;
  232.     elseif cflag==4
  233.         plot(t1,Ps,'r--')
  234.         cflag=1 ;
  235.         fig=fig+1;
  236.     end
  237. Ps=O. ;
  238. j=1 ;
  239. end
  240.     if cflag==2|cflag==3|cflag==4
  241.     hold
  242.     fig=fig+1;
  243.     end
  244. %
  245. %Plot Optimized x %
  246. if n>2
  247.     for i=1:3:(3*fix((n-3)/3)+1)
  248.         figure(fig);
  249.         plot(tx,real(x(:,i)),'b',tx,real(x(:,i+1)),'m:',tx, ...
  250.             real(x(:,i+2)),'g-.')
  251.         title('Plot of Optimized x')
  252.         xlabel('t')
  253.         ylabel('x vectors')
  254.         fig=fig+1;
  255.     end
  256. end
  257. if (n-3*fix(n/3))==1
  258.     figure(fig);
  259.     plot(tx,real(x(:,n)),'b')
  260. elseif (n-3*fix(n/3))==2
  261. figure(fig);
  262. plot( tx , real(x(:,n-1)),'b', tx , real(x(:,n)), 'm:' )
  263. end
  264. title('Plot of Optimized x')
  265. xlabel('t')
  266. ylabel('x vectors')
  267. fig=fig+1;
  268. %
  269. %Plot Optimized u %
  270. if mb>2
  271. for i=1:3:(3*fix((mb-3)/3)+1)
  272.     figure(fig);
  273.     plot(tu,real(u(:,i)),'b',tu,real(u(:,i+1)),'m:', ...
  274.         tu,real(u(:,i+2)),'g-.')
  275.     title('Plot of Optimized u')
  276.     xlabel('t')
  277.     ylabel('u vectors')
  278.     fig=fig+1;
  279. %
  280.     end
  281. end
  282. if (mb-3*fix(mb/3))==1
  283.     figure(fig);
  284.     plot(tu,real(u(:,mb)),'b')
  285. elseif (mb-3*fix(mb/3))==2
  286.     figure(fig);
  287.     plot(tu,real(u(:,mb-1)),'b',tu,real(u(:,mb)),'m:')
  288. end
  289. title('Plot of Optimized u')
  290. xlabel('t')
  291. ylabel('u vectors')
  292. %
  293. end
  294. %% %%%%%%%%%%%%%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement