Anda di halaman 1dari 22

Numerical Methods Lecture 3

Nonlinear Equations

by Pavel Ludvk
Introduction

Definition (Root or zero of a function)


A root (or a zero) of a function f is a solution of an equation
f (x) = 0.

We learn several root-finding methods:


the bisection method,
false position,
the secant method,
the NewtonRaphson method.

Numerical Methods Lecture 3 by Pavel Ludvk 2 / 18


Introduction

Definition (Root or zero of a function)


A root (or a zero) of a function f is a solution of an equation
f (x) = 0.

We learn several root-finding methods:


the bisection method,
false position,
the secant method,
the NewtonRaphson method.

Numerical Methods Lecture 3 by Pavel Ludvk 2 / 18


Introduction

General Root-Finding Strategy


1. Bracket the roots, i.e. find the disjoint intervals contai-
ning the individual roots.
2. Set the initial approximation(s).
3. Repeat applying the iterative formula until the stop-
ping criterion is met.

Numerical Methods Lecture 3 by Pavel Ludvk 3 / 18


Introduction

General Root-Finding Strategy


1. Bracket the roots, i.e. find the disjoint intervals contai-
ning the individual roots.
2. Set the initial approximation(s).
3. Repeat applying the iterative formula until the stop-
ping criterion is met.

Differences Between Methods


the number of starting values,
the guarantee, or otherwise, of convergence,
the speed of convergence,
the cost, in terms of the work to be done per iteration.

Numerical Methods Lecture 3 by Pavel Ludvk 3 / 18


Bisection Method - Description

The bisection method is the simplest of all the methods for


finding a root of a nonlinear equation. We start with an interval
containing a root and divide it into a left and a right half.We
decide which of the two halves contains a root and proceed
with a further division of that half. We do this repeatedly until
the interval containing a root is sufficiently narrowed down to
meet the level of accuracy required. If we take the mid-point of
the latest interval to be an approximation to a root, we can say
that this is accurate to within half the width of the interval.

Numerical Methods Lecture 3 by Pavel Ludvk 4 / 18


Bisection Method - Algorithm

Theorem
Let f be a continuous function on an interval [a, b] and
f (a)f (b) < 0. Then there exists a number c (a, b) that f (c) = 0.

Algorithm for solving f (x) = 0 by Bisection Method


1. Find an interval [a, b] in which f (x) = 0.
a+b
2. Set x to 2 .
ba
3. Test for convergence: Find out if 2 6 .
4. Calculate f (x ).
5. If f (x ) and f (b) have opposite sign set a to x , otherwise
set b to x .

6. Repeat from step 2.

Numerical Methods Lecture 3 by Pavel Ludvk 5 / 18


Bisection Method - Matlab

Exercise A
Write an M-File for one step of the Bisection Method. Input:
Function f and limitpoints of an interval [an , bn ]. Output: Limit-
points of an interval [an+1 , bn+1 ].

Exercise B
Create an M-File for the Bisection Method. The program should
stop before doing 24 iterations.

Numerical Methods Lecture 3 by Pavel Ludvk 6 / 18


Answers

Answer A

p = (a+b)/2;
if f(a)*f(p)<0
b=p;
elseif f(p)*f(b)<0
a=p
else break
end

Numerical Methods Lecture 3 by Pavel Ludvk 7 / 18


Answer B

f = ; % the function formula
epsilon = ; % the precision
maxn = ; % limitation on the number of steps
a(1) = ; b(1) = ; % initial approximation
for n = 1 : maxn
p(n) = (a(n)+b(n))/2;
if (b(n)-a(n))/2 <= epsilon, break
elseif f(a(n))*f(p(n))<0
a(n+1) = a(n); b(n+1) = p(n);
elseif f(p(n))*f(b(n))<0
a(n+1) = p(n); b(n+1) = b(n);
else break
end
end
disp( a(n) p(n) b(n))
disp([a p b])



Numerical Methods Lecture 3 by Pavel Ludvk 8 / 18
Exercises

1. Solve the equation e0.2x e0.8x 2 = 0 with an accuracy


103 .
2. Show that a function f (x) = x sin x cos x has just one root
in the interval [0, /2] and find it to an accuracy of four de-
cimal places.
3. Derive a formula for an error upper bound after n steps.
4. Try to use the Bisection Method with starting interval [2, 1]
to find a root of the function f (x) = 1x . What did happen?
Explain why.
5. Can you tell the number of steps needed for Bisection Me-
thod to find the root to a given accuracy without carrying it
out? How?

Numerical Methods Lecture 3 by Pavel Ludvk 9 / 18


The Secant Method Description
The Secant Method is a version of the bisection method that
takes a more informed approach to reducing the interval
containing a root. It is likely therefore to require a smaller
number of iterations.
The idea is to approximate the function by its secant and
instead of looking for the root of the original function to use
the root of the secant line as an approximation.

A formula for a secant line of the


function f connecting two points
[a, f (a)] and [b, f (b)] is

f (b) f (a)
y = f (a) + (x a).
ba

Numerical Methods Lecture 3 by Pavel Ludvk 10 / 18


The Secant Method Algorithm

Mathematical Formula

x0 , x1 = initial guesses,
xn1 xn2
xn = xn1 f (xn1 ) .
f (xn1 ) f (xn2 )

Numerical Methods Lecture 3 by Pavel Ludvk 11 / 18


The Secant Method Algorithm

Algorithm for solving f (x) = 0 by the Secant Method


1. Find an interval containing x such that f (x) = 0.
ba
2. Set x to b f (b) f (b)f (a) .
3. Test for convergence: Find out if |b x | 6 .
4. Set a to b and b to x .

5. Repeat from step 2.

Numerical Methods Lecture 3 by Pavel Ludvk 11 / 18


The Secant Method Matlab


f = ; % the function formula
epsilon = ; % the precision
maxn = ; % limitation on the number of steps
x(1) = ; x(2) = ; % initial approximation
for n = 3 : maxn
x(n) = x(n-1) - f(x(n-1))*(x(n-1)-x(n-2))/(f(x(
n-1))-f(x(n-2)));
if abs(x(n)-x(n-1)) <= epsilon, break, end
disp( x(n-2) x(n-1) x(n) Error)
disp([x(1:n-2) x(2:n-1) x(3:n) (x(3:n)-x(2:n-1))
])

Numerical Methods Lecture 3 by Pavel Ludvk 12 / 18


Exercises

1. Solve the problems from the section about Bisection Me-


thod.

4
2. Use the Secant Method to calculate the 2 to an accuracy
of six decimal places.
3. Think about the stopping criterion. Is it precise? If the
consider error is small, does it necessarily mean we have
a good approximation? Try to find an counterexample.
4. Look up the commands fzero and roots in the help of
Matlab.

Numerical Methods Lecture 3 by Pavel Ludvk 13 / 18


Newton-Raphson Method Description
A feature of the secant method is its attempt to follow the
graph of the function using a straight line. The Newton method
pursues the idea by allowing the two points of the secant
method to merge into one. In so doing it uses a single point
and the tangent to the function at that point to construct a
new estimate to the root.

A formula for a zero


point of a tangent line of
the function f construc-
ted at point a is

f (a)
x=a+ .
f 0 (a)

Numerical Methods Lecture 3 by Pavel Ludvk 14 / 18


Newton-Raphson Method Algorithm

Mathematical Formula

x0 = initial guess,
f (xn )
xn+1 = xn .
f 0 (xn )

Numerical Methods Lecture 3 by Pavel Ludvk 15 / 18


Newton-Raphson Method Algorithm

Algorithm for solving f (x) = 0 by the Newton-Raphson


Method
1. Find an interval containing x such that f (x) = 0.

2. Choose a starting point a.


f (a)
3. If f 0 (a) 6= 0 replace a by x = a f 0 (a) , otherwise restart
using a different starting point a.
4. Test for convergence: Find out if |x a| 6 .

5. Repeat from step 2.

Numerical Methods Lecture 3 by Pavel Ludvk 15 / 18


Newton-Raphson Method Convergence
Theorem (Convergence Theorem)
Let en denote the error after step n of the N-R Method. Further-
more, let f be twice continuously differentiable and f (p) = 0. If
f 0 (p) 6= 0, then N-R Method is locally convergent to p and
en+1
lim =M
n e2n

(i. e., method is quadratically convergent), where

f 00 (p)
M= .
2f 0 (p)

Local Convergence
A method is locally convergent if there exists an interval
containing a root such that the sequence generated by this
method converge to the root.
Numerical Methods Lecture 3 by Pavel Ludvk 16 / 18
The Newton-Raphson Method Matlab

f = ; % the function formula
%%%%%%%%%%%%%REQUIRES SYMBOLIC TOOLBOX
syms x ; % creating the symbolic variable x
symdf = diff(f(x)); % the derivative function
formula, a symbolic function
df = matlabFunction(symdf); % conversion of a
symbolic function to a normal one
%%%%%%%%%%%%%OTHERWISE COMPUTE DERIVATIVE MANUALLY
epsilon = ; % the precision
maxn = ; % limitation on the number of steps
clear x; x(1) = ; % clearing x as a symbolic
variable; setting initial approximation
for n = 2 : maxn
x(n) = x(n-1) - f(x(n-1))/df(x(n-1));
if abs(x(n)-x(n-1)) <= epsilon, break, end
disp( x(n-1) x(n) Error)
disp([x(1:n-1) x(2:n) (x(1:n)-x(2:n))])

Numerical Methods Lecture 3 by Pavel Ludvk 17 / 18



Exercises

1. Solve the problems from the section about Bisection Me-


thod.
4
2. Use the Newton Method to calculate the 2 to an accuracy
of six decimal places.
3. Think about the stopping criterion. Is it precise? If the
consider error is small, does it necessarily mean we have
a good approximation? Try to find an counterexample.
4. Prove that Newton Method applied to f (x) = ax + b conver-
ges in one step.
5. A 10-cm-high cone contains 60 cm3 of ice cream, including
a hemispherical scoop on top. Find the radius of the scoop
to four correct decimal places.

Numerical Methods Lecture 3 by Pavel Ludvk 18 / 18

Anda mungkin juga menyukai