Lab 4: Integration and Sage

Please read all of the instructions before getting started.

Goal

Use Sage to explore indefinite and definite integrals.

Instructions

This lab should be completed in groups of 2-4. I would prefer if no one worked alone. I recommend keeping this window open as a tab in your browser, so that you can refer back to these instructions as you are working on the lab.

First, one member of your group will need to log into the Sage Notebook server. Go to http://prep.sagenb.org (Sage recommends that you use Firefox). Once you are logged into the Sage Notebook server, you will be in your "Home" directory. Click on "New Worksheet" towards the top left. A small window will pop up asking you what you want to call the worksheet. Name your worksheet

MA2550 Lab 4 (name-1, name-2, etc.)


where you should replace name-1, name-2, etc. with the appropriate first names of your group members separated by commas. You will be presented with a blank Sage worksheet.

Before you actually do anything with this worksheet, I want you to share it with me and the other members of your group. Click "Share" in the third row of menus that appear in the upper right hand corner of your worksheet. As you did on Lab 1, type in the Sage usernames of the members of your group (separated by commas). You should also share the worksheet with me (my Sage username is dcernst). Once you have typed in the appropriate usernames, click "Invite Collaborators". This should return you to your blank worksheet.

Follow the steps below:

1. Place your cursor above the single Sage cell until a blue horizontal line appears and then "shift+click" to open the text editor for an HTML block.

2. Center and bold the title "Lab 4 for Calculus I (Fall 2010)" and then click "Save changes".

Indefinite Integrals

We can use Sage to evaluate many indefinite integrals. Recall that if $f$ is a function, then

(1)
\begin{align} \int f(x)\ dx=F(x)+C, \end{align}

where $F$ is an antiderivative of $f$ (assuming it has one). The syntax for integrating a function in Sage is as follows:

integral("function", "variable of integration")

where "function" is the function that you want to integrate (without the quotes) and "variable of integration" is the variable that you want to integrate with respect to (typically $x$). For example, if you want to find the indefinite integral of $y=\sin(x)$, then you would type:

integral(sin(x),x)

3. Try typing the expression above in an empty Sage cell and then clicking "evaluate" (or typing "shift+enter").

One thing that you should notice is that the answer is missing the constant of integration (i.e. "$+C$"). You'll just have to remember that this is always missing.

We can also integrate functions that we have previously defined.

4. In an empty Sage cell, define the function $f(x)=3x^2-2x^3$ by typing the following.

f(x)=3*x^2-2*x^3

5. Next, enter the following into an empty Sage cell and evaluate it.

integral(f(x),x)

Recall that we can always verify that we have the correct answer for an indefinite integral by differentiating the result. Let's try that now.

6. Enter the following into a Sage cell.

f_int(x)=integral(f(x),x)

It will look like nothing happened, but all we did was tell Sage to let f_int denote the indefinite integral of $f$ (I made up the notation f_int).

7. To see what f_int actually is, type the following into a Sage cell:

show(f_int(x))

8. To differentiate f_int, type the following into a Sage cell.

diff(f_int(x),x)

If you did everything correctly, the answer that you got should be equal to the original function $f$.

9. Now, mimic steps 4, 6, 7, and 8 for the function $g(x)=x\sin(x)$. (Note: There is no need to do mimic step 5 if you mimic steps 6 and 7.)

Approximating Definite Integrals Using Riemann Sums

Recall that if $f$ is a function, then

(2)
\begin{align} \int_a^b f(x)\ dx =\lim_{n\to \infty}\sum_{i=1}^n f(x_i^*)\Delta x, \end{align}

where $\Delta x=(b-a)/n$ and each $x_i^*$ is some point chosen in the $i$th subinterval. If this limit exists, then the definite integral equals a number, which represents the net signed area over $[a,b]$. If $f$ is an integrable function, then we can use right endpoints in the definition. In this case, $x_i^*=x_i=a+i\Delta x$. Putting this all together, if $f$ is integrable, then

(3)
\begin{align} \int_a^b f(x)\ dx =\lim_{n\to \infty}\sum_{i=1}^n f\left(a+i\frac{b-a}{n}\right)\cdot \frac{b-a}{n} \end{align}

If we don't stick a limit in front of the sum, we obtain an approximation for the definite integral. That is,

(4)
\begin{align} \int_a^b f(x)\ dx \approx \sum_{i=1}^n f\left(a+i\frac{b-a}{n}\right)\cdot \frac{b-a}{n}. \end{align}

The larger $n$ is, the better the approximation. We can use Sage to approximate definite integrals using large $n$ values (that we would never want to do by hand).

Let's approximate $\int_0^1 f(x)\ dx$, where $f$ is the function we defined earlier. To do this, we will have Sage evaluate an expression like the following:

sum(f(a+k*(b-a)/n)*(b-a)/n,k,1,n)

However, we need to tell Sage what all of these letters are. Note: I'm using k here instead of i since Sage thinks that i is an imaginary number.

10. Type each of the following into their own Sage cells and evaluate each one.

a=0

b=1

var('k, n')

We've just defined the values that we will use for our interval and we are also telling Sage that we want to use n and k as variables.

11. Now, enter the following expression and evaluate it.

f_int_approx(n)=sum(f(a+k*(b-a)/n)*(b-a)/n,k,1,n)

We just defined a function called f_int_approx (which I made up), which is a function of the number of rectangles.

12. Approximate $\int_0^1 f(x)\ dx$ using 1000, 10,000, and 100,000 rectangles. To do this, you will need to enter the following in consecutive Sage cells.

f_int_approx(1000)

f_int_approx(10000)

f_int_approx(100000)

You can easily see what decimal these expressions are approximately equal to by using the .n() function:

f_int_approx(100000).n()

Note: The .n() function tells Sage to approximate.

13. What value does it appear that our approximations are approaching? Open an HTML cell by shift+clicking on a blue line and type in your answer.

Definite Integrals

Thankfully, we can compute the exact values of definite integrals using Sage. The syntax for evaluating a definite integral using Sage is similar to the syntax for indefinite integrals, except that we need to specify the interval:

integral("function", "variable of integration", a,b)

14. Using Sage, evaluate the exact value of $\int_0^1 f(x)\ dx$.

Was your guess in step 13 correct?

15. Using Sage, compute $\int_0^{\pi} g(x)\ dx$, where $g$ is defined as in step 9. (Note: You do not need to redefine $g$ since you previously defined this function.)

Getting Help

If you need help, you can always contact me, or even better, post a question in the course forum. In addition, there are some useful links on the Sage help page.

Due Date

This lab is due by 5PM on Tuesday, November 30.