Advertisement
Guest User

lsim() trims time vector in an odd fashion

a guest
Nov 21st, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 3.40 KB | None | 0 0
  1. clc
  2. close all
  3. clear all
  4.  
  5. %% System parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. t_f = 5*2;
  7. N = 50*2;
  8. h = t_f/N;
  9. t_vec = zeros(N + 1, 1);
  10. for i = 2:N + 1
  11.     t_vec(i) = t_vec(i - 1) + h;
  12. end
  13. alpha = 0.2;
  14. x_0 = [-0.8; 1];
  15. continousSystem = ss([0, 1; 2, -3], [0;  1], [], []);
  16.  
  17. %% Discretization of the continouus problem %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  18. % Using ZOH
  19. discreteSystemZOH = c2d(continousSystem, h, 'zoh');
  20. A_D = discreteSystemZOH.A;
  21. B_D = discreteSystemZOH.B;
  22.  
  23. % Using Forward Rectangular Method
  24. A_D_prime = diag(ones(2, 1)) + h*continousSystem.A;
  25. B_D_prime = h*continousSystem.B;
  26.  
  27. %% Construct and solve the optimal control problem
  28. A_D_list = {A_D, A_D_prime};
  29. B_D_list = {B_D, B_D_prime};
  30.  
  31. % H matrix
  32. dim_H = [3*(N + 1), 3*(N + 1)];
  33. H = zeros(dim_H);
  34. H_hat = [1, 0,     0;
  35.          0, 1,     0;
  36.          0, 0, alpha];
  37. for j = 1:3:dim_H(1)
  38.     H(j:j + 3 - 1, j:j + 3 - 1) = H_hat;
  39. end
  40.  
  41. % y_0 vector (initial guess)
  42. dim_y_0 = [3*(N + 1), 1];
  43. y_0 = zeros(dim_y_0);
  44. y_0(1:2) = x_0;
  45.  
  46. y_vals = zeros(dim_y_0, 2);
  47. f_vals = zeros(2, 1);
  48. f_vals = zeros(2, 1);
  49. for i = 1:2
  50.     clear A_eq B_eq
  51.  
  52.     % A_eq and B_eq matrices
  53.     dim_A_eq = [2*(N + 1), 3*(N + 1)];
  54.     dim_y = [3*(N + 1), 1];
  55.     dim_B_eq = [2*(N + 1), 1];
  56.     A_eq = zeros(dim_A_eq);
  57.     B_eq = zeros(dim_B_eq);
  58.     A_eq(1:2, 1:2) = diag(ones(2, 1));
  59.     indent_counter = 0;
  60.     for j = 3:2:dim_A_eq(1)
  61.         A_eq(j:j + 2 - 1, indent_counter*3 + 1:indent_counter*3 + 1 + 2 - 1) = -A_D_list{i};
  62.         A_eq(j:j + 2 - 1, indent_counter*3 + 1 + 2) = -B_D_list{i};
  63.         A_eq(j:j + 2 - 1, indent_counter*3 + 1 + 2 + 1:indent_counter*3 + 2 + 1 + 1 + 2 -1) = diag(ones(2, 1));
  64.         ++indent_counter;
  65.     end
  66.     B_eq(1:2) = x_0;
  67.  
  68.     % Solve the optimization problem
  69.     [y, fval, info, lambda] = qp(y_0, H, [], A_eq, B_eq);
  70.     y_val(:, i) = y;
  71.     f_vals(i) = fval;
  72. end
  73.  
  74. %% Create plots for the optimal inputs %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  75. f1 = figure;
  76. stairs(t_vec, y_val(3:3:end, 1), 'b-')
  77. hold on
  78. stairs(t_vec, y_val(3:3:end, 2), 'r-')
  79. legend('$u(t)$', '$u(t)^\prime$');
  80. xlabel('t [s]');
  81. grid on
  82.  
  83. %cleanfigure();
  84. %matlab2tikz('plot_optinputs.tex',
  85. %            'figurehandle', f1,
  86. %            'width', '8cm',
  87. %            'height', '4.75cm',
  88. %            'parseStrings', false,
  89. %            'encoding', 'UTF-8',
  90. %            'showInfo', false);
  91.  
  92. %'\pgfplotsset{grid style={dotted, dash pattern=on 0pt off 3\pgflinewidth,line width=0.3pt,lightgray}}'
  93.  
  94. %% Simulate the exact discrete time system with the optimal input %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  95. [y_out, t_out, x_out] = lsim(discreteSystemZOH, y_val(3:3:end, 1), t_vec(:,1), x_0);
  96. f2 = figure;
  97. subplot(1, 2, 1);
  98. stairs(t_out, y_out(:, 1), 'b-');
  99. hold on
  100. stairs(t_vec, y_val(3:3:end, 1), 'r-');
  101. grid on
  102. legend('$x_1(t)$', '$u(t)$');
  103. xlabel('t [s]');
  104. subplot(1, 2, 2);
  105. stairs(t_out, y_out(:, 2), 'b-');
  106. hold on
  107. stairs(t_vec, y_val(3:3:end, 1), 'r-');
  108. grid on
  109. legend('$x_2(t)$', '$u(t)$');
  110. xlabel('t [s]');
  111.  
  112. %cleanfigure();
  113. %matlab2tikz('plot_optstates.tex',
  114. %            'figurehandle', f2,
  115. %            'width', '14cm',
  116. %            'height', '4.5cm',
  117. %            'parseStrings', false,
  118. %            'encoding', 'UTF-8',
  119. %            'showInfo', false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement