Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168717
Image: ubuntu2004

Integration Techniques

This worksheet shows how to use Sage to compute definite and indefinite integrals (i.e., with or without bounds). In addition, at the bottom there is code for helping you compute area under curves, using either vertical or horizontal slices.

%auto var('x,y')
(x, y)

Using the Integrate command

We need to use the integrate command for all the problems in this unit. The three examples below illustrate

  1. How to compute an indefinite integral f(x) dx\displaystyle \int f(x)\ dx.
  2. How to compute a definite integral abf(x) dx\displaystyle \int_a^b f(x)\ dx.
  3. How to compute an improper integral abf(x) dx\displaystyle \int_a^b f(x)\ dx where the function may have an asymptote at aa or bb, or one of the bounds in infinity. Note that if the integral does not converge, Sage will tell you it is divergent.
f(x) = x*sin(x^2) integrate(f)
x |--> -1/2*cos(x^2)
f(x) = x*sin(x^2) a=0 b=sqrt(pi/2) integrate(f,a,b)
1/2
f(x)=x^2*e^(-3*x) a=0 b=infinity integrate(f,a,b)
2/27

To find the area between two curves, you first have to find where the curves intersect.  Then you can setup multiple integrals to compute the area as needed. To use the code below:

  1. Enter you two functions (I called them blue and red)
  2. Determine where they intersect.
  3. Change the bounds on the plot to include all the intersection points.
  4. Integrate between consecutive intersection points, making sure you integrate (top-bottom), which is simply red - blue or blue -red.
var('x') blue=x^3 red=x solve(blue==red,x)
[x == -1, x == 1, x == 0]
p=plot(blue,-1,1,color='blue') p+=plot(red,-1,1,color='red') p.show()
integrate(blue-red,-1,0) + integrate(red-blue,0,1)
1/2

If you are given the functions in terns of yy, and need to solve, just repeat the above by switching the xx and yy values.  The code below repeats the code above, just in terms of yy. Now when you setup your integral, make sure you integrate left to right.

var('x,y') blue=y+6 red=y^2 solve(blue==red,y)
[y == 3, y == -2]
p=plot(blue,-2,3,rgbcolor='blue') p+=plot(red,-2,3,rgbcolor='red') d=p.get_minmax_data() p=implicit_plot(x==red,(x,d['ymin'],d['ymax']),(y,d['xmin'],d['xmax']),cmap=['red']) p+=implicit_plot(x==blue,(x,d['ymin'],d['ymax']),(y,d['xmin'],d['xmax']),cmap=['blue']) p.show()
integrate(blue-red,-2,3)
125/6

Absolute values can be difficult to work with in Sage, unless you use a numerical integration technique. For this reason, I would avoid right now trying to set up code to automatically compute the area between any two curves without any user input.

f(x)=x^3 a=-1 b=1 print abs(f(x)).nintegrate(x,a,b)[0]
0.5
print abs(f(x)).integrate(x,a,b)
integrate(abs(x)^3, x, -1, 1)
print integrate(sqrt(f(x)^2),x,a,b,'sympy')
1/2
f(x)=x^3+x a=-1 b=1 print abs(f(x)).nintegrate(x,a,b)[0]
1.5
print abs(f(x)).integrate(x,a,b)
integrate(abs(x^3 + x), x, -1, 1)
print integrate(sqrt(f(x)^2),x,a,b,'sympy')
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/notebook/sage_notebook/worksheets/admin/40/code/152.py", line 7, in <module> exec compile(ur'print integrate(sqrt(f(x)**_sage_const_2 ),x,a,b,\u0027sympy\u0027)' + '\n', '', 'single') File "", line 1, in <module> File "/usr/local/sage/local/lib/python2.6/site-packages/sage/misc/functional.py", line 416, in integral return x.integral(*args, **kwds) File "expression.pyx", line 5962, in sage.symbolic.expression.Expression.integral (sage/symbolic/expression.cpp:24542) File "/usr/local/sage/local/lib/python2.6/site-packages/sage/calculus/calculus.py", line 615, in integral return SR(result) File "parent.pyx", line 323, in sage.structure.parent.Parent.__call__ (sage/structure/parent.c:4171) File "ring.pyx", line 643, in sage.symbolic.ring.UnderscoreSageMorphism._call_ (sage/symbolic/ring.cpp:6638) AttributeError: 'Integral' object has no attribute '_sage_'