Sie sind auf Seite 1von 10

DATE:

EME115, Solution to homework set #6.

DUE:

1. We wish to solve the integral,


I =

March 2, 2014
March 10, 2014 at 1:00pm

f (t)dt

NOT FOR DISTRIBUTION

of a function which can be differentiated arbitrarily many times everywhere in the interval [7/8, 25/8]. The function f has been evaluated
in the following points,
f (1)
5
f( )
4
3
f( )
2
7
f( )
4
f (2)
9
f( )
4
5
f( )
2
11
f( )
4
f (3)

= 0.266367400
= 0.295622750
= 0.308642948
= 0.307833065
= 0.296297436
= 0.277234152
= 0.253531641
= 0.227563149
= 0.201131426
(8)

a. Find the left Riemann sum, IlR .


A:
n
1
2
4
8

(n)

IlR
-5.327347984e-01
-5.626648357e-01
-5.624197130e-01
-5.582731359e-01

(n)

IlR I
1.872e-02
-1.121e-02
-1.097e-02
-6.820e-03
(8)

b. Find the right Riemann sum, IrR .


A:
n
1
2
4
8

(n)

IrR
-4.022628521e-01
-4.974288625e-01
-5.298017264e-01
-5.419641426e-01

(n)

IrR I
1.492e-01
5.402e-02
2.165e-02
9.489e-03

(4)
c. Find the mid-point sum, Imp
.
A:

n
1
2
4

(n)

Imp
-5.925948730e-01
-5.621745903e-01
-5.541265588e-01

(n)

Imp I
-4.114e-02
-1.072e-02
-2.673e-03

D EPARTMENT OF A PPLIED S CIENCE | U NIVERSITY OF C ALIFORNIA | D AVIS , C ALIFORNIA 95616


T EL : 530.752.0360 | FAX : 530.752.2444

COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

Solution to EME115, homework set #6.

Page 2
(8)

(4)

d. Find the trapezoidal sums, Itr and Itr .


A:

NOT FOR DISTRIBUTION

n
1
2
4
8

(n)

Itr
-4.674988252e-01
-5.300468491e-01
-5.461107197e-01
-5.501186392e-01

(n)

Itr I
8.395e-02
2.141e-02
5.343e-03
1.335e-03
(8)

e. Find the Simpson sum, IS .


A:
n
2
4
8

(n)

IS
-5.508961904e-01
-5.514653432e-01
-5.514546124e-01

(n)

IS I
5.572e-04
-1.192e-05
-1.193e-06

3
1
f. The exact result of the integral is I = sinh
sinh
. Rank the
3
1
results (a-e) according to accuracy.
A:
The exact result is included in the tables above. We find the
quality of the results in descending order:
S mp tr rR lR.

g. Of the summations, lR rR mp tr S, which is the best for evaluating the integral


Z 25/8
I =
f (t)dt
7/8

Also, calculate an approximation to I based on the answer.


A:
has boundaries such that we can only use the
The integral, I,
mid-point formula for n = 9, 3, 1. The best result is found for
n = 9:
8
1
1X

f (i + 1) = 0.6085560
I
4 i=0
4

2
COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

Solution to EME115, homework set #6.

2. Consider the integral, I(f ) =

R2
0

Page 3

f (t)dt, where f (t) = x3 tanh x.

a. Find the exact value of the integral.


A:
The exact result is: I = 4 ln(cosh 2) 2.674997253.
(n)

NOT FOR DISTRIBUTION

b. Calculate IrR (f ), for n = 1, 2, 4, 8.


A:
n
1
2
4
8

(n)

IrR
1.407194484e+01
7.274378264e+00
4.703556427e+00
3.621846515e+00

(n)

IrR I
1.140e+01
4.599e+00
2.029e+00
9.468e-01

(n)

c. Calculate IlR (f ), for n = 1, 2, 4, 8.


A:
n
1
2
4
8

(n)

IlR
0.000000000e+00
2.384058440e-01
1.185570217e+00
1.862853410e+00

(n)

IlR I
-2.675e+00
-2.437e+00
-1.489e+00
-8.121e-01

(n)

d. Calculate Itr (f ), for n = 1, 2, 4, 8.


A:
n
1
2
4
8

(n)

Itr
7.035972420e+00
3.756392054e+00
2.944563322e+00
2.742349962e+00

(n)

Itr I
4.361e+00
1.081e+00
2.696e-01
6.735e-02

(n)
e. Calculate Imp
(f ), for n = 1, 2, 4, 8.
A:

n
1
2
4
8

(n)

Imp
4.768116881e-01
2.132734589e+00
2.540136603e+00
2.641316255e+00

(n)

Imp I
-2.198e+00
-5.423e-01
-1.349e-01
-3.368e-02

(n)

f. Calculate IS (f ), for n = 2, 4, 8 (notice that the minimum intervals for Simpson is n=2).
A:
n
1
2
4
8
16

(n)

IS
................
2.663198599e+00
2.673953744e+00
2.674945509e+00
2.674994157e+00

(n)

IS I
.........
-1.180e-02
-1.044e-03
-5.174e-05
-3.095e-06

3
COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

Solution to EME115, homework set #6.

Page 4

g. Evaluate the errors between the n = 8 results in (b-f) and the


exact result from (a).
A:
See tables in previous answers.
Please use 8 or 9 significant digits for your calculations.

NOT FOR DISTRIBUTION

3. [MANDATORY] Consider the integral


I(f ) =

t1
t0

f (t)dt .

The attached sample code solves this problem with a left Riemann
sum, along with the first Richardson extrapolation. The code does
this for a sequence of increasing number of segments n so tn =
(t1 t0 )/n, such that convergence can be observed and Richardson

extrapolations made. The code is set up for f (t) = t, t0 = 1, t1 = 2,


with n = 2ng for ng = 0, 1, 2, , 20.
a. Run the code and validate the result you expect.
A:
ng
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

n = 2ng
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576

(n)

IlR
1.000000000000000
1.112372435695794
1.166413628918445
1.192872487446321
1.205959577787193
1.212467412138600
1.215712393603079
1.217332649905064
1.218142219417056
1.218546864511338
1.218749152142928
1.218850287229825
1.218900852591053
1.218926134726108
1.218938775657244
1.218945096088721
1.218948256295915
1.218949836397410
1.218950626447616
1.218951021472541
1.218951218985038

(n)

(n)

Imp = R(1) [IlR ]


..................
1.224744871391589
1.220454822141095
1.219331345974198
1.219046668128064
1.218975246490008
1.218957375067558
1.218952906207049
1.218951788929048
1.218951509605620
1.218951439774518
1.218951422316722
1.218951417952280
1.218951416861164
1.218951416588380
1.218951416520198
1.218951416503110
1.218951416498904
1.218951416497822
1.218951416497466
1.218951416497534

True result is I(f ) = 23 (23/2 1) 1.21895141649743. Thus,


4
COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

1.218951416497434
1.218951416476929
1.218951416497414
1.218951410028102
1.218951416451738
1.218951416497235
1.218950466781565
1.218951395289875
1.218951416121084
1.218951416491358
1.218865507989908
1.218945156857086
1.218951005387826
1.218951390450256
1.218951414863789

(n)
(n)

(n)

(n)

(n)

(n)

R(2) [IS ] = R(3) [Itr ]


R(1) [IS ] = R(2) [Itr ]
= R(1) [Itr ]

(n)

the code seems to be giving the right result, and the Richardson
extrapolation seems to be giving it faster than the core method.
b. Revise the code for the trapezoid method. Run and verify.
A:
ng
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

n = 2ng
1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576

(n)

Itr
1.207106781186547
1.215925826289068
1.218190324215082
1.218760835094640
1.218903751611352
1.218939499050680
1.218948437059119
1.218950671633084
1.218951230281066
1.218951369943343
1.218951404858931
1.218951413587827
1.218951415770053
1.218951416315608
1.218951416451994
1.218951416486096
1.218951416494603
1.218951416496753
1.218951416497288
1.218951416497377
1.218951416497456

(n)

(n)

IS = R(1) [Itr ]
..................
1.218865507989908
1.218945156857086
1.218951005387826
1.218951390450256
1.218951414863789
1.218951416395265
1.218951416491072
1.218951416497060
1.218951416497436
1.218951416497460
1.218951416497458
1.218951416497462
1.218951416497460
1.218951416497456
1.218951416497463
1.218951416497439
1.218951416497470
1.218951416497466
1.218951416497406
1.218951416497482

c. Complete the Romberg table in the code; i.e., for each n, continue as many Richard extrapolations as possible. You should
probably decrease NG. Show the resulting output.
A:
We here have q = 2 and r = 2. See table to the left .

n
1
2
4
8
16
32

(n)

Itr
1.207106781186547
1.215925826289068
1.218190324215082
1.218760835094640
1.218903751611352
1.218939499050680

IS

Page 5

ng
0
1
2
3
4
5

NOT FOR DISTRIBUTION

(n)

R(3) [IS ] = R(4) [Itr ]

(n)

(n)

R(4) [IS ] = R(5) [Itr ]

Solution to EME115, homework set #6.

5
COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

NOT FOR DISTRIBUTION

Solution to EME115, homework set #6.

ng
0
1
2
3
4
5

Page 6

d. Include an approximation to the power q of the leading error


terms; as estimated from three consecutive (increasing) segmentations of the interval. Show the resulting output.
A:
We here have q = 2 and r = 2. See table below. The table has
the estimated q given at q, where the bold numbers indicate what
we expect and use in the Richardson extrapolation.
n
1
2
4
8
16
32

(n)

Itr
1.207106781186547
1.215925826289068
1.218190324215082
1.218760835094640
1.218903751611352
1.218939499050680

q
2
1.96
1.99
2.00
2.00

(n)
(n)
= R(1) [Itr ]
S

1.218865507989908
1.218945156857086
1.218951005387826
1.218951390450256
1.218951414863789

(n)

R(2) [Itr ]

1.218950466781565
1.218951395289875
1.218951416121084
1.218951416491358

(n)

R(3) [Itr ]

1.218951410028102
1.218951416451738
1.218951416497235

(n)

R(4) [Itr ]

1.218951416476929
1.218951416497414

10

(n)

R(5) [Itr ]

1.218951416497434

12

4
3.77
3.92
3.98

5.48
5.81

7.14

The questions in this problem are sequential, such that each one builds
on the preceding. Attach code listing for each item.

Niels Grnbech Jensen

6
COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

/* CODE FOR 3a. */


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/times.h>
#include<sys/param.h>
/* WE DEFINE THE MAXIMUM NUMBER OF DIFFERENT SEGEMENTATIONS */
#define NG 20
main()
{
int
i, pp, n, ng, qq, rr;
double t0, t1, ti, dt, lR, pp_qq;
double R_l[NG+1][NG+1];
extern double func();
/* Initialize the array R_l */
for (i=0; i<NG; i++) for (n=0; n<NG; n++) R_l[i][n] = 0.0;
/* Interval end-points */
t0 = 1.0;
t1 = 2.0;
/* Interval segmentation in powers of ..... */
pp = 2;
/* First Interval segmentation */
n = 1;
/* ng'th generation of segmentation, n=pp^ng */
ng = 0;
/* q and r for the chosen method */
qq = 1;
rr = 1;
/* enter the loop over increasing segmentations n */
do {
/* Relevant segment size */
dt = (t1-t0)/n;
/* Make the left Riemann sum for n intervals */
lR = 0.0;
for (i=0; i<n; i++) {
ti = t0+i*dt;
lR += func(ti);
}
lR *= dt;
/* Dump the result in the array */
R_l[ng][0] = lR;
/* check if Richardson extrapolation can be done */
if (ng > 0) {
pp_qq = pow(1.0*pp, 1.0*qq);
/* calculate the Richardson extrapolation */
R_l[ng][1] = (pp_qq*R_l[ng][0]-R_l[ng-1][0])/(pp_qq-1.0);
}
fprintf (stdout, "%2d \& %7d \& %.15e \& %.15e \\\\ \n", ng, n, R_l[ng][0], R_l[ng][1]);
fflush(stdout);
ng++;
n *= pp;
} while (ng <= NG);
}
double func(x)
double x;
{
return sqrt(x);
}

/* CODE FOR 3b. */


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/times.h>
#include<sys/param.h>
/* WE DEFINE THE MAXIMUM NUMBER OF DIFFERENT SEGEMENTATIONS */
#define NG 20
main()
{
int
i, pp, n, ng, qq, rr;
double t0, t1, ti, dt, tr, pp_qq;
double Tr_[NG+1][NG+1];
extern double func();
/* Initialize the array Tr_ */
for (i=0; i<NG; i++) for (n=0; n<NG; n++) Tr_[i][n] = 0.0;
/* Interval end-points */
t0 = 1.0;
t1 = 2.0;
/* Interval segmentation in powers of ..... */
pp = 2;
/* First Interval segmentation */
n = 1;
/* ng'th generation of segmentation, n=pp^ng */
ng = 0;
/* q and r for the chosen method */
qq = 2;
rr = 2;
/* enter the loop over increasing segmentations n */
do {
/* Relevant segment size */
dt = (t1-t0)/n;
/* Make the left Riemann sum for n intervals */
tr = 0.5*func(t0);
for (i=1; i<n; i++) {
ti = t0+i*dt;
tr += func(ti);
}
tr += 0.5*func(t1);
tr *= dt;
/* Dump the result in the array */
Tr_[ng][0] = tr;
/* Do ng Richardson extrapolations */
PP_qq = pow(1.0*pp, 1.0*qq);
for (i=1; i<=ng; i++) {
/* calculate the Richardson extrapolation */
Tr_[ng][i] = (pp_qq*Tr_[ng][i-1]-Tr_[ng-1][i-1])/(pp_qq-1.0);
pp_qq += rr;
}
fprintf (stdout, "%2d \& %7d", ng, n);
for (i=0; i<=ng; i++) {
fprintf (stdout, " \& %.15e ", Tr_[ng][i]);
}
fprintf (stdout, " \\\\ \n");
fflush(stdout);
ng++;
n *= pp;
} while (ng <= NG);
}
double func(x)
double x;
{
return sqrt(x);
}

/* CODE FOR 3c. */


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/times.h>
#include<sys/param.h>
/* WE DEFINE THE MAXIMUM NUMBER OF DIFFERENT SEGEMENTATIONS */
#define NG 20
main()
{
int
i, pp, n, ng, qq, rr;
double t0, t1, ti, dt, tr, pp_qq;
double Tr_[NG+1][NG+1];
extern double func();
/* Initialize the array Tr_ */
for (i=0; i<NG; i++) for (n=0; n<NG; n++) Tr_[i][n] = 0.0;
/* Interval end-points */
t0 = 1.0;
t1 = 2.0;
/* Interval segmentation in powers of ..... */
pp = 2;
/* First Interval segmentation */
n = 1;
/* ng'th generation of segmentation, n=pp^ng */
ng = 0;
/* q and r for the chosen method */
qq = 2;
rr = 2;
/* enter the loop over increasing segmentations n */
do {
/* Relevant segment size */
dt = (t1-t0)/n;
/* Make the left Riemann sum for n intervals */
tr = 0.5*func(t0);
for (i=1; i<n; i++) {
ti = t0+i*dt;
tr += func(ti);
}
tr += 0.5*func(t1);
tr *= dt;
/* Dump the result in the array */
Tr_[ng][0] = tr;
/* Do ng Richardson extrapolations */
PP_qq = pow(1.0*pp, 1.0*qq);
for (i=1; i<=ng; i++) {
/* calculate the Richardson extrapolation */
Tr_[ng][i] = (pp_qq*Tr_[ng][i-1]-Tr_[ng-1][i-1])/(pp_qq-1.0);
pp_qq += rr;
}
fprintf (stdout, "%2d \& %7d", ng, n);
for (i=0; i<=ng; i++) {
fprintf (stdout, " \& %.15e ", Tr_[ng][i]);
}
fprintf (stdout, " \\\\ \n");
fflush(stdout);
ng++;
n *= pp;
} while (ng <= NG);
}
double func(x)
double x;
{
return sqrt(x);
}

/* CODE FOR 3d. */


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/times.h>
#include<sys/param.h>
/* WE DEFINE THE MAXIMUM NUMBER OF DIFFERENT SEGEMENTATIONS */
#define NG 4
main()
{
int
i, pp, n, ng, qq, rr;
double t0, t1, ti, dt, tr, pp_qq;
double Tr_[NG+1][NG+1], qq_approx[NG+1][NG+1];;
extern double func();
/* Initialize the array Tr_ */
for (i=0; i<NG; i++) for (n=0; n<NG; n++) {
Tr_[i][n] = 0.0;
qq_approx[i][n] = 0.0;
}
/* Interval end-points */
t0 = 1.0;
t1 = 2.0;
/* Interval segmentation in powers of ..... */
pp = 2;
/* First Interval segmentation */
n = 1;
/* ng'th generation of segmentation, n=pp^ng */
ng = 0;
/* q and r for the chosen method */
qq = 2;
rr = 2;
/* enter the loop over increasing segmentations n */
do {
/* Relevant segment size */
dt = (t1-t0)/n;
/* Make the left Riemann sum for n intervals */
tr = 0.5*func(t0);
for (i=1; i<n; i++) {
ti = t0+i*dt;
tr += func(ti);
}
tr += 0.5*func(t1);
tr *= dt;
/* Dump the result in the array */
Tr_[ng][0] = tr;
if (ng>1) {
qq_approx[ng][0] = log((Tr_[ng-1][0]-Tr_[ng-2][0])/(Tr_[ng][0]-Tr_[ng-1][0]))/log(1.0*pp);
}
/* Do ng Richardson extrapolations */
pp_qq = pow(1.0*pp, 1.0*qq);
for (i=1; i<=ng; i++) {
/* calculate the Richardson extrapolation */
Tr_[ng][i] = (pp_qq*Tr_[ng][i-1]-Tr_[ng-1][i-1])/(pp_qq-1.0);
if (ng-i>1) {
qq_approx[ng][i] = log((Tr_[ng-1][i]-Tr_[ng-2][i])/(Tr_[ng][i]-Tr_[ng-1][i]))/log(1.0*pp);
}
pp_qq *= pow(1.0*pp, 1.0*rr);;
}
fprintf (stdout, "%2d \& %7d", ng, n);
for (i=0; i<=ng; i++) {
fprintf (stdout, " \& %.15e ", Tr_[ng][i] /* -(pow(2.0, 1.5)-1.0)*2.0/3.0 */ );
if (i==ng) {
fprintf (stdout, " \& \{\\bf %2d\} ", (qq+ng*rr));
} else if (ng-i>1) {
fprintf (stdout, " \& %.2f ", qq_approx[ng][i]);
} else {
fprintf (stdout, " \& ");
}
}
fprintf (stdout, " \\\\ \n");
fflush(stdout);
ng++;
n *= pp;
} while (ng <= NG);
}
double func(x)
double x;
{
return sqrt(x);
}

Das könnte Ihnen auch gefallen