You are on page 1of 14

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) TE ( x) R( x)
The truncation error is given by

TE ( x)

Note:

1
f "( )( x x 0 )( x x1 )
2

max | | ( x x 0 )( x x1 ) | h

/4,

h= | x1

and xi.

x 0 | , and max| f " ( )

| = 1.

The maximum roundoff error is


Max R(x) = 5x10

-7

Thus we need

( x) TE ( x) R( x) < h 2 / 8 + 5x10-7
which gives

< 10

-6

-3

h < 2x10 .

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

j 0

j 0

pn ( x) c j e jx c j z j pn ( z )
(the rest of the proof follows naturally).

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
2
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9

f
0.223890779
0.16660698
0.110362267
0.055539784
0.002507683
-0.048387764
-0.096804954
-0.14244937
-0.185036033
-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:
5th
f

Dx

D x

Dx

Dx

Dx

Linear

Quad

Cubic

4th order

order

0.223891

-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 5x 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.

Ramin Shamshiri

EGM6341, HW #5

Due 26/2/09

#20

Consider the following table of values for

J0(x) =

2x

J1/2 ( x)

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


x
0
0.1
0.2
0.3
0.4
0.5
0.6

J0(x)
1
0.99833
0.99335
0.98507
0.97355
0.95885
0.94107

x
0.7
0.8
0.9
1
1.1
1.2
1.3

J0(x)
0.92031
0.8967
0.87036
0.84147
0.81019
0.7767
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
2

f
3

f
4

0.00167

-0.00331

-1E-05

5E-05

0.1

0.99833

0.00498

-0.00330

-6E-05

0.2

0.99335

0.00828

-0.00324

-6E-05

4E-05

0.3

0.98507

0.01152

-0.00318

-0.0001

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

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 4f 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 4f 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.

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
-2
-1
0
1
2
3

f
-5
1
1
1
7
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
2.0000
3.2000
-1.6000
-6.4000
-5.2000
8.0000
x

e2
-3.0000
4.2000
2.4000
-2.4000
-4.2000
3.0000

e3
0
0
0
0
0
0

e4
0
0
0
0
0
0

y1
-7.0000
-2.2000
2.6000
7.4000
12.2000
17.0000
f

y2
-2.0000
-3.2000
-1.4000
3.4000
11.2000
22.0000

y3
-5.0000
1.0000
1.0000
1.0000
7.0000
25.0000

y4
-5.0000
1.0000
1.0000
1.0000
7.0000
25.0000

-2

-5

-6

-1

12

18

25

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

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
326313
348812
371806
395285

419327
443655
468529
493852
519615

545811
572433
599475
626909
654790

683100
711709
740756
770188
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
304319
326313
348812
371806
395285
419327
443655
468529
493852
519615
545811
572433
599475

dfi

Ramin Shamshiri

21994
22499
22994
23479
24042
24328
24874
25323
25763
26196
26622
27042

d2fi

505
495
485
563
286
546
449
440
433
426
420

d3fi

-10
-10
78
-277
260
-97
-9
-7
-7
-6

d4fi

0
88
-355
537
-357
88
2
0
1

d5fi

88
-443
892
-894
445
-86
-2
1

EGM6341, HW #5

d6fi

-531
1335
-1786
1339
-531
84
3

d7fi

1866
-3121
3125
-1870
615
-81

d8fi

-4987
6246
-4995
2485
-696

d9fi

11233
-11241
7480
-3181

Due 26/2/09

626909
654790
683100
711709
740756
770188
800000

Ramin Shamshiri

27434
27881
28310
28609
29047
29432
29812

392
447
429
299
438
385
380

-28
55
-18
-130
139
-53
-5

-22
83
-73
-112
269
-192
48

-23
105
-156
-39
381
-461
240

EGM6341, HW #5

-24
128
-261
117
420
-842
701

-27
152
-389
378
303
-1262
1543

54
179
-541
767
-75
-1565
2805

750
125
-720
1308
-842
-1490
4370

Due 26/2/09

f
_guessed
3

1st two
error

f _noise
3

3rd
error

diff

304319
21994
326313

505
22499

348812

-10

-10

-10

-9.6875

-0.3125

-0.3125

78

-9.375

87.375

89

-1.625

-277

-9.0625

-267.938

-267

-0.9375

260

-8.75

268.75

267

1.75

-97

-8.4375

-88.5625

-89

0.4375

-9

-8.125

-0.875

-0.875

-7

-7.8125

0.8125

-7

-7.5

0.5

-6

-7.1875

1.1875

1.1875

-28

-6.875

-21.125

-21.125

-20

55

-6.5625

61.5625

61.5625

60

-18

-6.25

-11.75

48

-59.75

-60

-130

-5.9375

-124.063

-144

19.9375

20

495
22994

371806

485
23479

395285

563
24042

419327

286
24328

443655

546
24874

468529

449
25323

493852

440
25763

519615

433
26196

545811

426
26622

572433

420
27042

599475

392
27434

626909

447
27881

654790

429
28310

Ramin Shamshiri

EGM6341, HW #5

Due 26/2/09

683100

299
28609

711709

139

-5.625

144.625

144

0.625

-53

-5.3125

-47.6875

-48

0.3125

-5

-5

438
29047

740756

385
29432

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

Ramin Shamshiri

EGM6341, HW #5

Due 26/2/09

#23
For f ( x) 1/ (1 x 2 ), 5 x 5 , 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)
-5
-3
-1
1
3
5

f(x)
0.038462
0.1
0.5
0.5
0.1
0.038462

df(x)
0.061538
0.4
0
-0.4
-0.06154

d2f(x)
0.33846
-0.4
-0.4
0.33846

d3f(x)
-0.73846
0
0.73846

d4f(x)
0.73846
0.73846

d5f(x)
0

% 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

Ramin Shamshiri

EGM6341, HW #5

Due 26/2/09

x(i)
-5
-4.9
-4.8
-4.7
-4.6
-4.5
-4.4
-4.3
-4.2
-4.1
-4
-3.9
-3.8
-3.7
-3.6
-3.5
-3.4
-3.3
-3.2
-3.1
-3
-2.9
-2.8
-2.7
-2.6
-2.5
-2.4
-2.3
-2.2
-2.1
-2
-1.9
-1.8
-1.7
-1.6
-1.5
-1.4
-1.3
-1.2
-1.1
-1
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
0

Ramin Shamshiri

f(x)
0.038462
0.039984
0.041597
0.043309
0.045126
0.047059
0.049116
0.051308
0.053648
0.056148
0.058824
0.06169
0.064767
0.068074
0.071633
0.075472
0.079618
0.084104
0.088968
0.094251
0.1
0.10627
0.11312
0.12063
0.12887
0.13793
0.14793
0.15898
0.17123
0.18484
0.2
0.21692
0.23585
0.25707
0.2809
0.30769
0.33784
0.37175
0.40984
0.45249
0.5
0.55249
0.60976
0.67114
0.73529
0.8
0.86207
0.91743
0.96154
0.9901
1

s
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
0.5
0.55
0.6
0.65
0.7
0.75
0.8
0.85
0.9
0.95
1
1.05
1.1
1.15
1.2
1.25
1.3
1.35
1.4
1.45
1.5
1.55
1.6
1.65
1.7
1.75
1.8
1.85
1.9
1.95
2
2.05
2.1
2.15
2.2
2.25
2.3
2.35
2.4
2.45
2.5

p5(x)
0.038462
0.013693
-0.00692
-0.0236
-0.03657
-0.04603
-0.05221
-0.05531
-0.05552
-0.05305
-0.04808
-0.0408
-0.0314
-0.02005
-0.00692
0.007813
0.023988
0.041446
0.060034
0.0796
0.1
0.12109
0.14274
0.16482
0.18719
0.20974
0.23234
0.25489
0.27728
0.2994
0.32115
0.34245
0.36319
0.38329
0.40268
0.42127
0.439
0.4558
0.4716
0.48635
0.5
0.51249
0.52379
0.53385
0.54263
0.55012
0.55628
0.56109
0.56454
0.56662
0.56731

x(i)
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
2.9
3
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4
4.1
4.2
4.3
4.4
4.5
4.6
4.7
4.8
4.9
5

EGM6341, HW #5

f(x)
0.9901
0.96154
0.91743
0.86207
0.8
0.73529
0.67114
0.60976
0.55249
0.5
0.45249
0.40984
0.37175
0.33784
0.30769
0.2809
0.25707
0.23585
0.21692
0.2
0.18484
0.17123
0.15898
0.14793
0.13793
0.12887
0.12063
0.11312
0.10627
0.1
0.094251
0.088968
0.084104
0.079618
0.075472
0.071633
0.068074
0.064767
0.06169
0.058824
0.056148
0.053648
0.051308
0.049116
0.047059
0.045126
0.043309
0.041597
0.039984
0.038462

s
2.55
2.6
2.65
2.7
2.75
2.8
2.85
2.9
2.95
3
3.05
3.1
3.15
3.2
3.25
3.3
3.35
3.4
3.45
3.5
3.55
3.6
3.65
3.7
3.75
3.8
3.85
3.9
3.95
4
4.05
4.1
4.15
4.2
4.25
4.3
4.35
4.4
4.45
4.5
4.55
4.6
4.65
4.7
4.75
4.8
4.85
4.9
4.95
5

p5(x)
0.56662
0.56454
0.56109
0.55628
0.55012
0.54263
0.53385
0.52379
0.51249
0.5
0.48635
0.4716
0.4558
0.439
0.42127
0.40268
0.38329
0.36319
0.34245
0.32115
0.2994
0.27728
0.25489
0.23234
0.20974
0.18719
0.16482
0.14274
0.12109
0.1
0.0796
0.060034
0.041446
0.023988
0.007813
-0.00692
-0.02005
-0.0314
-0.0408
-0.04808
-0.05305
-0.05552
-0.05531
-0.05221
-0.04603
-0.03657
-0.0236
-0.00692
0.013693
0.038462

Due 26/2/09

Ramin Shamshiri

EGM6341, HW #5

Due 26/2/09

Ramin Shamshiri

EGM6341, HW #5

Due 26/2/09

Ramin Shamshiri

n
0
1
2
3
4

x
2
2.1
2.2
2.3
2.4

f
0.223890779
0.16660698
0.110362267
0.055539784
0.002507683

2.404808135

6
7
8
9
10

2.5
2.6
2.7
2.8
2.9

-0.048387764
-0.096804954
-0.14244937
-0.185036033
-0.224311548

EGM6341, HW #5

Due 26/2/09

You might also like