How to use variable in main function as input argument to sub function? ERROR: Exiting fzero: aborting search...
이전 댓글 표시
Hello! First a brief intro... My task is to first create a function that calculates the volume of a reservoir as a function of the consumption rate and time (both given). Then, I am supposed to use that function as a SUBFUNCTION as part of another function that will determine the time it takes for the volume to decrease to a certain percentage of the initial volume (percentage is also given).
The given volume function is :
V(r,t) = 10^9 - 10^8 * (1 - exp (-t/100)) - r*t
For testing purposes, I am using:
the consumption rate, r = 10^7 liters
the percentage, x = .50
The answer should be, t_drain = 54.2 days...
My plan was to
- Create the main function that makes the percentage variable and rate variable global
- Create a time array with increments of .4167 days (about 1 hour)
- Create a sub function that calculates volume of the reservoir as a function of time
- Use that time array as an input argument to the sub function
- Have the sub function also calculate difference between the critical volume (when the volume of reservoir = percent of initial volume) and the volume at any time
- Use fzero in the main function to determine at what time the volume had decreased to the percent of the initial volume
What I am having the most trouble understanding is how I can use the array of times I created, time, as an input argument for my sub function. I don't think I did it right.
Here is what I have so far:
function t_drain = DC (x, r)
global perc rate time
perc = x;
rate = r;
%Sets maximum time t.
t_max = 1000;
%Creates array of times with each interval being one hour
time = 0:0.4167:t_max;
function v_diff = diff (time)
global t v_limit
t = time;
%Calculates water volume as a function of consumption rate and time.
v = 10^9 + 10^8 .* (1-exp(-t./100)) - rate.*t;
%Calculates limit for water volume to reach x percent of initial 10^9 L.
v_limit = perc * 10^9;
%Calculates difference between final volume and volume at time t.
v_diff = v - v_limit;
end
t_drain = fzero (@diff, 50);
end
Running this with x = .50 and r = 10^7 I get the error:
Exiting fzero: aborting search for interval containing a sign change because NaN or Inf function value encountered during search. Function Value at -92631.9 is -Inf. Check function or try again with a different starting value.
ans = NaN
However, to make sure that my algebra/math wasn't wrong I did a command window session where I ran the sub function and was able to see where v_diff was zero so I am pretty confident in that actual expressions within the sub function, just not the input argument.
I appreciate any help. Thank you.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!