As we know, integration is about finding the anti-derivative of a function. The theory is, every function is a derivative of something. If we have a function,
f(x) = x^2 + 8x + 7 then that would be the derivative of the following function
\begin{align*} \int_{}^{} f(x) \; dx &= {\int_{}^{} x^2 \; dx} + {\int_{}^{} 8x \; dx} + {\int_{}^{} 7 \; dx} + C \\ &= {\frac{x^{2 + 1}}{3}} + {\frac{8x^{1 + 1}}{2}} + {\frac{7x^{0 + 1}}{1}} + C \\ &= {\frac{x^{3}}{3}} + {\frac{8x^{2}}{2}} + 7x + C \\ &= {\frac{x^{3}}{3}} + 4x^2 + 7x + C \\ \end{align*}
Now, what if something like this were to be applied to a real-world problem?
Let’s say we have a function {\text{rate}}(t) = 0.5t + 20 which calculates how many gallons of water is flowing at t minute.
We can plot this function with the mosaicCalc library to visualize the change of the rate of water flowing at each minute.
rate <- makeFun((0.5 * t) + 20 ~ t)
It’s a linear function. This should be a breeze. Let’s create a problem and try to solve it:
If the water was flowing for 130 minutes, how many percent of it were the total of water flowing from the 20th to the 100th minute?
This problem has two problems packed into one. What is the total gallons of water that accumulates from minute 0 to minute 130, and from minute 20 to minute 100. We solve for the first one first.
Since the original function is to calculate the rate of the flowing water, we need to find the anti-derivative of this function to calculate the total gallons of water that accumulates over time.
\begin{align*} \int_{}^{} {\text{rate}}(t) \; dt &= {\int_{}^{} 0.5t \; dt} + {\int_{}^{} 20 \; dt} + C \\ &= {\frac{0.5t^{1 + 1}}{2}} + {\frac{20t^{0 + 1}}{1}} + C \\ &= {\frac{0.5t^{2}}{2}} + {20t} + C \\ \end{align*}
To get this function in R is just by using the
mosaicCalc::antiD() function.
anti_rate <- antiD(rate(x) ~ x)
print(anti_rate)
## function (x, C = 0)
## (0.5 * x^2)/2 + 20 * x + C
And we have the same function!
Let’s find the total gallons of water that accumulates from minute 0 to minute 130. To do this, we just have to find the difference between \int_{}^{} {\text{rate}}(0) \; dt and \int_{}^{} {\text{rate}}(130) \; dt
This can be rewritten and solved as a definite integral:
\int_{0}^{130} (0.5t + 20) \; dt
\begin{align*} &= ({\frac{0.5(130)^{2}}{2}} + {20(130)}) - ({\frac{0.5(0)^{2}}{2}} + {20(0)}) \\ &= ({\frac{0.5(16900)}{2}} + {2600}) - 0 \\ &= ({\frac{8450}{2}} + {2600}) - 0 \\ &= ({4225} + {2600}) - 0 \\ &= ({4225} + {2600}) - 0 \\ &= 6825 \\ \end{align*}
We can define an R function just to do this very thing.
accumulate <- function(x, y) {
anti_rate(y) - anti_rate(x)
}
Now we can use this function to solve our problem easily.
part1 <- accumulate(0, 130)
paste(part1, "gallons of water")
## [1] "6825 gallons of water"
This 6825 value would be the area of this shaded area in the plot below.
Not that much different from the first part, we just need to solve
\int_{20}^{100} (0.5t + 20) \; dt
part2 <- accumulate(20, 100)
paste(part2, "gallons of water")
## [1] "4000 gallons of water"
Once again, we can look at how this work behind the screens.
\begin{align*} &= ({\frac{0.5(100)^{2}}{2}} + {20(100)}) - ({\frac{0.5(20)^{2}}{2}} + {20(20)}) \\ &= ({\frac{5000}{2}} + {2000}) - ({\frac{200}{2}} + {400}) \\ &= ({2500} + {2000}) - ({100} + {400}) \\ &= 4500 - 500 \\ &= 4000 \\ \end{align*}
The result of which would be the area of the shaded area in the following plot.
To complete, we need to find the percentage of the gallons of water
added from minute 20 to 100 from the total water added from minute 0 to
minute 130. As we have put the results into variables part1
and part2, we can just divide part2 by
part1, and that would be our answer.
result <- part2 / part1
paste(round(result * 100, digits = 2), "%", sep = "")
## [1] "58.61%"
58.61%. We can visualize the difference between the two areas like so.
Based on the answer we found earlier, the pink area would be 58% of the blue area.
Kaplan, Daniel. 2022. MOSAIC Calculus. GitHub Pages. https://dtkaplan.github.io/MC2/