This Python script implements and compares five common numerical methods for finding the roots of a mathematical function. The program calculates the root for a predefined function (f(x) = x² - 3
) using each method and displays the step-by-step iterations in a series of tables for easy comparison.
The script is organized into functions, each implementing a specific root-finding algorithm:
bisection(f, a, b, E)
: Implements the Bisection method.secant(f, a, b, E)
: Implements the Secant method.modified_secant(f, a, b, E)
: Implements a modified version of the Secant method.newton_raphson(f, df, x, N, E)
: Implements the Newton-Raphson method, which requires the function's derivative.fixed_point(f, g, a, N, E)
: Implements the Fixed-Point Iteration method.display_tables(...)
: A helper function that usesmatplotlib
to render the results from each algorithm in a clean, tabular format.
The script requires the following Python libraries:
pandas
matplotlib
You can install them by running:
pip install pandas matplotlib
-
Save the code: Ensure the code is saved in a Python file (e.g.,
root_finder.py
). -
Run from the terminal: Navigate to the file's directory and execute the script. The tables will be displayed in a plot window.
python root_finder.py