0
0
NumPydata~20 mins

np.linalg.solve() for linear systems in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linear System Solver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of solving a 2x2 linear system
What is the output of the following code that solves a system of linear equations?
import numpy as np
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
x = np.linalg.solve(A, b)
print(x)
NumPy
import numpy as np
A = np.array([[3, 1], [1, 2]])
b = np.array([9, 8])
x = np.linalg.solve(A, b)
print(x)
A[3. 2.]
B[2. 3.]
C[1. 4.]
D[4. 1.]
Attempts:
2 left
💡 Hint
Remember that np.linalg.solve finds x in Ax = b.
data_output
intermediate
2:00remaining
Number of solutions for a singular matrix
What happens when you try to solve a linear system with a singular matrix using np.linalg.solve()? Consider this code:
import numpy as np
A = np.array([[1, 2], [2, 4]])
b = np.array([5, 10])
x = np.linalg.solve(A, b)

What is the result?
NumPy
import numpy as np
A = np.array([[1, 2], [2, 4]])
b = np.array([5, 10])
x = np.linalg.solve(A, b)
AReturns solution [5, 10]
BReturns solution [1, 2]
CReturns solution [0, 0]
DRaises a LinAlgError due to singular matrix
Attempts:
2 left
💡 Hint
Singular matrices do not have unique solutions.
🔧 Debug
advanced
2:00remaining
Identify the error in solving a linear system
What error will this code produce?
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([7, 8])
x = np.linalg.solve(A, b)
NumPy
import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6]])
b = np.array([7, 8])
x = np.linalg.solve(A, b)
ALinAlgError: Last 2 dimensions of the array must be square
BValueError: shapes (2,3) and (2,) not aligned
CTypeError: unsupported operand type(s) for +: 'int' and 'str'
DNo error, returns solution
Attempts:
2 left
💡 Hint
np.linalg.solve requires a square coefficient matrix.
🚀 Application
advanced
2:00remaining
Using np.linalg.solve to find intersection point
Two lines are given by equations:
3x + 2y = 13
7x - y = 19
Use np.linalg.solve to find the intersection point (x, y). What is the output?
NumPy
import numpy as np
A = np.array([[3, 2], [7, -1]])
b = np.array([13, 19])
x = np.linalg.solve(A, b)
print(x)
A[4. 1.]
B[2. 3.]
C[3. 2.]
D[1. 5.]
Attempts:
2 left
💡 Hint
Set up the system as Ax = b and solve.
🧠 Conceptual
expert
2:00remaining
Understanding conditions for np.linalg.solve success
Which condition must be true for np.linalg.solve(A, b) to successfully find a unique solution to Ax = b?
AMatrix A must be square and invertible
BMatrix A must be rectangular with more rows than columns
CVector b must be all positive values
DMatrix A must be symmetric
Attempts:
2 left
💡 Hint
Think about when a system has a unique solution.