简体   繁体   中英

Math: when do we use logarithms and how does it work?

I were solving:

We know the content of the evaporator (content in ml), the percentage of foam or gas lost every day (evap_per_day) and the threshold (threshold) in percentage beyond which the evaporator is no longer useful. All numbers are strictly positive. The program reports the nth day (as an integer) on which the evaporator will be out of use.

My solution with recursion:

if (content > (initialContent / 100) * threshold) {
   double postContent = content - (content / 100) * evap_per_day;
   iterations++;
   return recursiveEvaporator(postContent, evap_per_day, threshold, initialContent, iterations);
}

But then I found more sophisticated solution: return (int)Math.ceil(Math.log(threshold / 100.0) / Math.log(1.0 - evap_per_day / 100.0)); Could you please explain me how does logarithms work here and why we choose natural logarithm?

  • First of all you have to obtain a clear image of e , that is the base of the natural logarithm. e - is constant that represents approximation of (1 + 1/n)^n that we call for when speaking about constant growth
    持续增长

    We see that newly appeared "addition" participated in further exponentiation.Roughly speaking: e^x is our income after x, where x is t*r (t-time; r-rate)

    • ln(y) is a reverse operation, we are aimed to know the time over rate we have to spend waiting for y income.

Bringing back the subject of your question ln(threshold) - is t*r(time * rate) ln(1 - evap_per_day) - is at*r to evoparate 90% !but not initial, again we need ln because 90% is constantly decreasing and we chould include it into account. We divide a product of ln(threshold) by ln(1 - evap_per_day) to get know the time.

So the correct solution is: (int)Math.ceil(Math.log(threshold / 100.0) / (ln(1.0 - evap_per_day / 100.0))

This is a case of using exponential decay and solving for time

The Exponential decay formula is A = A_o(1 - r)^t where A is the final quantity, A_o is the inital quantity, r is the rate of decay and t is the time.For this question we want to know the number of days until the intial amount is at or below a threshold percentage of the initail amount, evaperating at a cetain percentage per day. We can rewrite the equation as so: (using the percent values for threshold and evapPerDay to make explaination easier) A_o(threshold) = A_o( 1 - evapPerDay)^t

simplifies to: threshold = (1 - evapPerDay)^t

now we use logs to solve for t

log(threshold) = log((1- evapPerDay)^t)

use one of the laws of logs to move the t

log(threshold) = t(log(1-evapPerDay))

solve for t

log(threshold)/log(1-evapPerDay) = t

Use ceiling to round up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM