Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.12 KB | None | 0 0
  1. function method_of_lines
  2.  
  3. %% Output time
  4. final_time = 5;
  5. %% Deltat
  6. deltat = 0.01;
  7. CFL = 0.75;
  8. %% Number of nodes in space
  9. n = 5000;
  10. %%Compute deltax
  11. x_min = 0.0;
  12. x_max = 1.0;
  13. deltax = (x_max -x_min)/n;
  14. [x,rho] = set_initial_condition(x_min ,deltax ,n);
  15. current_time = 0.0;
  16. drhodt = zeros(n,1);
  17. y = zeros(n,1)
  18.  
  19.     while current_time < final_time
  20.         deltat = find_deltat(rho , deltax , CFL);
  21.         if (current_time+deltat > final_time)
  22.             deltat = final_time - current_time;
  23.         end
  24.  
  25.         current_time = current_time + deltat;
  26.         y(1) = 1 - 1*exp(-8*exp(-3*rho(1)));
  27.        
  28.         for i = 2:n-1
  29.             y(i) = 1 - 1*exp(-8*exp(-3*rho(i)));
  30.         end
  31.        
  32.         y(n) = 1 - 1*exp(-8*exp(-3*rho(n)));
  33.  
  34.         drhodt(1) = -1/(deltax) * (y(2) - y(1));
  35.         for i = 2:n-1
  36.             drhodt(i) = -1/(deltax) * (y(i+1) - y(i));
  37.         end
  38.         drhodt(n) = -1/(deltax) * (y(1) - y(n));
  39.  
  40.         rho = rho + deltat*drhodt;
  41.  
  42.         figure (1);
  43.         plot(x,rho ,'-o', 'LineWidth', 5);
  44.         time = strcat(num2str(current_time), 's');
  45.         tex = strcat('t = ', time);
  46.         text(0.6,2, tex);
  47.         legend('Finite -Difference')
  48.         title('Time marching a P.D.E.')
  49.         xlabel('x')
  50.         ylim ([0 ,2.5]);
  51.         ylabel('Traffic Density')
  52.         grid on
  53.         drawnow
  54.     end
  55.         figure (2);
  56.         plot(x,y,'-o', 'LineWidth', 5);
  57.         legend('Finite -Difference')
  58.         title('Time marching a P.D.E.')
  59.         xlabel('x')
  60.         ylim ([0 ,1.5]);
  61.         ylabel('Traffic Velocity')
  62.         grid on
  63.         drawnow
  64.  
  65. end
  66. function [x,rho] = set_initial_condition(x_min ,deltax ,n)
  67. x = zeros(n,1);
  68. rho = zeros(n,1);
  69. for i = 1:n
  70.     x(i) = x_min + (i)*deltax;
  71.     rho(i) = 1;
  72. end
  73. for i = 1:n
  74.     if 0.25 <= x(i) && x(i) <= 0.75
  75.         rho(i) = -16*x(i)^2 + 16*x(i) - 2.0;
  76.     end
  77. end
  78. end
  79. function deltat = find_deltat(rho ,deltax ,CFL)
  80. n = size(rho ,1);
  81. deltat = 1E100;
  82. %find minimum dt
  83. for i = 1:n
  84.     max_speed = abs(-24*exp(-3*rho(i) -8*exp(-3*rho(i))));
  85.     deltat = min(CFL*deltax/max_speed , deltat);
  86. end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement