7.1 - #15: find the total area enclosed by the functions f and g, where:

\[ f(x)=x, \space g(x)=√x \]
library(ggplot2)
f <- function(x)(x)
g <- function(x)(sqrt(x))
(i <- as.numeric(integrate(g, lower = 0, upper = 1)[1]) - as.numeric(integrate(f, lower = 0, upper = 1)[1]))
## [1] 0.1666667
f
## function(x)(x)

\[\int_{0}^{1} \bigg(g(x) - f(x)\bigg) dx = 0.1666667 \]

ggplot(mapping = aes(x = 0:1, y=0:1)) +
  stat_function(fun = g, size = 1, aes(color = "g(x)")) +
  stat_function(fun = f, size = 1, aes(color = "f(x)")) +
  labs(x = "x", y = "y") +
  theme_light() +
  theme(legend.position = "bottom")

Shading the area challenge - done through linear fit as an alternative way than the above ggplot

x <- c(0, 1); y <- c(0, 1)
model_f <- lm(y~x)
model_g <- lm(y~I(sqrt(x)))

plot(y~x)
abline(model_f, col="red")
x_fitted_g <- seq(0, 1, 0.01)
y_fitted_g <- model_g$coefficients[1] + sqrt(x_fitted_g) * model_g$coefficients[2]
polygon(x_fitted_g, y_fitted_g, col='lightgray', border=NA)         
lines(x=x_fitted_g, y=y_fitted_g, pch=11, col="blue")