0% found this document useful (0 votes)
18 views13 pages

U9-Newton Interpolation

Numerical methods
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views13 pages

U9-Newton Interpolation

Numerical methods
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Newton interpolation

Study the content and do the excercises in this document. Read all the comments for the instructions. It will help
you to use Matlab more optimally.

Lesson Outcomes
At the end of this lesson the student should be able to:

• Define Divided Differences


• Approximating data or functions with Newton interpolating polynomial
• Construct a Newton divided difference table and use it to determine the polynomial of least degree that
interpolates a table of pairs of values.

1. Introduction
With the determination of Lagrange-interpolating polynomials, the Lagrange functions have to be recalculated
every instance when a data pair is added. Newton interpolating polynomials are based on divided differences
and only a term is added to the existing interpolating polynomial for each data pair that is added.

2. Divided differences
Suppose that is the Lagrange polynomial that agrees with the function f at the distinct numbers
Although this polynomial is unique, alternate algebraic representations are useful in certain
situations. The divided differences of f with respect to are used to express in the form

where are constants.

To find the constant , we evaluate the polynomial at which gives

Similarly, evaluating at , we obtain

so

We now introduce the divided-difference notation:

• The zeroth divided difference of the function f with respect to is denoted by , which is just the
value of f at :

1
• The first divided difference of fwith respect to and is denoted by and is defined as

• The second divided difference, , is defined as

• The process ends with the single nth divided difference,

Take note that can be rewritten as . Therefore the interpolating takes the form

From the evaluation of and , the required constants are

for each So can be rewritten as

The table below shows the generation of the divided differences

2
Example 1
Consider the data in the given table.

Determine divided differences for the data by executing the instructions below.

% Determining divided differences


% x values in the 1st column
% y-values in 2nd column (zeroth divided difference)
clear
format long
D=[2 -3;4 1];
[n c]=size(D);
for i=3:n+1
for j=1:n-(i-2)
D(j,i)=(D(j+1,i-1)-D(j,i-1))/(D(j+i-2,1)-D(j,1));
end
end
D

Check the calculations by hand to make sure you know how it is calculated.

3
Exercise 1
Consider the data in the given table.

Determine divided differences for the data by copying, changing and executing the instructions above. Check
the calculations per hand to make sure you know how it is calculated.

% Copy, change and execute the instruction.

Example 2
Consider the data in the given table.

Determine divided differences for the data by executing the instructions below.

% Determining divided differences


% x values in the first column
% y-values in 2nd column (zeroth divided difference)
clear
D=[2 -3;4 1;5 1.5];
[n c]=size(D);
for i=3:n+1
for j=1:n-(i-2)
D(j,i)=(D(j+1,i-1)-D(j,i-1))/(D(j+i-2,1)-D(j,1));
end
end
D

Check the calculations per hand to make sure you know how it is calculated.

Exercise 2
Consider the data in the given table.

Determine divided differences for the data by copying, changing and executing the instructions above. Check
the calculations per hand to make sure you know how it is calculated.

% Copy, change and execute the instruction.

4
Exercise 3
Consider the data in the given table.

Determine divided differences for the data by copying, changing and executing the instructions above. Check
the calculations per hand to make sure you know how it is calculated.

% Copy, change and execute the instruction.

Example 3
When working with big data sets, this kind of working becomes unpractical. Determine the divided differences
for the given data in the table by executing the instructions below.

% Determining divided differences


clear
D=[2 -3;4 1;5 1.5;8 0];
[n c]=size(D);
for i=3:n+1
for j=1:n-(i-2)
D(j,i)=(D(j+1,i-1)-D(j,i-1))/(D(j+i-2,1)-D(j,1));
end
end
D

Exercise 4
Determine the divided differences for the data in the given table by copying, changing and executing the
instructions above.

% Copy, change and execute the instruction.

3. Newton interpolating polynomials


If the points for are given, the n-th degree Newton interpolating polynomial is given by

5
Example 4
Determine the Newton interpolating polynomial with least degree of one which will generate the data in
the given table and the straight line through the points. Simplify both. Determine approximate values in the
points 2, 3 and 4. Sketch the data pairs and interpolating polynomial on the same set of axes.

% Newton interpolating polynomial with degree of at least one


clear
D=[2 -3;4 1];
[n c]=size(D);
for i=3:n+1
for j=1:n-(i-2)
D(j,i)=(D(j+1,i-1)-D(j,i-1))/(D(j+i-2,1)-D(j,1));
end
end
D

According to the divided difference

The straight line through the points is

Compare it with Example 1 of Lesson 8. It is the same according to the existence and uniqueness theorem for
interpolating polynomials.

P1=@(x)D(1,2)+D(1,3)*(x-D(1,1));
P1(2)
P1(3)
P1(4)
% Coefficients of the polynomial with constant term first
syms q
eval(coeffs(P1(q)))

% Sketch data pairs


plot(D(:,1),D(:,2),' o')
hold on

6
fplot(P1,[1 5])
hold off

Exercise 5
Determine the Newton interpolating polynomial with least degree of one which will generate the data in
the given table and the straight line through the points by following the above steps. Simplify both. Determine
approximate values in the points -2, -1 and 0. Sketch the data pairs and interpolating polynomial on the same
set of axes.

% Copy, change and execute the instruction.

Compare it with Exercise 1 of Lesson 8. It is the same according to the existence and uniqueness theorem for
interpolating polynomials.

Example 5
Determine the Newton interpolating polynomial with least degree of two which will generate the data
in the given table. Determine approximate values in the points 2, 3, 4, 4.5 and 5. Sketch the data pairs and
interpolating polynomial on the same set of axis.

% Newton interpolating polynomial with degree of at least two


clear
D=[2 -3;4 1;5 1.5];
N1=newtonpol(D)
syms c
eval(coeffs(N1(c)))

According to the divided differences

PN2 = @(x) -3 + 2*(x-2) - 0.5*((x-2).*(x-4));


%PN2 = @(x)D(1,2)+D(1,3)*(x-D(1,1))+D(1,4)*(x-D(1,1)).*(x-D(2,1));
PN2(2)
PN2(3)
PN2(4)
PN2(4.5)
PN2(5)

7
% Coefficients of the polynomial with constant term first
syms q
eval(coeffs(PN2(q)))

% Sketch data pairs


plot(D(:,1),D(:,2),' o')
hold on
fplot(PN2,[1 6])
hold off

Compare it with Example 2 of Lesson 8. The polynomial is the same according to the existence and uniquenes
theorem for interpolating polynomials.

Determine the absolute difference between the approximate value for the data for according to the
Lagange- and Newton interpolating polynomials.

PL2 = lagrangepol(D)
PN2(4.7352)
PL2(4.7352)
abs(PN2(4.7352)-PL2(4.7352))

According to the existence and uniqueness theorem for interpolation polynomials, there should be no difference
between these values. As already indicated, the two polynomials are equal. The difference between the values
in the order of is due to machine rounding (rounding done by Matlab itself due to limited calculation ability
of computers).

Note that the values displayed, 1.464940480000000, are values rounded to 15 decimals.

Exercise 6
Determine the Newton interpolating polynomial with least degree of two which will generate the data in the
given table by following the above steps. Determine approximate values in the points -2, -1, 0, 0.5 and 1. Give
the values rounded off to four decimals. Sketch the data and interpolating polynomial on the same set of axes.

% Copy, change and execute the instruction.


D=[-2 1;0 3;1 -1];
N2=newtonpol(D)
syms q
eval(coeffs(N2(q)))
N2(-2)
N2(-1)
N2(0)

8
N2(0.5)
N2(1)

Compare it with Exercise 2 of Lesson 8. It is the same according to the existence and uniqueness theorem for
interpolating polynomials.

Exercise 7
Determine the Newton interpolating polynomial with least degree of three which will generate the data in
the given table by following the above steps. Determine approximate values in the points -2, -1, 0, 0.5 and 1.
Give the values rounded off to four decimals. Sketch the data and interpolating polynomial on the same set of
axes.

% Copy, change and execute the instruction.

Compare it with Exercise 3 of Lesson 8. It is the same according to the existence and uniqueness theorem for
interpolating polynomials.

Example 6
When working with big data sets, this kind of working becomes unpractical. A Matlab fuction which determines
a Newton interpolating polynomial from a given data set is given at the end of this document. To determine
a Newton interpolating polynomial with leasst degree of nine that will generate the given data set, the Matlab
function can be called by executing the instructions below. Study the code in the Matlab function and execute
the instructions below. Determine approximate values in the points 1, 3, 6, 7.25, 8.5, 11 and 13. Give the values
rounded off to four decimals. Sketch the data and interpolating polynomial on the same set of axes.

% Determine a Newton interpolating polynomial with a Matlab function.


clear
D=[2 -3;4 1;5 1.5;7 1.7;7.5 3;8 4;8.1 2;9 -2.5;10 -3;12 0];
[P9 D]=newtonpol(D)
plot(D(:,1),D(:,2),' o')
round(P9(1),4)
round(P9(3),4)
round(P9(6),4)
round(P9(7.25),4)
round(P9(8.5),4)
round(P9(11),4)
round(P9(13),4)
hold on

9
fplot(P9,[2 12])
hold off

According to the divided differences

Compare it with Example 3 of Lesson 8. It is the same according to the existence and uniqueness theorem for
interpolating polynomials.

Exercise 8
Determine the Newton interpolating polynomial with least degree of seven which will generate the data
in the given table by copying, changing and executing the instructions above. Determine approximate values in
the points -1.5, 2, 4 and 5.5. Give the values rounded off to four decimals. Sketch the data and interpolating
polynomial on the same set of axes. Which of the approximate values are reliable?

% Copy, change and execute the instruction.

Compare it with Exercise 4 of Lesson 8. It is the same according to the existence and uniqueness theorem for
interpolating polynomials.

3. Error formula (Remainder term)


The error formule (remainder term) as discussed in Lesson 8 holds for all interpolating polynomial.

4. Exercising
Exercise 9
Show that the interpolating polynomial which will generate the data in the given table, is of third degree.

10
% Give the instructions and execute them.

Exercise 10
Show that the two interpolating polynomials both generates the data in the given table and adhere to the
conditions of the existence and uniqueness theorem for interpolating polynomials.

% Give the instructions and execute them.

Exercise 11
For a function the Newton interpolating polynomial is given by

at nodes

Determine .

% Give the instructions and execute them.

Exercise 12
Complete the missing elements in the divided differences table.

11
5. Can you do the following in Matlab?
1. Newton interpolating polynomial define and approximate values in points determine.

Function
Copy and save the code of the function in a new "script" as name.m where "name" is the name of the function.
You can then call and execute it directly from the "Command Window" if it is saved in the "Current Folder", the
folder as indicated just below the toolbar. You can change the "Current Folder" by clicking on it. The function
can also be copied in a "script" or "live script" together with other instructions, including the instruction calling
the function. It just has to be at the end of the document as done here.

Newton interpolating polynomial


function [P,D]=newtonpol(D)
% Determine Newton interpolating polynomials
% D: data with x values in column 1 and y values in column 2
% [x0 y0; x1 y1; x2 y2; ...]
% Divided differences are saved in D.
% Newton interpolating polynomial is saved in P.
format long
[n c]=size(D);
% 1st divided difference is in column 3, etc.
for k=3:n+1
for j=1:n-(k-2)
D(j,k)=(D(j+1,k-1)-D(j,k-1))/(D(j+k-2,1)-D(j,1));
end
end
P=@(x)D(1,2);
for k=3:n+1
L=@(x)1;
for j=1:k-2
L=@(x)L(x).*(x-D(j,1));
end
P=@(x)P(x)+D(1,k).*L(x);
end
end

Function For Lagrange Polynomials

function P=lagrangepol(D)

12
% Determine Lagrange interpolating polynomials
% D: data with x values in column 1 and y values in column 2 [x0 y0; x1 y1; x2
y2; ...]
% Lagrange interpolating polynomial is saved in P.
format long
[n c]=size(D);
P=@(x)0;
for k=1:n
L=@(x)1;
for j=1:k-1
L=@(x)L(x).*(x-D(j,1))./(D(k,1)-D(j,1));
end
for j=k+1:n
L=@(x)L(x).*(x-D(j,1))./(D(k,1)-D(j,1));
end
P=@(x)P(x)+D(k,2).*L(x);
end
end

13

You might also like