Midpoint Rule for Double Integrals with Python

Calculus Level 1

John wishes to compare the accuracy of the midpoint rule algorithm, applying it to double integrals.

Firstly, the midpoint rule algorithm for double integrals aims to use rectangular prisms to approximate the volume under a curve, or in other words approximately evaluate the value of a double integral.

In this case, he uses n 2 n^2 number of rectangular prisms, and n divisions along both the x and y axis. A diagram for 4 rectangular prisms is shown below to illustrate how he integrates along the bound of x = 2 x = 4 y = 3 y = 6 \displaystyle \large\int_{x=2}^{x=4} \large \int_{y=3}^{y=6} . The midpoints are shown as dots.

John wants to evaluate the following integral with 2500 rectangular prisms and 10000 rectangular prisms, with equal number of divisions in each axis:

y = 2 y = 6 x = 3 x = 5 x 2 + y 3 d x d y \displaystyle \int_{y=2}^{y=6} \int_{x=3}^{x=5} x^2+y^3 dx dy

Find the percentage increase in accuracy if you use 10000 (100 divisions in each axis) rectangular prisms vs 2500 (50 divisions in each axis). to evaluate the integral (the difference of the two numerically evaluated integrals, divided by the value of the actual value of the integral(which is calculated analytically), and then multiplied by 100)

Where the percentage increase in accuracy is A A , enter your answer in the box as 1000 A \lfloor{1000A} \rfloor


The answer is 5.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Steven Chase
Oct 28, 2019

Simulation code is attached. Results are at the end of the file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import math

# Vref is result from hand calculation

Vref = 98.0*6.0/3.0 + (6.0**4.0)/2.0 - 98.0*2.0/3.0 - 8.0

##############################################################

N = 50

dx = (5.0-3.0)/N
dy = (6.0-2.0)/N

x = 3.0 + dx/2.0

V50 = 0.0

for j in range(1,N+1):

    y = 2.0 + dy/2.0

    for k in range(1,N+1):

        q = x**2.0 + y**3.0

        V50 = V50 + q*dx*dy

        y = y + dy

    x = x + dx

##############################################################

N = 100

dx = (5.0-3.0)/N
dy = (6.0-2.0)/N

x = 3.0 + dx/2.0

V100 = 0.0

for j in range(1,N+1):

    y = 2.0 + dy/2.0

    for k in range(1,N+1):

        q = x**2.0 + y**3.0

        V100 = V100 + q*dx*dy

        y = y + dy

    x = x + dx

##############################################################

print "Vref"
print Vref
print ""
print "V50"
print V50
print ""
print "V100"
print V100
print ""

A = (V100-V50)*100.0/Vref

print "A"
print A
print ""
print "1000 * floor(A)"
print math.floor(1000.0*A)

##############################################################

#Vref
#770.666666667

#V50
#770.6144

#V100
#770.6536

#A
#0.00508650519097

#1000 * floor(A)
#5.0

Nice solution; similar to mine

Krishna Karthik - 1 year, 7 months ago

How do I put my code into the solution like you did?

Krishna Karthik - 1 year, 7 months ago

Log in to reply

You can just put three of the following marks at the top and at the bottom, and put your code in between: ` (the mark right above the tab key)

Steven Chase - 1 year, 7 months ago
Krishna Karthik
Oct 27, 2019

ϕ = lim n F n + 1 F n \displaystyle \phi = \lim_{n \to \infty} \frac{F_{n+1}}{F_n}

The convergence of the fibonacci quotient to ϕ \phi

Using this as a latex editor

@Lil Doug Good. All is done. Btw, the above "solution" was used as a latex editor for a school project.

Krishna Karthik - 8 months, 3 weeks ago

Log in to reply

@Krishna Karthik how to use latex editor?

Talulah Riley - 8 months, 3 weeks ago

Log in to reply

I just post stuff into the solutions box and then copy-paste

Krishna Karthik - 8 months, 3 weeks ago

Log in to reply

@Krishna Karthik @Krishna Karthik how to do that? Where is the box?

Talulah Riley - 8 months, 3 weeks ago

Log in to reply

@Talulah Riley I post solutions and then I use snipping tool to make an image of the solution.

Krishna Karthik - 8 months, 3 weeks ago

Log in to reply

@Krishna Karthik @Krishna Karthik But where is that application.?

Talulah Riley - 8 months, 3 weeks ago

Log in to reply

@Talulah Riley On mac you have to press cmd+shift+4; on windows open up "snipping tool"; it's a default app that's on every Windows PC.

Krishna Karthik - 8 months, 3 weeks ago

@Lil Doug Are you online?

Krishna Karthik - 8 months, 3 weeks ago

Log in to reply

@Krishna Karthik now I am online

Talulah Riley - 8 months, 3 weeks ago

Log in to reply

Coming soon. wait 10 mins.

Krishna Karthik - 8 months, 3 weeks ago

@Lil Doug gimme a challenge. Issue me one on chess.

Krishna Karthik - 8 months, 3 weeks ago

@Lil Doug I'm ready now.

Krishna Karthik - 8 months, 3 weeks ago

Log in to reply

@Krishna Karthik the chat was not working?

Talulah Riley - 8 months, 3 weeks ago

@Krishna Karthik Bye

Talulah Riley - 8 months, 3 weeks ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...