Docsity

Solving Nonlinear Equations with Newton's Method and Interpolation - Prof. Ashwani K. Kapi, Assignments of Mathematics

Toggle sidebar
bg1
Numerical Computing F’05 A. Kapila
Assignment 4 Solutions
1. (Pencil-and-paper and Matlab) Consider the three equations
x2
1+x2
2+x2
3= 1,
x2
1+x2
3=1
4,
x2
1+x2
2−4x3= 0.
These are equations of three surfaces; a sphere, a cylinder and a paraboloid. We seek the point of
intersection of these surfaces by solving them simultaneously using Newton’s method for nonlinear
systems.
As we discussed in class, (also see section 7.3 of the text), Newton’s method applies to nonlinear
systems of form
fi(x1, x2,···xn) = 0, i = 1,2,···n.
Given the nth iterate
x(n)
1
x(n)
2
···
x(n)
n
,
the method computes the increment
∆x(n)
1
∆x(n)
2
···
∆x(n)
n
by solving a related linear system
J(n)∆x(n)=−f(n),
where Jis the Jacobian matrix. The n+ 1 th iterate is then given by x(n+1) =x(n)+ ∆x(n).
(a) Compute by hand the Jacobian matrix of the given 3 ×3 system.
(b) Implement Newton’s method numerically to solve the given system. Your code must print at each
iteration the following information: the iteration number, the solution at that iteration, and the
norm of the increment to the solution at that iteration. Terminate the solution when the norm is
smaller than 10−6.Use the initial guess x1=x2=x3= 1.
You may use as a guide the Matlab routines that are available on line under the title Newton2d
scripts, or you may write your own script. If the online scripts are used, note that they are
provided in a single file for ease of access. You must put each script in an m-file of its own.
Further, the scripts must be modified to suit the problem and the display requirements stated
above.
(a) The system can be written as
f1(x1, x2, x3) = x2
1+x2
2+x2
3−1=0,
f2(x1, x2, x3) = x2
1+x2
3−1
4= 0,
f3(x1, x2, x3) = x2
1+x2
2−4x3= 0.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Solving Nonlinear Equations with Newton's Method and Interpolation - Prof. Ashwani K. Kapi and more Assignments Mathematics in PDF only on Docsity!

Numerical Computing F’05 A. Kapila Assignment 4 Solutions

  1. (Pencil-and-paper and Matlab) Consider the three equations

x^21 + x^22 + x^23 = 1 ,

x^21 + x^23 =

x^21 + x^22 − 4 x 3 = 0.

These are equations of three surfaces; a sphere, a cylinder and a paraboloid. We seek the point of intersection of these surfaces by solving them simultaneously using Newton’s method for nonlinear systems. As we discussed in class, (also see section 7.3 of the text), Newton’s method applies to nonlinear systems of form fi(x 1 , x 2 , · · · xn) = 0, i = 1, 2 , · · · n. Given the n th iterate (^) 

   

x (n) 1 x (n) 2 · · · x( nn)

the method computes the increment (^) 

   

∆x( 1 n) ∆x( 2 n) · · · ∆x( nn)

by solving a related linear system J(n)∆x(n)^ = −f (n), where J is the Jacobian matrix. The n + 1 th iterate is then given by x(n+1)^ = x(n)^ + ∆x(n).

(a) Compute by hand the Jacobian matrix of the given 3 × 3 system. (b) Implement Newton’s method numerically to solve the given system. Your code must print at each iteration the following information: the iteration number, the solution at that iteration, and the norm of the increment to the solution at that iteration. Terminate the solution when the norm is smaller than 10−^6. Use the initial guess x 1 = x 2 = x 3 = 1. You may use as a guide the Matlab routines that are available on line under the title Newton2d scripts, or you may write your own script. If the online scripts are used, note that they are provided in a single file for ease of access. You must put each script in an m-file of its own. Further, the scripts must be modified to suit the problem and the display requirements stated above.

(a) The system can be written as

f 1 (x 1 , x 2 , x 3 ) = x^21 + x^22 + x^23 − 1 = 0 ,

f 2 (x 1 , x 2 , x 3 ) = x^21 + x^23 −

f 3 (x 1 , x 2 , x 3 ) = x^21 + x^22 − 4 x 3 = 0.

The Jacobian matrix is        ∂f 1 ∂x 1

∂f 1 ∂x 2

∂f 1 ∂x 3 ∂f 2 ∂x 1

∂f 2 ∂x 2

∂f 2 ∂x 3 ∂f 3 ∂x 1

∂f 3 ∂x 2

∂f 3 ∂x 3

2 x 1 2 x 2 2 x 3 2 x 1 0 2 x 3 2 x 1 2 x 2 − 4

(b) The Newton iteration for the increment is found by solving the linear system  

2 x 1 2 x 2 2 x 3 2 x 1 0 2 x 3 2 x 1 2 x 2 − 4

(n)  

∆x 1 ∆x 2 ∆x 3

(n) = −

x^21 + x^22 + x^23 − 1 x^21 + x^23 − 1 / 4 x^21 + x^22 − 4 x 3

(n) .

The fresh iterate is given by  

x 1 x 2 x 3

(n+1)

x 1 x 2 x 3

(n)

∆x 1 ∆x 2 ∆x 3

(n) .

See the attached Matlab log for implementation.

  1. Answer each question with a brief explanation:

(a) There are arbitrarily many different mathematical functions that interpolate a given set of data points; true or false? (b) There is a unique polynomial that interpolates a given set of data points, true or false? (c) Why is interpolation by a high-degree polynomial often unsatisfactory? (d) In interpolating a continuous function by a polynomial, what key features determine the error in approximating the function by the interpolant? Under what circumstances will the error become smaller as the number of interpolation points is increased? Under what circumstances can the error be large even as the number of interpolation points is increased?

(a) True. For example, both f 1 (x) = x and f 2 (x) = x^2 interpolate (0, 0) and (1, 1). (b) False. For n data points, there is a unique polynomial interpolant of degree n− 1. However, there is an infinity of higher-degree polynomial interpolants. The example of part (a) above applies here as well. (c) Too many wiggles. (d) When f (x) is interpolated by pn− 1 (x), a polynomial of degree n − 1 , the error is given by

f (x) = pn− 1 (x) =

W (x) n!

f (n)(cx),

where W (x) = (x − x 1 )(x − x 2 ) · · · (x − xn), and cx ∈ [x 1 , xn]. Thus the error depends upon the size of the n th derivative of f, upon the number of data points, and through w on the distribution of data points. When n is increased, the error will typically become smaller if the derivative f (n)^ is moderate. However the error can rise if f (n)^ increases substantially with n.

(b) Now, consider piecewise quadratic interpolation. Again, let the spacing be h. The error of interpolation, as derived in class, is now given by

E = |f (x) − p(x)| ≤ M h^3 9

where M is the upper bound on |f ′′′(x)| = 2/x^3 on the interval [1, 7]. The bound is attained at x = 1 and is 2. Therefore, tolerance is satisfied when

2 h^3 9

≤ 10 −^6

whence h ≤ 0. 07794.

  1. (Pencil-and-paper and Matlab)

(a) Write a Matlab function DD(x,y) which accepts as input the x and y vectors corresponding to a data set (xi, yi), i = 1, 2 , · · · n, and produces as output an n × n lower triangular matrix D whose elements below the main diagonal are the divided differences. You may use the pseudo code provided in class for computing D. Your printed output should display D. Note that the diagonal elements of D are the coefficients of the Newton interpolating polynomial for the given data. (b) Find the coefficients of the Newton interpolant, p(x), for the following data.

x y 1 3. 7 2 2. 9 3 2. 7 4 1. 9 5 1. 6 6 − 2. 7 7 − 2. 8 8 − 3. 2 9 − 3. 8 10 − 4. 0

(c) Construct a cubic spline interpolant, s(x), to the data in part (b) by using the Matlab spline command. (d) On a single, labelled graph with appropriate legends, display the discrete data points, the piecewise linear interpolant, the polynomial interpolant computed in part (b) and the spline interpolant computed in part (c). List the advantages and disadvantages of each. Which of the three do you prefer and why?

(a) See the attached scripts. (b) See the attached scripts and output. (c) See the attached scripts. (d) See the figure below. In this particular example the piecewise linear interpolant seems to capture the trend of the data best. The spline comes next, but has the advantage of being smooth at the nodes, unlike the piecewise linear interpolant. The Newton polynomila has far too many wiggles, and excessive error near the ends.

1 2 3 4 5 6 7 8 9 10

−

−

−

0

2

4

6

data linear Newton spline

function [F,JF]=f3d(x)

% Input

% x: column vector (x1; x2; x3)

% Output:

% F: function (F1;F2;F3)

% JF: jacobian of F with respect to x

% Define function F

x1=x(1);

x2=x(2);

x3=x(3);

F1=x1^2+x2^2+x3^2-1;

F2=x1^2+x3^2-1/4;

F3=x1^2+x2^2-4*x3;

F=[F1;F2;F3];

% Define jacobian

J11=2x1; J12=2x2; J13=2*x3;

J21=2x1; J22=0; J23=2x3;

J31=2x1; J32=2x2; J33=-4;

JF=[J11,J12,J13;J21,J22,J23;J31,J32,J33];

% RESULTS

iter = 1 norm(increment) = 6.6667e-

iter = 2 norm(increment) = 2.6801e-

iter = 3 norm(increment) = 7.6326e-

iter = 4 norm(increment) = 6.5158e-

iter = 5 norm(increment) = 4.8156e-

iter = 6 norm(increment) = 2.6306e-

x =

4.4076e-

8.6603e-

2.3607e-

% script a4prob5.m

% % Supply data

% x=[1:10];

y=[3.7 2.9 2.7 1.9 1.6 -2.7 -2.8 -3.2 -3.8 -4.0];

n=length(x); m=length(y);

if (m ~= n), error('x and y are incompatible'); end D=DD(x,y);

% % The diagonal elements of D provide the Newton coefficients

%

% Evaluate Newton interpolant (for plotting) by using nested % multiplication

t=linspace(1,10,200); p=D(n,n);

for k=n-1:-1: p=(t-x(k)).*p+D(k,k);

end %

% Plot data, linear interpolant (lines joining data points) and the Newton

% interpolant %

plot(x,y,'ro',x,y,'b--',t,p,'k') hold on

% % Generate and plot the spline interpolant using Matlab's spline command

%

ss=spline(x,y,t); plot(t,ss,'g');

legend('data','linear','Newton','spline') %%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Matlab function DD function D=DD(x,y)

%

% Input: % n-vectors x and y representing the data set (x(i),y(i))

% % Output:

% n X n lower triangular matrix D, with divided differences along % diagonal

n=length(x); m=length(y);

if (m ~= n), error('x and y are incompatible'); end

% D=zeros(n,n);