Chapter 2 – Simulation of continuous systems Examples Pure pursuit Chemical reactor Inventory Water reservoir General continuous simulation principle Distributed lag models
Pure pursuit Statement: Fighter aircraft sights an enemy bomber and starts chasing the bomber. Chase terminates when the fighter is close enough to the bomber and destroys the bomber or fighter gives up the chase after sometime Assumptions: Bomber (target) flies along a curve known to the fighter. The motion of fighter and bomber are two dimensional. Starting position of the bomber is known to the pursuer.
Model Bomber’s path is known means Bomber is at Time t 0 1 2 3 4 5 … Xb(t) 80 90 99 108 117 125 … Yb(t) 0 -2 -6 -8 -15 -24 … Fighter flies at constant speed Vf At the end of time t (may be 1 min) fighter changes his direction to point bomber Fighter is at (0,50) when he spots bomber
Model Figure showing F and B at time 0 and the direction of motion of F F 50 (0,0) B 80
Model Figure showing F and B at time 1 and time 2 and the direction of motion of F F (0,0) B (90,-2)
Model Figure showing F and B at time 2 and the direction of motion of F F (0,0) B (99,-6)
Fighter’s position At time 0 fighter is at (0,50) Xf(0)=0, Yf(0)=50 At time 0 bomber is at (80,0) Distance between F and B is sqrt( (80-0)2+(0- 50)2)) = d cos =80/d sin = -50/d 50 d Xf(1)=Xf(0)+Vf* cos Yf (1)=Yf(0)+ Vf* sin 80 Vf
General formula Xf(t+1)=Xf(t)+Vf*cos Yf(t+1) = Yf(t)+Vf*sin sin = [Yb(t)-Yf(t)]/Dist(t) cos = [Xb(t)-Xf(t)]/Dist(t)
Algorithm Read endt, thresh, vf Read xb(t), yb(t) for t=1 to endt Read xf(0), yf(0) For t=1 to endt dist=sqrt((xb(t)-xf(t))2+(yb(t)-yf(t)2)) If dist < thresh then exit Else sin = [yb(t)-yf(t)]/dist cos = [xb(t)-xf(t)]/dist t=t+1 xf(t+1)=xf(t)+vf*cos yf(t+1) = yf(t)+vf*sin End for If t > endt then Print “target escaped”, t Stop Else Print “target caught” End
Nature of pure pursuit problem Continuous Deterministic
Chemical reaction Statement: In a certain chemical reaction when two substances A and B are brought together. They produce a substance C. It is known that 1 gm of A and 1 gm of B produce 2 gms of C. Rate of formation of C is proportional to amounts of A and B present. In addition to this forward reaction there is also a backward reaction decomposing C back to A and B. The rate of decomposition is proportional to amount of C present in the mixture. In other words at any time if a, b, c are the amounts of A, B, C the following differential equations express the rates of increases: da/dt= k2c – k1ab db/dt= k2c – k1ab dc/dt= 2k1k2ab – 2k2c where k1, k2 are rate constants The constants k1, k2 vary with temperature and pressure. Given k1, k2 and initial quantities of A and B we wish to determine how much of C has been produced at any time. Such determinations of rates of chemical reactions are important in many industrial applications.
Solution Start at time 0 and increment time by small step t. Assume quantities of chemicals are not altered from t to t+ t. a, b, c are found using equations a(t+ t) = a(t)+da(t)/dt * t … etc Suppose simulation is performed for time T, this T is divided into large number of t. Thus T= N * t. At time 0 we know a, b, c. That is a(0), b(0) and c(0) are known. Hence we can find a(t) =a(0)+[k2*c(0)-k1*a(0)*b(0)] and similar expressions for finding b, c.
Complete solution T, t, N Print a, b, c Stop Read k1, k2, a(0), b(0), c(0) T, t, N Print a, b, c i=0 i=i+1 Stop y Compute a(i* t), b(i* t), c(i* t) I ≤ N n
Inventory Statement: Various costs in this problem are holding, ordering, loss of good will cost. The goal is to find reorder point and quantity to minimize the total cost. Assumptions: Lag time in receiving order = 3 days. Holding cost = 1 Re per item per night. Loss of good will cost = profit per item sale + 2 Rs = 10 Rs. Ordering cost = 75 Rs per order. Demand once lost is lost forever. Only one outstanding order allowed. Initial conditions: Starting inventory = 165. No outstanding orders. Demand: Any where between 0 to 99 and equi-probable.
Hand simulation Policy 1: Reorder point -125 Reorder quantity - 150 Day Opening Demand Ending Order Costs Stock Stock Hold Order Loss 1 165 80 85 yes 85 75 2 85 70 15 no 15 3 15 65 0 no 500 4 150 25 125 no 125 5 125 15 110 yes 110 75 6 110 90 20 no 20 7 20 25 0 no 50 8 150 35 115 no 115 ….. Find total cost for this policy 1.
Hand simulation Policy 2: Reorder point -100 Reorder quantity - 150 Day Opening Demand Ending Order Costs Stock Stock Hold Order Loss 1 165 90 75 yes 75 75 2 75 60 15 no 15 3 15 75 0 no 600 4 150 25 125 no 125 5 125 15 110 no 110 6 110 90 20 yes 20 75 200 ….. Find total cost for this policy 2. And for more policies and find the policy with minimum total cost.
Solution A D stock=stock-dem Read P,Q n due=0? y Initialize dueday, today, totcost, stock, due stock < P? B y totcost=totcost+75 dueday=today+3 due=1 stock=stock+Q due=0 C n today=dueday? y today=today+1 n D Generate dem Print totcost today <30? n dem <stock? n totcost=totcost+10*dem y y A C B
Water reservoir Mixed simulation (discrete as well as continuous). Rains at discrete days. Consumption is continuous. Statement: Water from a reservoir is supplied to people of a town. Rains are not routine. The volume of rain is random. The consumption is fixed. Excess water while it is raining goes waste. Output: Average wasted water, Average shortage You can generate in advance the rainy days, amount of rain. Assumptions: Capacity = 10000 gallons. Consumption = 2500 gallons per day.
Hand simulation Simulation for 10 days. Rainy days are 2, 9 Amount of rain are 5500, 2400 gallons Day Water Rain Consumption Wastage Shortage 1 10000 - 2500 - - 2 7500 5500 2500 500 - 3 10000 - 2500 - - 4 7500 - 2500 - - 5 5000 - 2500 - - 6 2500 - 2500 - - 7 0 - 2500 - 2500 8 0 - 2500 - 2500 9 0 2400 2500 - 100 10 0 - 2500 - 2500 Average wastage = 500/10 Average shortage = 7600/10
Solution Read cap, inilev, nodays Initialize cons, wast,curlev, day=1 B n wast=wast+curlev+rain-cap Read cap, inilev, nodays day <=nodays? y Initialize cons, wast,curlev, day=1 avwast=wast/nodays avshort=short/nodays curlev=min(cap, curlev+rain) curlev=curlev-cons day=day+1 Generate rday, rain Print avwast, avshort n B n curlev < 0? day = rday? y y n curlev=0 short=short+cons-curlev curlev+rain >cap? y A
Continuous simulation principle Find changes in the system state after an interval t Assume all changes to be at t+ t Update changes to state Test if end of simulation happens Continuous simulation also called fixed time step or time to time simulation
Distributed lag model / Recurrence model In some model some variables are functions of others in the previous time step Common in econometrics Example I=2+0.1Y-1 T=0.2Y-1 Y=C-1+I-1+G-1 C=20+0.7(Y-1-T-1) Suffix -1 is used to denote lagged variable Given an initial set of values the values at the end of year are found I-Investment, Y-National income, G-Govt. expenditure, T-Taxes, C-Consumption
Lag models Example Given initial Y we can find remaining variables I = 2+0.1Y-1 Y= 45.45+2.27(I+G) T=0.2Y C=20+0.7(Y-T) Given initial Y we can find remaining variables
Cobweb model P0=1.0, a=1.0, b=0.9, c=12.4, d=1.2 Example – linear supply and demand S=a+bP-1, D=c-dP where b, d > 0 For simplicity assume market is cleared each month That is D=S Given initial P, find S and using D=S find next P P0=1.0, a=1.0, b=0.9, c=12.4, d=1.2 S=1.0+0.9P-1, D=12.4-1.2P
Cobweb model Taking P0=1.0, we get S1=1.9 From D1=S1=1.9 we get P1=8.75 Using this P1 we get S2=8.875 and From D2=S2=8.875 we get P2=2.9375 Subsequent S and P are 3.644, 7.297; 7.567, 4.027; 4.625, 6.479; 6.832, 4.64 …
Cobweb model - convergent Time versus Price graph Price Time
Cobweb model - convergent Supply and Demand graph Supply Quantity S / D Demand Price
Cobweb model S= -2.4+1.2P-1, D=10-0.9P Taking P0=5.0, we get S1= 3.6 From D1=S1=3.6 we get P1=7.1 Using this P1 we get S2=6.12 and From D2=S2=6.12 we get P2=4.3 Subsequent S and P are 2.76, 8.04; 9.648, 0.39; ... Price variations are 2.1, 2.8, 3.74, 7.65, … Variations are divergent
Cobweb model - divergent Time versus Price graph Price Time
Cobweb model - divergent Supply and Demand graph Supply Quantity S / D Demand Price
References Examples Pure pursuit – N.Deo, Chapter 1 Chemical reactor – N.Deo, Chapter 2 Inventory – N.Deo, Chapter 1 Water reservoir – N.Deo, Chapter 2 General continuous simulation principle – N.Deo, Chapter 3, 3.1 Distributed lag models - G.Gordon, Chapter 3