This repository contains a Python implementation of the Bisection Method for finding roots of nonlinear equations. The code includes input validation, iteration, and termination based on the desired error or reaching the maximum number of iterations. Results are saved into an Excel file.
- Bisection Method Theory
- Dependencies
- Installation
- Usage
- Code Explanation
- Example
- Files in the Repository
- Input Parameters
- Troubleshooting
- Author
The Bisection Method is a numerical technique to find roots of a continuous function where the function changes signs over an interval. The main idea leverages the Intermediate Value Theorem, which states that if a function changes sign over an interval, it must cross zero within that interval.
Steps:
- Choose initial guesses ( x_l ) and ( x_u ) such that ( f(x_l) \cdot f(x_u) < 0 ).
- Compute the midpoint ( x_c ) of ( x_l ) and ( x_u ).
- Replace either ( x_l ) or ( x_u ) with ( x_c ) such that the interval continues to bracket the root.
- Iterate until the desired relative error is achieved or the maximum number of iterations is reached.
To run this code, you need the following libraries:
numpymathxlwt
To install the required libraries, you can use pip:
pip install numpy xlwt-
Clone the repository.
-
Ensure the script and the Excel file (
LAB1.xls) are in the same directory. -
Run the script using Python:
python bisection_method.py
-
Provide the required inputs when prompted:
- Enter the first initial value (( x_l )).
- Enter the second initial value (( x_u )).
- Enter the desired percentage relative error.
- Enter the number of iterations.
-
The script will compute the bisection method iterations and save the results in an Excel file named
LAB1.xls.
The code starts by importing the necessary libraries and taking user input for the initial values, desired relative error, and number of iterations. Then, it initializes arrays to store intermediate results. The main iteration loop of the Bisection Method computes the midpoints and function values, updating the intervals as necessary, until the termination criteria are met. Finally, the results are written into an Excel sheet.
Below is a snippet from the code illustrating the main iteration logic:
#initialization
x_l=np.zeros([ite])
x_u=np.zeros([ite])
x_c=np.zeros([ite])
f_xl=np.zeros([ite])
f_xu=np.zeros([ite])
f_xc=np.zeros([ite])
rel_err=np.zeros([ite])
itern=np.zeros([ite])
#storing initial computed values into array
x_l[0]=xl
x_u[0]=xu
f_xl[0]=fxl
f_xu[0]=fxu
#begin iteration
for i in range(ite):
#storing the values of iteration
itern[i]=i+1
#Bisection Formula
x_c[i]=(x_l[i]+x_u[i])/2
...
...The code completes by saving the final results into the Excel file LAB1.xls.
Below is an example of how to use the script:
-
Run the script:
python bisection_method.py
-
Enter the input values:
Enter 1st initial value: 20 Enter 2nd initial value: 30 Enter desired percentage relative error: 0.001 Enter number of iterations: 50 -
Output:
- The script will compute the Bisection Method iterations and print intermediate results on the console.
- The final results will be saved in an Excel file named
LAB1.xls.
bisection_method.py: The main script for performing the Bisection MethodLAB1.xls: Excel file generated by running the script
The script prompts for the following input values:
- Initial guess for the lower bound (
xl). - Initial guess for the upper bound (
xu). - Desired percentage relative error (
err). - Number of iterations (
ite).
- Initial Input Values: Ensure that the initial guesses bracket the root (
f(xl) * f(xu) < 0). If they do not, the script will print "Wrong initial input". - Function Evaluation: The function
(667.38/x)*(1-math.exp(-0.146843*x))-40is hardcoded. Modify it as needed for different functions. - Excel File Creation: Ensure you have write permissions in the directory where the script is run to save the Excel file.
- Python Version: This script is compatible with Python 3. Ensure you have Python 3 installed.
Script created by sudipto3331.
This documentation should guide you through understanding, installing, and using the Bisection Method script. For further issues or feature requests, please open an issue in the repository on GitHub. Feel free to contribute by creating issues and submitting pull requests. Happy coding!