In this numerical computing tutorial, we explain the basics of the fixed point iteration for solving nonlinear equations. We also explain how to implement the fixed point iteration in Python for solving nonlinear equations. The motivation for studying the fixed point iteration comes from the fact that this algorithm is the basis of a number of numerical algorithms used in control engineering, linear algebra, machine learning, etc. The YouTube tutorial is given below.
Basics of the Fixed Point Iteration
Fixed point iteration is a simple method for solving nonlinear equations. Consequently, it is very easy to implement this method in Python or in some other programming languages. It has a scalar and matrix form. In this tutorial, we explain the scalar form of the fixed point iterations.
The goal is to solve a nonlinear equation
(1)
where
For example, we want to solve these equations:
(2)
Instead of directly solving (1), we instead solve an equivalent problem
(3)
where
(4)
Then, consider this example
(5)
Here, it should be kept in mind that the form of the equation
Now we are ready to define the fixed point.
Definition of the fixed point: The value
(6)
is called the fixed point of
(7)
The fixed point iteration is a method for iteratively computing the fixed point
(8)
where for
The main questions are
- When we can use the fixed point iteration to approximate the solution?
- What are the conditions that the function
needs to satisfy such that the fixed point iteration converges?
Conditions on the function g(x) such that the fixed point iteration converges: Let us assume that
- The function
is continuously differentiable on some interval . - The function
is such that for . - There exists a constant
, such that and such that
(9)
That is, the constant
Then, the series of points
Python Implementation of the Fixed Point Iteration
Here, we present a Python implementation of the fixed point iteration. The convergence error is measured by
(10)
As a demonstration, we consider the following example:
(11)
We consider the two forms of the function
(12)
and
(13)
The accuracy of the final value is measured by computing
(14)
The closer the value
The Python implementation of the fixed point iteration is given below.
# -*- coding: utf-8 -*-
"""
Python implementation of the fixed point iteration
Author: Aleksandar Haber
"""
import numpy as np
# f function
def fFunction(x):
y=x**2+3*x+2
return y
# g function
# first version
def gFunction(x):
y=(1/3)*(-x**2-2)
return y
# g function
# second version
def gFunction2(x):
y=-np.sqrt(-3*x-2)
return y
# function implementing the fixed point iteration
def fixedPointIteration(gF,initialGuess, toleranceError =10**(-6), maxIteration=1000):
#iteration
k=0
# initialize the iteration
x_k=initialGuess
# initialize the error
error=1000
print("Iteration no: ", k, "** x= ", x_k)
while(k<maxIteration and error>toleranceError):
# fixed point iteration
x_kp1=gF(x_k)
error=np.abs(x_kp1-x_k)
# update the variables
x_k=x_kp1
k=k+1
print("Iteration no: ", k, "** x= ",x_k)
return x_kp1
# initial guess
x0= -1.5
# max number of iteration
maxIterationNumber=500
# tolerance error
tE=10**(-5)
# First g function
solution=fixedPointIteration(gFunction,x0, tE, maxIterationNumber)
# value of the function f
fFunction(solution)
# Second g function
# initial guess
x0= -1.2
solution2=fixedPointIteration(gFunction2,x0, tE, maxIterationNumber)
# value of the function f
fFunction(solution2)