Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For the following python program code it using an editor (DO NOT CODE IN THE COM

ID: 3551966 • Letter: F

Question

For the following python program code it using an editor (DO NOT CODE IN THE COMMAND LINE) and show results for full points. Also for more information on Exercise 3.7 as indicated on the problem: http://www.chegg.com/homework-help/questions-and-answers/complete-exercises-36-37-seperately-indicated-points-code-python-programs-editor-code-comm-q5015394.

Make an adaptive Trapezoidal integration rule. A problem with the Trapezoidal integration rule (3.9) is to decide how many trapezoids (n) to use in order to achieve a desired accuracy. Let E be the error in the Trapezoidal method, i.e., the difference between the exact integral and that produced by (3.9). We would like to prescribe a (small) tolerance epsilon and find an n such that E

Explanation / Answer

IMPORTANT: Regarding Exercise 3.5. After seeing this question, I'm terribly sorry to inform you that my answer to Problem 3.5 was wrong in this context. It is in fact answer to 3.7, I have now updated the answer to 3.5, please look at its comment.

Here is adaptive_trapezint.py
http://pastebin.com/JMrwNiwW


I have included diff2 from Chapter 3.1.9

Code preview:

I have removed comments here, they are present in adaptive_trapezint.py

def trapezint(f, a, b, n):
    step = ((b - a) * 1.0) / n
    val = 0
    while (a < b):
        val += 0.5 * step * (f(a) + f(a + step))
        a += step
    return val
   
def adaptive_trapezint(f, a, b, eps=1E-5):
    n = 10000
    step = ((b - a) * 1.0) / n
    max_diff = 0
    x = a
    while (x < b):
        tmp = abs(diff2(f, x))
        x += step
        if max_diff < tmp:
            max_diff = tmp
    h = math.sqrt(12 * eps) * (((b - a) * max_diff) ** -0.5)
    return trapezint(f, a, b, n)


For eps = 1E-9 we get

adaptive_trapezint(math.cos, 0, math.pi)
numerical value = -2.54078713364e-14
exact value     = 0
error           = -2.54078713364e-14

adaptive_trapezint(math.exp, 0, math.log(3))
numerical value = 2.00000000061
exact value     = 2.0
error           = 0.00000000061

adaptive_trapezint(math.sin, 0, math.pi)
numerical value = 1.99999999936
exact value     = 2.0
error           = -0.00000000064

adaptive_trapezint(math.sin, 0, 0.5 * math.pi)
numerical value = 1.00008739206
exact value     = 1.0
error           = 0.00008739206

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote