Guest User

Untitled

a guest
May 28th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. using DifferentialEquations
  2. using Interpolations
  3. srand(180394)
  4.  
  5. #Parameters #
  6. delta_t = 0.01;
  7. T=6.0;
  8. ft = 0:delta_t:T;
  9. n_t = length(ft);
  10. Kelvin = 273.15;
  11. y = 53+Kelvin;
  12. targetDirection = 50+Kelvin;
  13. x_bar0 = 55+Kelvin;
  14. B = 0.003147210784442288
  15. A0 = -0.0038056797994587615
  16. A1 = -0.2917177677115467
  17. c0 = 1.1346634322086298
  18. c1 = 84.09653156407676
  19. L=[[-0.5 0.5];[7 -7]];
  20. R= 0.05;
  21. qx0=8000;
  22. lambda = 3000;
  23.  
  24. dzeta = zeros(n_t,2);
  25. dzeta[1,1] = 0.5;
  26. dzeta[1,2] = 0.5;
  27. for i in 2:n_t
  28. dzeta[i,:] = dzeta[1,:]'*expm((i-1)*delta_t*L)
  29. end
  30.  
  31. ## Riccati gain ##
  32. function riccati_gain!(dPi,Pi,p,t)
  33. A0,A1,B,R,L,qz,qx0,ft = p
  34.  
  35. # Parameters dependent of t #
  36. qz = interpolate((collect(ft),),qz,Gridded(Linear()))[t]
  37.  
  38. dPi[1] = -Pi[1]*A0-transpose(A0)*Pi[1] +Pi[1]*B*inv(R)*transpose(B)*Pi[1]-L[1,1]*Pi[1]-L[1,2]*Pi[2]-(qz+qx0)
  39. dPi[2] = -Pi[2]*A1-transpose(A1)*Pi[2] +Pi[2]*B*inv(R)*transpose(B)*Pi[2]-L[2,1]*Pi[1]-L[2,2]*Pi[2]-(qz+qx0)
  40. end
  41.  
  42. ## Offset Function ##
  43. function offset_func!(ds,s,p,t)
  44. A0,A1,B,R,qz,ft,z,qx0,x_bar0,L,c0,c1,sol_Pi = p
  45.  
  46. # Parameters dependent of t #
  47. Pi_1 = sol_Pi(t)[1]
  48. Pi_2 = sol_Pi(t)[2]
  49. qz = interpolate((collect(ft),),qz,Gridded(Linear()))[t]
  50.  
  51. ds[1] = -transpose(A0-B*inv(R)*transpose(B)*Pi_1)*s[1]+qz*z+qx0*x_bar0-Pi_1*c0-L[1,1]*s[1]-L[1,2]*s[2]
  52. ds[2] = -transpose(A1-B*inv(R)*transpose(B)*Pi_2)*s[2]+qz*z+qx0*x_bar0-Pi_2*c1-L[2,1]*s[1]-L[2,2]*s[2]
  53. end
  54.  
  55. ## Xbar Function ##
  56. function x_barj_func!(dx,x,p,t)
  57. A0,A1,B,R,sol_Pi,sol_offset,L,c0,c1,dzeta,ft = p
  58. T= ft[end]
  59.  
  60. #Parameters dependent of t #
  61. if t <= T
  62. Pi_1 = sol_Pi(t)[1]
  63. Pi_2 = sol_Pi(t)[2]
  64.  
  65. Offset_1 = sol_offset(t)[1]
  66. Offset_2 = sol_offset(t)[2]
  67.  
  68. dzeta_1 = interpolate((collect(ft),),dzeta[:,1],Gridded(Linear()))[t]
  69. dzeta_2 = interpolate((collect(ft),),dzeta[:,2],Gridded(Linear()))[t]
  70. else
  71. Pi_1 = sol_Pi(T)[1]
  72. Pi_2 = sol_Pi(T)[2]
  73.  
  74. Offset_1 = sol_offset(T)[1]
  75. Offset_2 = sol_offset(T)[2]
  76.  
  77. dzeta_1 = dzeta[end,1]
  78. dzeta_2 = dzeta[end,2]
  79. end
  80.  
  81. dx[1] = (A0-B*inv(R)*transpose(B)*Pi_1)*x[1]+L[1,1]*x[1] + L[2,1]*x[2] + dzeta_1 * c0 - dzeta_1*B*inv(R)*transpose(B) * Offset_1
  82. dx[2] = (A1-B*inv(R)*transpose(B)*Pi_2)*x[2]+L[1,2]*x[1] + L[2,2]*x[2] + dzeta_2 * c1 - dzeta_2*B*inv(R)*transpose(B) * Offset_2
  83.  
  84. end
  85.  
  86.  
  87. #Initialisation fixed point #
  88. epsilon = 0.07
  89. x_bar = (55+Kelvin)*ones(n_t,)
  90. x_bar_old = zeros(n_t,)
  91. iteration = 0
  92. test = false
  93.  
  94. # Fixed Point algorithm #
  95. while (iteration < 200) && (!test)
  96.  
  97. # qz(t)= abs(lambda*int_{0}^{t}(x(z)dz) #
  98. qz = zeros(n_t,);
  99. for i in 2:n_t
  100. qz[i] = qz[i-1] + (x_bar[i,1]-y)*delta_t
  101. end
  102. qz = abs.(lambda*qz)
  103.  
  104. # Pi^j_t : final condition #
  105. Riccati_gain_T_1 = (qz[n_t]+qx0)
  106. Riccati_gain_T_2 = (qz[n_t]+qx0)
  107.  
  108. prob_riccati = ODEProblem(riccati_gain!,[Riccati_gain_T_1;Riccati_gain_T_2],(T,0.0),[A0,A1,B,R,L,qz,qx0,ft])
  109. sol_Pi = solve(prob_riccati)
  110.  
  111. # s^j_t : final condition #
  112.  
  113. Offset_function_T_1 = -(qz[n_t]*targetDirection+qx0*x_bar[1])
  114. Offset_function_T_2 = -(qz[n_t]*targetDirection+qx0*x_bar[1])
  115.  
  116. # sol_Pi used as parameters #
  117. prob_offset = ODEProblem(offset_func!,[Offset_function_T_1;Offset_function_T_2],(T,0.0),[A0,A1,B,R,qz,ft,targetDirection,qx0,x_bar0,L,c0,c1,sol_Pi])
  118. sol_offset = solve(prob_offset)
  119.  
  120. # x_barj_t : initial condition #
  121. x_barj_1_0 = x_bar0*dzeta[1,1]
  122. x_barj_2_0 = x_bar0*dzeta[1,2]
  123.  
  124. #sol_pi and sol_offset as parameters#
  125. prob_xbarj = ODEProblem(x_barj_func!,[x_barj_1_0; x_barj_2_0],(0.0,T),[A0,A1,B,R,sol_Pi,sol_offset,L,c0,c1,dzeta,ft])
  126. sol_xbarj = solve(prob_xbarj)
  127.  
  128. # x_bar_t #
  129. x_bar_old = x_bar
  130. x_bar = [sol_xbarj(temp)[1]+sol_xbarj(temp)[2] for temp in ft]
  131.  
  132. Δ = abs.(x_bar-x_bar_old)
  133. iteration = iteration + 1
  134. test = sum(Δ) < epsilon
  135. end
Add Comment
Please, Sign In to add comment