https://github.com/sudipto3331/false-position-numerical-method-implementation-in-python
This repository contains a Python implementation of the False Position Method, also known as the Regula Falsi Method, for finding roots of nonlinear equations. Additionally, the script compares the performance of the False Position Method with the Bisection Method, saving results in an Excel file (`LAB2.xls`).
https://github.com/sudipto3331/false-position-numerical-method-implementation-in-python
excel excel-export false-position-method numerical-methods python3 spyder-python-ide
Last synced: 12 months ago
JSON representation
This repository contains a Python implementation of the False Position Method, also known as the Regula Falsi Method, for finding roots of nonlinear equations. Additionally, the script compares the performance of the False Position Method with the Bisection Method, saving results in an Excel file (`LAB2.xls`).
- Host: GitHub
- URL: https://github.com/sudipto3331/false-position-numerical-method-implementation-in-python
- Owner: sudipto3331
- License: mit
- Created: 2024-08-27T07:16:04.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-27T07:22:54.000Z (over 1 year ago)
- Last Synced: 2025-01-07T17:29:52.637Z (about 1 year ago)
- Topics: excel, excel-export, false-position-method, numerical-methods, python3, spyder-python-ide
- Language: Python
- Homepage: https://sudiptomondal.me/
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# False Position Method Implementation in Python
This repository contains a Python implementation of the False Position Method, also known as the Regula Falsi Method, for finding roots of nonlinear equations. Additionally, the script compares the performance of the False Position Method with the Bisection Method, saving results in an Excel file (`LAB2.xls`).
### Table of Contents
- [False Position Method Theory](#false-position-method-theory)
- [Dependencies](#dependencies)
- [Installation](#installation)
- [Usage](#usage)
- [Code Explanation](#code-explanation)
- [Example](#example)
- [Files in the Repository](#files-in-the-repository)
- [Input Parameters](#input-parameters)
- [Troubleshooting](#troubleshooting)
- [Author](#author)
### False Position Method Theory
The False Position Method is a numerical technique for finding roots of a function. It works by drawing a secant line between two points that bracket the root and using the intersection of this line with the x-axis as the next approximation. It combines elements of the Bisection and Secant methods.
**Steps:**
1. Choose initial guesses \( x_l \) and \( x_u \) such that \( f(x_l) \times f(x_u) < 0 \).
2. Compute the intersection point \( x_c \) of the line connecting \( (x_l, f(x_l)) \) and \( (x_u, f(x_u)) \).
3. Replace either \( x_l \) or \( x_u \) with \( x_c \) such that the interval continues to bracket the root.
4. Iterate until the desired relative error is achieved or the maximum number of iterations is reached.
### Dependencies
To run this code, you need the following libraries:
- `numpy`
- `math`
- `xlwt`
- `xlrd`
- `xlutils`
### Installation
To install the required libraries, you can use `pip`:
```sh
pip install numpy xlwt xlrd xlutils
```
### Usage
1. Clone the repository.
2. Ensure the script and the Excel file (`LAB2.xls`) are in the same directory.
3. Run the script using Python:
```sh
python false_position_method.py
```
4. 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.
5. The script will compute the iterations and save the results, comparing both methods in an Excel file named `LAB2.xls`.
### Code Explanation
The code starts by importing the necessary libraries and defining the function whose root is to be found. It then defines two functions for the Bisection Method and False Position Method. The script takes user input for initial values, desired relative error, and number of iterations, and then calls these functions. The results are written into an Excel sheet.
Below is a snippet from the code illustrating the main logic:
```python
def fnc(x):
return (667.38/x)*(1-math.exp(-0.146843*x))-40
def bisection(fxl, fxu, err, ite):
# Initialization and iteration logic for Bisection Method
...
def fasleposition(fxl, fxu, err, ite, index):
# Initialization and iteration logic for False Position Method
...
```
The code completes by saving the final results into the Excel file `LAB2.xls`, showing a comparison between the two methods.
### Example
Below is an example of how to use the script:
1. **Run the script**:
```sh
python false_position_method.py
```
2. **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
```
3. **Output**:
- The script will compute the iterations using both the Bisection Method and False Position Method and print intermediate results on the console.
- The final results, comparing both methods, will be saved in an Excel file named `LAB2.xls`.
### Files in the Repository
- `false_position_method.py`: The main script for performing the False Position and Bisection Methods.
- `LAB2.xls`: Excel file generated by running the script.
### Input Parameters
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`).
### Troubleshooting
1. **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".
2. **Function Evaluation**: The function `(667.38/x)*(1-math.exp(-0.146843*x))-40` is hardcoded. Modify it as needed for different functions.
3. **Excel File Creation**: Ensure you have write permissions in the directory where the script is run to save the Excel file.
4. **Python Version**: This script is compatible with Python 3. Ensure you have Python 3 installed.
## Author
Script created by sudipto3331.
---
This documentation should guide you through understanding, installing, and using the False Position and Bisection Methods 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!