egm6341_sol_hw_05

14
Ramin Shamshiri EGM6341, HW #5 Due 26/2/09 Homework #5 Due 26/2/09 #6 Suppose that you are to making a table of values of sin(x), 0 < x < π/2 with a stepsize of h. Assume linear interpolation is to be used with the table, and suppose the total error, including the effects due to rounding in the table entries is to be at most . What should h equal (choose it in a convenient size for actual use) and to how many significant digits should the table entries be given? Solution: The total error e(x) is the sum of the truncation error TE(x) and the round-off error R(x) ) ( ) ( ) ( x R x TE x The truncation error is given by ) )( )( ( " 2 1 ) ( 1 0 x x x x f x TE 0 and x i . Note: max | 4 / | ) )( ( | 2 1 0 h x x x x , h= | | 0 1 x x , and max| ) ( " f | = 1. The maximum roundoff error is Max R(x) = 5x10 -7 Thus we need ) ( ) ( ) ( x R x TE x < 8 / 2 h + 5x10 -7 < 10 -6 which gives h < 2x10 -3 .

Upload: raminshamshiri

Post on 16-Apr-2015

70 views

Category:

Documents


2 download

DESCRIPTION

EGM6341_Sol_HW_05

TRANSCRIPT

Page 1: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

Homework #5

Due 26/2/09

#6 Suppose that you are to making a table of values of sin(x), 0 < x < π/2 with a stepsize of h. Assume linear interpolation is to be used with the table, and suppose the total error, including the effects due to rounding in

the table entries is to be at most 𝟏𝟎−𝟔. What should h equal (choose it in a convenient size for actual use) and to how many significant digits should the table entries be given? Solution: The total error e(x) is the sum of the truncation error TE(x) and the round-off error R(x)

)()()( xRxTEx

The truncation error is given by

))()(("2

1)( 10 xxxxfxTE 0 and xi.

Note: max | 4/|))((| 210 hxxxx , h= || 01 xx , and max| )(" f | = 1.

The maximum roundoff error is

Max R(x) = 5x10-7

Thus we need

)()()( xRxTEx < 8/2h + 5x10-7 < 10

-6

which gives h < 2x10-3

.

Page 2: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

#11 Let 𝐱𝟎 , … , 𝐱𝐧 be distinct real points, and consider the following interpolation problem. Choose a function

𝐏𝐧 𝐱 = 𝐂𝐣𝐞𝐣𝐱

𝐧

𝐣=𝟎

such that 𝐏𝐧 𝐱 = 𝐲𝐢 𝐢 = 𝟎,𝟏, … , 𝐧 with the {yi} given data. Show there is a unique choice of c0,…cn. Hint: the problem can be reduced to that of ordinary polynomial interpolation.

Solution:

Let z = ex. Then the polynomial becomes

)()(00

zpzcecxp nj

j

n

j

jxj

n

jn

…(the rest of the proof follows naturally).

Page 3: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

#18 Do an inverse interpolation problem using the table for J0(x) given in Section 3.2

x f

2 0.223890779

2.1 0.16660698

2.2 0.110362267

2.3 0.055539784

2.4 0.002507683

2.5 -0.048387764

2.6 -0.096804954

2.7 -0.14244937

2.8 -0.185036033

2.9 -0.224311548

Find the value of x for which J0(x)=0, that is, calculate an accurate estimate of the root. Estimate your accuracy, and compare this with the actual value x=2.4048255577. Solution:

f x Dx D2x D

3x D

4x D

5x Linear Quad Cubic 4th order

5th

order

0.223891 2 -1.74569 0.28407 -0.7793 0.7648 -1.672 2.39084 2.40144 2.40465 2.404825 2.4048256

0.166607 2.1 -1.77794 0.41528 -0.9487 1.2202 -2.787 2.39622 2.40385 2.40482 2.404826 2.4048255

0.110362 2.2 -1.82407 0.57096 -1.2110 1.9545 -4.946 2.40131 2.40481 2.40483 2.404825 2.4048257

0.05554 2.3 -1.88565 0.76321 -1.6159 3.2050 -9.336 2.40473 2.40483 2.40482 2.404826 2.4048252

0.002508 2.4 -1.96497 1.00939 -2.2505 5.4511 -18.826 2.40493 2.40481 2.40483 2.404822

-0.04838 2.5 -2.06521 1.33562 -3.2728 9.7211 2.40008 2.40633 2.40415 2.40535

-0.0968 2.6 -2.19085 1.78286 -4.9830

-0.14245 2.7 -2.34815 2.41824

-0.18504 2.8 -2.54612

-0.22431 2.9

Dkx= kth order divided difference;

I have discussed the procedure for this problem in class. The inverse interpolation can be carried out as long as the function is monotonic in that region. The result using the 5th order polynomial fit is 2.40482565. The difference between 4th and 5th order polynomial fittings is 3.56E-7. Since 5

x is still reliable, we believe that the fifth order polynomial fit is reliable. Thus the error in the 5th order fit is no higher than 3.6E-7. NOTE: I used Excel to do the computation; it automatically keeps 15 decimal places. Comparing with the exact result given (2.4048255577), the relative error is 3.95E-8. Thus the interpolation result is very accurate.

Page 4: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

#20 Consider the following table of values for

J0(x) = 1/2 ( )2

J xx

Taken from Abramowitz and Stegun (1964, Chap. 10)

x J0(x) x J0(x)

0 1 0.7 0.92031

0.1 0.99833 0.8 0.8967

0.2 0.99335 0.9 0.87036

0.3 0.98507 1 0.84147

0.4 0.97355 1.1 0.81019

0.5 0.95885 1.2 0.7767

0.6 0.94107 1.3 0.7412

Based on the rounding error in the table entries, what should be the max degree polynomial that used to interpolate with the table? Solution:

x f f 2f

3f

4f

0 1 0.00167 -0.00331 -1E-05 5E-05

0.1 0.99833 0.00498 -0.00330 -6E-05 0

0.2 0.99335 0.00828 -0.00324 -6E-05 4E-05

0.3 0.98507 0.01152 -0.00318 -0.0001 0

0.4 0.97355 0.01470 -0.00308 -1E-04 3E-05

0.5 0.95885 0.01778 -0.00298 -0.00013 -1E-05

0.6 0.94107 0.02076 -0.00285 -0.00012 6E-05

0.7 0.92031 0.02361 -0.00273 -0.00018 -2E-05

0.8 0.89670 0.02634 -0.00255 -0.00016 2E-05

0.9 0.87036 0.02889 -0.00239 -0.00018

1 0.84147 0.03128 -0.00221

1.1 0.81019 0.03349 -0.00201

1.2 0.77670 0.03550

1.3 0.74120

It is clear that 4f exhibits oscillation which is due to the noise in the data f(xi). Hence 4

f is not reliable in this case. Thus we should use at most a polynomial of degree 3 in interpolating the data. Note: the error in 4

f comes from the round off error in f itself since it only has 5 significant figures. This is an excellent example showing that the rounding also affects the order of polynomials that we can use.

Page 5: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

#21 Following data are taken from a polynomial of degree < 5. What is the degree of the polynomial?

x f

-2 -5

-1 1

0 1

1 1

2 7

3 25

Solution: The degree of polynomial is 3.

𝑷 𝒙 = 𝒙𝟑 − 𝒙 + 𝟏 %input x(i) data x=[-2;-1;0;1;2;3]; %input y(i) data y=[-5;1;1;1;7;25]; p1=polyfit(x,y,1);p2=polyfit(x,y,2);p3=polyfit(x,y,3);p4=polyfit(x,y,4); %evaluating p1,p2,p3,p4 for x(i) data % Initialize y1 to y4 and set all cells to zeros y1=zeros(6,1); y2=zeros(6,1); y3=zeros(6,1); y4=zeros(6,1); % Evaluating polynimails for i=1:6 y1(i,1)=polyval(p1,x(i)); y2(i,1)=polyval(p2,x(i)); y3(i,1)=polyval(p3,x(i)); y4(i,1)=polyval(p4,x(i)); end %generating errors e1=y-y1 e2=y-y2 e3=y-y3 e4=y-y4

e1 e2 e3 e4

2.0000 -3.0000 0 0

3.2000 4.2000 0 0

-1.6000 2.4000 0 0

-6.4000 -2.4000 0 0

-5.2000 -4.2000 0 0

8.0000 3.0000 0 0

y1 y2 y3 y4

-7.0000 -2.0000 -5.0000 -5.0000

-2.2000 -3.2000 1.0000 1.0000

2.6000 -1.4000 1.0000 1.0000

7.4000 3.4000 1.0000 1.0000

12.2000 11.2000 7.0000 7.0000

17.0000 22.0000 25.0000 25.0000

x f f

2f

3f

4f

-2 -5 6 -6 6 0

-1 1 0 0 6 0

0 1 0 6 6

1 1 6 12

2 7 18

3 25

Clearly the order of the polynomial is 3 since 4f =0 .

Page 6: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

#22 The following data have noise in them that is large relative to rounding error. Find the noise and change the data appropriately. Only the function values are given since the node points are unnecessary for computing the forward difference table.

304319 419327 545811 683100

326313 443655 572433 711709

348812 468529 599475 740756

371806 493852 626909 770188

395285 519615 654790 800000

Solution %input f(i) fi=[304319;326313;348812;371806;395285;419327;443655;468529;493852;519615;545811;572433;599475;626909;654790;683100;711709;740756;770188;800000]; %initializing Delta f(i) dfi=zeros(19,1); d2fi=zeros(18,1); d3fi=zeros(17,1);d4fi=zeros(16,1);d5fi=zeros(15,1);d6fi=zeros(14,1);d7fi=zeros(13,1);d8fi=zeros(12,1);d9fi=zeros(11,1); for i=1:19 dfi(i)=fi(i+1)-fi(i); end; for i=1:18 d2fi(i)=dfi(i+1)-dfi(i); end; for i=1:17 d3fi(i)=d2fi(i+1)-d2fi(i); end; for i=1:16 d4fi(i)=d3fi(i+1)-d3fi(i); end; for i=1:15 d5fi(i)=d4fi(i+1)-d4fi(i); end; for i=1:14 d6fi(i)=d5fi(i+1)-d5fi(i); end; for i=1:13 d7fi(i)=d6fi(i+1)-d6fi(i); end; for i=1:12 d8fi(i)=d7fi(i+1)-d7fi(i); end; for i=1:11 d9fi(i)=d8fi(i+1)-d8fi(i); end;

fi dfi d2fi d3fi d4fi d5fi d6fi d7fi d8fi d9fi

304319 326313 21994 348812 22499 505 371806 22994 495 -10 395285 23479 485 -10 0 419327 24042 563 78 88 88 443655 24328 286 -277 -355 -443 -531 468529 24874 546 260 537 892 1335 1866 493852 25323 449 -97 -357 -894 -1786 -3121 -4987 519615 25763 440 -9 88 445 1339 3125 6246 11233 545811 26196 433 -7 2 -86 -531 -1870 -4995 -11241 572433 26622 426 -7 0 -2 84 615 2485 7480 599475 27042 420 -6 1 1 3 -81 -696 -3181

Page 7: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

626909 27434 392 -28 -22 -23 -24 -27 54 750 654790 27881 447 55 83 105 128 152 179 125 683100 28310 429 -18 -73 -156 -261 -389 -541 -720 711709 28609 299 -130 -112 -39 117 378 767 1308 740756 29047 438 139 269 381 420 303 -75 -842 770188 29432 385 -53 -192 -461 -842 -1262 -1565 -1490 800000 29812 380 -5 48 240 701 1543 2805 4370

Page 8: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

f f 2f

3f

3f

_guessed 3f _noise

1st two

error diff

3rd

error

304319

21994

326313 505

22499 -10 -10 0 0 0

348812 495

22994 -10 -9.6875 -0.3125 0 -0.3125

371806 485

23479 78 -9.375 87.375 89 -1.625

395285 563

24042 -277 -9.0625 -267.938 -267 -0.9375

419327 286

24328 260 -8.75 268.75 267 1.75

443655 546

24874 -97 -8.4375 -88.5625 -89 0.4375

468529 449

25323 -9 -8.125 -0.875 0 -0.875

493852 440

25763 -7 -7.8125 0.8125 0

519615 433

26196 -7 -7.5 0.5 0

545811 426

26622 -6 -7.1875 1.1875 0 1.1875 0

572433 420

27042 -28 -6.875 -21.125 0 -21.125 -20

599475 392

27434 55 -6.5625 61.5625 0 61.5625 60

626909 447

27881 -18 -6.25 -11.75 48 -59.75 -60

654790 429

28310 -130 -5.9375 -124.063 -144 19.9375 20

Page 9: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

683100 299

28609 139 -5.625 144.625 144 0.625 0

711709 438

29047 -53 -5.3125 -47.6875 -48 0.3125

740756 385

29432 -5 -5 0 0

770188 380

29812

800000

f, 2f, and 3f can be easily computed.

Alternating signs are observed in 3f but not in 2f. Using the pattern (,-3,3,-) in 3f, we can see obviously that

there are two places where f has isolated errors in the data. They correspond to 3f =…78, -277, 260, -97… and ….-

130, 139… Further examination of the data indicates that in 3f =…-28, 55,… may also correspond to an isolated

error in the data. However, this last one overlaps with the 2nd error in 3f; it is hard to discern from 3f column.

Despite the presence of error/noise, 3f still has finite values in other places that we can identify. Using a linear

interpolation for 3f (from -10 to -5 based on the first entry and last entry of the 3f column), a guessed 3f is

obtained and listed as 3f_guessed in the above table. The difference, 3f -3f_guessed is listed as 3f _noise.

From 3f _noise, we see that 1 =-89 and 2 =-48. Subtracting the contribution due to these two errors from 3f, a

difference (listed as “diff”) is obtained. This “diff” can be used to detect the third error; we easily deduce that 3 =20. Hence the corrected data are:

419327 419238

626909 626929

683100 683052

Page 10: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

#23 For 2( ) 1/ (1 ), 5 5f x x x , produce pn(x) using n+1 evenly spaced nodes on [-5, 5]. Calculate pn(x)

at a large number of points, and graph it or its error on [-5, 5]. As in Fig. 3.6 Answer: For n=5

x(i) f(x) df(x) d2f(x) d3f(x) d4f(x) d5f(x)

-5 0.038462 0.061538 0.33846 -0.73846 0.73846 0 -3 0.1 0.4 -0.4 0 0.73846 -1 0.5 0 -0.4 0.73846 1 0.5 -0.4 0.33846 3 0.1 -0.06154 5 0.038462

% input x(i) x=-5:0.1:5;x=x'; n=5; X=-5:10/n:5;X=X'; FX=zeros(6,1); dF0X=zeros(5,1); d2F0X=zeros(4,1); d3F0X=zeros(3,1); d4F0X=zeros(2,1); d5F0X=zeros(1,1); for i=1:6 FX(i)=1/(1+(X(i))^2); end for i=1:5 dF0X(i)=FX(i+1)-FX(i); end for i=1:4 d2F0X(i)=dF0X(i+1)-dF0X(i); end for i=1:3 d3F0X(i)=d2F0X(i+1)-d2F0X(i); end for i=1:2 d4F0X(i)=d3F0X(i+1)-d3F0X(i); end for i=1:1 d5F0X(i)=d4F0X(i+1)-d4F0X(i); end % calculating f(xi)=1/(1+(x(i))^2) and s(i) fx=zeros(101,1); s=zeros(101,1); p5x=zeros(101,1); for i=1:101 fx(i)=1/(1+(x(i))^2); s(i)=(x(i)-x(1))/2; p5x(i)=FX(1)+(s(i)*dF0X(1))+(((s(i))*(s(i)-1)*(d2F0X(1)))/2)+(((s(i))*(s(i)-1)*(s(i)-2)*(d3F0X(1)))/6)+(((s(i))*(s(i)-1)*(s(i)-2)*(s(i)-3)*(d4F0X(1)))/24); end

Page 11: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

x(i) f(x) s p5(x) x(i) f(x) s p5(x) -5 0.038462 0 0.038462 0.1 0.9901 2.55 0.56662 -4.9 0.039984 0.05 0.013693 0.2 0.96154 2.6 0.56454 -4.8 0.041597 0.1 -0.00692 0.3 0.91743 2.65 0.56109 -4.7 0.043309 0.15 -0.0236 0.4 0.86207 2.7 0.55628 -4.6 0.045126 0.2 -0.03657 0.5 0.8 2.75 0.55012 -4.5 0.047059 0.25 -0.04603 0.6 0.73529 2.8 0.54263 -4.4 0.049116 0.3 -0.05221 0.7 0.67114 2.85 0.53385 -4.3 0.051308 0.35 -0.05531 0.8 0.60976 2.9 0.52379 -4.2 0.053648 0.4 -0.05552 0.9 0.55249 2.95 0.51249 -4.1 0.056148 0.45 -0.05305 1 0.5 3 0.5 -4 0.058824 0.5 -0.04808 1.1 0.45249 3.05 0.48635 -3.9 0.06169 0.55 -0.0408 1.2 0.40984 3.1 0.4716 -3.8 0.064767 0.6 -0.0314 1.3 0.37175 3.15 0.4558 -3.7 0.068074 0.65 -0.02005 1.4 0.33784 3.2 0.439 -3.6 0.071633 0.7 -0.00692 1.5 0.30769 3.25 0.42127 -3.5 0.075472 0.75 0.007813 1.6 0.2809 3.3 0.40268 -3.4 0.079618 0.8 0.023988 1.7 0.25707 3.35 0.38329 -3.3 0.084104 0.85 0.041446 1.8 0.23585 3.4 0.36319 -3.2 0.088968 0.9 0.060034 1.9 0.21692 3.45 0.34245 -3.1 0.094251 0.95 0.0796 2 0.2 3.5 0.32115 -3 0.1 1 0.1 2.1 0.18484 3.55 0.2994 -2.9 0.10627 1.05 0.12109 2.2 0.17123 3.6 0.27728 -2.8 0.11312 1.1 0.14274 2.3 0.15898 3.65 0.25489 -2.7 0.12063 1.15 0.16482 2.4 0.14793 3.7 0.23234 -2.6 0.12887 1.2 0.18719 2.5 0.13793 3.75 0.20974 -2.5 0.13793 1.25 0.20974 2.6 0.12887 3.8 0.18719 -2.4 0.14793 1.3 0.23234 2.7 0.12063 3.85 0.16482 -2.3 0.15898 1.35 0.25489 2.8 0.11312 3.9 0.14274 -2.2 0.17123 1.4 0.27728 2.9 0.10627 3.95 0.12109 -2.1 0.18484 1.45 0.2994 3 0.1 4 0.1 -2 0.2 1.5 0.32115 3.1 0.094251 4.05 0.0796 -1.9 0.21692 1.55 0.34245 3.2 0.088968 4.1 0.060034 -1.8 0.23585 1.6 0.36319 3.3 0.084104 4.15 0.041446 -1.7 0.25707 1.65 0.38329 3.4 0.079618 4.2 0.023988 -1.6 0.2809 1.7 0.40268 3.5 0.075472 4.25 0.007813 -1.5 0.30769 1.75 0.42127 3.6 0.071633 4.3 -0.00692 -1.4 0.33784 1.8 0.439 3.7 0.068074 4.35 -0.02005 -1.3 0.37175 1.85 0.4558 3.8 0.064767 4.4 -0.0314 -1.2 0.40984 1.9 0.4716 3.9 0.06169 4.45 -0.0408 -1.1 0.45249 1.95 0.48635 4 0.058824 4.5 -0.04808 -1 0.5 2 0.5 4.1 0.056148 4.55 -0.05305 -0.9 0.55249 2.05 0.51249 4.2 0.053648 4.6 -0.05552 -0.8 0.60976 2.1 0.52379 4.3 0.051308 4.65 -0.05531 -0.7 0.67114 2.15 0.53385 4.4 0.049116 4.7 -0.05221 -0.6 0.73529 2.2 0.54263 4.5 0.047059 4.75 -0.04603 -0.5 0.8 2.25 0.55012 4.6 0.045126 4.8 -0.03657 -0.4 0.86207 2.3 0.55628 4.7 0.043309 4.85 -0.0236 -0.3 0.91743 2.35 0.56109 4.8 0.041597 4.9 -0.00692 -0.2 0.96154 2.4 0.56454 4.9 0.039984 4.95 0.013693 -0.1 0.9901 2.45 0.56662 5 0.038462 5 0.038462 0 1 2.5 0.56731

Page 12: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

Page 13: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

Page 14: EGM6341_Sol_HW_05

Ramin Shamshiri EGM6341, HW #5 Due 26/2/09

n x f

0 2 0.223890779

1 2.1 0.16660698

2 2.2 0.110362267

3 2.3 0.055539784

4 2.4 0.002507683

5 2.404808135 0 6 2.5 -0.048387764

7 2.6 -0.096804954

8 2.7 -0.14244937

9 2.8 -0.185036033

10 2.9 -0.224311548