0% found this document useful (0 votes)
84 views

Computational Lab in Physics: Finding Roots of Nonlinear Functions

This document describes numerical methods for finding the roots, or zeros, of nonlinear functions. It discusses the bisection method, which uses interval bisection to bracket a root, and Newton's method, which uses iterative improvements based on the function's derivative to converge on a root. The bisection method is simple and robust but slow, while Newton's method can converge faster but may diverge if the initial point is too far from the root. A combined Newton-bisection method is proposed to take advantage of both approaches. Assignments are given to implement these root-finding methods in C++ programs.

Uploaded by

benefit187
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Computational Lab in Physics: Finding Roots of Nonlinear Functions

This document describes numerical methods for finding the roots, or zeros, of nonlinear functions. It discusses the bisection method, which uses interval bisection to bracket a root, and Newton's method, which uses iterative improvements based on the function's derivative to converge on a root. The bisection method is simple and robust but slow, while Newton's method can converge faster but may diverge if the initial point is too far from the root. A combined Newton-bisection method is proposed to take advantage of both approaches. Assignments are given to implement these root-finding methods in C++ programs.

Uploaded by

benefit187
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Computational Lab in Physics:

Finding roots of nonlinear functions.

Steven Kornreich
www.beachlook.com

Picture above shows the function exp(x)*log(x)-cos(x) in the interval x=0-4. The
function crosses the ordinate axis at some value, called the root. We can find roots of
nonlinear equations numerically when an analytic solution is not possible.
Finding roots of “simple” polynomials

 Quadratic equation
 ax2+bx+c=0
 x=(-b±√(b2-4ac))/2a
 Cubic equation p a
x   u 
 x3+ax2+bx+c=0 3u 3
a2
p b
3
2a 3  9ab
q c
27
2 3
q q p
u3  
2 4 27 2
Most functions cannot be given in
closed form: Need numerical approach.

 Bisection Method
 Simplestand most robust.
 How does it work:
 We want to find the roots of f(x), i.e.
f(x)=0.
 f(x) is continuous in [a,b]
 Start with an interval [a,b] such that f(x)
changes sign in the interval.
 f(a)f(b)<0
 There must be at least one real root on the
interval [a,b].
3
Example: f(x)=ln(x)-cos(5x)

 Interval:
 a=0.2
 b=2
 Function at the boundaries:
 f(a)=-2.149
 f(b)=1.53
 Therefore:
 f(a)f(b)<0
 At least one root.
 From graph, we see there
are actually 3 roots.
 Bisection will find one of the
roots.
 Multiple bisections will find
the rest.

4
Bisection procedure
 Divide the interval [a,b] into two equal
intervals.
 Middle point x1=(a+b)/2.
 Three possibilities

  0, root in [a, x1 ]

f (a ) f ( x1 )    0, root in [ x1 , b]
  0, root is x
 1

 If x1 is not the root, we have a new interval.


 Bisect new interval and test.

5
Example Procedure: f(x)=ln(x)-cos(5x)
 Interval [0.2,2]
 1st Iteration:
 f(a)=-2.149, f(b)=1.53 [ ]
 x1=(a+b)/2=1.1,
 f(x1)=-0.61
 New interval [1.1,2]
 2nd Iteration
 x2=1.55, f(x2) = 0.33
 New interval [1.1,1.55]
 3rd Iteration
 x3= 1.325, f(x3) = -0.66
 New Interval [1.325,1.55]
6
Continue iterating…
 When do you stop?
 Convergence criteria:
 Want f(xi)=0 for the true root.
 Usually set |f(xi)|<δ for a small number.
 Smaller δ, closer to the true root.
 Small δ might require more iterations.
 Interval size:
 alternately, can use |xi-xi-1|<δ.
 For our example
 δ=10-2, needs 9 iterations:
 xr=1.49, f(xr)=6.9 x 10-3.
 δ=10-3, needs 12 iterations:
 xr=1.489, f(xr)=-1.2 x 10-5.
 δ=10-5, needs 19 iterations:
 xr=1.48892, f(xr)=5.9 x 10-6.

7
Bisection Method in ROOT
while (fabs(func->Eval(x_root))>1e-5) {
++iteration;

// Here is the calculation using the bisection method


TF1* func = new TF1("func","log(x)- x_root=(lowLim+uppLim)/2.0;
cos(5*x)",0,3); cout << "x_" <<iteration <<" = " << x_root;
func->SetNpx(1000); cout << ", f(x)= " << func->Eval(x_root) << endl;
if (func->Eval(x_root)==0.0) break;
TCanvas* funcCnv = new
if (func->Eval(lowLim)*func->Eval(x_root)<0) {
TCanvas("funcCnv","Function",500,500);
uppLim=x_root;
func->Draw(); }
TLine* xaxis = new TLine(0,0,3,0); else {
xaxis->Draw(); lowLim=x_root;
double lowLim=0.2; }
}
double uppLim=2;
TMarker* rootMarker = new TMarker(x_root2,func-
double x_root = uppLim; >Eval(x_root2),20);
size_t iteration = 0; rootMarker->SetMarkerColor(4);
double delta = 1e-5; // rootMarker->SetMarkerSize(1.5);
rootMarker->Draw("same");

8
Newton’s Method
 Use a Taylor expansion of the function:
f '( x0 ) 2 f ''( x0 ) 3 f '''( x0 )
f ( x)  f ( x0 )  ( x  x0 )  ( x  x0 )  ( x  x0 )
1 2! 3!
 Keep only first two terms

f ( x)  f ( x0 )  ( x  x0 ) f '( x0 )
 Linear approximation, the root of this
approximation is then
 x1=x0-f(x0)/f’(x0)
 If x1 is not a root, repeat the procedure around
x1.
 For many classes of function, speeds up the
method.
 Note: It also has weaknesses:
 Very slow convergence if f’(x)~0 near the root.
 Local minima cause wild jumps from one point
to the next. 9
Example: f(x)=ln(x)-cos(5x)
 For precision 10-5.
x_1 = 2, f(x)= 1.53221871
x_2 = 2.690155793, f(x)= 0.3558518309
x_3 = 2.606217121, f(x)= 0.06395045778
x_4 = 2.581850754, f(x)= 0.006717235596
x_5 = 2.578603106, f(x)= 0.0001236111542

10
Combination of Newton+Bisection
 Newton Method drawbacks:
 When close to root:
 numerical derivative error ~ root value.
 If x1 is far, f’(x1) could have opposite sign
than near f’(x_root):
 Subsequent iterations diverge!
 Solution: combine Newton+Bisection
 Select interval [a,b] containing root.
 If x in [a,b] for Newton method, ok.
2

 Otherwise use bisection to determine x2.


11
Assignments
 Chapter 14
 1. Bisection Method in ROOT (not in book)
 Write a program for the bisection method in ROOT.
 Use the function: f(x)=exp(x)ln(x)-cos(x)=0
 use TF1’s.
 Find a root between x=[0,4], precision 10-8.
 Plot the function and put a blue marker at location of root.
 Exercise (2) from book, Chapter 14.
 Write a c++ program for combined Newton+Bisection Method
(See section 14.2, i.e. Not in ROOT use examples in book).
 Use program starting with 111-points on a grid of initial values
in the interval x=[35,40]
 find all the roots of sin(x2) in that interval using your program.
 Addition: Print all the roots to an output file
 Use this format:
x1 = …
x2 = …

12

You might also like