# Solving Systems of Linear Equations

### A notebook detailing codes for solving a system of linear equations using the Gaussian Elimination Method:


```python
def Gaussian(M):
    # eliminate columns
    for i in range(len(M[0])):
        for j in range(i+1, len(M)):
            r = [(jValue * (-(M[j][i] / M[i][i]))) for jValue in M[i]]
            M[j] = [sum(pair) for pair in zip(M[j], r)]
    # backsolve by substitution
    ans = []
    M.reverse() # makes it easier to backsolve
    for sol in range(len(M)):
            if sol == 0:
                ans.append(M[sol][-1] / M[sol][-2])
            else:
                inner = 0
                # substitute in all known coefficients
                for x in range(sol):
                    inner += (ans[x]*M[sol][-2-x])
                # the equation is now reduced to ax + b = c form
                # solve with (c - b) / a
                ans.append((M[sol][-1]-inner)/M[sol][-sol-2])
    ans.reverse()
    return ans
```

Given:
\begin{equation}
2x+y+z = 1 \\
6x+2y+z = -1 \\
-2+2y+z = 7
\end{equation}


```python
M = [[2, 1, 1, 1],
     [6, 2, 1, -1],
     [-2, 2, 1, 7]]
Mans = Gaussian(M)
print 'x = %i' %Mans[0]
print 'y = %i' %Mans[1]
print 'z = %i' %Mans[2]
```

    x = -1
    y = 2
    z = 1


066a2c6b03bd6fae432c28e53eafa922ef123fd5
ipynb
Jupyter Notebook
Numerical Method Codes/Gaussian Elimination Method.ipynb
lindleezy/Numerical-Methods
1cb8f65d09f9ae7b00e6530a6960055dcc6c1174
MIT
Numerical Method Codes/Gaussian Elimination Method.ipynb
lindleezy/Numerical-Methods
1cb8f65d09f9ae7b00e6530a6960055dcc6c1174
MIT
Numerical Method Codes/Gaussian Elimination Method.ipynb
lindleezy/Numerical-Methods
1cb8f65d09f9ae7b00e6530a6960055dcc6c1174
MIT
Qwen/Qwen-72B
1. YES
2. YES