CoCalc Shared FilesBasic commands for computing integrals in SAGE.sagews
Author: Ian Morrison
Views : 19

%var x, theta

# For many more examples and a detailed guide to using SAGE, see "Sage for Undergraduates" by Greg Bard posted in our blackboard content area.

# For the time being, you will mainly want to use SAGE to check your antiderivatives. Here are a few examples.

integral(x^2, x)


1/3*x^3
# SAGE knows all the standard calculus functions so

integral(x*sin(x^2),x)

-1/2*cos(x^2)

# As you can see you simply ask for "integral(f(x),x)" w1here f(x) isq the function for which you want an anti-derivative and x is the variable of integration. Thus

integral(x*sin(a*x^2),x), integral(x*sin(a*x^2),a)

Error in lines 2-2 Traceback (most recent call last): File "/projects/62d388a2-cf5e-4876-8b71-a11d342ee228/.sagemathcloud/sage_server.py", line 879, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> NameError: name 'a' is not defined

# This is the most annoying gotcha of SAGE. To use any symbol except "x" as a variable you need to tell SAGE you want to by saying var('a') — note the quotes and parentheses.

var('a')

integral(x*sin(a*x^2),x), integral(x*sin(a*x^2),a)

a (-1/2*cos(a*x^2)/a, -cos(a*x^2)/x)

# Now a tough one

integral(sec(x),x)

log(sec(x) + tan(x))

# To compute a definite integral, just add the limits of integration after the name of the variable.

integral(x^2, x, 2, 5)

39

# Sage also knows important mathematical constants like π (ask for pi) and e and the exact values of trig functions at "standard" angles like pi/3 etc.

integral(x*sin(x^2),x, sqrt(pi/6), sqrt(pi/3))

1/4*sqrt(3) - 1/4

# Note, however, that if you feed SAGE a "numerical" value (anything with a decimal in it), then SAGE will give back a decimal answer.

integral(x*sin(x^2),x, (pi/6)^0.5, (pi/3)^0.5)

0.1830127018922192

# We can force a decimal answer when we want one by calling the numerical_approx function.

numerical_approx(integral(x*sin(x^2),x, sqrt(pi/6), sqrt(pi/3)))

0.183012701892219

# Sometimes SAGE knows too much. You may see answers that involve functions we have not studied, complex numbers etc.

integral(sin(x^3), x)

-1/12*((I*sqrt(3) + 1)*gamma(1/3, I*x^3) + (-I*sqrt(3) + 1)*gamma(1/3, -I*x^3))*x/(x^3)^(1/3)

# We can force a numerical answer, when that's what we want, and SAGE not only gives us an approximation but adds an e1stimate of the error in it.

numerical_integral(x^2, 2, 5)

numerical_integral(sin(x)^3 + sin(x),  0, pi)


(38.99999999999999, 4.3298697960381095e-13) (3.333333333333333, 3.700743415417188e-14)

# Note that in this case we do NOT state what the variable is: it better be "x".

numerical_integral(a*x^2, 2, 5)


Error in lines 1-1 Traceback (most recent call last): File "/projects/62d388a2-cf5e-4876-8b71-a11d342ee228/.sagemathcloud/sage_server.py", line 879, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "", line 1, in <module> File "sage/gsl/integration.pyx", line 259, in sage.gsl.integration.numerical_integral (build/cythonized/sage/gsl/integration.c:2090) raise ValueError(("The function to be integrated depends on " ValueError: The function to be integrated depends on 2 variables (a, x), and so cannot be integrated in one dimension. Please fix additional variables with the 'params' argument


integral((2*x-x^2-x^3)*x^2, 0,1)

2/15


`