pg. 339 7.1.4
Find the area of the shaded region in the given graph:
To find the area between two curves on an interval from \(a\) to \(b\), find the area beneath the first curve \(f(x)\) and subtract from it the area beneath the second curve \(g(x)\) so that \(\int_{a}^{b} (f(x)-g(x))dx\).
ar.bet.cur = function(equation1, equation2, fromx, tox){
# Plot max and mins
max1 = optimize(equation1,c(fromx, tox),maximum=T)$objective
max2 = optimize(equation2,c(fromx, tox),maximum=T)$objective
overall.max = max(max1,max2)+1
min1 = optimize(equation1,c(fromx, tox),maximum=F)$objective
min2 = optimize(equation2,c(fromx, tox),maximum=F)$objective
overall.min = min(min1,min2)-1
# Plots
curve(equation1, from=fromx, to=tox, n=300, ylim = c(overall.min, overall.max), xlab="X", ylab="Y", col="blue",lwd=2)
curve(equation2, from=fromx, to=tox, n=300, ylim = c(overall.min, overall.max), add=TRUE, xlab="X", ylab="Y", col="red",lwd=2)
legend( "topleft", inset = c(0,0), cex = .5,bty = "n", legend = c(equation1, equation2), text.col = c("red", "blue"), col = c("red", "blue"), pt.bg = c("red","blue"), pch = c(0,0))
x = integrate(equation1, lower = fromx, upper = tox)
matches = regmatches(x, gregexpr("[[:digit:]]+\\.*[[:digit:]]*", x))
sol1 = as.numeric(matches$value)
y = integrate(equation2, lower = fromx, upper = tox)
matches2 = regmatches(y, gregexpr("[[:digit:]]+\\.*[[:digit:]]*", y))
sol2 = as.numeric(matches2$value)
sol = sol1-sol2
sol = cat("Area between curves: ", sol)
return(sol)
}
For problem 4, we have \(f(x)= \frac{1}{2}x+3\) and \(g(x)=\frac{1}{2}cos(x)+1\) from \(0\) to \(2\pi\).
equation1 = function(x){(1/2)*x+3}
equation2 = function(x){(1/2)*cos(x)+1}
ar.bet.cur(equation1, equation2, 0, 2*pi)
## Area between curves: 22.43598
## NULL
For problem 5, we have \(f(x)= -3x^3+3x+2\) and \(g(x)=x^2+x-1\) from \(-1\) to \(1\).
equation1 = function(x){-3*(x^3)+3*x+2}
equation2 = function(x){(x^2)+x-1}
ar.bet.cur(equation1, equation2, -1, 1)
## Area between curves: 2.666667
## NULL