Skip to content

Commit 9410e55

Browse files
authored
Update README.md
1 parent 0233ef5 commit 9410e55

File tree

1 file changed

+198
-1
lines changed

1 file changed

+198
-1
lines changed

README.md

Lines changed: 198 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,199 @@
1-
# Gauss-Seidel-Elimination-Numerical-Method-Implementation-in-Python
1+
# Gauss Seidel Elimination Numerical Method Implementation in Python
2+
23
This repository contains a Python implementation of the Gauss-Seidel Elimination method for solving systems of linear equations. The code reads coefficients from an Excel file (`data.xls`), performs Gauss-Seidel iteration with relaxation, and saves the results in a new Excel file. The method also includes error checking and verifies the results.
4+
5+
## Table of Contents
6+
- [Gauss-Seidel Elimination Theory](#gauss-seidel-elimination-theory)
7+
- [Dependencies](#dependencies)
8+
- [Installation](#installation)
9+
- [Usage](#usage)
10+
- [Code Explanation](#code-explanation)
11+
- [Example](#example)
12+
- [Files in the Repository](#files-in-the-repository)
13+
- [Input Parameters](#input-parameters)
14+
- [Troubleshooting](#troubleshooting)
15+
- [Author](#author)
16+
17+
## Gauss-Seidel Elimination Theory
18+
The Gauss-Seidel method is an iterative technique for solving systems of linear equations. It improves upon the Jacobi method by using the latest updates to the values of the unknowns immediately within the iteration.
19+
20+
**Steps:**
21+
1. Initialize initial guesses for all unknowns.
22+
2. Iterate over each equation and solve for the current unknown using the latest available values of other unknowns.
23+
3. Apply a relaxation factor to accelerate convergence.
24+
4. Check for convergence by comparing the relative errors to a desired threshold.
25+
26+
## Dependencies
27+
To run this code, you need the following libraries:
28+
- `numpy`
29+
- `xlrd`
30+
- `xlwt`
31+
32+
## Installation
33+
To install the required libraries, you can use `pip`:
34+
```sh
35+
pip install numpy xlrd xlwt
36+
```
37+
38+
## Usage
39+
1. Clone the repository.
40+
2. Ensure the script and the Excel file (`data.xls`) are in the same directory.
41+
3. Run the script using Python:
42+
```sh
43+
python gauss_seidel_elimination.py
44+
```
45+
4. Provide the required inputs when prompted:
46+
- Enter the desired percentage relative error.
47+
- Enter the number of iterations.
48+
- Enter the relaxation factor.
49+
5. The script will read the coefficients from the Excel file, perform Gauss-Seidel Elimination, and write the results into a new Excel file named `xlwt_example.xls`.
50+
51+
## Code Explanation
52+
The code begins by importing the necessary libraries and defining a helper function `brcond_Seidel` to check the convergence condition. It then reads the coefficients and constants from the Excel file into numpy arrays. The main iteration loop performs Gauss-Seidel updates and checks for convergence. The results are written into a new sheet in an Excel file.
53+
54+
Below is a snippet from the code illustrating the main logic:
55+
56+
```python
57+
import numpy as np
58+
import xlrd
59+
from xlwt import Workbook
60+
61+
def brcond_Seidel(E, err, n):
62+
a = 0
63+
for i in range(n):
64+
if E[i] < err:
65+
a += 1
66+
return a / n
67+
68+
err = float(input('Enter desired percentage relative error: '))
69+
ite = int(input('Enter number of iterations: '))
70+
relaxation = float(input('Enter relaxation factor: '))
71+
72+
loc = ('data.xls')
73+
wb = xlrd.open_workbook(loc)
74+
sheet = wb.sheet_by_index(3)
75+
76+
n = sheet.nrows
77+
a = np.zeros([n, n+1])
78+
E = np.zeros([n])
79+
rel_err = np.zeros([ite, n])
80+
X = np.zeros([n])
81+
x = np.zeros([ite, n])
82+
x_new = np.zeros([ite, n])
83+
itern = np.zeros([ite])
84+
p = np.zeros([n])
85+
86+
for i in range(sheet.ncols):
87+
for j in range(sheet.nrows):
88+
a[j, i] = sheet.cell_value(j, i)
89+
90+
# Iteration for Gauss-Seidel begins here
91+
for j in range(ite):
92+
itern[j] = j + 1
93+
for i in range(n):
94+
summation = 0
95+
for k in range(n):
96+
if k != i:
97+
summation += a[i, k] * x[j, k]
98+
x_new[j, i] = (a[i, n] - summation) / a[i, i]
99+
if j > 0:
100+
x[j, i] = x_new[j, i] * relaxation + (1 - relaxation) * x[j-1, i]
101+
else:
102+
x[j, i] = x_new[j, i]
103+
if j > 0:
104+
rel_err[j, i] = abs((x[j, i] - x[j-1, i]) / x[j, i]) * 100
105+
E[i] = rel_err[j, i]
106+
107+
if j > 0 and brcond_Seidel(E, err, n) == 1:
108+
break
109+
elif j == ite - 1:
110+
break
111+
112+
x[j+1, :] = x[j, :]
113+
114+
num_of_iter = j
115+
X = x[j, :]
116+
117+
print('The values of the unknown variables are respectively:')
118+
print(X)
119+
120+
wb = Workbook()
121+
sheet1 = wb.add_sheet('Sheet 1')
122+
123+
sheet1.write(0, n, 'Gauss')
124+
sheet1.write(0, n+1, 'Seidel')
125+
sheet1.write(1, 0, 'Number of iteration')
126+
127+
for i in range(1, n+1):
128+
sheet1.write(1, i, 'x_' + str(i))
129+
for i in range(n+1, 2*n+1):
130+
sheet1.write(1, i, 'Relative error of x_' + str(i-n))
131+
132+
for i in range(num_of_iter+1):
133+
sheet1.write(i+2, 0, itern[i])
134+
for j in range(n):
135+
sheet1.write(i+2, 1+j, x[i, j])
136+
sheet1.write(i+2, n+1+j, rel_err[i, j])
137+
138+
sheet1.write(i+4, 0, 'The')
139+
sheet1.write(i+4, 1, 'unknown')
140+
sheet1.write(i+4, 2, 'values')
141+
sheet1.write(i+4, 3, 'are:')
142+
for k in range(n):
143+
sheet1.write(i+4, k+4, X[k])
144+
145+
wb.save('xlwt_example.xls')
146+
147+
# Result Verification
148+
for i in range(n):
149+
summation = 0
150+
for j in range(n):
151+
summation += a[i, j] * X[j]
152+
p[i] = summation - a[i, j+1]
153+
154+
print('The verification results are:')
155+
print(p)
156+
print('The implementation is correct if verification results are all zero')
157+
```
158+
159+
The code completes by saving the final results into the Excel file `xlwt_example.xls` and prints verification results.
160+
161+
## Example
162+
Below is an example of how to use the script:
163+
164+
1. Prepare the `data.xls` file with the system of equations in matrix form.
165+
2. **Run the script**:
166+
```sh
167+
python gauss_seidel_elimination.py
168+
```
169+
170+
3. **Enter the input values**:
171+
```
172+
Enter desired percentage relative error: 0.001
173+
Enter number of iterations: 50
174+
Enter relaxation factor: 1.0
175+
```
176+
177+
4. **Output**:
178+
- The script will compute the results using the Gauss-Seidel Elimination method and write the intermediate and final results into the Excel file (`xlwt_example.xls`).
179+
- The console will display the values of the unknowns and the verification results.
180+
181+
## Files in the Repository
182+
- `gauss_seidel_elimination.py`: The main script for performing the Gauss-Seidel Elimination method.
183+
- `data.xls`: Excel file from which the matrix data is read and into which the results are written.
184+
185+
## Input Parameters
186+
The initial input data is expected to be in the form of a matrix within the `data.xls` file. Each row represents coefficients of the variables in the equations along with the constants.
187+
188+
## Troubleshooting
189+
1. **Excel File**: Ensure that the input matrix is correct and placed in the `data.xls` file.
190+
2. **Matrix Format**: Confirm that the matrix is complete and correctly formatted.
191+
3. **Excel File Creation**: Ensure you have write permissions in the directory where the script is run to save the Excel file.
192+
4. **Python Version**: This script is compatible with Python 3. Ensure you have Python 3 installed.
193+
194+
## Author
195+
Script created by sudipto3331.
196+
197+
---
198+
199+
This documentation should guide you through understanding, installing, and using the Gauss-Seidel Elimination 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!

0 commit comments

Comments
 (0)