Advertisement
martaczaska

Untitled

May 24th, 2021
980
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.05 KB | None | 0 0
  1. clc;
  2. clear all;
  3.  
  4. global rho_max;
  5. global u_max;
  6.  
  7. % problem parameters
  8. rho_max = 200.0;
  9. u_max = 60.0;
  10. rho_L = 200.0;
  11. rho_R = 20.0;
  12.  
  13. % we will consider x in range [-2,2], and t in range [0,2]
  14. T = 4/60;
  15. XL = -2;
  16. XR = 2;
  17.  
  18. % discretization
  19. delta_x = 4/400;
  20. delta_t = 0.8*delta_x/u_max;
  21. delt_by_delx = delta_t/delta_x;
  22.  
  23. xs = XL:delta_x:XR;
  24. ts = 0:delta_t:T;
  25.  
  26. Nx = length(xs);
  27. Nt = length(ts);
  28.  
  29. rho = zeros(Nt,Nx);
  30.  
  31. % put your initial conditions here
  32. rho(:,1) = rho_R;
  33.  
  34. % zad 2,3
  35. % for j = 1:Nx
  36. %   if j <= (Nx/2)
  37. %     rho(1,j) = rho_max;
  38. %   else
  39. %     rho(1,j) = rho_R;
  40. %   end
  41. % end
  42.  
  43. %zad 4
  44. for j = 1:Nx
  45.     rho(1,j) = rho_max/2;
  46. end
  47.  
  48. % a few sample trajectories
  49. traj(1,:) = [-1.5 -0.5 0.5];
  50. Ntraj = length(traj);
  51.  
  52. for n = 2:Nt
  53.   % incoming flux at x = XL  
  54.   flux_minus = numflux(rho(n-1,1),rho(n-1,1));  
  55.  
  56.   for i = 1:Nx
  57.     % compute discrete fluxes F(xi-1/2) and F(xi+1/2)
  58.     % assume the disturbance never reaches the domain boundaries
  59.     % Note: F(xi-1/2) is taken from the previous step of the loop
  60.     rho_i = rho(n-1,i);
  61.     if (i == Nx)
  62.       rho_i_plus_1 = rho_i;
  63.     else
  64.       rho_i_plus_1 = rho(n-1,i+1);
  65.     end
  66.     flux_plus = numflux(rho_i,rho_i_plus_1);
  67.  
  68.     % add code to model periodic red light
  69.     if (i == floor(Nx/2))
  70.         if ((n >= 0 && n <= ((1/60)/delta_t)) || (n >= ((2/60)/delta_t) && n <= ((3/60)/delta_t)))
  71.             flux_plus = 0;
  72.         end
  73.     end
  74.  
  75.     % compute the density  
  76.     rho(n,i) = rho(n-1,i)-delt_by_delx*(flux_plus-flux_minus);
  77.     % save flux for the previous x
  78.     flux_minus = flux_plus;
  79.   end
  80.  
  81.   % compute the sample trajectories, fix the code below!
  82.     for k = 1:Ntraj
  83.         traj(n,k) = traj(n-1,k)+u_max*delta_t*(1-interpolate_rho(traj(n-1,k),rho(n-1,:),xs)/rho_max);
  84.     end
  85. end
  86.  
  87. [XX YY] = meshgrid(xs,ts);
  88. pcolor(XX,YY,rho);
  89. shading('interp');
  90. colorbar;
  91. xlabel('Odległość względem sygnalizatora [km]');
  92. ylabel('Czas [min/100]');
  93.  
  94. hold on; plot(traj(:,1),ts','w-');
  95. hold on; plot(traj(:,2),ts','w-');
  96. hold on; plot(traj(:,3),ts','w-');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement