Book Image

Mastering Python for Finance

Book Image

Mastering Python for Finance

Overview of this book

Table of Contents (17 chapters)
Mastering Python for Finance
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Incremental search


A crude method of solving a nonlinear function is by doing an incremental search. Using an arbitrarily starting point , we can obtain values of for every increment of . We assume that the values of , , … are going in the same direction as indicated by their sign. Once the sign changes, a solution is deemed as found. Otherwise, the iterative search terminates when it crosses the boundary point .

A pictorial example of the root-finder method for iteration is given in the following graph:

An example can be seen from the Python code:

'''
Python code:
Incremental search method
'''
""" An incremental search algorithm """
import numpy as np
 
 
def incremental_search(f, a, b, dx):
    """
    :param f: The function to solve
    :param a: The left boundary x-axis value
    :param b: The right boundary x-axis value
    :param dx: The incremental value in searching
    :return: The x-axis value of the root,
                number of iterations used
    """
    fa = f(a)    
    c...