Conteúdo do Curso
Mathematics for Data Analysis and Modeling
Mathematics for Data Analysis and Modeling
System of Linear Equations
A system of linear equations (SLE) is a set of equations where each equation is a linear combination of variables. The goal is to find a solution that satisfies all the equations simultaneously.
Example
Let's look at the example of a system of linear equations:
We have 3 unknown variables x
, y
and z
and have 3 equations that include all of these variables.
Solving the system
How can we solve the system? Firstly, let's rewrite it in a matrix form:
Expressing the system of linear equations in matrix form provides us with a straightforward approach for solving the system utilizing the inverse matrix:
To use this approach we have to be sure that matrix A can be inversed:
The square matrix A can be inversed if and only if its determinant is nonzero.
Example 1
import numpy as np # Define the coefficients matrix A A = np.array([[2, 3, -1], [4, -1, 2], [1, 2, -3]]) # Define the constants vector н y = np.array([10, 4, -1]) # Check the determinant of matrix A print(f'Determinant is {round(np.linalg.det(A), 3)}') # Calculating inversed matrix A_inv = np.linalg.inv(A) # Finding solution using inversed matrix x = np.dot(A_inv, y) # Print the solution print(f'Solution: {np.round(x, 3)}')
We have found the solution using the inverted matrix.
Example 2
import numpy as np # Define the coefficients matrix A A = np.array([[1, 2, 3], [3, 1, 4], [4, 3, 7]]) # Define the constants vector b b = np.array([10, 20, 30]) # Check the determinant of matrix A det_A = np.linalg.det(A) # Print the determinant value print(f'Determinant of A: {det_A}') A_inv = np.linalg.inv(A)
The code above produces an error - the matrix is singular (has zero determinant) so we can't solve the system of equations.
The explanation for this is quite simple: the matrix rows are linearly dependent (the third row is the sum of the first two). As a result, the third equation doesn't provide any additional information, and we are left with a system of 3 variables but only 2 unique equations. As a result such a system either have no solutions or there are lots of solutions.
Obrigado pelo seu feedback!