Docsity

Newton's Method in MATLAB: Writing a Function and Analyzing Convergence - Prof. Victor E. , Lab Reports of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
CHE/COSC/MATH 4340-01 Numerical Analysis
Computer Laboratory Assignment 04
Due Date: Tuesday, 02/24/09
Write a well documented report of what you did and the answers for all the problems below. It should
contain your name and the problem number.
Problem 1
Write a MATLAB function to find the solution of f(x) = 0 using Newton’s Method. Recall
Algorithm 1 The pseudocode for Newton’s Method
Input: x0,M,
δ
,
ε
,f(x),f′(x)
Set v=f(x0)
if |v| ≤
ε
then stop
for k=1 to Mdo
x1=x0−v/f′(x0)
v=f(x1)
if |x1−x0| ≤
δ
or |v| ≤
ε
then stop
x0=x1
end for
You should write the function in the following form,
[ir,r,err1,err2,numiter]=newton(func1,func2,xinit,delta,eps,maxiter)
where the input arguments
•func1 is an inline function which evaluates the function,
•func2 is an inline function which evaluates the function’s derivative,
•xinit is the initial guess,
•delta is the desired tolerance for |x1−x0|,
•eps is the desired tolerance for |v|,
•maxiter is the maximum number of Newton iterations allowed.
The output arguments
•ir is an integer indicating whether or not the Newton’s iteration is converged, use ir=0 if it
converges or ir=-1 otherwise,
•ris the resulting zero of f,
•err1 is the actual error |x1−x0|at the final stage of iteration,
•err2 is the actual value of |f(r)|,
•numiter is the number of iterations used.
1
pf2

Partial preview of the text

Download Newton's Method in MATLAB: Writing a Function and Analyzing Convergence - Prof. Victor E. and more Lab Reports Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

CHE/COSC/MATH 4340-01 Numerical Analysis

Computer Laboratory Assignment 04 Due Date: Tuesday, 02/24/

Write a well documented report of what you did and the answers for all the problems below. It should contain your name and the problem number.

Problem 1

Write a MATLAB function to find the solution of f ( x ) = 0 using Newton’s Method. Recall

Algorithm 1 The pseudocode for Newton’s Method

Input: x 0 , M , δ , ε, f ( x ), f ′( x ) Set v = f ( x 0 ) if | v | ≤ ε then stop for k = 1 to M do x 1 = x 0 − v / f ′( x 0 ) v = f ( x 1 ) if | x 1 − x 0 | ≤ δ or | v | ≤ ε then stop x 0 = x 1 end for

You should write the function in the following form,

[ir,r,err1,err2,numiter]=newton(func1,func2,xinit,delta,eps,maxiter)

where the input arguments

  • func1 is an inline function which evaluates the function,
  • func2 is an inline function which evaluates the function’s derivative,
  • xinit is the initial guess,
  • delta is the desired tolerance for | x 1 − x 0 |,
  • eps is the desired tolerance for | v |,
  • maxiter is the maximum number of Newton iterations allowed.

The output arguments

  • ir is an integer indicating whether or not the Newton’s iteration is converged, use ir=0 if it converges or ir=-1 otherwise,
  • r is the resulting zero of f,
  • err1 is the actual error | x 1 − x 0 | at the final stage of iteration,
  • err2 is the actual value of | f ( r )|,
  • numiter is the number of iterations used.

Don’t forget to put sufficient and appropriate comments (explanations) in your MATLAB M file describ- ing, what the function does along with the descriptions of input and output arguments. For example, the following comments are desirable:

% Compute a zero of a given function by Newton’s Method. % The function takes the following input arguments: % func1 = an inline function to be solved. % and so on.....

Having it this way, whenever you type help newton from MATLAB prompt, it will display the com- ments that you have written. Test your procedure to find all roots of the following functions:

f ( x ) = tan( x / 4 ) − 1 with x 0 = 0 f ( x ) = x^3 − 5 x^2 + 3 x − 7 with x 0 = 1000 f ( x ) = x^3 − 3 x + 2 with x 0 = 10 f ( x ) = 7 x^4 + 3 x^2 + π for any x 0.

In all your computations use δ = ε = 1. 0 e − 15.

Problem 2

Modify your code in the previous problem such that it will output a vector containing the sequence of numbers of err1. Let err1(k) be the Newton’s Method error at the k-th iteration. Comment on the convergence of Newton’s Method in all cases (where the method converges) by tabulating err1(k)/err1(k-1) and err1(k)/err1(k-1)^2 , k=2,...,numiter, for all zeros found in Problem 1.

Some MATLAB commands that you would find useful

  • To create an inline function, the inline command is used. For example, typing

func1 = inline(’x - tan(x)’);

from the MATLAB prompt will create an inline function f ( x ) = x − tan( x ).

  • To break the on going loop before it reaches its maximum number of iterations, use the break command. Recall from the algorithm that whenever the convergence criteria is reached, the loop has to be stopped.
  • In the algorithm there is a conditional statement that requires comparing two statements, namely EITHER this OR that is true. To express this conditional statement in MATLAB, use the syntax if this | that.
  • To take an absolute value of a given number use abs command. For example, | v | is expressed as abs(v).