0% found this document useful (0 votes)
80 views

Numerical Analysis

The document provides code to numerically evaluate integrals using the trapezoidal rule and repeated midpoint rule in Python. For the trapezoidal rule, the function evaluates the integral from 0 to 1 of the standard normal probability density function for varying step sizes h. The results converge to the true integral value as calculated by the integrate.quad function as h decreases. For the repeated midpoint rule, the code defines a function to evaluate the integral but does not show example evaluations.

Uploaded by

Joshua Cook
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Numerical Analysis

The document provides code to numerically evaluate integrals using the trapezoidal rule and repeated midpoint rule in Python. For the trapezoidal rule, the function evaluates the integral from 0 to 1 of the standard normal probability density function for varying step sizes h. The results converge to the true integral value as calculated by the integrate.quad function as h decreases. For the repeated midpoint rule, the code defines a function to evaluate the integral but does not show example evaluations.

Uploaded by

Joshua Cook
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Math 481

Homework 4

Joshua Cook
November 6, 2014

3.6
3.6.29
the Repeated Midpoint Rule
Z b
I=
f (x) dx h(f1/2 + f3/2 + + fn(1/2) )
a

f (s1/2 )h + f (s3/2 )h + + f (sn+(1/2) )h


Here the sequence of S values is given by
a s0 t0 = s1/2 s1 t1 = s3/2 tn tn = sn+(1/2) sn+1 b
Therefore, the Repeated Midpoint rule will converge on a Riemann sum as h 0.
the Trapezoidal Rule


Z b
1
1
I=
f (x) dx h f0 + f1 + f2 + + fn2 + fn2 + fn
2
2
a
1
1
f0 h + f1 h + f2 h + + fn2 h + fn1 h + fn h
2
2
1
f0 (sk+1 sk ) + f1 (sk+1 sk ) + f2 (sk+1 sk ) + . . .
2
1
+ fn2 (sk+1 sk ) + fn1 (sk+1 sk ) + fn (sk+1 sk )
2
+ f (t0 )(s1/2 s0 ) + f (t1 )(s2 s1 ) + f (t2 )(s3 s2 ) + . . .
+ f (tn2 )(sn1 sn2 ) + f (tn1 )(sn sn1 ) + f (tn )(sn+1/2 sn )
Here the sequence of S values is given by
a t0 = s0 s1/2 t1 = s1 t2 = s2 tn2 = sn2 tn1 = sn1 tn = sn = b
Therefore, the Trapezoidal rule will converge on a Riemann sum as h 0.
the Parabolic Rule
Z b
h
I=
f (x) dx (f0 + 4f1 + 2f2 + 4f3 + + 4n3 + 2fn2 + 4fn1 + fn )
3
a
(sk+1 sk )
4(sk+1 sk )
2(sk+1 sk )
4(sk+1 sk )
f0
+ f1
+ f2
+ f3
3
3
3
3
4(sk+1 sk )
2(sk+1 sk )
4(sk+1 sk ) (sk+1 sk )
+ + fn3
+ fn2
+ fn1
+
fn
3
3
3
3
f (t0 )(s1/3 s0 ) + f (t1 )(s5/3 s1/3 ) + f (t2 )(s7/3 s5/3 ) + f (t3 )(s11/3 s7/3 ) + . . .
+ f (tn3 )(sn2 sn10/3 ) + f (tn2 )(sn4/3 sn2 ) + f (tn1 )(sn sn4/3 ) + f (tn )(sn+1/3 sn )

Math 481
Homework 4

Joshua Cook
November 6, 2014

Here the sequence of S values is given by

a s0 = t0 s1/3 t1 s5/3 t2 s7/3 t3 s11/3 . . .


sn10/3 tn3 sn2 tn2 sn4/3 tn1 sn4/3 tn sn sn+1
Therefore, the Parabolic rule will converge on a Riemann sum as h 0.
Not a Riemann Sum
h
(5f0 + f1 + f2 + 10f3 + f4 + f5 + 10f6 + + 10fn3 + fn2 + fn1 + 5fn )
4
= 5/4f0 (sk+1 sk ) + 1/4f1 (sk+1 sk ) + 1/4f2 (sk+1 sk ) + 10/4f3 (sk+1 sk ) + 1/4f4 (sk+1 sk )

I=

+ 1/4f5 (sk+1 sk ) + 10/4f6 (sk+1 sk ) + + 10/4fn3 (sk+1 sk )


+ 1/4fn2 (sk+1 sk ) + 1/4fn1 (sk+1 sk ) + 5/4fn (sk+1 sk )
= f (t0 )(s5/4 s0 ) + f (t1 )(s5/4 s1 ) + . . .
Note that the second term is sufficient to show this, as the interval for f0 overlaps with the interval within
which f1 falls.

Math 481
Homework 4

Joshua Cook
November 6, 2014

3.6.30
Given [a, b] an divided equally into r parts with H = ba
r . Given an m-point formula for integral approximation
over a given interval [tk , tk+1 ]. Then, k is the value of that integral approximation where = (k+1 k ),
with wk is a weight applied to each term

k =

m
X

f (k )wk k

that this evaluates to the integral for f a constant function over the same period implies that it is a Riemann
sum, in other words,
ti = 0 0 1 n n n+1 = ti+1
If it is a Riemann sum, then
m
X

k = ti+1 ti = H

k=0

In other words, the sum of the total infinitesimal widths is the width of the total interval. Then,
k = H

m
X

wk f (k )

Sr =

r
X

w j j =

j=0

r
X

 X

m
wj H
wk f (k )

j=0

where wk is the weight with regard to the r-point approximation.


As we take the limit when H 0, then each k reduces to a single value on the interval times the weight of
the function at that value.

lim Sr = lim

H0

H0

= lim

H0

= lim

H0

r
X
j=0
r
X
j=0
m
X
k=0

j
H

m
X

wk f (k )

k=0

 X

r
wk H
f (k )
j=0

Because these are equally space on the interval [a, b], this Riemann sum is equivalent to the integral on the
same interval when H 0.

Math 481
Homework 4

3.6.31

Joshua Cook
November 6, 2014

Newtons Three-Eighths Rule


Z

x3

x0



3h
3h5 IV
f (x) dx =
f0 + 3f1 + 3f3 + f3
f ()
8
80

Show that for n = 3k, k N


Z

xn

x0



3h
nh5 IV
f (x) dx =
f0 + 3f1 + 3f3 + 2f3 + + 2fn3 + 3fn2 + 3fn1 + fn
f ()
8
80

Proof by Induction
Base case k = 1 Gives the original three-eighths rule.
Base case k = 2

x6

x3

f (x) dx =
x0

x6

f (x) dx +

f (x) dx



3h
3h5 IV
3h5 IV
3h
f0 + 3f1 + 3f3 + f3
f () +
f3 + 3f4 + 3f5 + f6
f ()
=
8
80
8
80


3h
6h5 IV
=
f0 + 3f1 + 3f3 + 2f3 + 3f4 + 3f5 + f6
f ()
8
80
x0

x3

Inductive case Show that if true for k = m, then this implies it is true for k = m + 1.
Given


Z xn
3h
f (x) dx =
f0 + 3f1 + 3f3 + 2f3 + + 2fn3 + 3fn2 + 3fn1 + fn
8
x0
Z xn+3
Z xn
Z xn+3
f (x) dx =
f (x) dx +
f (x) dx
x0
x0
xn


Z xn
3h5 IV
3h
fn + 3fn+1 + 3fn+2 + fn+3
f ()
=
f (x) dx +
8
80
x0


3h
f0 + 3f1 + 3f3 + 2f3 + + 2fn3 + 3fn2 + 3fn1 + fn
=
8


3h
3h5 IV
fn + 3fn+1 + 3fn+2 + fn+3
+
f ()
8
80


3h
f0 + 3f1 + 3f3 + 2f3 + + 2fn + 3fn+1 + 3fn+2 + fn+3
=
8
Therefore, the proposition has been shown to be true by induction.

nh5 IV
f ()
80

nh5 IV
f ()
80

(n + 3)h5 IV
f ()
80

Math 481
Homework 4

Joshua Cook
November 6, 2014

Comparison to the Parabolic Rule


Parabolic Rule
b

f (x) dx =
a



h
6h5 IV
f0 + 4f1 + 2f2 + 4f3 + 2f4 + 4f5 + f6
f ()
3
180

Three-Eighths Rule, n = 6
Z

f (x) dx =
a



3h
6h5 IV
f0 + 3f1 + 3f3 + 2f3 + 3f4 + 3f5 + f6
f ()
8
80

Note that the error for the parabolic rule is smaller, making it generally preferable.

Math 481
Homework 4

Joshua Cook
November 6, 2014

3.7
3.7.32
import numpy as np
import math
from scipy import integrate
f = lambda x: math.sqrt(2/math.pi)*math.exp(-x**2/2)
the Trapezoid Rule
Z
I=
a



1
1
f (x) dx h f0 + f1 + f2 + + fn2 + fn2 + fn
2
2

f_rounded = np.array([[0.00,0.7978846],[0.125,0.7916754],[0.250,0.7733362],[0.375,0.7437102],[0.500,0.70
def trap_int(f,h):
n = 1./h
sum = 0
for i in range(int(n)):
print "add\t\tf("+str(float(i)*h)+"): \t", f_rounded[i*8/n][1],
sum = sum + f_rounded[i*8/n][1]*h
print "sum: ", sum
print "subtract\tf(0)/2:\t\t", f_rounded[0][1]/2,
sum = sum - 0.5*f_rounded[0][1]*h
print "sum: ", sum
print "add\t\tf(1)/2:\t\t", f_rounded[8][1]/2,
sum = sum + 0.5*f_rounded[8][1]*h
print "sum: ", sum
return sum
h=1
trap_int(f,h)
add
subtract
add

f(0.0):
f(0)/2:
f(1)/2:

0.64091300000000007

0.7978846 sum:
0.3989423 sum:
0.2419707 sum:

0.7978846
0.3989423
0.640913

Math 481
Homework 4

Joshua Cook
November 6, 2014

h = 0.5
trap_int(f,h)
add
add
subtract
add

f(0.0):
f(0.5):
f(0)/2:
f(1)/2:

0.7978846
0.7041307
0.3989423
0.2419707

sum:
sum:
sum:
sum:

0.3989423
0.75100765
0.5515365
0.67252185

0.7978846
0.7733362
0.7041307
0.6022749
0.3989423
0.2419707

sum:
sum:
sum:
sum:
sum:
sum:

0.19947115
0.3928052
0.568837875
0.7194066
0.619671025
0.6801637

0.7978846
0.7916754
0.7733362
0.7437102
0.7041307
0.6563219
0.6022749
0.54411
0.3989423
0.2419707

sum:
sum:
sum:
sum:
sum:
sum:
sum:
sum:
sum:
sum:

0.099735575
0.198695
0.295362025
0.3883258
0.4763421375
0.558382375
0.6336667375
0.7016804875
0.6518127
0.6820590375

0.67252184999999998
h = .25
trap_int(f,h)
add
add
add
add
subtract
add

f(0.0):
f(0.25):
f(0.5):
f(0.75):
f(0)/2:
f(1)/2:

0.68016370000000004
h = .125
trap_int(f,h)
add
add
add
add
add
add
add
add
subtract
add

f(0.0):
f(0.125):
f(0.25):
f(0.375):
f(0.5):
f(0.625):
f(0.75):
f(0.875):
f(0)/2:
f(1)/2:

0.68205903750000019
integrate.quad(lambda x: f(x),0.,1.)
(0.682689492137086, 7.579375928402476e-15)

Math 481
Homework 4

Joshua Cook
November 6, 2014

3.7.32
the Repeated Midpoint Rule
Z

f (x) dx h(f1/2 + f3/2 + + fn(1/2) )

I=
a

def mdp_int(f,h):
n = 1./h
sum = 0
for i in range(int(n)):
print "add\t\tf("+str(0.5*h+float(i)*h)+"): \t", f_rounded[(0.5+i)*h*8][1],
sum = sum + f_rounded[(0.5+i)*h*8][1]*h
print "sum: ", sum
return sum
h=1
mdp_int(f,1)
add

f(0.5):

0.7041307 sum:

0.7041307

0.7733362 sum:
0.6022749 sum:

0.3866681
0.68780555

0.7041307
h = 0.5
mdp_int(f,0.5)
add
add

f(0.25):
f(0.75):

0.68780554999999999
h = 0.25
mdp_int(f,.25)
add
add
add
add

f(0.125):
f(0.375):
f(0.625):
f(0.875):

0.683954375

0.7916754 sum: 0.19791885


0.7437102 sum: 0.3838464
0.6563219 sum: 0.547926875
0.54411 sum: 0.683954375

Math 481
Homework 4

Joshua Cook
November 6, 2014

3.7.40
Simpsons Rule
Z

x2

f (x) dx =
x0

x2

y2

x2 

f (x, y) dx dy =
x0

x0

y0

h
h5
(f0 + 4f1 + f2 ) f IV ()
3
90

k
(f (x, y0 ) + 4f (x, y1 ) + f (x, y2 ))
3


dx

Z
k x2
f (x, y0 ) dx
3 x0
Z
k x2
f (x, y1 ) dx
+4
3 x0
Z
k x2
+
f (x, y2 )) dx
3 x0

hk
=
f0,0 + 4f1,0 + f2,0 dx
9

hk
+4
4f0,1 + 16f1,1 + 4f2,1 dx
9

hk
+
f0,2 + 4f1,2 + f2,2 dx
9

hk 
=
f0,0 + f0,2 + f2,0 + f2,2 + 4(f0,1 + f1,0 + f1,2 + f2,2 ) + 16f1,1
9
=

the Error Term Each integration has an error term. The y-integration Gives
Ey =

k5 4
f (1 , 1 )
90 y 4

Ex =

h5 4
f (2 , 2 )
90 x4

While the x-integration gives

You might also like