You are on page 1of 69

Computational Physics (PHY 362)

FA 16

Dr. Muhammad Kamran

Department of Physics

CIIT Islamabad
Lecture 5
Lecture 5 26/09/2016

Finding roots of an equation

Let f be a real- or complex-valued function of a real or


complex variable.

Computational Physics Dr. M. Kamran 3 / 18


Lecture 5 26/09/2016

Finding roots of an equation

Let f be a real- or complex-valued function of a real or


complex variable.

A number r, real or complex, for which f (r ) = 0 is called a


root of that equation.

Computational Physics Dr. M. Kamran 3 / 18


Lecture 5 26/09/2016

Finding roots of an equation

Let f be a real- or complex-valued function of a real or


complex variable.

A number r, real or complex, for which f (r ) = 0 is called a


root of that equation.

Finding a root of f (r) − g (r) = 0 is the same as solving the


equation f (r) = g (r).

Computational Physics Dr. M. Kamran 3 / 18


Lecture 5 26/09/2016

Finding roots of an equation

Let f be a real- or complex-valued function of a real or


complex variable.

A number r, real or complex, for which f (r ) = 0 is called a


root of that equation.

Finding a root of f (r) − g (r) = 0 is the same as solving the


equation f (r) = g (r).
Numerical root-nding methods use iteration, producing a
sequence of numbers.

◇ Convergence towards the root as a limit.

Computational Physics Dr. M. Kamran 3 / 18


Lecture 5 26/09/2016

Finding roots of an equation

Let f be a real- or complex-valued function of a real or


complex variable.

A number r, real or complex, for which f (r ) = 0 is called a


root of that equation.

Finding a root of f (r) − g (r) = 0 is the same as solving the


equation f (r) = g (r).
Numerical root-nding methods use iteration, producing a
sequence of numbers.

◇ Convergence towards the root as a limit.

One or more initial guesses of the root as starting values.

◇ Each iteration of the algorithm produces a successively

more accurate approximation to the root.

Computational Physics Dr. M. Kamran 3 / 18


Lecture 5 26/09/2016

Finding roots of an equation

Let f be a real- or complex-valued function of a real or


complex variable.

A number r, real or complex, for which f (r ) = 0 is called a


root of that equation.

Finding a root of f (r) − g (r) = 0 is the same as solving the


equation f (r) = g (r).
Numerical root-nding methods use iteration, producing a
sequence of numbers.

◇ Convergence towards the root as a limit.

One or more initial guesses of the root as starting values.

◇ Each iteration of the algorithm produces a successively

more accurate approximation to the root.

The iterative methods give an approximation to the root,


not an exact solution.

Computational Physics Dr. M. Kamran 3 / 18


Lecture 5 26/09/2016

Finding roots of an equation

5x + 9 = 0.

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

9
5x + 9 = 0. x =− .
5

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

9
5x + 9 = 0. x =− .
5

6x
2 − 7x + 2 = 0.

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

9
5x + 9 = 0. x =− .
5
2 1 2
6x − 7x + 2 = 0. x = ; .
2 3

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

9
5x + 9 = 0. x =− .
5
2 1 2
6x − 7x + 2 = 0. x = ; .
2 3

x
3 = 1.

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

9
5x + 9 = 0. x =− .
5
2 1 2
6x − 7x + 2 = 0. x = ; .
2 3

3 −1 ± 3i
x = 1. x = 1; .
2

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

9
5x + 9 = 0. x =− .
5
2 1 2
6x − 7x + 2 = 0. x = ; .
2 3

3 −1 ± 3i
x = 1. x = 1; .
2

3x
4 − 5x2 + 2 = 0.

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

9
5x + 9 = 0. x =− .
5
2 1 2
6x − 7x + 2 = 0. x = ; .
2 3

3 −1 ± 3i
x = 1. x = 1; .
2

4 2
3x − 5x2 + 2 = 0. x = ±1; ± .
3

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

9
5x + 9 = 0. x =− .
5
2 1 2
6x − 7x + 2 = 0. x = ; .
2 3

3 −1 ± 3i
x = 1. x = 1; .
2

4 2
3x − 5x2 + 2 = 0. x = ±1; ± .
3

cos (3x) = cos (7x) .

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

9
5x + 9 = 0. x =− .
5
2 1 2
6x − 7x + 2 = 0. x = ; .
2 3

3 −1 ± 3i
x = 1. x = 1; .
2

4 2
3x − 5x2 + 2 = 0. x = ±1; ± .
3
nπ nπ
cos (3x) = cos (7x) . x = ; .
2 5

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

9
5x + 9 = 0. x =− .
5
2 1 2
6x − 7x + 2 = 0. x = ; .
2 3

3 −1 ± 3i
x = 1. x = 1; .
2

4 2
3x − 5x2 + 2 = 0. x = ±1; ± .
3
nπ nπ
cos (3x) = cos (7x) . x = ; .
2 5

a+b b−a
cos a − cos b = 2 sin [ ] sin [ ].
2 2

Computational Physics Dr. M. Kamran 4 / 18


Lecture 5 26/09/2016

Finding roots of an equation

For more complicated functions, the analytic solution may


not be possible to calculate.

Computational Physics Dr. M. Kamran 5 / 18


Lecture 5 26/09/2016

Finding roots of an equation

For more complicated functions, the analytic solution may


not be possible to calculate.

We have to nd the solution numerically.

Computational Physics Dr. M. Kamran 5 / 18


Lecture 5 26/09/2016

Finding roots of an equation

For more complicated functions, the analytic solution may


not be possible to calculate.

We have to nd the solution numerically.

Following methods of nding the root(s) will be studied in


this course.

◇ Bisection method.

◇ Regula Falsi method.

◇ Newton Raphson method.

◇ Secant method.

◇ Fixed point method.

Computational Physics Dr. M. Kamran 5 / 18


Lecture 5 26/09/2016

Intermediate-value theorem

If the function f is continuous on the closed interval [a, b],


and if f (a) ≤ y ≤ f (b) or f (b) ≤ y ≤ f (a), then there exists a
point c such that a ≤ c ≤ b and f (c) = y.

Corollary: If a continuous function has values of opposite


sign inside an interval, then it has a root in that interval.

Computational Physics Dr. M. Kamran 6 / 18


Lecture 5 26/09/2016

Intermediate-value theorem

If the function f is continuous on the closed interval [a, b],


and if f (a) ≤ y ≤ f (b) or f (b) ≤ y ≤ f (a), then there exists a
point c such that a ≤ c ≤ b and f (c) = y.

Corollary: If a continuous function has values of opposite


sign inside an interval, then it has a root in that interval.

Figure: Intermediate-value
theorem.

Computational Physics Dr. M. Kamran 6 / 18


Lecture 5 26/09/2016

Bisection method

As the name suggests, this method repeatedly bisects an


interval and then selects a subinterval in which a root must
lie.

Computational Physics Dr. M. Kamran 7 / 18


Lecture 5 26/09/2016

Bisection method

As the name suggests, this method repeatedly bisects an


interval and then selects a subinterval in which a root must
lie.

The method is applicable where f is a continuous function


dened on an interval [a, b] and where f (a) and f (b) have
opposite signs.

Computational Physics Dr. M. Kamran 7 / 18


Lecture 5 26/09/2016

Bisection method

As the name suggests, this method repeatedly bisects an


interval and then selects a subinterval in which a root must
lie.

The method is applicable where f is a continuous function


dened on an interval [a, b] and where f (a) and f (b) have
opposite signs.

It is a very simple and robust method, but it is also


relatively slow.

Computational Physics Dr. M. Kamran 7 / 18


Lecture 5 26/09/2016

Bisection method

As the name suggests, this method repeatedly bisects an


interval and then selects a subinterval in which a root must
lie.

The method is applicable where f is a continuous function


dened on an interval [a, b] and where f (a) and f (b) have
opposite signs.

It is a very simple and robust method, but it is also


relatively slow.

At each step in this algorithm, we have an interval [a, b]


and the values u = f (a), v = f (b).

Computational Physics Dr. M. Kamran 7 / 18


Lecture 5 26/09/2016

Bisection method

As the name suggests, this method repeatedly bisects an


interval and then selects a subinterval in which a root must
lie.

The method is applicable where f is a continuous function


dened on an interval [a, b] and where f (a) and f (b) have
opposite signs.

It is a very simple and robust method, but it is also


relatively slow.

At each step in this algorithm, we have an interval [a, b]


and the values u = f (a), v = f (b).
Condition: uv < 0.

Computational Physics Dr. M. Kamran 7 / 18


Lecture 5 26/09/2016

Bisection method

As the name suggests, this method repeatedly bisects an


interval and then selects a subinterval in which a root must
lie.

The method is applicable where f is a continuous function


dened on an interval [a, b] and where f (a) and f (b) have
opposite signs.

It is a very simple and robust method, but it is also


relatively slow.

At each step in this algorithm, we have an interval [a, b]


and the values u = f (a), v = f (b).
Condition: uv < 0.
Next, construct the midpoint of the interval, c = (a + b) /2.

Computational Physics Dr. M. Kamran 7 / 18


Lecture 5 26/09/2016

Bisection method

If w ≡ f (c) = 0, the objective of the algorithm has been


fullled.

Computational Physics Dr. M. Kamran 8 / 18


Lecture 5 26/09/2016

Bisection method

If w ≡ f (c) = 0, the objective of the algorithm has been


fullled.

However, in the usual case, w ≠ 0, and either wu <0 or


wv < 0.

Computational Physics Dr. M. Kamran 8 / 18


Lecture 5 26/09/2016

Bisection method

If w ≡ f (c) = 0, the objective of the algorithm has been


fullled.

However, in the usual case, w ≠ 0, and either wu <0 or


wv < 0.
If wu < 0, a root of f exists in the interval [a, c].

Computational Physics Dr. M. Kamran 8 / 18


Lecture 5 26/09/2016

Bisection method

If w ≡ f (c) = 0, the objective of the algorithm has been


fullled.

However, in the usual case, w ≠ 0, and either wu <0 or


wv < 0.
If wu < 0, a root of f exists in the interval [a, c].
Consequently, store the value of c in b and w in v.

Computational Physics Dr. M. Kamran 8 / 18


Lecture 5 26/09/2016

Bisection method

If w ≡ f (c) = 0, the objective of the algorithm has been


fullled.

However, in the usual case, w ≠ 0, and either wu <0 or


wv < 0.
If wu < 0, a root of f exists in the interval [a, c].
Consequently, store the value of c in b and w in v.

If wu > 0, then we cannot be sure that f has a root in [a, c].


→ However, since wv < 0, f must have a root in [c, b].

Computational Physics Dr. M. Kamran 8 / 18


Lecture 5 26/09/2016

Bisection method

If w ≡ f (c) = 0, the objective of the algorithm has been


fullled.

However, in the usual case, w ≠ 0, and either wu <0 or


wv < 0.
If wu < 0, a root of f exists in the interval [a, c].
Consequently, store the value of c in b and w in v.

If wu > 0, then we cannot be sure that f has a root in [a, c].


→ However, since wv < 0, f must have a root in [c, b].
Store the value of c in a and w in u.

Computational Physics Dr. M. Kamran 8 / 18


Lecture 5 26/09/2016

Bisection method

If w ≡ f (c) = 0, the objective of the algorithm has been


fullled.

However, in the usual case, w ≠ 0, and either wu <0 or


wv < 0.
If wu < 0, a root of f exists in the interval [a, c].
Consequently, store the value of c in b and w in v.

If wu > 0, then we cannot be sure that f has a root in [a, c].


→ However, since wv < 0, f must have a root in [c, b].
Store the value of c in a and w in u.

In either case, now the interval is half  the original interval.

Computational Physics Dr. M. Kamran 8 / 18


Lecture 5 26/09/2016

Bisection method

If w ≡ f (c) = 0, the objective of the algorithm has been


fullled.

However, in the usual case, w ≠ 0, and either wu <0 or


wv < 0.
If wu < 0, a root of f exists in the interval [a, c].
Consequently, store the value of c in b and w in v.

If wu > 0, then we cannot be sure that f has a root in [a, c].


→ However, since wv < 0, f must have a root in [c, b].
Store the value of c in a and w in u.

In either case, now the interval is half  the original interval.

Repeat the steps until the interval is satisfactorily small.

Computational Physics Dr. M. Kamran 8 / 18


Lecture 5 26/09/2016

Bisection method

At the end, the best estimate of the root would be


(a + b) /2, where [a, b] is now the last interval in the
procedure.

Computational Physics Dr. M. Kamran 9 / 18


Lecture 5 26/09/2016

Bisection method

At the end, the best estimate of the root would be


(a + b) /2, where [a, b] is now the last interval in the
procedure.

Figure: Bisection method for calculating the roots.

Computational Physics Dr. M. Kamran 9 / 18


Lecture 6
Lecture 6 26/09/2016

Convergence analysis

Now let us investigate the accuracy with which the


bisection method determines a root of a function.

Computational Physics Dr. M. Kamran 11 / 18


Lecture 6 26/09/2016

Convergence analysis

Now let us investigate the accuracy with which the


bisection method determines a root of a function.

Suppose that f is a continuous function that takes values of


opposite sign at the ends of an interval [a0 , b0 ].
There is a root r in [a0 , b0 ], and if we use the midpoint
c0 = (a0 + b0 ) /2 as our estimate of r, we have

b0 − a0
∣r − c0 ∣ ≤ . (1)
2

Computational Physics Dr. M. Kamran 11 / 18


Lecture 6 26/09/2016

Convergence analysis

Now let us investigate the accuracy with which the


bisection method determines a root of a function.

Suppose that f is a continuous function that takes values of


opposite sign at the ends of an interval [a0 , b0 ].
There is a root r in [a0 , b0 ], and if we use the midpoint
c0 = (a0 + b0 ) /2 as our estimate of r, we have

b0 − a0
∣r − c0 ∣ ≤ . (1)
2

After nth iteration, we have

bn − an
∣r − cn ∣ ≤ . (2)
2

Computational Physics Dr. M. Kamran 11 / 18


Lecture 6 26/09/2016

Convergence analysis

Since the widths of the intervals are divided by 2 in each


step, we conclude that

b0 − a0
∣r − cn ∣ ≤ . (3)
2n+1

Computational Physics Dr. M. Kamran 12 / 18


Lecture 6 26/09/2016

Convergence analysis

Since the widths of the intervals are divided by 2 in each


step, we conclude that

b0 − a0
∣r − cn ∣ ≤ . (3)
2n+1

If an error tolerance has been prescribed in advance, it is


possible to determine the number of steps required in the
bisection method

Computational Physics Dr. M. Kamran 12 / 18


Lecture 6 26/09/2016

Convergence analysis

Since the widths of the intervals are divided by 2 in each


step, we conclude that

b0 − a0
∣r − cn ∣ ≤ . (3)
2n+1

If an error tolerance has been prescribed in advance, it is


possible to determine the number of steps required in the
bisection method

If we set ∣r − cn ∣ < , we obtain

b−a log (b − a) − log 2


< ⇒n> . (4)
2n+1 log 2

Computational Physics Dr. M. Kamran 12 / 18


Lecture 6 26/09/2016

Convergence analysis

Since the widths of the intervals are divided by 2 in each


step, we conclude that

b0 − a0
∣r − cn ∣ ≤ . (3)
2n+1

If an error tolerance has been prescribed in advance, it is


possible to determine the number of steps required in the
bisection method

If we set ∣r − cn ∣ < , we obtain

b−a log (b − a) − log 2


< ⇒n> . (4)
2n+1 log 2

Figure: Convergence
analysis.

Computational Physics Dr. M. Kamran 12 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

This method retains the main feature of the bisection


method.
◇ A root is trapped in a sequence of intervals of decreasing

size.

Computational Physics Dr. M. Kamran 13 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

This method retains the main feature of the bisection


method.
◇ A root is trapped in a sequence of intervals of decreasing

size.

However, rather than selecting the midpoint of each


interval, this method uses the point where the secant lines
intersect the x-axis.

Computational Physics Dr. M. Kamran 13 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

This method retains the main feature of the bisection


method.
◇ A root is trapped in a sequence of intervals of decreasing

size.

However, rather than selecting the midpoint of each


interval, this method uses the point where the secant lines
intersect the x-axis.

Figure: Regula Falsi


method.

Computational Physics Dr. M. Kamran 13 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

A secant line of a curve is a line that (locally) intersects


two points on the curve.

Computational Physics Dr. M. Kamran 14 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

A secant line of a curve is a line that (locally) intersects


two points on the curve.

As the two points are brought together (or, more precisely,


as one is brought towards the other), the secant line tends
to a tangent line.

Computational Physics Dr. M. Kamran 14 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

A secant line of a curve is a line that (locally) intersects


two points on the curve.

As the two points are brought together (or, more precisely,


as one is brought towards the other), the secant line tends
to a tangent line.

Figure: Secant and tangent to a curve.

Computational Physics Dr. M. Kamran 14 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

A secant line of a curve is a line that (locally) intersects


two points on the curve.

As the two points are brought together (or, more precisely,


as one is brought towards the other), the secant line tends
to a tangent line.

Figure: Secant and tangent to a curve.

In gure 2, the secant line over the interval [a, b] is the


chord between (a, f (a)) and (b, f (b)).
The two right triangles in the gure are similar.

Computational Physics Dr. M. Kamran 14 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

f(b) −f (a)
= . (5)
b−c c−a

Computational Physics Dr. M. Kamran 15 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

f(b) −f (a)
= . (5)
b−c c−a

from which we obtain

af (b) − b f (a) a−b


c = = b − f (b ) [ ]
f (b) − f (a) f (a) − f (b)
(6)
b−a
= a − f (a) [ ].
f (b) − f (a)

Computational Physics Dr. M. Kamran 15 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

f(b) −f (a)
= . (5)
b−c c−a

from which we obtain

af (b) − b f (a) a−b


c = = b − f (b ) [ ]
f (b) − f (a) f (a) − f (b)
(6)
b−a
= a − f (a) [ ].
f (b) − f (a)

We then compute f (c ) and proceed to the next step with


the interval

◇ [a, c] if f (a) f (c) < 0.


◇ [c, b] if f (c) f (b) < 0.

Computational Physics Dr. M. Kamran 15 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

In general case, the false position method starts with the


interval [a0 , b0 ] containing a root, with f (a0 ) and f (b 0 )
having opposite signs.

Computational Physics Dr. M. Kamran 16 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

In general case, the false position method starts with the


interval [a0 , b0 ] containing a root, with f (a0 ) and f (b 0 )
having opposite signs.

It nds where the secant line joining (ak , f (ak )) and


(bk , f (bk )) crosses the x-axis and then selects it to be the
new endpoint.

Computational Physics Dr. M. Kamran 16 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

In general case, the false position method starts with the


interval [a0 , b0 ] containing a root, with f (a0 ) and f (b 0 )
having opposite signs.

It nds where the secant line joining (ak , f (ak )) and


(bk , f (bk )) crosses the x-axis and then selects it to be the
new endpoint.

After kth step

Computational Physics Dr. M. Kamran 16 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

In general case, the false position method starts with the


interval [a0 , b0 ] containing a root, with f (a0 ) and f (b 0 )
having opposite signs.

It nds where the secant line joining (ak , f (ak )) and


(bk , f (bk )) crosses the x-axis and then selects it to be the
new endpoint.

After kth step

ak f (bk ) − bk f (ak )
ck = . (7)
f (bk ) − f (ak )

Computational Physics Dr. M. Kamran 16 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

In general case, the false position method starts with the


interval [a0 , b0 ] containing a root, with f (a0 ) and f (b 0 )
having opposite signs.

It nds where the secant line joining (ak , f (ak )) and


(bk , f (bk )) crosses the x-axis and then selects it to be the
new endpoint.

After kth step

ak f (bk ) − bk f (ak )
ck = . (7)
f (bk ) − f (ak )

If f (ak ) and f (ck ) have the same sign, set ak+1 = ck and
bk+1 = bk .

Computational Physics Dr. M. Kamran 16 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

In general case, the false position method starts with the


interval [a0 , b0 ] containing a root, with f (a0 ) and f (b 0 )
having opposite signs.

It nds where the secant line joining (ak , f (ak )) and


(bk , f (bk )) crosses the x-axis and then selects it to be the
new endpoint.

After kth step

ak f (bk ) − bk f (ak )
ck = . (7)
f (bk ) − f (ak )

If f (ak ) and f (ck ) have the same sign, set ak+1 = ck and
bk+1 = bk .
If the signs are dierent, set ak+1 = ak and bk+1 = ck .
Computational Physics Dr. M. Kamran 16 / 18
Lecture 6 26/09/2016

Regula Falsi (false position) method

The process is repeated until the root is approximated


suciently well.

Computational Physics Dr. M. Kamran 17 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

The process is repeated until the root is approximated


suciently well.
For some functions, the false position method may
repeatedly select the same endpoint.

Computational Physics Dr. M. Kamran 17 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

The process is repeated until the root is approximated


suciently well.
For some functions, the false position method may
repeatedly select the same endpoint.
When the same endpoint is retained twice, the false
position method is modied.


⎪ ak f (bk ) − 2bk f (ak )


⎪ if f (ak ) f (bk ) < 0

⎪ f (bk ) − 2f (ak )
(m) ⎪⎪
c = ⎨ (8)
k ⎪


⎪ 2ak f (bk ) − bk f (ak )

⎪ if f (ak ) f (bk ) > 0

⎪ 2f (bk ) − f (ak )

Computational Physics Dr. M. Kamran 17 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

The process is repeated until the root is approximated


suciently well.
For some functions, the false position method may
repeatedly select the same endpoint.
When the same endpoint is retained twice, the false
position method is modied.


⎪ ak f (bk ) − 2bk f (ak )


⎪ if f (ak ) f (bk ) < 0

⎪ f (bk ) − 2f (ak )
(m) ⎪⎪
c = ⎨ (8)
k ⎪


⎪ 2ak f (bk ) − bk f (ak )

⎪ if f (ak ) f (bk ) > 0

⎪ 2f (bk ) − f (ak )

The modied Regula Falsi method changes the slope of the
straight line so that it is closer to the root.

Computational Physics Dr. M. Kamran 17 / 18


Lecture 6 26/09/2016

Regula Falsi (false position) method

Figure: Modied Regula Falsi method.

Computational Physics Dr. M. Kamran 18 / 18

You might also like