130 likes | 222 Views
ENGR 25. Chp3 Tutorial: Prob 3.14 Solution. Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu. Water Reservoir Vol. Problem 3-14 appears to have an inconsistency
E N D
ENGR 25 Chp3Tutorial: Prob 3.14Solution Bruce Mayer, PE Licensed Electrical & Mechanical EngineerBMayer@ChabotCollege.edu
Water Reservoir Vol • Problem 3-14 appears to have an inconsistency • The fzero built-in function operates on any SINGLE variable function as described on pg 156-157 of the text book. • In this case want to input a V(t) function to determine the %-decrease time. • The problem, however says that TWO parameters, x & r, must also be sent to the V(t) before using fzero
Use GLOBALS for x&r • We can “work around” the fzero single-var requirement for V(t) by declaring x & r as GLOBAL Variables as described on pgs 153-155 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 them being in the V-function argument
From MATLAB Help • global X Y Z defines X, Y, and Z as global in scope. • Ordinarily, each MATLAB function, defined by an M-file, has its own local variables, which are separate from those of other functions, and from those of the base workspace. • However, if several functions, and possibly the base workspace, ALL declare a particular name as global, they all share a single copy of that variable. Any assignment to that variable, in any function, is available to ALL the functions declaring it global.
Physical Analysis • The Reservoir Volume vs time Function • Now need to find the time, td, for the volume to decrease to x-percent of the initial value for a given drinking rate, r • i.e., Find td such that
Physical Analysis • Now need to define a function for which zeros exist • In this case define a DIFFERENCE Function • Now ΔV will be ZERO whent = td so that V(td) = Vfinal • Thus the function to be ‘zeroed’ is the DIFFERENCE between Vfinal and V(t)
MATLAB GamePlan • Write the single variable (var = t) function deltaV(t) for use in fzero • This function receives PARAMETERS r & x as GLOBAL vars rd & xd • Write the calling function, time_to_decrease(x,r), that calls into action fzero with deltaV as the fzero argument • The calling function sends rd & xd to deltaV by way of the GLOBAL declaration
Cmd Window Session • For x = 50%, and r = 1e7 liters per day >> td = time_to_decrease(50, 1e7) rd = 10000000 xd = 50 t_dec = 54.1832 td = 54.1832 • Thus for a 1e7 liter/day consumption rate, a decrease to 50% of the initial volume takes about 54 days
Cmd Window Session • For x = 73%, and r = 4e6 liters/day >> td = time_to_decrease(73, 4e6) rd = 4000000 xd = 73 t_dec = 81.4258 td = 81.4258 • Thus for a 4e6 liter/day consumption rate, a decrease to 73% of the initial volume volume takes about 81 days
Plot for r = 1e7 50% 54d