Lesson 2
Solving Linear Equation Systems with SciPy
Introduction to Linear Equation Systems

Linear equation systems are collections of linear equations that share the same set of variables. They are fundamental in both mathematics and real-world applications, such as engineering, physics, and economics, where multiple relationships can be modeled simultaneously. For instance, when dealing with electrical circuits, mechanical systems, or economic models, you'll often encounter linear systems that need solutions.

In this lesson, we'll explore how to solve linear equation systems using Python, with a focus on leveraging the SciPy library.

Preparing the Environment and Recall

Before we begin, let's recall some basics. You might remember that NumPy provides a way to work with arrays and matrices, which are essential for representing and solving linear systems.

SciPy builds on top of NumPy and provides a suite of functions specifically designed for scientific and technical computing.

Recall

Let's refresh it a bit. A linear equation in its general form can be written as:

a1x1+a2x2++anxn=ba_1x_1 + a_2x_2 + \cdots + a_nx_n = b

where a1,a2,,ana_1, a_2, \ldots, a_n are coefficients, x1,x2,,xnx_1, x_2, \ldots, x_n are variables, and bb is a constant term.

In matrix form, a system of linear equations can be represented as:

Ax=bAx = b

where:

  • A is the coefficient matrix.
  • x is the column vector of variables (unknowns).
  • b is the constant terms vector.
Formulating Linear Equation Systems

Here's an example of formulating a 3x3 linear system:

Consider the following equations:

{3x+2yz=12x2y+4z=2x+0.5yz=0\begin{cases} 3x + 2y - z = 1 \\ 2x - 2y + 4z = -2 \\ -x + 0.5y - z = 0 \end{cases}

We can represent these equations in matrix form as:

A=[32122410.51],b=[120]A = \begin{bmatrix} 3 & 2 & -1 \\ 2 & -2 & 4 \\ -1 & 0.5 & -1 \end{bmatrix}, \quad b = \begin{bmatrix} 1 \\ -2 \\ 0 \end{bmatrix}

where A contains coefficients for the variables x, y, z, and b contains free coefficients.

Using SciPy's `solve` Function

The solve function from the scipy.linalg module is used to find the solution vector x for the equation Ax=bAx = b. Let's walk through the steps involved.

First, we need to import the required modules. These are scipy.linalg for the solve function and numpy for handling arrays.

Python
1from scipy.linalg import solve 2import numpy as np

We define the matrix A and vector b using NumPy arrays, which will represent our system of equations.

Python
1A = np.array([[3, 2, -1], 2 [2, -2, 4], 3 [-1, 0.5, -1]]) 4 5b = np.array([1, -2, 0])

We use the solve function to find the solution vector x.

Python
1x = solve(A, b)

The solve function works by calculating the determinant of matrix A. If det(A) ≠ 0, it finds the inverse of A and solves for x using the formula:

x=A1bx = A^{-1}b

If det(A) = 0, the matrix A is singular, meaning it does not have an inverse, and the system may have no solutions or infinitely many solutions. In such cases, the solve function will raise a LinAlgError.

Finally, we print the solution to see the result of our computation.

Python
1print("Solution for vector x:", x) # Solution for vector x: [1. -2. -2.]
Verifying Solutions

Verifying the solution is a crucial step to ensure correctness. We can verify our solution by recalculating AxAx and checking if it equals bb using NumPy's dot function.

Python
1lhs = np.dot(A, x)

Here, lhs stands for "left-hand side," and it holds the result of A multiplied by x. We then print both the calculated and original b vectors to check for consistency.

Python
1print("Verification (Ax):", lhs) 2print("Original b vector:", b)

Output:

1Verification (Ax): [ 1. -2. 0.] 2Original b vector: [ 1 -2 0]

As you can see, the computed AxAx equals the original bb vector, confirming the solution is correct.

Summary and Transition to Practice

In this lesson, you've learned how to solve linear equation systems using SciPy's solve function. We covered the representation of equations in matrix form, the setup and solving of the system, and the verification of the solution using NumPy. Practice solving similar systems using the exercises that follow to reinforce these concepts. Understanding these fundamentals will be valuable as you progress through more complex applications of linear algebra and computational mathematics.

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