06.09.2021 Views

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 3. INTERPOLATION 104<br />

Exercise 3.1-6: Consider a function f(x) such that f(2) = 1.5713, f(3) =<br />

1.5719,f(5) = 1.5738, and f(6) = 1.5751. Estimate f(4) us<strong>in</strong>g a second degree <strong>in</strong>terpolat<strong>in</strong>g<br />

polynomial (<strong>in</strong>terpolat<strong>in</strong>g the first three data po<strong>in</strong>ts) and a third degree <strong>in</strong>terpolat<strong>in</strong>g<br />

polynomial (<strong>in</strong>terpolat<strong>in</strong>g the first four data po<strong>in</strong>ts). Round the f<strong>in</strong>al results to four decimal<br />

places. Is there any advantage here <strong>in</strong> us<strong>in</strong>g a third degree <strong>in</strong>terpolat<strong>in</strong>g polynomial?<br />

<strong>Julia</strong> code for Newton <strong>in</strong>terpolation<br />

Consider the follow<strong>in</strong>g f<strong>in</strong>ite difference table.<br />

x f(x) f[x i ,x i+1 ] f[x i−1 ,x i ,x i+1 ]<br />

x 0 y 0<br />

y 1 −y 0<br />

x 1 −x 0<br />

= f[x 0 ,x 1 ]<br />

f[x<br />

x 1 y 1 ,x 2 ]−f[x 0 ,x 1 ]<br />

1 x 2 −x 0<br />

= f[x 0 ,x 1 ,x 2 ]<br />

y 2 −y 1<br />

x 2 −x 1<br />

= f[x 1 ,x 2 ]<br />

x 2 y 2<br />

Table 3.1: Divided differences for three data po<strong>in</strong>ts<br />

There are 2+1=3divided differences <strong>in</strong> the table, not count<strong>in</strong>g the 0th divided differences.<br />

In general, the number of divided differences to compute is 1+... + n = n(n +1)/2.<br />

However, to construct Newton’s form of the <strong>in</strong>terpolat<strong>in</strong>g polynomial, we need only n divided<br />

differences and the 0th divided difference y 0 . These numbers are displayed <strong>in</strong> red <strong>in</strong><br />

Table 3.1. The important observation is, even though all the divided differences have to be<br />

computed <strong>in</strong> order to get the ones needed for Newton’s form, they do not have to be all<br />

stored. The follow<strong>in</strong>g <strong>Julia</strong> code is based on an efficient algorithm that goes through the<br />

divided difference calculations recursively, and stores an array of size m = n +1 at any given<br />

time. In the f<strong>in</strong>al iteration, this array has the divided differences needed for Newton’s form.<br />

Let’s expla<strong>in</strong> the idea of the algorithm us<strong>in</strong>g the simple example of Table 3.1. The code<br />

creates an array a =(a 0 ,a 1 ,a 2 ) of size m = n +1, which is three <strong>in</strong> our example, and sets<br />

a 0 = y 0 ,a 1 = y 1 ,a 2 = y 2 .<br />

(The code actually numbers the components of the array start<strong>in</strong>g at 1, not 0. We keep it 0<br />

here so that the discussion uses the familiar divided difference formulas.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!