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.
Path: blob/main/142-Labs/Lab 05 - Numerical Integration - The Trapezoidal Rule and Simpson's Rule.ipynb
Views: 491
Lab 05 - Numerical Integration: The Trapezoidal Rule and Simpson's Rule
Overview
As we learned in Calculus I, there are two ways to evaluate a definite integral: using the Fundamental Theorem of Calculus or using numerical approximations such as Reimann Sums. While the FTC is nice in theory, it cannnot be applied in many cases, as antiderivatives are often difficult or even impossible to find. With the help of computers, using numerical integration is the most practical way to evaluate definite integrals. In this lab, we will use the Trapezoidal Rule and Simpson's Rule to approximate definite integrals.
Important SageMath Commands Introduced in this Lab
ParseError: KaTeX parse error: Undefined control sequence: \hfill at position 32: …|l|l|} \hline \̲h̲f̲i̲l̲l̲ ̲\textbf{Command…Related Course Material
In Calculus I, we learned how to use rectangles to approximate the definite integral. Another geometic figure which can be used to approximate the definite integeral is a trapezoid. Recall that the area of a trapzoid is . In order to use trapezoids to approximate the definite integral, we subdivide the interval into subintervals of equal length . Then, on each subinterval we let and and . Thus, the area of the trapzoid in the subinterval is . Therefore, we have the following approximation of the definite integral using the Trapezoid Rule: where is the number of trapezoids we are using in our approximation and .
By using trapezoids, we are essentially approximating the curve using piecewise linear functions. In Simpson's Rule, we will instead approximate using piecewise quadratic functions. This time, we start by subdividing the interval into subintervals of equal length = . On each pair of subintevals and with odd, we approximate the area spanned by the two subintervals with . Therefore, we have the following approximation of the definite integral using Simpson's Rule: where is half of the number of subintervals we are using in our approximation and .
Example 1
Let us begin by practicing using lists (or arrays in some programming languages) and loops. In order to create a list of elements, simply surround the elements in [ ].
We can return specific values of the list by adjoining ParseError: KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \textbf{[iParseError: KaTeX parse error: Expected 'EOF', got '}' at position 2: ]}̲ immediately after the name of the name of the list. The number is refered to as the index which is the position of the element in the list.
The starting index of a list is , not as you might expect. Therefore, a list which has five elements would use the indices through and not through . This is common to most programming languages.
We can adjoin two lists together by using the command.
Be careful when appending elements to a list since if you reexecute the above code, will contain two elements of 'durian' .
Note that all of the elements in the list are strings. We can add non-strings to the list as well if we prefer. Also, we can adjoin multiple elements at once by using the command.
Suppose we wanted to print all of the elements of on individual lines. One way to do this is to have multiple lines containing print statements as we did previously. This method is not ideal, however, if our list contains many elements. A better way to accomplish this is to use a loop. If we type the code ParseError: KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \textbf{"for iParseError: KaTeX parse error: Expected 'EOF', got '}' at position 13: in fruits:"}̲, then SageMath will iterate the variable through each element of the list . We can then follow the loop with the line which will print the element as iterates through all of the elements of
Note: the variable is arbitrary. You can use any letter or word you want as the iterative variable in the loop.
Example 2
Create a list called "lst" which contains the numbers through .
One way to do the above is to list all ten numbers from 1 through 10. This is doable, but not if we wanted to list the first one thousand numbers. The command ParseError: KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \textbf{range(iParseError: KaTeX parse error: Expected 'EOF', got '}' at position 2: )}̲ returns the list containing the numbers through . This does not give us exactly the list we want, but we can use this along with a loop to create the desired list.
The above code creates a blank list, and then inserts the the numbers which are one more than the elements of $\textbf{range(}\textbf{range()}$ was the list containing the numbers 0 through 9, the new list contains the numbers through .
A third way we could do this is to use the command.
If you are using SageMath 9.2 or other versions of SageMath which use Python3, then did return a list, but instead it just returned the command In order to convert this command into a list, you need to use the ParseError: KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \textbf{list(\dotsParseError: KaTeX parse error: Expected 'EOF', got '}' at position 2: )}̲ command.
Note that we used to obtain the list of numbers 1 through 10. This is because the range command starts at the first number and stops at the last number minus 1.
Now, add the numbers 11 through 13 to .
Return the sum of the fourth and thirteenth elements of .
Use a loop to print the square of each number in on its own line.
If all of the elements in a list are numbers, then you can use the command to return the sum of all of the elements in the list.
Example 3
We will use SageMath to approximate using the Trapezoid Rule with trapezoids. First, we define our function and assign and to their appropriate values.
Now, we will create a list which contains the summands of our Trapezoid Rule formula. Recall that the Trapezoid Rule is
We need to add the terms in the summation to our list. Note that the summation is a sum containing terms. Therefore, we will use a loop which goes through terms. Remember that if we use the ParseError: KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \textbf{range(nParseError: KaTeX parse error: Expected 'EOF', got '}' at position 2: )}̲ command, then will go from to , so we will instead use to go from to .
Our list now contains all of our summands. We can check by calling the name of the list to see what it contains.
We can also have SageMath tell us exactly how many elements a list contains by using the command.
Lastly, we can complete our approximation by adding all of the summands together using the command and multiplying by .
In order to get a comprehensable answer, we can use the to round the answer to decimal places.
Therefore, by using the Trapezoid Rule, we get
We can use SageMath to see how close our approximation is to the actual answer.
Therefore, our approximation is accurate to within three decimal places.
Note that, we can combine all of the steps we used to compute the Trapezoid Rule approximation into a single function which will allow us to approximate the definite integral quicker.
We have just created a function which accepts arguments for our function, for our lower bound, for our upper bound, and for the number of trapezoids. We can check that this function does return what we expect by using it to calculate the Trapezoid Rule approximation we just did in detail.
Again, to make sense of this answer, we can use the command.
Example 4
Now, we will use SageMath to approximate using Simpson's Rule with .
Again, we will create a list which contains all of our summands of our Simpson's Rule formula. Recall that Simpson's Rule is Note that this time our summation starts at and stops at , so we can use
Now, we can get our approximation by adding all of the elements in summand and multiplying by
Therefore, Simpson's Rule gives the approximation Now, check how close the approximation is to the actual answer.
It follows that our approximation using Simpson's Rule is accurate to 6 decimal places.
Create the function which combines all the steps used to estimate the definite integral using Simpson's Rule. Then check that your function gives the same result as above.
Example 5
Use the functions and to approximate the following definite integrals with . Also, determine the abosolute error in each approximation. Remember that in order to use a variable other than , you have to define it as a variable using the command.