4

Click here to load reader

ME_345_Matlab

  • Upload
    mrr348

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ME_345_Matlab

Matlab Programming - Traffic Flow Problem

Mario Roque

February 21, 2014

I pledge on my honor that I have abided by the Stevens Honor Code

x

Traffic Flow Problem

Previously I had spoken about trying to use both Java and Matlab to further my abilities in bothprogramming languages, but I quickly realized that I had bitten off more than I could chew. I alsorealized that there really is no real reason to use Matlab for something that it wasn’t made to do.Matlab being a child of Fortran was specifically made to solve physics and mathematical problems.It works best when working on these type of problems, but it is weaker for other programs. Javaand Matlab are too different to even compare and even harder to use both simultaneously. Java is amuch more basic programming language which makes it much more difficult to solve math problemsbecause there aren’t really too many libraries or packages to use. For this reason I decided to focussolely on Matlab and to continue to solve computational physics problems. I chose problems givenby the old professor who used to teach computational physics at NYU. I assumed I would have aneasier time solving certain problems than the students taking the course because they had to useC. I was wrong because the problem itself is rather complicated and then to emulate the problemin Matlab became rather difficult, but with some painstaking work I was able to come out withsolutions that truly show why Matlab is so useful.

The first problem is called the traffic flow problem which wikipedia states, ”is the study of interac-tions between vehicles, drivers and infrastructures with the aim of understanding and developing anoptimal road network with efficient movement of traffic and minimal traffic congestion problems”.Traffic flow problem obeys the continuity equation for density of vehicles ρ(x, t),

∂ρ

∂t+∂(ρv)

∂x= 0 (1)

where v(x,t) = vmax(1- ρρmax

) is the velocity field, with vmax the speed limit and ρmax the densitycorresponding to vehicles filling space bumper to bumper. The problem was to first come up withcertain values for the max density and velocity by solving the partial differential equation using theForward Time Central-Space and the Lax method. The second part involves actually plotting in 3Dthe density as a function of space and time, comparing the solutions given by the different methods.

1

Page 2: ME_345_Matlab

ME-345-Modeling and Simulation

To solve the problem one must first understand what the actual equation is saying. The dif-ferential equation is basically showing that velocity is dependent on the density of cars. This makessense because the velocity of vehicles is dependent on how many cars there are in traffic. If trafficwere bumper to bumper there will be no velocity at all. The higher the density of the lower thespeed is restricted too. The wave speed c(ρ) and the vehicle speed are as followed:

v(ρ) = vmax(1 − ρ

ρmax) (2)

c(ρ) = vmax(1 − 2ρ

ρmax) (3)

From this relationship you can find the relationship between c and v:

∂ρ

∂t+ c(ρ)

∂(ρ)

∂x= 0 (4)

Now if we take the flow of traffic to be q(ρ), we can describe the flux of cars or the number ofvehicles per unit time:

q(ρ) = ρv = ρvmax(1 − ρ

ρmax) = vmax(ρ− ρ2

ρmax) (5)

If we take the derivative of the flow with respect to density gives you an equation that showsthe relationship of traffic flow to the density wave speed or the derivative of flow with respect tothe density gives the density wave speed c(ρ):

∂q

∂ρ= vmax(1 − 2ρ

ρmax) = c(ρ) (6)

Density wave speed describes the change in the flow of vehicles per change in density. If thedensity is equal to half the max density, then the wave speed is zero. We can see the movementof cars from this equation and Matlab will display a movie of the change in density over time.We should be able to see the wave speed moving in the -x-direction when ρ > ρmax

2 and in the+x-direction when ρ < ρmax

2 . When t is very large though there will be in theory no more changein the vehicle density with respect to time.

To create an accurate model through Matlab then we have to set boundaries and certain pa-rameters to create an accurate representation of vehicles at a stoplight. We begin with t = 0 andwe create a periodic boundary condition like a race track and a square wave initial condition shownbelow:

ρ(x, 0) = ρmax,L

4< x < 0 (7)

We also have to set parameters for all variables as shown below:

• Maximum Density = ρmax = 1.0 car per meter

• Speed Limit = vmax = 25 m/s

• Length of Track = L = 1600 m

• Number of grid points = N = 800

2 Matlab Programming - Traffic Flow Problem

Page 3: ME_345_Matlab

ME-345-Modeling and Simulation

• Resolution = dx = L/N = 2m

• Time Step = dt = 0.08 seconds

• Time of evolution = N = time for a vehicle to go around the track once

• Time for last car to move = N/4

For FTCS and Lax method the equations necessary for each method are shown below:FTCS:

ρn+1j = ρnj +

µ

2(ρnj+1 − ρnj−1) (8)

Lax:

ρn+1j =

1

2(ρnj+1 − ρnj−1 +

µ

2(ρnj+1 − ρnj−1) (9)

Where

µ =v(x, t)∂t

∂x(10)

The courant number µ is the ratio of spatial propagation in time to the resolution ∂x.

Figure 1: FCTS

3 Matlab Programming - Traffic Flow Problem

Page 4: ME_345_Matlab

ME-345-Modeling and Simulation

Figure 2: Lax

From the graphs of both methods it is clear that the better method for the traffic flow problemis the Lax Method. The FCTS method runs into problem when the las car in the line of trafficmoves. The solution produces extreme oscillations and breaks down. The method could not handlethe sudden change of the square wave. The Lax Method is the more complex method and as suchuses more information to produce a stable solution and simulation.

Enhancing My Ability to be a LifeLong Learner

The purpose of the my code was to simulate the traffic problem which is an issue that is dealt withmy mathematicians and engineers alike. As far as the Matlab code is concerned it is pretty selfexplanatory and I wrote as many comments as possible to assure that anyone could understand thethought process involved in creating the simulation. I think this problem especially provides insightin the strengths of Matlab. The integrated libraries specify in solving mathematical problems andthe 3D plotting is simplest on Matlab further proving the point that Matlab is best used to solveproblems rather then to program for other purposes. This is true because Matlab isn’t as flexibleas other programs because it is not as basic. At the same time as having endless libraries andfunctions to work off of there is no real flexibility in Matlab and the simplest ideas like memoryallocation is impossible which could create programs that could take hours for it to run. I had nevercreated a 3D plot on Matlab which is why I chose this problem to solve. As far as further venturesinto Matlab and getting better in Matlab depends purely on whether there are more problems tosolve. I would rather program for fun using java or python purely because they are more flexiblelanguages. As far as enhancing my ability to be a lifelong learner I am always pushing myself todoing as much as possible in a school, from involving my self in multiple hack-a-thons ( HackNYis coming soon), but to also play intramural sports and taking as many classes possible to learn asmuch as possible. Being in school is the best time to learn as much as you possibly can becausethen this becomes a routine, an healthy habit which is necessary to continue learning when you areno longer obliged to learn.

4 Matlab Programming - Traffic Flow Problem