Solving linear systems (A\b) in MATLAB - Time & Space Complexity
When solving linear systems using A\b in MATLAB, it's important to know how the time needed grows as the system size increases.
We want to understand how the work changes when the matrix gets bigger.
Analyze the time complexity of the following code snippet.
n = 1000;
A = rand(n);
b = rand(n,1);
x = A \ b;
This code creates a random square matrix A and vector b, then solves for x in the equation A*x = b.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Matrix factorization and back substitution steps inside A\b.
- How many times: These steps involve nested loops over the matrix size, roughly repeating n times inside n times.
As the matrix size n grows, the number of operations grows much faster because each element interacts with many others.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1,000 |
| 100 | About 1,000,000 |
| 1000 | About 1,000,000,000 |
Pattern observation: When n increases ten times, operations increase about a thousand times, showing a cubic growth.
Time Complexity: O(n^3)
This means the time needed grows roughly with the cube of the matrix size, so doubling n makes the work about eight times bigger.
[X] Wrong: "Solving A\b takes time proportional to n, like a simple loop."
[OK] Correct: The solution involves nested operations on the matrix, not just one pass, so it grows much faster than n.
Understanding how solving linear systems scales helps you explain performance in real tasks like simulations or data analysis, showing you know how algorithms behave with bigger data.
"What if the matrix A is sparse (mostly zeros)? How would the time complexity change?"
Practice
x = A\b do?Solution
Step 1: Understand the expression A\b
The operator \ in MATLAB is used to solve linear systems of the form Ax = b.Step 2: Identify the purpose of A\b
It finds the vector x that satisfies the equation Ax = b without explicitly calculating the inverse of A.Final Answer:
Solves the linear system Ax = b for x -> Option AQuick Check:
A\b solves Ax = b [OK]
- Confusing A\b with matrix multiplication
- Thinking it calculates inverse of A
- Using b\A instead of A\b
Solution
Step 1: Recall MATLAB syntax for solving linear systems
The correct syntax uses the backslash operator: x = A\b;Step 2: Check other options for correctness
x = b/A; uses b/A which is incorrect; x = inv(A)\b; uses inv(A)\b which solves inv(A)x = b (wrong system); x = A*b; multiplies A and b which is not solving the system.Final Answer:
x = A\b; -> Option AQuick Check:
Use backslash operator for solving Ax = b [OK]
- Using b/A instead of A\b
- Using inv(A)\b which solves wrong system
- Confusing multiplication with solving
A = [2 1; 3 4] and b = [5; 10], what is the result of x = A\b?Solution
Step 1: Write the system of equations
2x1 + x2 = 5
3x1 + 4x2 = 10.Step 2: Solve using elimination
Multiply first equation by 4: 8x1 + 4x2 = 20.
Subtract second: 5x1 = 10 -> x1 = 2.
Substitute: x2 = 5 - 2*2 = 1.Step 3: Verify solution
A*[2; 1] = [2*2 + 1*1; 3*2 + 4*1] = [5; 10] = b.Final Answer:
[2; 1] -> Option DQuick Check:
A\b = [2; 1] [OK]
- Miscalculating substitution (e.g., picking [1; 2])
- Confusing with matrix multiplication A*b
- Selecting x = b
A = [1 2; 3 4]; b = [5; 6; 7]; x = A\b;
Solution
Step 1: Check dimensions of A and b
Matrix A is 2x2, vector b is 3x1. For Ax = b, number of rows in A must equal length of b.Step 2: Identify dimension mismatch
Since A has 2 rows but b has 3 elements, dimensions are incompatible, causing an error when solving.Final Answer:
Matrix A and vector b have incompatible dimensions -> Option BQuick Check:
Rows of A must match length of b [OK]
- Ignoring dimension mismatch errors
- Thinking backslash works for any sizes
- Assuming A must be square always
A = [1 2 3; 4 5 6] and vector b = [7; 8]. What does x = A\b compute in MATLAB?Solution
Step 1: Recognize matrix shape and operation
A is 2x3 (more columns than rows), so system is underdetermined.Step 2: Understand MATLAB's backslash behavior for non-square A
MATLAB computes the least-squares solution x that minimizes ||Ax - b|| (minimum norm among solutions).Final Answer:
A least-squares solution minimizing ||Ax - b|| -> Option CQuick Check:
Backslash solves least-squares for non-square A [OK]
- Expecting error for non-square A
- Thinking it calculates inverse always
- Assuming exact solution always exists
Ready to prove it?
Pass the test and this pattern moves unknown
