Lesson 5
Solving Systems of Equations with NumPy
Introduction to Systems of Linear Equations

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.

Understanding Systems of Linear Equations

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 text
13x + y = 9 2x + 2y = 8

This can be represented in matrix form as:

Plain text
1[[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.

Using NumPy to Solve Systems of Equations: Step-by-Step Example

We'll now explore how to solve the above system of equations using NumPy. Here's the step-by-step breakdown:

  1. Define Matrices and Vectors: First, define the coefficients matrix A and the constants vector b.

    Python
    1import numpy as np 2 3A = np.array([[3, 1], [1, 2]]) 4b = np.array([9, 8])

    Here, A is a 2x2 matrix, and b is a vector with two constants.

  2. Solve the System: Use np.linalg.solve() to compute the solution.

    Python
    1solution = np.linalg.solve(A, b)
  3. Display the Result: Finally, print out the solution.

    Python
    1print("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.

Relating to Matrix Inversion

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:

Python
1coeff_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.

Additional Example and Practical Considerations

Let's consider another example for better understanding:

Suppose we have:

Plain text
12x + 3y = 13 23x + 4y = 20

This corresponds to:

Plain text
1A = [[2, 3], b = [13, 2 [3, 4]] 20]

You can solve this similarly with:

Python
1A = 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.

Summary and Preparing for Practice

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.