* [[en:ecovirt:roteiro:math:integralr|{{:ecovirt:rlogo.png?20|}}]] * [[en:ecovirt:roteiro:math:exec_integral| Exercises]] ====== Integration ====== Integration is related to the calculus of areas under curves, volumes, and several other applications [[http://www.tech.plym.ac.uk/maths/resources/PDFLaTeX/indef_integrals.pdf|Introduction to integration]] ===== Indefinite integrals ===== Integrals can be seen as antiderivatives, that is, the inverse operation for the derivative. Let's check this out on Maxima. Take the cases in the exercise done in the last tutorial: ^ Find out the derivatives and then the antiderivates: ^^^^^^ - $ f(x) = exp(x) + x^7$ - $ f(x) = x + sin(x) $ - $ f(x) = 5x^3 + 2$ - $ f(x) = cos(x) + sin(x) $ - $ f(x) = x^2 + x^3cos(x)$ - $ f(x) = exp(x) ln(x) $ - $ f(x) = x^5sin(x)$ - $ f(x) = \frac{1}{x} $ - $ f(x) = \frac{1}{x^2} $ - $ f(x) = \frac{exp(x)}{x} $ - $ f(x) = \frac{sin(x)}{x^2}$ The function for integration on Maxima is the //integrate//, which has for arguments the function and the variable over which to integrate. There are two forms to use this function: integrate(2*x, x); 'integrate(2*x,x); The first one returns the solution of the integral, and the second returns a symbolic representation of the integral ===== Definite integrals ===== {{:ecovirt:roteiro:math:integral1.jpeg?250 |}} The definite integral is the area under the curve of a function, for a given interval. Let's see this for our well known function $f(x)=x^2$ in the interval from 0 to 1. In mathematical notation: $\int_0^1 f(x)~dx$ ==== Approximate area ==== Let's try to solve this problem with the tools we have((but beware of the [[http://en.wikipedia.org/wiki/Law_of_the_instrument|Maslow Hammer]]: "if all you have is a hammer, everything looks like a nail")). We don't know how to calculate the area under a curve, but we know how to calculate the area of a sequence of rectangles. Let's try to turn this curve into a series of rectangles! Let's draw a graph of our problem: ############################## ## area under the curve f(x)= x^2; ## for the interval 0 - 1 ############################# par(mfrow=c(2,2)) seq.x=seq(0,1.5, by=0.1) seq.y=seq.x^2 plot(seq.x,seq.y, type="l", bty="l", cex.lab=1.5, cex.axis=1.2, main= "Function x^2", xlab="x", ylab="y") abline(v=0, lty=2) abline(h=0, lty=2) seq.x1=seq(0,1,by=0.1) seq.y1=seq.x1^2 polygon(c(1,0,seq.x1,1), c(0,0,seq.y1,0),col="red") title(sub=paste("Area= ??")) #savePlot("area_x2.jpeg", type="jpeg") === Calculating the area === ########################### #### Area approximation ### ########################### n.seq1=length(seq.x1) plot(seq.x,seq.y, type="l", bty="l", cex.lab=1.5, cex.axis=1.2, main= "Minimum height",xlab="x", ylab="y") abline(v=0, lty=2) abline(h=0, lty=2) abline(v=1, lty=2) barplot(height=seq.y1[-n.seq1],width=0.1, space=0, col="red", add=TRUE, yaxt="n") ############################## ## area of the rectangles ############################## h1=seq.y1[-n.seq1] (ar1= sum(h1*0.1)) title(sub=paste("Area=",ar1)) === Another solution === plot(seq.x,seq.y, type="l", bty="l", cex.lab=1.5, cex.axis=1.2, main= "Maximum height", xlab="x", ylab="y") abline(v=0, lty=2) abline(h=0, lty=2) abline(v=1, lty=2) barplot(height=seq.y1[-1],width=0.1, space=0, col="red", add=TRUE,, yaxt="n") lines(seq.x,seq.y) ################################# ## area of the rectangles ################################ h2=seq.y1[-1] (ar2= sum(h2*0.1)) title(sub=paste("Area=",ar2)) === Average height === plot(seq.x,seq.y, type="l", bty="l", cex.lab=1.5, cex.axis=1.2, main= "Average height", xlab="x", ylab="y") abline(v=0, lty=2) abline(h=0, lty=2) abline(v=1, lty=2) barplot(height=diff(seq.y1)/2+seq.y1[-n.seq1],width=0.1, space=0, col="red", add=TRUE, yaxt="n") lines(seq.x,seq.y) ################################# ## area of the rectangles ################################ h3=diff(seq.y1)/2+seq.y1[-n.seq1] (ar3= sum(h3*0.1)) title(sub=paste("Area=",ar3)) ################################ ==== Smaller intervals ==== Now let's decrease the size of the x intervals, starting from the code we wrote on the average height of the rectangle. This simulates a process in which the interval size goes to zero: $\Delta x \to 0$. We can formulate this as: $$\int_a^b f(x)~dx = \lim\limits_{\Delta x \to 0} \sum\limits_{i=1}^n f(x_i^*)\Delta x_i$$ === $d_x=0.1$ === #################################################### ## Decreasing the interval (base) of the rectangle## #################################################### x11() par(mfrow=c(2,2)) plot(seq.x,seq.y, type="l", bty="l", cex.lab=1.5, cex.axis=1.2, main= "f(x)=x^2\t ; dx=0.1", xlab="x", ylab="y") abline(v=0, lty=2) abline(h=0, lty=2) abline(v=1, lty=2) barplot(height=diff(seq.y1)/2+seq.y1[-n.seq1],width=0.1, space=0, col="red", add=TRUE, yaxt="n") lines(seq.x,seq.y) title(sub=paste("Area=",ar3)) === $d_x=0.05$ === ############## ### dx=0.05 ## ############## dx=0.05 seq.05= seq(0,1, by=dx) seq.05y=seq.05^2 plot(seq.x,seq.y, type="l", bty="l", cex.lab=1.5, cex.axis=1.2, main= paste("dx=", dx), xlab="x", ylab="y") abline(v=0, lty=2) abline(h=0, lty=2) abline(v=1, lty=2) barplot(height=diff(seq.05y)/2+seq.05y[-length(seq.05y)],width=dx, space=0, col="red", add=TRUE, yaxt="n") lines(seq.x,seq.y) ################################# ## area of the rectangles ################################ h4=diff(seq.05y)/2+seq.05y[-length(seq.05y)] (ar4= sum(h4*dx)) title(sub=paste("Area=",ar4)) === $d_x=0.01$ === ############## ### dx=0.01 ## ############## dx=0.01 seq.01= seq(0,1, by=dx) seq.01y=seq.01^2 plot(seq.x,seq.y, type="l", bty="l", cex.lab=1.5, cex.axis=1.2, main= paste("dx=", dx), xlab="x", ylab="y") abline(v=0, lty=2) abline(h=0, lty=2) abline(v=1, lty=2) barplot(height=diff(seq.01y)/2+seq.01y[-length(seq.01y)],width=dx, space=0, col="red", add=TRUE, yaxt="n") lines(seq.x,seq.y) ################################# ## area of the rectangles ################################ h5=diff(seq.01y)/2+seq.01y[-length(seq.01y)] (ar5= sum(h5*dx)) title(sub=paste("Area=",ar5)) === $d_x=0.001$ === ############## ### dx=0.001 ## ############## dx=0.001 seq.001= seq(0,1, by=dx) seq.001y=seq.001^2 plot(seq.x,seq.y, type="l", bty="l", cex.lab=1.5, cex.axis=1.2, main= paste("dx=", dx), xlab="x", ylab="y") abline(v=0, lty=2) abline(h=0, lty=2) abline(v=1, lty=2) barplot(height=diff(seq.001y)/2+seq.001y[-length(seq.001y)],width=dx, space=0, col="red", add=TRUE, yaxt="n") lines(seq.x,seq.y) ################################# ## area of the rectangles ################################ h6=diff(seq.001y)/2+seq.001y[-length(seq.001y)] (ar6= sum(h6*dx)) title(sub=paste("Area=",ar6)) ===== Maxima ===== {{:ecovirt:roteiro:maximalogo.png?100 |}} Now it's time to integrate some functions on Maxima. Open the file {{ecovirt:roteiro:math:integral.wxm|}} and apply the integral over the functions presented in the tutorial. {{tag>maxima integral}}