Bruce Mayer, PE Registered Electrical & Mechanical Engineer BMayer@ChabotCollege
Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege
description
Transcript of Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege
[email protected] • ENGR-25_HW-01_Solution.ppt1
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
ENGR 25
Chp3Chp3Tutorial: Tutorial: Prob 3.14Prob 3.14SolutionSolution
[email protected] • ENGR-25_HW-01_Solution.ppt2
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Water Reservoir VolWater 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
[email protected] • ENGR-25_HW-01_Solution.ppt3
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Use GLOBALS for x&rUse 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
[email protected] • ENGR-25_HW-01_Solution.ppt4
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
From MATLAB HelpFrom 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.
[email protected] • ENGR-25_HW-01_Solution.ppt5
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Physical AnalysisPhysical Analysis
The Reservoir Volume vs time Function
rtetV t 10089 11010
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
initialfinald Vx
VtV100
[email protected] • ENGR-25_HW-01_Solution.ppt6
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Physical AnalysisPhysical Analysis
Now need to define a function for which zeros exist
In this case define a DIFFERENCE Function
GuessGuess
final
VVV
ortVVtV
target
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)
[email protected] • ENGR-25_HW-01_Solution.ppt7
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
MATLAB GamePlanMATLAB 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
[email protected] • ENGR-25_HW-01_Solution.ppt8
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Fcn deltaV.mFcn deltaV.mfunction diffV = deltaV(t);% Bruce Mayer, PE * 25Sep07% ENGR25 * Problem 3-14%% Function to find Vfinal - V(t) where%% Vfinal = 1e9*(x/100)%%% x = the percent decrease relative to the 1e9 starting vol%% V(t) = 1e9+1e8(1-exp(-t/100) - r*t per eqn in P3-14%%% r = water consumption rate%% 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, deltaV.m, as GLOBALS%global rd xdV_of_t = 1e9 + 1e8*(1-exp(-t/100)) - rd*t;Vfinal = 1e9*(xd/100); %% calc DIFFERENCE between the final and V(t) valuesdiffV = Vfinal - V_of_t;
[email protected] • ENGR-25_HW-01_Solution.ppt9
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
time_to_decrease(x,r).mfunction t_dec = time_to_decrease(x,r);% Bruce Mayer, PE * 25Sep07% 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 Globalsso that they are not%% confused with local variables x & rglobal rd xdrd = rxd = x%% calc time to decrease stored volume by x-percent using% fzero on diffV caluation using fcn deltaV%% use general guess of 10 days for the diffv(t_dec) = 0t_dec = fzero('deltaV', 10);
[email protected] • ENGR-25_HW-01_Solution.ppt10
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Cmd Window SessionCmd Window Session
For x = 50%, and r = 1e7 liters per day>> td = time_to_decrease(50, 1e7)rd = 10000000xd = 50t_dec = 54.1832td = 54.1832
Thus for a 1e7 liter/day consumption rate, a decrease to 50% of the initial volume takes about 54 days
[email protected] • ENGR-25_HW-01_Solution.ppt11
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Cmd Window SessionCmd Window Session
For x = 73%, and r = 4e6 liters/day>> td = time_to_decrease(73, 4e6)rd = 4000000xd = 73t_dec = 81.4258td = 81.4258
Thus for a 4e6 liter/day consumption rate, a decrease to 73% of the initial volume volume takes about 81 days
[email protected] • ENGR-25_HW-01_Solution.ppt12
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Plot for r = 1e7Plot for r = 1e7
0 20 40 60 80 100 120-20
0
20
40
60
80
100
Time (days)
Re
srv
oir
Ca
pa
cit
y (
%)
* r
- 1
e7
50 %
54d
[email protected] • ENGR-25_HW-01_Solution.ppt13
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Appendix – UseFul PlotsAppendix – UseFul Plots% Bruce Mayer, PE * 22Feb09 * Rev 13Sep09 * Rev 14Sep10% ENGR25 * Problem 3-14% Prob3_14_Plot_1009.m%r = 1e7; % set CONSUMPTION RATE HERE %% the Reservoir volume function - u is dummy variable for tVres = @(u) 1e9 + 1e8*(1 - exp(-u/100)) - r*u%t = linspace(0, 200);Voft = Vres(t);plot(t, Voft), grid, title('Unit-filled plot')display('showing unit plot - hit any key to continue')pause%Vpct = 100*Voft/Vres(0);plot(t, Vpct), grid, title('Normalized Plot')%display('showing NORMALIZED plot - hit any key to continue')pause% Compare Exponential and Linear TermseTerm = 1e8*(1 - exp(-t/100));LinTerm = r*t;plot(t, eTerm, t, LinTerm), grid, legend('eTerm, LinTerm')%display('showing unit-filled comparison plot - hit any key to continue')pause%%Compare Exponential and Linear Terms by RATIOLToverET = LinTerm./eTerm;plot(t, LToverET), grid, title('LinTerm/eTerm Plot')%display('showing linTerm/eTerm RATIO comparison plot - hit any key to continue‘)