You are on page 1of 2

PYTHON PROGRAMMING LAB DATE:

EXPERIMENT-5

5. To solve the system of linear equations using Gauss - Siedal method

The Gauss-Seidel Method is a specific iterative method, that is always using the latest
estimated value for each elements in xx. For example, we first assume the initial values
for x2,x3,⋯,xnx2,x3,⋯,xn (except for x1x1), and then we can calculate x1x1. Using the
calculated x1x1 and the rest of the xx (except for x2x2), we can calculate x2x2. We can
continue in the same manner and calculate all the elements in xx. This will conclude the first
iteration. We can see the unique part of Gauss-Seidel method is that we are always using the
latest value for calculate the next value in xx. We can then continue with the iterations until the
value converges. Let us use this method to solve the same problem we just solved above.

SOURCE CODE:
import numpy as np
from scipy.optimize import curve_fit
 
from matplotlib import pyplot as plt
 
x = np.linspace(0, 10, num = 40)
 
# The coefficients are much bigger.
y = 10.45 * np.sin(5.334 * x) + np.random.normal(size = 40)
 
def test(x, a, b):
    return a * np.sin(b * x)
 
param, param_cov = curve_fit(test, x, y)
 
print("Sine function coefficients:")
print(param)
print("Covariance of coefficients:")
print(param_cov)
 
ans = (param[0]*(np.sin(param[1]*x)))
 
plt.plot(x, y, 'o', color ='red', label ="data")
plt.plot(x, ans, '--', color ='blue', label ="optimized data")
plt.legend()
plt.show()

OUTPUT:
Sine funcion coefficients:
[ 0.70867169 0.7346216 ]
Covariance of coefficients:
[[ 2.87320136 -0.05245869]
PYTHON PROGRAMMING LAB DATE:

[-0.05245869 0.14094361]]

You might also like