Traffic Flow

We begin by defining a velocity field (traffic velocity at time t and position x(t)):

v[x_,t_]=x/100+t;

We use this velocity field to define an ODE that couples position and velocity (by describing the change of position with respect to time) along with an appropriate initial condition according to which our car starts at time t=0 at the position x(0)=0:

ode = x'[t] ==  v[x[t],t];
ic = x[0]==0;

We can easily produce a simple slopefield (or stream plot in Mathematica) that displays the velocity field, i.e., the path along which cars travel. We assign the plot to the variable splot so that we can use it later.

splot=StreamPlot[{1,v[x,t]},{t,0,10},{x,0,10}]

If we want to know the solution of our specific initial value problem, then we can solve the ODE as follows (and remember the solution in the variable oursolution for later use)

oursolution=DSolve[{ode,ic},x[t],t]

and then combine a plot of this solution with the stream plot (saved in splot above) to see the path of a car that starts at time t=0 at the position x(0)=0:

Show[splot,Plot[Evaluate[x[t]/.oursolution],{t,0,10},PlotRange->{0,10},PlotStyle->Red]]

Since Mathematica makes it easy to animate our solution, let’s do it:

Manipulate[Show[splot,Plot[Evaluate[x[t]/.oursolution],{t,0,T},PlotRange->{0,10},PlotStyle->Red]],{{T,0.001},0.001,10}]

And now, let's add a second car that starts at the same position x=0, but two units of time later, i.e., at time t = 2:

Manipulate[Show[splot,Plot[Evaluate[x[t]/.oursolution],{t,0,T},PlotRange->{0,10},PlotStyle->Red],Plot[Evaluate[x[t]/.DSolve[{ode,x[2]==0},x[t],t]],{t,0,T},PlotRange->{0,10},PlotStyle->Green]],{{T,0.001},0.001,10}]

Alternatively, we may prefer to use a numerical ODE solver over an analytic one. Then we could use NDSolve instead of DSolve and write

Manipulate[Show[splot,Plot[Evaluate[x[t]/.NDSolve[{ode,ic},x,{t,0,T}]],{t,0,T},PlotRange->{0,10},PlotStyle->Red]],{{T,0.001},0.001,10}]

Here is a slope field plot of a real traffic situation that includes a shock wave that one might have encountered during rush hour on I-680 in Walnut Creek, CA, in April 1996. The yellow lines are individual vehicle trajectories and the underlying color ranges from black=0 mph to blue=40 mph (taken from http://www2.ece.ohio-state.edu/~coifman/shock/).