|
1 | | -# Bisection-Method-Implementation-in-Python |
| 1 | +## Bisection Method Implementation in Python |
| 2 | + |
2 | 3 | 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. |
| 4 | + |
| 5 | +### Table of Contents |
| 6 | +- [Bisection Method Theory](#bisection-method-theory) |
| 7 | +- [Dependencies](#dependencies) |
| 8 | +- [Installation](#installation) |
| 9 | +- [Usage](#usage) |
| 10 | + |
| 11 | +### Bisection Method Theory |
| 12 | +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. |
| 13 | + |
| 14 | +**Steps:** |
| 15 | +1. Choose initial guesses \( x_l \) and \( x_u \) such that \( f(x_l) \cdot f(x_u) < 0 \). |
| 16 | +2. Compute the midpoint \( x_c \) of \( x_l \) and \( x_u \). |
| 17 | +3. Replace either \( x_l \) or \( x_u \) with \( x_c \) such that the interval continues to bracket the root. |
| 18 | +4. Iterate until the desired relative error is achieved or the maximum number of iterations is reached. |
| 19 | + |
| 20 | +### Dependencies |
| 21 | +To run this code, you need the following libraries: |
| 22 | +- `numpy` |
| 23 | +- `math` |
| 24 | +- `xlwt` |
| 25 | + |
| 26 | +### Installation |
| 27 | +To install the required libraries, you can use `pip`: |
| 28 | +```sh |
| 29 | +pip install numpy xlwt |
| 30 | +``` |
| 31 | + |
| 32 | +### Usage |
| 33 | +1. Clone the repository. |
| 34 | +2. Run the script using Python: |
| 35 | + |
| 36 | +```sh |
| 37 | +python bisection_method.py |
| 38 | +``` |
| 39 | + |
| 40 | +3. Provide the required inputs when prompted: |
| 41 | + - Enter the first initial value (\( x_l \)). |
| 42 | + - Enter the second initial value (\( x_u \)). |
| 43 | + - Enter the desired percentage relative error. |
| 44 | + - Enter the number of iterations. |
| 45 | + |
| 46 | +4. The script will compute the bisection method iterations and save the results in an Excel file named `LAB1.xls`. |
| 47 | + |
| 48 | +### Code Explanation |
| 49 | +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. |
| 50 | + |
| 51 | +Below is a snippet from the code illustrating the main iteration logic: |
| 52 | + |
| 53 | +```python |
| 54 | +#initialization |
| 55 | +x_l=np.zeros([ite]) |
| 56 | +x_u=np.zeros([ite]) |
| 57 | +x_c=np.zeros([ite]) |
| 58 | + |
| 59 | +f_xl=np.zeros([ite]) |
| 60 | +f_xu=np.zeros([ite]) |
| 61 | +f_xc=np.zeros([ite]) |
| 62 | + |
| 63 | +rel_err=np.zeros([ite]) |
| 64 | +itern=np.zeros([ite]) |
| 65 | +#storing initial computed values into array |
| 66 | +x_l[0]=xl |
| 67 | +x_u[0]=xu |
| 68 | + |
| 69 | +f_xl[0]=fxl |
| 70 | +f_xu[0]=fxu |
| 71 | +#begin iteration |
| 72 | +for i in range(ite): |
| 73 | + #storing the values of iteration |
| 74 | + itern[i]=i+1 |
| 75 | + #Bisection Formula |
| 76 | + x_c[i]=(x_l[i]+x_u[i])/2 |
| 77 | + ... |
| 78 | + ... |
| 79 | +``` |
| 80 | + |
| 81 | +The code completes by saving the final results into the Excel file `LAB1.xls`. |
| 82 | + |
| 83 | +### Files in the Repository |
| 84 | +- `bisection_method.py`: The main script for performing the Bisection Method |
| 85 | +- `LAB1.xls`: Excel file generated by running the script |
| 86 | + |
| 87 | +Feel free to contribute by creating issues and submitting pull requests. Happy coding! |
0 commit comments