Polynomial evaluation and roots in MATLAB - Time & Space Complexity
When working with polynomials in MATLAB, it is important to understand how the time needed to evaluate or find roots changes as the polynomial gets bigger.
We want to know how the number of steps grows when the polynomial degree increases.
Analyze the time complexity of the following code snippet.
% Evaluate polynomial at a point x
p = [3 -2 0 5]; % coefficients for 3x^3 - 2x^2 + 0x + 5
x = 2;
result = 0;
n = length(p);
for i = 1:n
result = result + p(i)*x^(n-i);
end
This code calculates the value of a polynomial at a given number x by adding each term one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each coefficient to calculate each term.
- How many times: The loop runs once for each coefficient, so n times where n is the polynomial degree plus one.
As the polynomial degree grows, the number of terms to calculate grows too, so the work grows roughly in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 multiplications and additions |
| 100 | About 100 multiplications and additions |
| 1000 | About 1000 multiplications and additions |
Pattern observation: Doubling the polynomial degree roughly doubles the work needed.
Time Complexity: O(n)
This means the time to evaluate the polynomial grows in direct proportion to its degree.
[X] Wrong: "Evaluating a polynomial always takes the same time no matter how big it is."
[OK] Correct: Larger polynomials have more terms, so they need more calculations, which takes more time.
Understanding how polynomial evaluation time grows helps you explain efficiency clearly and shows you can think about how code scales with input size.
"What if we used Horner's method to evaluate the polynomial? How would the time complexity change?"
Practice
polyval(p, x) do when p is a vector of polynomial coefficients and x is a number?Solution
Step 1: Understand the function inputs
The vectorpcontains polynomial coefficients, andxis the point where we want to evaluate the polynomial.Step 2: Identify the function purpose
polyvalcalculates the polynomial value atxusing the coefficients inp.Final Answer:
Evaluates the polynomial at the value x. -> Option AQuick Check:
polyval = evaluate polynomial [OK]
- Confusing polyval with roots function
- Thinking polyval finds zeros of polynomial
- Assuming polyval plots the polynomial
p in MATLAB?Solution
Step 1: Recall MATLAB root-finding function
MATLAB uses the functionrootsto find zeros of a polynomial given its coefficients.Step 2: Check syntax correctness
The correct syntax isroots(p), wherepis the coefficient vector.Final Answer:
roots(p) -> Option BQuick Check:
roots function syntax = roots(p) [OK]
- Using polyval instead of roots
- Trying non-existent functions like findroots
- Passing wrong arguments to roots
p = [1 -3 2]; % Represents x^2 - 3x + 2 r = roots(p);
Solution
Step 1: Identify the polynomial
The vectorp = [1 -3 2]represents the polynomialx^2 - 3x + 2.Step 2: Find roots of the polynomial
Factoring:(x-1)(x-2) = 0so roots are1and2. MATLAB returns a column vector[2; 1], matching option[2 1].Final Answer:
[2 1] -> Option CQuick Check:
Roots of x^2-3x+2 = 1, 2 [OK]
- Mixing up coefficient order
- Forgetting roots are values of x
- Assuming roots are negative
p = [2 -5 3] at x = 4. What is the error?p = [2 -5 3]; y = roots(p, 4);
Solution
Step 1: Understand function usage
The functionrootsonly takes one input: the coefficient vectorp.Step 2: Identify correct function for evaluation
To evaluate polynomial at a point, usepolyval(p, x), notroots.Final Answer:
roots does not take a second argument; use polyval instead. -> Option DQuick Check:
roots(p) finds zeros; polyval(p,x) evaluates [OK]
- Passing extra arguments to roots
- Confusing roots and polyval functions
- Assuming roots evaluates polynomial
p = [1 -6 11 -6] representing x^3 - 6x^2 + 11x - 6. Which MATLAB code correctly finds the roots and evaluates the polynomial at x = 2?Solution
Step 1: Find roots of polynomial
Useroots(p)to get zeros of polynomialp.Step 2: Evaluate polynomial at x=2
Usepolyval(p, 2)to find polynomial value at 2.Final Answer:
r = roots(p); y = polyval(p, 2); -> Option AQuick Check:
roots(p) finds zeros, polyval(p,x) evaluates [OK]
- Swapping roots and polyval calls
- Passing wrong arguments to roots
- Using roots with two arguments
Ready to prove it?
Pass the test and this pattern moves unknown
