Advertisement
Guest User

MATLAB code for solving PMP example

a guest
Oct 23rd, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.36 KB | None | 0 0
  1. clearvars
  2. close all
  3.  
  4. % Defining your example system
  5. A = [-1 0.5; 0.3 -1];
  6. B = [1; 1];
  7. x0 = [1; 0];
  8. xT = [0; 1];
  9. T = 5;
  10. rho = 100;
  11.  
  12. % Defining some intermediate constants
  13. I = eye(2);
  14. O = zeros(2);
  15. Ah = [A -1/(2*rho)*(B*B'); -2*I -A'];
  16. Bh = [O; 2*I];
  17.  
  18. % X = (expm(Ah * T) - I) * inv(Ah) * Bh can also be calculated with
  19. % [expm(Ah * T) X; 0 I] = expm([Ah Bh; 0 0] * T)
  20. % for more details see: https://en.wikipedia.org/wiki/Discretization
  21. expAB = expm([Ah Bh; zeros(2, 6)] * T);
  22. M11 = expAB(1:2,1:2);   % Upper left part of expm(Ah * T)
  23. M12 = expAB(1:2,3:4);   % Upper right part of expm(Ah * T)
  24. M21 = expAB(3:4,1:2);   % Lower left part of expm(Ah * T)
  25. M22 = expAB(3:4,3:4);   % Lower right part of expm(Ah * T)
  26. R1 = expAB(1:2,5:6);    % Upper part of X
  27. R2 = expAB(3:4,5:6);    % Lower part of X
  28.  
  29. % Using equation (21) then lambda0 and lambdaT can be calculate with
  30. % [lambda0; lambdaT] = [-M12 O; -M22 I] \ ([M11 R1-I; M21 R2] * [x0; xT])
  31. % However we only need lambda0, which can be solved by only using the top
  32. % half of (19), which can be simplifyfied down to:
  33. % xT = M11 * x0 + M12 * lambda0 + R1 * xT
  34. % Solving this for lambda0 gives:
  35. lambda0 = M12 \ ((I - R1) * xT - M11 * x0);
  36.  
  37. % So all the initial conditions are known and pluging this into (18) should
  38. % give use the trajectory as a function of time:
  39. f = @(t) expm(Ah * t) * ([x0; lambda0] - Ah \ Bh * xT) + Ah \ Bh * xT;
  40.  
  41. %% Solution using f(t) obtained from (18)
  42.  
  43. N = 1e3;                % Number of samples used for the trajectory
  44. t = linspace(0, T, N)% Corresponding time vector
  45. X = zeros(4, N);        % Allocate memory for the trajectory
  46. U = zeros(1, N);        % Allocate memory for the input
  47.  
  48. % Calculate the state and input at each time instance
  49. for k = 1 : N
  50.     X(:,k) = f(t(k));
  51.     U(k) = -(0.5 / rho) * B' * X(3:4,k);
  52. end
  53.  
  54. % Plotting the results
  55. figure
  56. subplot(2,1,1)
  57. plot(t, X(1:2,:))       % Since the full state consists out of [x; lambda]
  58. legend('x_1', 'x_2', 'location', 'north')
  59. subplot(2,1,2)
  60. plot(t, U)
  61. ylabel('u')
  62. xlabel('t')
  63.  
  64. %% Time varying feedback control
  65.  
  66. % Calculate the top layer of expAB as a function of time so the optimal
  67. % solution given the current state can be found:
  68. Gam = @(t) [I O O] * expm([Ah Bh; zeros(2, 6)] * (T-t));
  69. % Here Gam(t) = [M11(t) M12(t) R1(t)] using T-t instead of T, and since:
  70. % lambda(t) = inv(M12(t)) * ((I - R1(t)) * xT - M11(t) * x(t))
  71. % u(t) = -1 / (2 * rho) * B' * lambda(t)
  72. % Combining these gives:
  73. % u(t) = -1 / (2 * rho) * B' * inv(M12(t)) * ((I - R1(t)) * xT - M11(t) * x(t))
  74. % I wanted to avoid creating too many temperary variables, so I used that
  75. % M11(t) = Gam(t) * [I; O; O]
  76. % M12(t) = Gam(t) * [O; I; O]
  77. % R1(t)  = Gam(t) * [O; O; I]
  78. % Since xT is constant, therefore the input is only a function t and x(t):
  79. u = @(t,x) (0.5/rho)*B' * ((Gam(t)*[O;I;O]) \ (Gam(t)*[I;O;O] * x + (Gam(t)*[O;O;I]-I) * xT));
  80.  
  81. % Using this control law then the dynamics of the system can be written as:
  82. dx = @(t,x) A * x + B * u(t,x);
  83.  
  84. % Since the dynamics is linear but timevarying I simulated it using ode45:
  85. [t, x] = ode45(dx, [0 T], x0);
  86.  
  87. % Calculate the applied control input from the results
  88. U = zeros(1, length(t));
  89. for k = 1 : length(t)
  90.     U(k) = u(t(k), x(k,:)');
  91. end
  92.  
  93. % Plotting the results
  94. figure
  95. subplot(2,1,1)
  96. plot(t, x)
  97. legend('x_1', 'x_2', 'location', 'north')
  98. subplot(2,1,2)
  99. plot(t, U)
  100. xlabel('t')
  101. ylabel('u')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement