Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
License: MIT
Image: ubuntu2004
Numerical Integration Python Lab
In this lab, we look at using computers to approximate the values of definite integrals. Recall that this corresponds to the area under a curve.
Remember that a line that starts with # is a comment line, not executable code. Also, if multiple lines are enclosed in three quote marks, those lines are also comments.
To execute a line of code, click anywhere inside the box, hold down the Shift key and press Enter.
Enter your name and your group members' names in the line below. The \n starts a new line. Then execute the line.
Trapezoidal Rule and Simpson's Rule
Methods of Approximating Area
Your Name
My group members are Name1 and Name2.
Now we need to call up the numeric Python library, numpy, and the plotting or graphing library, pyplot. Execute the following line of code.
We will use the function over the interval [0, 8]. That is, we are going to approximate . While this function does, in fact, integrate, we will use it to demonstrate the techniques.
The true, exact value of the area is Let us save this value, so we can judge how close our approximations are to the true value.
This line of code is the first that you will copy into Practice 1.
Part I: The Trapezoidal Rule
In the following input line, we define a program called trapezoidal, which computes the approximate area under a curve over a given interval using the Trapezoidal Rule.
In the next input line, we use the lambda command to define a funtion quickly. We set the number of sub-intervals, n, to be 4.
Next we set the variable trap_4 to be the output of the trapezoidal program. Then we print out the value of it.
Finally, we compute the error in our approximation by subtracting the value from the exact value.
For Practice 1, you will copy the following input line and paste it into the second empty slot after Practice 1.
Now run the program for n = 8. Change the name of the variable to trap_8.
For Practice 1, you will copy the following input line and paste it into the third empty slot after Practice 1.
Now run the program for n = 16. Change the name of the variable to trap_16.
For Practice 1, you will copy the following input line and paste it into the fourth empty slot after Practice 1.
For Practice 1 below,
In the first empty slot below, copy the third input line. Redefine the value of exact and name it exact1. Let exact1 = Remember to use the command np.cos(25).
Copy the three previous input lines into the next three empty slots.
In the second slot,
change the name of the function to w and define it using the lambda command. Don't forget to use t*np.sin(t**2). The * marks explicit multiplication and the ** marks an exponent.
Leave n as 4.
Remember to change the name of the function to w when you call Trapezoidal.
Remember to change the interval to 0 to 5 when you call Trapezoidal.
Change the error to exact1 - trap_4.
In the third slot,
Leave n as 8.
Remember to change the name of the function to w when you call Trapezoidal.
Remember to change the interval to 0 to 5 when you call Trapezoidal.
Change the error to exact1 - trap_8.
In the fourth slot,
Leave n as 16.
Remember to change the name of the function to w when you call Trapezoidal.
Remember to change the interval to 0 to 5 when you call Trapezoidal.
Change the error to exact1 - trap_16.
Don't forget to Shift + Enter to execute the code.
There are several ways to copy a line of code. The easiest here is to
click anywhere in the input line you want to copy.
Use CTRL + A (pc) or CMD + A (Mac) to select all in the input line. (Recall that an input "line" will probably be multiple lines of code. A single input line has In followed by square brackets (with a number in the middle after execution).)
Then use CTRL + c or CMD + c to copy the code.
Next, click inside the empty input line where you want to copy the code.
Use CTRL + v or CMD + v to paste.
Practice 1: Estimate the area under the curve of the function over the interval . That is, approximate using the Trapezoidal Rule.
Part II: Simpson's Rule
In the following input line, we define a program called simps_with_plot, which computes the approximate area under a curve over a given interval using Simpson's Rule.
In this example, we are going to call up the program three times, with different values for n, the number of subintervals. We will return to the function v with exact value given by exact, over the interval
In Practice 2, you will copy the following input line to use with the function w.
For Practice 2 below,
Copy the previous input line into the empty slot below Practice 2.
Remember to change the name of the function to w when you call simps_with_plot. It is already defined from before.
Remember to change the interval to 0 to 5 when you call simps_with_plot.
Change the error to exact1 - trap_#, with the appropriate number for all three calls.
Don't forget to Shift + Enter to execute the code.
There are several ways to copy a line of code. The easiest here is to
click anywhere in the input line you want to copy.
Use CTRL + A (pc) or CMD + A (Mac) to select all in the input line. (Recall that an input "line" will probably be multiple lines of code. A single input line has In followed by square brackets (with a number in the middle after execution).)
Then use CTRL + c or CMD + c to copy the code.
Next, click inside the empty input line where you want to copy the code.
Use CTRL + v or CMD + v to paste.
Practice 2: Estimate the area under the curve of the function over the interval . That is, approximate using Simpson's Rule.