Welcome to the final lesson in our course on using NumPy for linear algebra. In this lesson, we dive into solving systems of equations, a common challenge in fields like physics, engineering, and economics. Systems of equations are essentially collections of two or more equations involving the same set of variables. Solving these systems is crucial for various practical applications.
A system of linear equations is simply a set of equations with multiple variables that you aim to solve. Mathematically, they can be represented in matrix form for computational convenience. Consider the following simple system of equations:
Plain text13x + y = 9 2x + 2y = 8
This can be represented in matrix form as:
Plain text1[[3, 1], [x] = [9] 2 [1, 2]] [y] [8]
Here, the matrix on the left is known as the coefficients matrix, and the one on the right is the constants vector.
We'll now explore how to solve the above system of equations using NumPy. Here's the step-by-step breakdown:
-
Define Matrices and Vectors: First, define the coefficients matrix
A
and the constants vectorb
.Python1import numpy as np 2 3A = np.array([[3, 1], [1, 2]]) 4b = np.array([9, 8])
Here,
A
is a 2x2 matrix, andb
is a vector with two constants. -
Solve the System: Use
np.linalg.solve()
to compute the solution.Python1solution = np.linalg.solve(A, b)
-
Display the Result: Finally, print out the solution.
Python1print("Coefficient Matrix A:\n", A) 2print("Constants Vector b:", b) 3print("Solution:", solution) 4 5# Output: 6# Coefficient Matrix A: 7# [[3 1] 8# [1 2]] 9# Constants Vector b: [9 8] 10# Solution: [2. 3.]
The solution array [2. 3.]
indicates that ( x = 2 ) and ( y = 3 ), which satisfies both equations.
In the previous course of this course path, we learned about matrix inversion, which is another method to solve systems of linear equations. By using an invertible coefficient matrix, we can find the solution of the system by multiplying the inverse of the coefficient matrix with the constants vector:
Python1coeff_matrix = np.array([[3, 1], [1, 2]]) 2constants = np.array([9, 8]) 3 4inverse = np.linalg.inv(coeff_matrix) 5solution_via_inverse = inverse.dot(constants) 6print("Solution from inversion method:", solution_via_inverse) 7 8# Output: 9# Solution from inversion method: [2. 3.]
Both methods, np.linalg.solve()
and matrix inversion, yield the same results for systems with invertible matrices. However, np.linalg.solve()
is often preferred due to efficiency and numerical stability.
Let's consider another example for better understanding:
Suppose we have:
Plain text12x + 3y = 13 23x + 4y = 20
This corresponds to:
Plain text1A = [[2, 3], b = [13, 2 [3, 4]] 20]
You can solve this similarly with:
Python1A = np.array([[2, 3], [3, 4]]) 2b = np.array([13, 20]) 3solution = np.linalg.solve(A, b) 4print("Solution:", solution) 5 6# Output: 7# Solution: [2. 3.]
Keep in mind that not all systems have solutions. If A
is singular (not invertible), an error will occur. It is wise to check the determinant of A
using np.linalg.det(A)
before attempting to solve, ensuring it is non-zero.
In this lesson, you learned how to solve systems of linear equations using NumPy, building upon your existing knowledge of matrix functions. Remember, correctly setting up your matrix and vector is key to successfully applying np.linalg.solve()
. Practice this skill with various systems to gain confidence.
You've reached the end of this course. Well done! Your journey through key linear algebra concepts using NumPy is complete. You have the skills needed to apply these techniques to solve real-world problems. Engage with the practice exercises that follow this lesson to solidify your understanding. Congratulations on your dedication and accomplishment!