10
COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014 NOT FOR DISTRIBUTION DATE: March 2, 2014 DUE: March 10, 2014 at 1:00pm EME115, Solution to homework set #6. 1. We wish to solve the integral, I = 3 1 f (t)dt of a function which can be differentiated arbitrarily many times every- where in the interval [7/8, 25/8]. The function f has been evaluated in the following points, f (1) = -0.266367400 f ( 5 4 ) = -0.295622750 f ( 3 2 ) = -0.308642948 f ( 7 4 ) = -0.307833065 f (2) = -0.296297436 f ( 9 4 ) = -0.277234152 f ( 5 2 ) = -0.253531641 f ( 11 4 ) = -0.227563149 f (3) = -0.201131426 a. Find the left Riemann sum, I (8) lR . A: n I (n) lR I (n) lR - I 1 -5.327347984e-01 1.872e-02 2 -5.626648357e-01 -1.121e-02 4 -5.624197130e-01 -1.097e-02 8 -5.582731359e-01 -6.820e-03 b. Find the right Riemann sum, I (8) rR . A: n I (n) rR I (n) rR - I 1 -4.022628521e-01 1.492e-01 2 -4.974288625e-01 5.402e-02 4 -5.298017264e-01 2.165e-02 8 -5.419641426e-01 9.489e-03 c. Find the mid-point sum, I (4) mp . A: n I (n) mp I (n) mp - I 1 -5.925948730e-01 -4.114e-02 2 -5.621745903e-01 -1.072e-02 4 -5.541265588e-01 -2.673e-03 DEPARTMENT OF APPLIED SCIENCE | UNIVERSITY OF CALIFORNIA | DAVIS,CALIFORNIA 95616 TEL: 530.752.0360 | FAX: 530.752.2444

SHW6 Combo

Embed Size (px)

Citation preview

Page 1: SHW6 Combo

COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

NO

T F

OR

DIS

TR

IBU

TIO

NDATE: March 2, 2014

DUE: March 10, 2014 at 1:00pmEME115, Solution to homework set #6.

1. We wish to solve the integral,

I =∫ 3

1f(t)dt

of a function which can be differentiated arbitrarily many times every-

where in the interval [7/8, 25/8]. The function f has been evaluated

in the following points,

f(1) = −0.266367400

f(5

4) = −0.295622750

f(3

2) = −0.308642948

f(7

4) = −0.307833065

f(2) = −0.296297436

f(9

4) = −0.277234152

f(5

2) = −0.253531641

f(11

4) = −0.227563149

f(3) = −0.201131426

a. Find the left Riemann sum, I(8)lR .

A:

n I(n)lR I

(n)lR − I

1 -5.327347984e-01 1.872e-02

2 -5.626648357e-01 -1.121e-02

4 -5.624197130e-01 -1.097e-02

8 -5.582731359e-01 -6.820e-03

b. Find the right Riemann sum, I(8)rR .

A:

n I(n)rR I

(n)rR − I

1 -4.022628521e-01 1.492e-01

2 -4.974288625e-01 5.402e-02

4 -5.298017264e-01 2.165e-02

8 -5.419641426e-01 9.489e-03

c. Find the mid-point sum, I(4)mp.

A:

n I(n)mp I

(n)mp − I

1 -5.925948730e-01 -4.114e-02

2 -5.621745903e-01 -1.072e-02

4 -5.541265588e-01 -2.673e-03

DEPARTMENT OF APPLIED SCIENCE | UNIVERSITY OF CALIFORNIA | DAVIS, CALIFORNIA 95616

TEL: 530.752.0360 | FAX: 530.752.2444

Page 2: SHW6 Combo

COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

NO

T F

OR

DIS

TR

IBU

TIO

NSolution to EME115, homework set #6. Page 2

d. Find the trapezoidal sums, I(8)tr and I

(4)tr .

A:

n I(n)tr I

(n)tr − I

1 -4.674988252e-01 8.395e-02

2 -5.300468491e-01 2.141e-02

4 -5.461107197e-01 5.343e-03

8 -5.501186392e-01 1.335e-03

e. Find the Simpson sum, I(8)S .

A:

n I(n)S I

(n)S − I

2 -5.508961904e-01 5.572e-04

4 -5.514653432e-01 -1.192e-05

8 -5.514546124e-01 -1.193e-06

f. The exact result of the integral is I = 3sinh 3

− 1sinh 1

. Rank the

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 evalu-

ating the integral

I =∫ 25/8

7/8f(t)dt

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

A:

The integral, I , has boundaries such that we can only use the

mid-point formula for n = 9, 3, 1. The best result is found for

n = 9:

I ≈1

4

8∑i=0

f(i ·1

4+ 1) = −0.6085560

2

Page 3: SHW6 Combo

COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

NO

T F

OR

DIS

TR

IBU

TIO

NSolution to EME115, homework set #6. Page 3

2. Consider the integral, I(f) =∫ 20 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.

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

A:

n I(n)rR I

(n)rR − I

1 1.407194484e+01 1.140e+01

2 7.274378264e+00 4.599e+00

4 4.703556427e+00 2.029e+00

8 3.621846515e+00 9.468e-01

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

A:

n I(n)lR I

(n)lR − I

1 0.000000000e+00 -2.675e+00

2 2.384058440e-01 -2.437e+00

4 1.185570217e+00 -1.489e+00

8 1.862853410e+00 -8.121e-01

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

A:

n I(n)tr I

(n)tr − I

1 7.035972420e+00 4.361e+00

2 3.756392054e+00 1.081e+00

4 2.944563322e+00 2.696e-01

8 2.742349962e+00 6.735e-02

e. Calculate I(n)mp (f), for n = 1, 2, 4, 8.

A:

n I(n)mp I

(n)mp − I

1 4.768116881e-01 -2.198e+00

2 2.132734589e+00 -5.423e-01

4 2.540136603e+00 -1.349e-01

8 2.641316255e+00 -3.368e-02

f. Calculate I(n)S (f), for n = 2, 4, 8 (notice that the minimum in-

tervals for Simpson is n=2).

A:

n I(n)S I

(n)S − I

1 . . . . . . . . . . . . . . . . . . . . . . . . .

2 2.663198599e+00 -1.180e-02

4 2.673953744e+00 -1.044e-03

8 2.674945509e+00 -5.174e-05

16 2.674994157e+00 -3.095e-06

3

Page 4: SHW6 Combo

COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

NO

T F

OR

DIS

TR

IBU

TIO

NSolution 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.

3. [MANDATORY] Consider the integral

I(f) =∫ t1

t0f(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 n = 2ng I(n)lR I

(n)mp = R(1)[I

(n)lR ]

0 1 1.000000000000000 . . . . . . . . . . . . . . . . . .

1 2 1.112372435695794 1.224744871391589

2 4 1.166413628918445 1.220454822141095

3 8 1.192872487446321 1.219331345974198

4 16 1.205959577787193 1.219046668128064

5 32 1.212467412138600 1.218975246490008

6 64 1.215712393603079 1.218957375067558

7 128 1.217332649905064 1.218952906207049

8 256 1.218142219417056 1.218951788929048

9 512 1.218546864511338 1.218951509605620

10 1024 1.218749152142928 1.218951439774518

11 2048 1.218850287229825 1.218951422316722

12 4096 1.218900852591053 1.218951417952280

13 8192 1.218926134726108 1.218951416861164

14 16384 1.218938775657244 1.218951416588380

15 32768 1.218945096088721 1.218951416520198

16 65536 1.218948256295915 1.218951416503110

17 131072 1.218949836397410 1.218951416498904

18 262144 1.218950626447616 1.218951416497822

19 524288 1.218951021472541 1.218951416497466

20 1048576 1.218951218985038 1.218951416497534

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

4

Page 5: SHW6 Combo

COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

NO

T F

OR

DIS

TR

IBU

TIO

NSolution to EME115, homework set #6. Page 5

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 n = 2ng I(n)tr I

(n)S = R(1)[I

(n)tr ]

0 1 1.207106781186547 . . . . . . . . . . . . . . . . . .

1 2 1.215925826289068 1.218865507989908

2 4 1.218190324215082 1.218945156857086

3 8 1.218760835094640 1.218951005387826

4 16 1.218903751611352 1.218951390450256

5 32 1.218939499050680 1.218951414863789

6 64 1.218948437059119 1.218951416395265

7 128 1.218950671633084 1.218951416491072

8 256 1.218951230281066 1.218951416497060

9 512 1.218951369943343 1.218951416497436

10 1024 1.218951404858931 1.218951416497460

11 2048 1.218951413587827 1.218951416497458

12 4096 1.218951415770053 1.218951416497462

13 8192 1.218951416315608 1.218951416497460

14 16384 1.218951416451994 1.218951416497456

15 32768 1.218951416486096 1.218951416497463

16 65536 1.218951416494603 1.218951416497439

17 131072 1.218951416496753 1.218951416497470

18 262144 1.218951416497288 1.218951416497466

19 524288 1.218951416497377 1.218951416497406

20 1048576 1.218951416497456 1.218951416497482

c. Complete the Romberg table in the code; i.e., for each n, con-

tinue 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←−.

ng

nI(n

)tr

I(n

)S

=R

(1)[I

(n)

tr]

R(1

)[I

(n)

S]=

R(2

)[I

(n)

tr]

R(2

)[I

(n)

S]=

R(3

)[I

(n)

tr]

R(3

)[I

(n)

S]=

R(4

) [I(n

)tr]

R(4

)[I

(n)

S]=

R(5

)[I

(n)

tr]

01

1.2

07

10

67

81

18

65

47

12

1.2

15

92

58

26

28

90

68

1.2

18

86

55

07

98

990

8

24

1.2

18

19

03

24

21

50

82

1.2

18

94

51

56

85

708

61

.21

89

50

46

67

815

65

38

1.2

18

76

08

35

09

46

40

1.2

18

95

10

05

38

782

61

.21

89

51

39

52

898

75

1.2

18

95

14

10

02

810

2

41

61

.21

89

03

75

16

113

52

1.2

18

95

13

90

45

025

61

.21

89

51

41

61

210

84

1.2

18

95

14

16

45

173

81

.21

89

51

41

64

769

29

53

21

.21

89

39

49

90

506

80

1.2

18

95

14

14

86

378

91

.21

89

51

41

64

913

58

1.2

18

95

14

16

49

723

51

.21

89

51

41

64

974

14

1.2

18

95

14

16

49

74

34

5

Page 6: SHW6 Combo

COPYRIGHT: NIELS GRONBECH-JENSEN, UC DAVIS, 2014

NO

T F

OR

DIS

TR

IBU

TIO

NSolution to EME115, homework set #6. Page 6

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

terms; as estimated from three consecutive (increasing) segmen-

tations 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.ng n I

(n)tr

q I(n)

S= R(1) [I

(n)tr

] q R(2)[I(n)tr

] q R(3) [I(n)tr

] q R(4) [I(n)tr

] q R(5) [I(n)tr

] q

0 1 1.207106781186547 2

1 2 1.215925826289068 1.218865507989908 4

2 4 1.218190324215082 1.96 1.218945156857086 1.218950466781565 6

3 8 1.218760835094640 1.99 1.218951005387826 3.77 1.218951395289875 1.218951410028102 8

4 16 1.218903751611352 2.00 1.218951390450256 3.92 1.218951416121084 5.48 1.218951416451738 1.218951416476929 10

5 32 1.218939499050680 2.00 1.218951414863789 3.98 1.218951416491358 5.81 1.218951416497235 7.14 1.218951416497414 1.218951416497434 12

The questions in this problem are sequential, such that each one builds

on the preceding. Attach code listing for each item.

Niels Grønbech Jensen

6

Page 7: SHW6 Combo

/* 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);}

Page 8: SHW6 Combo

/* 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);}

Page 9: SHW6 Combo

/* 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);}

Page 10: SHW6 Combo

/* 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);}