Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %
- % This script uses Godunov scheme to
- % simulate the traffic flow problem
- %
- clear all
- global rho_max;
- global u_max;
- %
- % problem parameters
- %
- rho_max=200.0
- u_max=60.0
- rho_L=20.0
- %
- % we will consider x in range [-2,2], and t in range [0,2]
- %
- T=4/60;
- XL=-2;
- XR=2;
- %
- % discretization
- %
- delta_x = 4/400
- delta_t = 0.8*delta_x/u_max
- delt_by_delx = delta_t/delta_x;
- xs = XL:delta_x:XR;
- ts = 0:delta_t:T;
- Nx = length(xs);
- Nt = length(ts);
- rho = zeros(Nt,Nx);
- %
- % put your initial conditions here
- %
- i = 1;
- Nx0 = -XL/delta_x;
- rho(1,:) = 100;
- %rho(i,:) = [rho_max*ones(1, Nx0), rho_L*ones(1, Nx-Nx0)];
- %
- % a few sample trajectories
- %
- traj(1,:) = [-2.0 -1.0 1.0];
- Ntraj = length(traj);
- for n=2:Nt
- % incoming flux at x = XL
- flux_minus = numflux(rho(n-1,1), rho(n-1,1));
- for i=1:Nx
- % compute discrete fluxes F(xi-1/2) and F(xi+1/2)
- % assume the disturbance never reaches the domain boundaries
- % Note: F(xi-1/2) is taken from the previous step of the loop
- rho_i = rho(n-1, i);
- if (i==Nx)
- rho_i_plus_1 = rho_i;
- else
- rho_i_plus_1 = rho(n-1,i+1);
- end
- flux_plus = numflux(rho_i, rho_i_plus_1);
- %
- % add code to model periodic red light
- %
- %if ((mod(ts(n), 2/60) < 1/60) && i == Nx0)
- % if i == 200
- % if ts(n) < 1/60 || (2/60 <= ts(n) && ts(n) < 3/60)
- % flux_plus = 0;
- % end
- % end
- %
- % compute the density
- %
- rho(n,i)=rho(n-1,i)+delt_by_delx*(flux_plus-flux_minus);
- % save flux for the previous x
- flux_minus = flux_plus;
- end
- %
- % compute the sample trajectories, fix the code below!
- %
- for j=1:Ntraj
- traj(n,j) = traj(n-1,j) + delta_t*u_max*(1-interpolate_rho(traj(n-1,j), rho(n-1,:), xs) / rho_max);
- end
- end
- [XX YY] = meshgrid(xs,ts);
- pcolor(XX,YY,rho);
- shading('interp');
- hold on;
- plot(traj(:,1), ts', 'w-')
- plot(traj(:,2), ts', 'w-')
- plot(traj(:,3), ts', 'w-')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement