An open API service indexing awesome lists of open source software.

https://github.com/sudipto3331/secant-numerical-method-implementation-in-python

This repository contains a Python implementation of the Secant 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 (`secant.xls`).
https://github.com/sudipto3331/secant-numerical-method-implementation-in-python

excel-export numerical-analysis numerical-methods python3 secant-method

Last synced: 18 days ago
JSON representation

This repository contains a Python implementation of the Secant 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 (`secant.xls`).

Awesome Lists containing this project

README

          

# Secant Method Implementation in Python

This repository contains a Python implementation of the Secant 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 (`secant.xls`).

### Table of Contents
- [Secant Method Theory](#secant-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)

### Secant Method Theory
The Secant Method is a root-finding algorithm which uses a succession of roots of secant lines to better approximate a root of a function. It is an iterative method that does not require the derivative of the function, unlike the Newton-Raphson method.

**Steps:**
1. Choose two initial guesses \( x_l \) and \( x_u \).
2. Compute the next approximation using:
\[
x_c = x_u - f(x_u) \frac{x_l - x_u}{f(x_l) - f(x_u)}
\]
3. Repeat 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`

### Installation
To install the required libraries, you can use `pip`:
```sh
pip install numpy xlwt
```

### Usage
1. Clone the repository.
2. Ensure the script and the Excel file (`secant.xls`) are in the same directory.
3. Run the script using Python:
```sh
python secant_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 in an Excel file named `secant.xls`.

### Code Explanation
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 Secant Method computes the intermediate values, evaluates the function, and calculates the relative error. Finally, the results are written into an Excel sheet.

Below is a snippet from the code illustrating the main logic:

```python
for i in range(ite):
itern[i] = i + 1

f_xl[i] = (667.38 / x_l[i]) * (1 - math.exp(-0.146843 * x_l[i])) - 40
f_xu[i] = (667.38 / x_u[i]) * (1 - math.exp(-0.146843 * x_u[i])) - 40

# Secant Formula
x_c[i] = x_u[i] - (f_xu[i] * (x_l[i] - x_u[i]) / (f_xl[i] - f_xu[i]))
f_xc[i] = (667.38 / x_c[i]) * (1 - math.exp(-0.146843 * x_c[i])) - 40

# calculating error
if i > 0:
rel_err[i] = ((x_c[i] - x_c[i-1]) / x_c[i]) * 100

# terminate if error criteria meets
if all([i > 0, abs(rel_err[i]) < err]):
break
elif f_xc[i] == 0:
break

if i == ite - 1:
break

x_l[i+1] = x_u[i]
x_u[i+1] = x_c[i]
```

The code completes by saving the final results into the Excel file `secant.xls`.

### Example
Below is an example of how to use the script:

1. **Run the script**:
```sh
python secant_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 the Secant Method and print intermediate results on the console.
- The final results will be saved in an Excel file named `secant.xls`.

### Files in the Repository
- `secant_method.py`: The main script for performing the Secant Method.
- `secant.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 are reasonable starting points for the root.
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 Sudipto.

---

This documentation should guide you through understanding, installing, and using the Secant 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!