Homework 3, Problem 1 (solutions)
Theory/Introduction
Diffraction problems often involve the Fresnel sine and cosine integrals C(u) and S(u). For this
problem the diffraction intensity is given by
I=I0/8([2C(u)+1]2+[2S(u)+1]2)
C(u) and S(u) are the integrals of cos(1/2 pi t2) and sin(1/2 pi t2), respectively.
Code
The integrals are done using the Gauss-Legendre quadrature from the textbook
diffract.f90
Results
Discussion/Summary
For the z=1 case I plotted the results for different N, the number of quadrature points. The most
difficult points to calculate are when the upper limit of the integrals is large. When the upper
limit is large, there are regions where the integrands oscillate rapidly, which requires more
integration points to calculate the integrals accurately. Fpr z=1, a N of about 40 was needed for
6 digits of accuracy in the worst case (large x region). Small z is harder because it also makes
the upper limit u larger.
<
M
B
@
Q
Q
s
a
!
0
R
,
,
z
,
T
,
z
y
,
1
&
.
v
u
$
Y
^
O
]
O
[
^
]
+
+
O
b
c
q
o
b
a
l
h
c
b
a
[
[
^
]
[
O
c
c
o
a
m
b
a
l
h
c
b
]
O
[
q
!
n
x
!
]
!
i
#
N
t
N
!
c
b
a
`
+
_
j
!
+
!
s
/
n
$
B
$
Q
v
u
$
n
i
#
d
Q
N
t
N
@
L
A
A
}
/
+
+
-
+
*
n
+
!
+
^
O
]
\
[
Z
Y
O
!
p
/
n
$
^
O
]
O
O
r
O
^
!
X
Q
/
/
n
$
n
i
#
"
n
N
f
/
n
$
<
D
A
M
H
<
D
A
M
J
G
H
M
@
H
?
C
B
K
B
@
1
&
,
5
'
2
/
&
,
5
'
2
[
^
]
!
<
C
?
0
0
2
&
.
0
2
.
'
2
0
2
.
R
0
)
(
4
)
2
1
0
2
.
0
2
.
.
n
i
T
S
'
T
S
0
2
&
.
0
T
T
z
2
<
<
H
?
H
H
<
H
<
s
G
?
C
?
C
/
0
<
D
A
M
J
G
G
G
<
z
.
'
/
/
n
$
2
&
.
.
0
z
T
.
R
0
2
.
R
'
0
0
.
2
H
<
2
.
R
T
S
'
&
2
c
q
o
b
a
l
h
c
b
a
0
R
,
,
,
)
)
4
)
,
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
)
'
}
T
|
%
%
$
#
$
p
,
0
=
'
/
n
i
k
.
$
M
@
A
M
M
A
>
=
;
=
8
3
=
3
3
6
9
8
9
6
=
3
8
3
7
:
7
=
(
3
(
9
8
7
(
5
6
5
4
3
'
2
1
%
%
$
#
$
p
,
0
=
'
/
n
i
k
.
$
=
;
=
8
3
3
3
6
9
8
9
6
=
3
8
3
7
:
7
=
(
3
(
9
8
7
(
5
6
5
4
3
'
2
1
%
%
$
#
$
p
,
0
=
'
/
n
i
k
.
$
=
=
;
9
5
)
4
3
'
1
%
%
$
#
$
p
,
0
=
'
/
n
i
k
.
$
=
D
A
B
A
+
+
-
+
*
;
)
4
8
'
R
,
=
;
)
4
5
'
&
,
=
;
)
4
)
'
{
%
%
$
#
$
p
,
0
=
'
/
n
i
k
.
$
!
)
(
'
&
%
%
$
#
$
p
,
$
$
#
n
i
@
@
<
$
n
n
#
i
"
i
p
i
y
T
,
3
1
,
8
1
,
5
1
,
1
1
,
5
,
,
2
%
%
0
=
'
/
n
i
k
.
$
d
$
p
n
"
B
@
B
@
<
!
0
1
&
.
R
,
0
1
&
.
%
%
0
+
.
#
n
$
#
n
i
,
0
=
'
/
n
i
k
.
$
!
z
,
T
%
%
0
.
#
n
$
#
n
i
,
0
=
'
/
n
i
k
.
$
M
B
@
!
z
y
,
1
&
%
%
0
.
#
n
$
#
n
i
,
$
$
#
n
i
Homework 3, Problem 2 (solutions)
Theory/Introduction
This problem compares the error behavior of three different formulas for the first derivative, the
2-point, 3-point, and 5-point formulas. The derivative formulas depend on a point spacing h. As h
decreases, the approximation error decreases. However, for small enough h, subtraction in the
formulas leads to roundoff error dominating over the approximation error.
Code
2pt.f90
Results
f(x)=sin(x), x=0.1
Fitting:
2-point, approximation error; slope = 1.0005, roundoff slope = -1.1(2); minimum error of
about 4.0e-10 at h of about 1.0e-08
3-point, approximation error; slope = 1.9999, roundoff slope = -0.9(3); minimum error of
about 8.0e-13 at h of about 2.0e-06
5-point, approximation error; slope = 3.9992, roundoff slope = -1.0(4); minimum error of
about 3.0e-15 at h of about 0.0005
f(x)=sin(x), x=10.0
f(x)=sin(x), x=100.0
f(x)=exp(x), x=0.1
f(x)=exp(x), x=10.0
f(x)=exp(x), x=100.0
Discussion
The actual approximation error dependence on h was very close to the prediction (slopes of 1, 2,
and 4 for the 2, 3, and 5-point formulas). For all formulas the roundoff error slope was close to
-1, with roundoff error beginning to dominate at larger h for the more accurate formulas.
Because all formulas exhibit such similar roundoff error h dependence, this suggests that the
roundoff error is coming from a similar cause- in this case probably subtraction.
The predicted minimum errors for the 2-point and 3-point formulas were 3e-08 and 3e-11,
assuming double precision and that the next higher derivative is of order 1 (text equation 7.15).
The h for these lowest errors was predicted to be 4e-08 and 3e-05, respectively. The actual
minimum errir was somewhat less that this, while the h where the minimum error occured was
consistent with this estimate. For the 5-point formula we did not derive a formula for the
minimum error, but the results seem consistent, with the optimum h increasing by around two
orders of magnitude.
Changing the value of x changes the relative error very little in these examples, because the
derivative is of similar magnitude to the function itself. The absolute error for the derivative of
exp(x) will of course be larger for large x.
t
(
'
%
&
%
$
t
Y
#
"
t
&
N
N
'
d
n
i
+
*
n
M
f
.
n
,
p
.
n
,
=
8
0
2
3
4
-
t
&
X
t
&
N
N
'
d
n
i
+
*
n
M
6
B
H
;
A
;
A
D
C
J
A
A
@
8
0
W
>
7
8
7
6
B
5
H
>
7
8
7
6
B
;
A
;
A
>
7
8
t
&
V
N
N
'
d
n
i
+
H
A
;
A
D
C
B
A
A
@
8
2
0
0
4
-
0
2
W
4
-
-
=
D
C
B
A
A
@
8
2
3
4
3
1
1
0
/
=
.
n
i
k
-
)
"
,
0
2
3
4
-
t
&
V
N
N
'
d
n
i
+
*
n
M
!
U
T
S
R
t
n
i
+
*
n
M
f
.
n
,
D
G
F
E
0
4
-
p
e
,
=
4
D
G
F
E
4
3
4
1
1
0
/
=
.
n
i
k
-
)
"
,
U
T
S
R
0
4
-
t
n
i
+
*
n
M
f
Q
O
P
O
!
N
n
i
+
*
n
M
f
.
n
,
A
0
4
-
p
e
,
=
A
4
3
1
1
0
/
=
.
n
i
k
-
)
"
,
0
4
-
N
n
i
+
*
n
M
f
!
L
?
n
i
"
+
n
*
.
.
n
,
B
H
2
=
2
F
E
H
D
G
F
E
;
D
C
K
A
A
@
8
?
a
0
4
-
4
3
0
2
-
0
0
2
4
-
=
7
8
7
6
5
=
4
3
2
1
1
0
/
=
.
n
i
k
-
)
"
,
,
n
n
+
i
*
i
)
p
#
i
t
(
'
%
&
%
$
t
#
"
p
!
!
!
!
!
7
8
7
6
7
7
5
2
0
2
-
,
)
i
4
0
<
5
;
8
7
6
5
:
9
w
.
0
4
-
4
0
2
3
4
-
-
-
"
3
2
3
W
I
D
G
F
E
H
D
G
F
E
;
D
C
B
A
A
@
8
?
a
>
0
I
3
0
0
4
0
0
4
4
-
D
G
F
E
0
0
-
4
0
0
4
4
4
0
2
3
4
-
-
-
"
-
H
D
G
F
E
;
D
C
J
A
A
@
8
?
a
-
4
0
2
3
4
-
-
-
"
+
n
i
p
0
2
4
-
0
2
3
4
-
t
&
*
n
M
f
.
n
f
D
2
3
4
3
X
N
N
'
d
,
7
D
C
J
A
A
@
8
1
1
0
/
=
.
n
i
n
i
+
*
n
M
!
G
/
0
2
2
4
>
7
-
0
0
2
4
-
0
2
W
4
-
-
=
k
-
)
"
,
f
6
;
A
8
2
3
4
3
Y
N
N
'
f
.
n
>
D
C
K
A
A
@
1
1
0
/
=
.
d
n
i
,
7
-
/
W
0
2
W
4
-
-
n
i
k
-
)
+
*
n
M
!
7
8
7
6
>
7
8
7
6
B
A
;
D
C
K
A
"
,
A
@
8
f
Homework 3, Problem 3 (solutions)
Introduction
In this problem we solve an elliptic integral for the period of a plane pendulum. It is easy in this
case to construct a Gaussian quadrature that is very accurate because the form of the integral is
that of a Gauss-Chebyshev integral. In this case the weights and abscissas for the Gaussian
quadrature are known analytically. In the integral the function f(x) and weight function w(x) are:
f(x) = 2/sqrt(1-k2x2)
w(x) = 1/sqrt(1-x2)
Code
Simple code: fixed n
pendulum.f90
Code with automatic n
To determine the n needed automatically, compare the value of the integral for n and n+1. The
relative error can be estimated from these two values.
pendulum2.f90
Results
Simple code, theta0=0.1
The small-amplitude period is 2pi. With a small angle the difference from 2*pi is very small:
n
1
3
5
7
9
T
6.2831853071795862
6.2871145483499413
6.2871145493104796
6.2871145493104796
6.2871145493104805
T/(2*pi)
1.0000000000000000
1.0006253581548621
1.0006253583077367
1.0006253583077367
1.0006253583077367
Code with automatic n
Assumes relative error less than 1.0e-06
theta0 N
0.10 4
0.30 4
0.50 4
0.70 4
0.90 7
1.10 7
1.30 7
1.50 7
1.70 10
1.90 10
period
6.28711455
6.31871154
6.38278970
6.48118971
6.61686647
6.79416186
7.01925030
7.30086480
7.65151350
8.08961444
period/2pi
1.00062536
1.00565418
1.01585253
1.03151338
1.05310701
1.08132444
1.11714838
1.16196872
1.21777620
1.28750213
2.10
2.30
2.50
2.70
2.90
3.10
13
16
19
28
49
49
8.64351941
9.35983654
10.32315984
11.71347810
14.04611196
20.97181119
1.37565884
1.48966425
1.64298192
1.86425794
2.23550815
3.33776742
Period as a function of theta0:
With the period and angle normalized:
Discussion
Notice that with one integration point (n=1), the Gauss-Chebyshev formula returns the exact
result for the period in the limit of small oscillations (T=2pi). As the initial angle approaches pi,
the period diverges, and the integral becomes more and more difficult to compute numerically.
R
#
*
(
)
(
'
&
T
S
%
&
n
%
$
i
-
,
n
"
H
f
0
n
.
p
0
n
.
s
F
E
#
H
=
@
>
6
0
0
n
.
U
s
s
;
E
G
9
:
9
8
9
=
#
H
s
U
3
3
J
4
4
2
1
=
0
n
i
k
/
+
"
.
2
2
9
:
#
s
8
E
G
6
5
=
H
E
3
<
=
6
U
9
M
2
6
/
+
2
C
.
/
;
6
5
/
"
D
s
,
=
B
3
J
/
K
V
#
H
=
#
H
J
0
E
6
3
4
4
.
.
-
n
i
F
E
@
>
6
3
C
B
4
4
2
1
=
0
n
i
k
/
+
"
.
E
T
S
R
2
3
C
B
/
%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