Lab05-solutions.pdf
December 15, 2016 | Author: Shweta Sridhar | Category: N/A
Short Description
solutions...
Description
MH2401
Algorithms & Computing III
AY 2013/14 S1
Lab 5: Solutions
Tasks. Topic 1: Road traffic system. We consider the model of a traffic system in a long road. We assume that the road can have a very large number of cars in queue. Let the number of cars on the road at any time t be denoted by the variable x ≡ x(t). Thus, x(t) denotes the queue length at any time t. Suppose that cars start arriving at the road at a rate of λ cars/min, and they are able to exit the road at the rate of µ cars/min. Then, the rate of change in the number of cars that are present on the road is given by the difference between the rate at which the cars arrive and the rate at which they depart. Hence, we get 1 dx (1) = λ − µ, x ≥ 0. dt We consider a model where the rate of arrival λ is constant. This might be the case at certain time periods in the day. The rate of departure of the cars depends on the number of cars already present on the road and is given by the expression x (2) µ(x) = c , x+1 where the constant c is the maximum rate of departure that is possible. We can numerically determine how the number of cars in the road evolve over time by considering an approximation of the differential equation by the difference equation x(t + ∆t) − x(t) = λ − µ(x(t)). (3) (t + ∆t) − t We will study the solution of the ordinary differential equation (1) by approximating it with the difference equation (3). In the problems below fix c = 25. 1.1. Create a plot of the queue length x against time t, for the following initial values of the queue length and the arrival rates λ. (i) x(0) = 0, λ = 20, (iii) x(0) = 35, λ = 20, (ii) x(0) = 0, λ = 24, (iv) x(0) = 35, λ = 24. For each of the above initial values, what can you infer about the behavior of the queue length in the long run, i.e., for large t? [Hint: You may want to use a small step size for the time, say ∆t = 0.001, to study the dynamics.] Solution. 1Although
the number of cars is a discrete quantity, assuming it to be a continuous function of time simplifies the modeling significantly.
1
2
1 2 3
f u n c t i o n [ times , x ] = queue_length ( x0 , tmin , tmax , lambda ) c = 25; delta_t = 0.001;
4
times = tmin : delta_t : tmax ; x = z e r o s (1 , length ( times )); x (1) = x0 ;
5 6 7 8
f o r k = 2: length ( times ) mu = c * x (k -1)/( x (k -1)+1); x ( k ) = x (k -1) + ( lambda - mu )* delta_t ; end
9 10 11 12 13
end
Example solution for (iii): >> [ times , x ] = queue_length (35 , 0 , 20 , 20); % 20 min of simulation >> p l o t ( times , x ) 35
30
25
20
15
10
5
0 0
5
10
15
20
The queue length tends to a constant value (steady state value) in the long run. 1.2. We say that the queue length x is at a steady state if it does not change over time, i.e., x(t) = xs for some constant xs . Mathematically derive the expression for the steady state queue length as a function of λ and c. [Hint: Since the steady state queue length is a constant, what is its derivative with respect to time?] Solution. Setting dx = 0 and solving for x gives us xs = dt
λ . c−λ
3
1.3. Create a plot of xs against λ/c, for 0 ≤ λ/c ≤ 1. For what values of arrival rate λ does the steady state queue length remain less than 24. Solution. >> c = 25; >> lambda = 0:0.01: c -0.01; % avoid lambda = c >> p l o t ( lambda /c , lambda ./( c - lambda )) 250
200
150
100
50
0 0
0.2
0.4
0.6
0.8
1
We can determine either from the plot, or by solving the expression xs ≤ 24 that λ must satisfy λ ≤ 24. 1.4. In this part we will study the behavior of how the queue length changes as we change the arrival rate. The specific scenario we are trying to simulate is the case of a rush hour traffic. This also shows how much time it takes for the traffic to regain a normal steady state after the rush hour is over. Suppose that we start the numerical simulation at time 7 am. Thus, t = 0 corresponds to 7 am. Assume that x(0) = 3, i.e., there are three cars on the road at t = 0. Now, consider the following scenario. (i) Till time t < 4, the arrival rate is 12 cars/min. (ii) At time t = 4, which corresponds to 7:04 am, the arrival rate changes to 20 cars/min, and remains so till time t < 10. (iii) At time t = 10, the arrival rate changes to 40 cars/min, and remains so till time t < 60. (iv) At t = 60, the arrival rate changes back to 20 cars/min. What is the maximum queue length before it starts decreasing? How much time does it take for the queue length to reach steady state, from the time t = 60? (You can round your answers to the nearest integer.) Solution. 1
[ times_1 , x_1 ] = queue_length (3 , 0 , 4 , 12);
4 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[ times_2 , x_2 ] = queue_length ( x_1 (end) , 4 , 10 , 20); [ times_3 , x_3 ] = queue_length ( x_2 (end) , 10 , 60 , 40); [ times_4 , x_4 ] = queue_length ( x_3 (end) , 60 , 260 , 20); X = [ x_1 x_2 (2:end) x_3 (2:end) x_4 (2:end)]; % don't repeat time index T = [ times_1 times_2 (2:end) times_3 (2:end) times_4 (2:end)]; max_indx = f i n d ( X == max( X ) , 1 , ' last ' ); % x decreases after this disp ([ ' The max value is = ' num2str(round( X ( max_indx )))]); % 762 % Find when x(t) reaches steady state. Look for when it stops changing % by a small amount. indx = f i n d ( X ( max_indx :end -1) - X ( max_indx +1: end) < eps , 1 , ' first ' ); disp ([ ' The time when x ( t ) reaches steady state is = ' ... num2str(round( T ( max_indx + indx )))]); % 247 min disp ([ ' It takes about ' ... num2str(round( T ( max_indx + indx ) - T (60000))) ... ' minutes from 60 min to reach steady state . ' ]); % 187 min p l o t (T , X ) 800 700 600 500 400 300 200 100 0 0
50
100
150
200
250
300
5
Topic 2: Population dynamics. In this question, we investigate a coupled system of differential equations that models the population dynamics of two competing species. Suppose that x ≡ x(t) denotes the population (in hundreds) of a prey species, say deer, over time. Also, suppose that y ≡ y(t) denotes the population (in hundreds) of a predator species, say lion, over time. We will investigate how the two species evolve over time where the change is determine by the following pair of coupled differential equations, dx x y = 2x 1 − − , dt 5 5 (4) dy x y = 3y 1 − − . dt 4 6 We can view x and y as parametric functions of t and numerically solve the system by replacing dx with x(t+∆t)−x(t) , and dy with y(t+∆t)−y(t) . This gives the corresponding dt ∆t dt ∆t system of difference equations !
x(t + ∆t) − x(t) x(t) y(t) = 2x(t) 1 − − , ∆t 5 5
(5)
!
y(t + ∆t) − y(t) x(t) y(t) = 3y(t) 1 − − . ∆t 4 6 In this topic, we will study the population dynamics by using an xy-plot to study the change in population of both the competing species simultaneously. 2.1. For each of the following initial conditions, use the above system of difference equations (5) to numerically solve the coupled system of differential equations over 0 ≤ t ≤ 4, and plot all solutions in the same xy-plane. (i) x(0) = 1, y(0) = 3, (iii) x(0) = 2, y(0) = 5, (ii) x(0) = 2, y(0) = 1, (iv) x(0) = 3, y(0) = 3. [Hint: You may want to use a small step size for the time, say ∆t = 0.001, to study the dynamics.] Solution. 1 2 3 4 5 6 7 8 9 10 11 12 13
f u n c t i o n evolve ( x0 , y0 , tbar ) delta_t = 0.001; times = 0: delta_t : tbar ; x = z e r o s ( s i z e ( times )); y = z e r o s ( s i z e ( times )); x (1) = x0 ; y (1) = y0 ; f o r k = 1: length ( times ) -1 x ( k +1) = x ( k ) + 2* x ( k )*(1 - x ( k )/5 - y ( k )/5) * delta_t ; y ( k +1) = y ( k ) + 3* y ( k )*(1 - x ( k )/4 - y ( k )/6) * delta_t ; end p l o t (x , y , 'r ') end >> >> >> >>
f i g u r e ; hold evolve (1 , 3 , evolve (2 , 1 , evolve (2 , 5 ,
on ; 4); p l o t (1 , 3 , ' ro ') 4); p l o t (2 , 1 , ' ro ') 4); p l o t (2 , 5 , ' ro ')
6
>> evolve (3 , 3 , 4); p l o t (3 , 3 , ' ro ') 5 4.5 4 3.5 3 2.5 2 1.5 1 0.5 0.5
1
1.5
2
2.5
3
3.5
4
4.5
2.2. A pair (xs , ys ) is called a stationary point of the coupled system if both derivatives dx and dy are zero at (xs , ys ). dt dt Mathematically determine all the stationary points (xs , ys ) of the coupled system. [Hints: 1) You will need to solve a 2-by-2 system of linear equations. 2) There are exactly four stationary points.] Solution. Both the derivatives in (4) are zero at (xs , ys ) if and only if xs y s xs y s (xs = 0 or + = 1) and (ys = 0 or + = 1). 5 5 4 6 We get four cases Case 1: xs = 0 and ys = 0: In this case, (xs , ys ) = (0, 0). Case 2: xs = 0 and x4s + y6s = 1: This gives (xs , ys ) = (0, 6). Case 3: x5s + y5s = 1 and ys = 0: This gives (xs , ys ) = (5, 0). Case 4: x4s + y6s = 1 and x5s + y5s = 1: This gives (xs , ys ) = (2, 3). 2.3. For each of the stationary points (xs , ys ) of the coupled system, use MATLAB’s quiver function to produce a velocity plot of the above differential equation over a unit square region in the xy-plane centered at the stationary point (xs , ys ), i.e., over the region xs − 0.5 ≤ x ≤ xs + 0.5, and ys − 0.5 ≤ y ≤ ys + 0.5. For each of the velocity plots, how would you expect the solution of the coupled system to behave around the stationary point? In other words, how does the population of the two species (deer and lion) change over time? Support your answer with a few plots of the solutions originating from suitably chosen points in the same plot. Solution.
7
1 2 3 4 5 6 7 8 9
f u n c t i o n species ( xs , ys ) dx = @ (x , y ) 2* x .*(1 - x /5 - y /5); dy = @ (x , y ) 3* y .*(1 - x /4 - y /6); % Create a grid of uniformly spaced x and y points [x , y ] = meshgrid ( xs -0.5:0.05: xs +0.5 , ys -0.5:0.05: ys +0.5); quiver (x , y , dx (x , y ) , dy (x , y ) , 'o ' ); % the velocity plot a x i s ([ xs -0.6 xs +0.6 ys -0.6 ys +0.6]); a x i s equal end
Case 1: If the population of either species is bigger than zero, then it increases in roughly the same proportion and diverges away from the point (0, 0). >> f i g u r e ; hold on ; species (0 , 0); >> f o r x = 0:0.05:0.1 , evolve (x , 0.1 , 0.5); end >> f o r y = 0:0.05:0.1 , evolve (0.1 , y , 0.5); end
0.5 0.4 0.3 0.2 0.1 0 −0.1 −0.2 −0.3 −0.4 −0.5 −0.6
−0.4
−0.2
0
0.2
0.4
0.6
Case 2: We expect the population to move towards the steady state value (0, 6), i.e., the deer species dies off. The solutions originating near (0, 6) move vertically towards a negatively sloped line passing through it and then moves towards (0, 6) when near that line. >> f i g u r e ; hold on ; species (0 , 6); >> f o r x = 0:0.1:0.5 evolve (x , 6.4 , 5) evolve (x , 5.6 , 5) end
8
6.6
6.4
6.2
6
5.8
5.6
5.4 −0.6
−0.4
−0.2
0
0.2
0.4
0.6
Case 3: We expect the population to move towards the steady state value (5, 0), i.e., the lion species dies off. The solution first moves horizontally towards a negatively sloped line passing through (5, 0) and then moves towards (5, 0) when near than line. >> f i g u r e ; hold on ; species (5 , 0); >> f o r y = 0:0.1:0.5 evolve (4.6 , y , 5) evolve (5.4 , y , 5) end
0.5 0.4 0.3 0.2 0.1 0 −0.1 −0.2 −0.3 −0.4 −0.5 4.4
4.6
4.8
5
5.2
5.4
5.6
Case 4: We expect the population of one species to increase at the expense of the other species. In the velocity plot, the population moves away from (2, 3) towards a negatively sloped line. >> f i g u r e ; hold on ; species (2 , 3); >> f o r x = 1.7:0.1:2.3
9
evolve (x , 2.7 , 5) evolve (x , 3.3 , 5) end >> f o r y = 2.7:0.1:3.3 evolve (1.7 , y , 5) evolve (2.3 , y , 5) end 3.6
3.4
3.2
3
2.8
2.6
2.4 1.4
1.6
1.8
2
2.2
2.4
2.6
View more...
Comments