Systems of equations · Python Programming Class 11–12 | StemPy Lab
Course 2.1·Lesson 4 of 5
Systems of equations
A system of linear equations is written with a matrix A of the coefficients, a vector x of the unknowns and a vector b of the right sides, as A times x equal to b. In NumPy, @ is the matrix product, * works place by place, and np.linalg.solve of A and b solves the system. np.allclose of A at x and b checks the answer. Two equations are two lines: they cross at one point when the determinant is not 0, are parallel with no solution, or are the same line with many solutions, and in the last two cases the determinant is 0 and np.linalg.solve raises a LinAlgError. The method works for any number of unknowns, for example the currents of a circuit found with the laws of Kirchhoff.
about 45 min1 simulation2 practice questions
Lesson 4 of 5Show all lessonsHide
Try the code and the simulations beside the lesson.
What you will be able to do
After this lesson you can
write a system of linear equations as a matrix equation,
solve it with np.linalg.solve and check the answer,
explain when a system has one solution, none, or many, and
solve problems with three unknowns, such as the currents of a circuit.
Two equations, two lines
You know how to solve 2x+y=7 and x−y=−1 by hand: add them, to get 3x=6, so x=2 and y=3. Each equation is a straight line, and the solution is the point where the lines cross. With three or ten unknowns, solving by hand takes too long. NumPy does it in one line.
First, we write the system with a matrix. The coefficients make a table A, the unknowns make a column x, and the right sides make a column b:
(211−1)(xy)=(7−1)
Multiplying out the left side gives the two equations. We write this short as Ax=b.
Matrices in NumPy
Predict first. Is A * B the same as A @ B? Then run it.
A matrix is a two-dimensional array, as in chapter 1.
A * B multiplies place by place, as for every array: [[0 2] [3 0]].
A @ B is the matrix product: each place is the sum of the products of a row of A and a column of B. Here it gives [[2 1] [4 3]]. These are different! The matrix product is the one that is needed for equations.
A @ v multiplies a matrix by a vector: 1×1+2×1=3 and 3×1+4×1=7.
A.T is the transpose (rows and columns exchanged). np.eye(2) is the identity matrix, which changes nothing in a product.
np.linalg.det(A) is the determinant: 1×4−2×3=−2. It is a single number that says whether the system can be solved.
Solving
Predict first. The solution is x=2, y=3. What will A @ solution be? Then run it.
Three small graphs of two straight lines each. On the left, one solution: the lines 2x plus y equal to 7 and x minus y equal to minus 1 cross at one black dot, at 2 and 3. In the middle, no solution: the lines x plus y equal to 2 and 2x plus 2y equal to 5 are parallel and never meet. On the right, many solutions: the lines x plus y equal to 2 and 2x plus 2y equal to 4 lie on top of each other and are the same line.
np.linalg.solve(A, b) finds the vector x for which Ax=b. The answer is [2. 3.], as we found by hand.
Always check.A @ solution must give back b. The test np.allclose(A @ solution, b) says True. (It uses allclose because of the tiny rounding errors.)
Use solve and not the inverse: you may know that x=A−1b, and np.linalg.inv(A) @ b gives the same answer, but solve is faster and more accurate.
Word problems
Three unknowns: a circuit
The solution is I1=3 A, I2=2 A and I3=1 A. The program checks it another way: the 3 ohm and 6 ohm resistors in parallel have the resistance 1/(1/3+1/6)=2 ohm, so the total resistance is 2+2=4 ohm and the current is 12/4=3 A. The two methods agree.
Set a to 0.5: the two lines have the same slope and never meet. That is a singular matrix.
No solution, or many solutions
Three small graphs of two straight lines each. On the left, one solution: the lines 2x plus y equal to 7 and x minus y equal to minus 1 cross at one black dot, at 2 and 3. In the middle, no solution: the lines x plus y equal to 2 and 2x plus 2y equal to 5 are parallel and never meet. On the right, many solutions: the lines x plus y equal to 2 and 2x plus 2y equal to 4 lie on top of each other and are the same line.
The picture shows three things that can happen with two lines. In the middle and on the right, the lines are parallel: their slopes are equal, and the determinant is zero.
Common mistakes
*Using ` for a matrix product.** Use @`.
Putting the coefficients in a different order in different rows. Each column must belong to the same unknown.
Forgetting to check the answer.np.allclose(A @ x, b).
Using integers and comparing with ==. Use np.allclose.
Ignoring a singular matrix. It tells you something about the problem.
Practice
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Warm-up. Open the lab Solve the system. You write a safe solver and a checker.
Build. Open the problem A diet of three foods and the problem No solution or many solutions.
Remember these
Ax=b: a matrix of the coefficients, a column of the unknowns, a column of the right sides.
A @ B is the matrix product. A * B is place by place.
np.linalg.solve(A, b) solves, and np.allclose(A @ x, b) checks.
Determinant not zero: one solution. Determinant zero: none or many, and NumPy raises a LinAlgError.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
How do you write x+2y=5 and 3x−y=1 as Ax=b?
(132−1)
,x =
(xy)
,b =
(51)
$.
What is the determinant of $
(1224)
$, and what does it tell you?
Which is the matrix product, A * B or A @ B?
Why use np.allclose to check a solution?
Enroll free in Solving Equations
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.