This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is

licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

- Intro to Sage

In this lab we will learn how to compute both definite integrals and indefinite integrals (antiderivatives) in Sage.

The Sage command is called "integral." The number of arguments depends on whether the integral is definite or indefinite.

To compute an indefinite integral in Sage, use the "integral" command with two arguments: integral(*function, variable*). In other words,

Note: Sage *does not* add the constant of integration (the ).

You can type the expression directly:

1/3*arctan(1/3*x) + 1/2*log(x^2 + 9)

You can also use a function name:

1/3*arctan(1/3*x) + 1/2*log(x^2 + 9)

Think about how you would do this one by hand.

You could break up the fraction:

You can compute using substitution (this gives you ).

You can compute using a little algebra (this gives you ).

[Remember, "log" in Sage is the natural logarithm, often denoted "ln." Also, we would normally write , but Sage leaves out the absolute value.]

-1/9*(-3*x^2 + 4)^(3/2)

This is a fairly simply substitution question.

What if we take away the in front?

1/2*sqrt(-3*x^2 + 4)*x + 2/3*sqrt(3)*arcsin(1/2*sqrt(3)*x)

This is more complicated, but you could do it by hand with trig substitution.

Note: Sage's output is somewhat misleading here. Notice the right after the 4 under the square root? Is the inside or outside the square root?

To compute a definite integral in Sage, use the "integral" command with four arguments: integral(*function, variable, lower limit, upper limit*). In other words,

-65*e^(-1) + 24

Here Sage has returned the exact answer. This integral could be computed by hand using integration by parts.

Let's convert the answer to a decimal:

0.0878363238562478

Sage can also handle definite integrals involving variables (the answer is not a number).

Some functions do not have elementary antiderivatives. For example, consider

0.746824132812427

Notice that Sage gives an exact answer involving a function called "erf." This is the "error function," defined as follows:

In other words, when we try to integrate , Sage gives us an answer in terms of this very integral. Since we are computing a definite integral, Sage is able to give us a numerical approximation.

For this and other examples where Sage can't find an exact answer, there is another command, numerical_integral, which provides a numerical approximation.

This command requires three arguments: the function to integrate, the lower bound of integration, and the upper bound of integration.

[Note: you do not specify the variable of integration, since there can be only one variable.]

Here is an example:

(0.746824132812427, 8.291413475940723e-15)

Notice that Sage returns an ordered pair. The first element is the numerical approximation of the integral, and the second is an estimate of the error in the approximation. Note the scientific notation in the error. In this example, the error is around .

Here is one that the integral command can't do at all:

integrate(sin(cos(x)), x, -1, 1)

So we'll try numerical_integral.

(1.4772859960737805, 1.6401169267974196e-14)

So the answer is approximately 1.4773.

Of course, numerical_integral can only be used for a definite integral.

You will see many applications of integrals in Calculus 2. For now, we can talk about area and motion.

Find the area under on the interval .

**Solution:** This area is .

(0.39841444459716535, 4.423288897350168e-15)

The net change in position is the integral of the velocity function. Suppose an object is traveling with velocity meters per second. How far does the object travel from to seconds?

**Solution:** The distance traveled is meters.

1900