Numerical Analysis
Numerical Analysis
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
Math 481
Homework 4
Joshua Cook
November 6, 2014
I=
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
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
x3
x0
3h
3h5 IV
f (x) dx =
f0 + 3f1 + 3f3 + f3
f ()
8
80
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
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
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
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