Report

Engineering 25 Chp3 Tutorial: Prob 3.14 Solution Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected] Engineering/Math/Physics 25: Computational Methods 1 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt P3.14 Water Reservoir Vol Using estimates of rainfall, evaporation, and water consumption, the town engineer developed the following model of the water volume in the reservoir as a function of time where V is the water volume in liters, t is time in days, and r is the town's consumption rate in liters per day. Write two user-defined functions. The first function should define the function V(t) for use with the fzero function. The second function should use fzero to compute how long it will take for the water volume to decrease to x percent of its initial value of 109 L. The inputs to the second function should be x and r. Test your functions for the case where x = 50 percent and r = 107 L/day. Engineering/Math/Physics 25: Computational Methods 2 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt A NearBy Reservoir Engineering/Math/Physics 25: Computational Methods 3 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt A NearBy Reservoir This 315-acre anglers oasis sits in the heart of Suburban Castro Valley but, you'd never know it from your surroundings once inside the park. Miles of parkland and trails make Chabot a haven for outdoor enthusiasts. Hikers can enjoy scenic walks on the 280-acre Fairmont Ridge. Park trails are designed for shared useage. Lake Chabot is well known among the running community. The park hosts Half Marathons, 5K's and the Kids Fun Run. In fact, Trail Runner magazine chose this setting as one of America's Most Scenic Races. Engineering/Math/Physics 25: Computational Methods 4 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Water Reservoir Vol Problem 3-14 appears to have an inconsistency • The fzero built-in function operates on any SINGLE variable function as described in Tab 3.2-1 of the text book. – In this case want to input a V(t), function, NOT a V(t,x,r) function to determine the %-decrease time. • The problem, however, says that TWO parameters, x & r, must also be sent to the V(t) function before using fzero Engineering/Math/Physics 25: Computational Methods 5 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Use GLOBALS for x&r We can “work around” the fzero single-varialbe requirement for V(t) by declaring x & r as GLOBAL Variables as described on pg 124 of the Text Declaring x & r as globals in BOTH the calling function and the V(t) function gives the single-Var function, V(t), access to r & x withOUT listing them in the V-function argument Engineering/Math/Physics 25: Computational Methods 6 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Physical Analysis The Reservoir Volume vs t Function V t 10 10 1 e 9 8 t 100 rt Now need to find the time, td, for the volume to decrease TO x-percent of the initial Volume for a given water consumption rate, r • i.e, Find td such that V t d V final V initial x / 100 Engineering/Math/Physics 25: Computational Methods 7 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Physical Analysis Now need to define a function for which zeros exist In this case define a DIFFERENCE Function V t V final V t Now ΔV will be ZERO when t = td so that V(td) = Vfinal Thus the function to be zeroed is the DIFFERENCE between Vfinal (the Goal) and V(t) (the Guess) • Want: Vgoal − Vguess = 0 Engineering/Math/Physics 25: Computational Methods 8 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Guess&Check Demo Make a Time Guess, tGuess Calc VGuess = V(tGuess) Calc % Full for the Guess Guess% V t 10 10 1 e 9 V init 8 10 t 100 9 Calc Difference del Goal% Guess% Guess AGAIN Engineering/Math/Physics 25: Computational Methods 9 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt rt Guess&Check Demo First Trial Run (Cmd Window) >> r = 1e7 r = 10000000>> >> VtoverVi = @(t) (1e9 + 1e8*(1 - exp(t/100)) - r*t)/1e9 VtoverVi = @(t)(1e9+1e8*(1-exp(-t/100))-r*t)/1e9 >> Goal = 0.55 Goal = 0.5500 >> Guess31 = VtoverVi(31) Guess31 = 0.7167 >> del31 = Goal - Guess31 del31 = -0.1667 Engineering/Math/Physics 25: Computational Methods 10 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Guess & Check Ans >> P3_14_ReservoirVolume_1302 r = 10000000 VtoverVi = @(t)(1e9+1e8*(1-exp(-t/100))-r*t)/1e9 Goal = 0.5500 Guess31 = 0.7167 del31 = -0.1667 Guess62 = 0.4262 del62 = 0.1238 Guess47 = 0.5675 del47 = -0.0175 Guess48 = 0.5581 del48 = -0.0081 Guess49 = 0.5487 del49 = 0.0013 Engineering/Math/Physics 25: Computational Methods 11 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Guess&Check .m-file % Bruce Mayer, PE % ENGR25 * 12Feb13 % Problem 3_14 % P3_14_ReservoirVolume_1302.m r = 1e7 % consumption rate % Create ANON fcn to return % of Initial Vol VtoverVi = @(t) (1e9 + 1e8*(1 - exp(-t/100)) - r*t)/1e9 Goal = 0.55 % to 55% of initial Volume Guess31 = VtoverVi(31) del31 = Goal - Guess31 Guess62 = VtoverVi(62) del62 = Goal - Guess62 Guess47 = VtoverVi(47) del47 = Goal - Guess47 Guess48 = VtoverVi(48) del48 = Goal - Guess48 Guess49 = VtoverVi(49) del49 = Goal - Guess49 Engineering/Math/Physics 25: Computational Methods 12 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt MATLAB GamePlan Write the single variable (var = t) function deltaV(t) for use in fzero • This function receives parameters r & x as GLOBAL variables rd & xd Write the calling function, time_to_decrease(x,r), that calls into action fzero with deltaV as its argument • The calling function sends rd & xd to deltaV by way of the GLOBAL declaration Engineering/Math/Physics 25: Computational Methods 13 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Structure Chart Note How GLOBALS ByPass the Normal Communication Path Engineering/Math/Physics 25: Computational Methods 14 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Fcn deltaV.m function diffV = deltaV(t); % Bruce Mayer, PE * 3Mar12 % ENGR25 * Problem 3-14 % % Function to find Vfinal - V(t) where %* Vfinal = 1e9*(x/100) %** x = the percent OF the 1e9 starting vol %* V(t) = 1e9 + 1e8(1-exp(-t/100) - r*t per eqn in P3-14 %** r => water consumption rate %** 1e8(1-exp(-t/100) => Rain Replenishment function over 100 day % time constant for the rainy season %** 1e9 => Starting Volume at beginning of rainy season % % This function is to be used in the fzero built-in function % fzero accepts ONLY SINGLE variable functions %* Thus Parameters X & R MUST be passed into this fcn as GLOBALS % global Rd Xd V_of_t = 1e9 + 1e8*(1-exp(-t/100)) - Rd*t; % Waterlevel dropping BY x-pct Vfinal = 1e9*(Xd/100); % % calc DIFFERENCE between the final and V(t) values diffV = Vfinal - V_of_t; Engineering/Math/Physics 25: Computational Methods 15 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt time_to_decrease(x,r).m function t_dec = time_to_decrease(x,r); % Bruce Mayer, PE * 01Mar12 % ENGR25 * Problem 3-14 % % Function to find: diffV = Vfinal - V(td) = 0 % calc diffV using Function deltaV % % Now function deltaV needs parameters x & r passed into % it as GLOBALS as discussed within deltaV.m %* need to change the names of the Globals so that they are not %* confused with local variables X & R global Rd Xd Rd = r Xd = x % % calc time to decrease stored volume by xpercent using % fzero on diffV caluation using fcn deltaV %* use general guess of 100 days [the rain replenishment time-constant] %* for the diffv(t_dec) = 0 t_dec = fzero('deltaV', 100) Engineering/Math/Physics 25: Computational Methods 16 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt The Calling Program % Bruce Mayer, PE % ENGR25 * 17Sep12 % file = Prob3_14_1209.m % UDF's: %* time_to_decrease.m %* deltaV.m % % Ask Town Engineer for Scenario-ParaMeter InPut xi = input('Percent of Initial Storage, x = '); ri = input('Consumption Rate in L/day, r = '); % % Call Into Action the solving function td = time_to_decrease(xi,ri); % % Display Results % disp('for x = ') disp(xi) disp('for r = ') disp(ri) disp(' ') disp('The time to Decrease in Days = ') disp(td) Engineering/Math/Physics 25: Computational Methods 17 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Example Call User Inputs • x = 50 & r = 1e7 Percent of Initial Storage, x = 50 Consumption Rate in L/day, r = 1e7 for x = 50 for r = 10000000 The time to Decrease in Days = 54.1832 Takes 54.2 days to drop to 50% initial for use rate of 1e7 Liters per Day Engineering/Math/Physics 25: Computational Methods 18 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Cmd Window Session For x = 50%, and r = 1e7 liters per day >> td50_1e7 = time_to_decrease(50,1e7) Rd = 10000000 Xd = 50 t_dec = 54.1832 td50_1e7 = 54.1832 Thus for a 1e7 liter/day consumption rate, a 50% decrease in volume takes about 54 days Engineering/Math/Physics 25: Computational Methods 19 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Cmd Window Session For x = 27%, and r = 4e6 liters/day >> td27_4e6 = time_to_decrease(27,4e6) Rd = 4000000 Xd = 27 t_dec = 204.2576 td27_4e6 = 204.2576 Thus for a 4e6 liter/day consumption rate, a decrease to 27% of the initial volume takes about 204 days Engineering/Math/Physics 25: Computational Methods 20 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt DeltaV Plot Anonymous fcn • For x = 27%, and r = 4e6 liters/day • dV = Goal – Guess – Goal = 1e9*(27/100) – Guess = (1e9 + 1e8*(1-exp(tg/100)) - 4e6*tg); >> dV27_4e6 = @(tg) (1e9*(1-73/100 )) (1e9 + 1e8*(1-exp(-tg/100)) - 4e6*tg); % Vfinal - Vguess >> tplot = linspace(-350,350,700); >> VolDiff = dV27_4e6(tplot); >> plot(tplot, VolDiff, 'LineWidth', 3), grid, xlabel('t (days)'), ylabel('DeltaV (L)'), title('P3-14: Need to ZERO‘) Engineering/Math/Physics 25: Computational Methods 21 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt DeltaV Plot 9 1.5 P3-14: Need to ZERO x 10 DeltaV (L) 1 0.5 0 -0.5 -1 -400 -300 -200 -100 0 t (days 100 200 Note that fzero can Return ERRONEOUS (negative) Results if Start-Pt is Off Engineering/Math/Physics 25: Computational Methods 22 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt 300 400 Erroneous result For x = 65%, and r = 1e6 liters/day >> td65_1e6 = time_to_decrease(65,1e6) Rd = 1000000 Xd = 65 t_dec = -184.8166 td65_1e6 = -184.8166 For THIS SET of Parameter fzero found a NEGTIVE (erroneous Root) Engineering/Math/Physics 25: Computational Methods 23 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Erroneous DeltaV Plot 8 6 P3-14: Need to ZERO x 10 5 4 DeltaV (L) 3 2 1 0 -1 -2 -3 -4 -400 -200 0 200 t (days 400 600 Fzero found the negative root • The can be fixed by changing the starting point to 200 Engineering/Math/Physics 25: Computational Methods 24 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt 800 Erroneous result For x = 65%, and r = 1e6 liters/day • Change This Line in the time_to_decrease code t_dec = fzero('deltaV', 200) The new (Mathematically Correct) result >> td65_1e6 = time_to_decrease(65,1e6) Rd = 1000000 Xd = 65 t_dec = 448.8765 td65_1e6 = 448.8765 Engineering/Math/Physics 25: Computational Methods 25 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Erroneous Comment Even though 448.9 Days is correct by the Equation, it is NOT physically reasonable Since Weather is CYCLICLE on a 365 day basis, anything over a Year violates nature and is thus NOT Applicable Some Advice • Always do a Reality Check on the Results returned by ANY computer program • When in Doubt PLOT Engineering/Math/Physics 25: Computational Methods 26 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt Engineering/Math/Physics 25: Computational Methods 27 Bruce Mayer, PE [email protected] • ENGR-25_HW-01_Solution.ppt