Bird
Raised Fist0
MATLABdata~10 mins

Polynomial evaluation and roots in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Polynomial evaluation and roots
Define polynomial coefficients
↓
Evaluate polynomial at x
↓
Calculate polynomial value
↓
Find roots of polynomial
↓
Output roots and values
Start by defining polynomial coefficients, then evaluate the polynomial at a point x, calculate its value, find roots, and output results.
Execution Sample
MATLAB
p = [1 -3 2];
x = 2;
y = polyval(p, x);
r = roots(p);
Defines polynomial p(x) = x^2 - 3x + 2, evaluates it at x=2, and finds its roots.
Execution Table
StepActionExpressionResult
1Define polynomial coefficientsp = [1 -3 2][1 -3 2]
2Set evaluation pointx = 22
3Evaluate polynomial at xy = polyval(p, x)0
4Calculate roots of polynomialr = roots(p)[1; 2]
5Output resultsy and ry=0, r=[1; 2]
💡 All steps completed; polynomial evaluated and roots found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
pundefined[1 -3 2][1 -3 2][1 -3 2][1 -3 2][1 -3 2]
xundefinedundefined2222
yundefinedundefinedundefined000
rundefinedundefinedundefinedundefined[1; 2][1; 2]
Key Moments - 2 Insights
Why does polyval(p, 2) return 0 even though the polynomial looks complex?
Because at x=2, the polynomial x^2 - 3x + 2 equals zero, as shown in execution_table step 3 where y=0.
Why are the roots returned as [2; 1] and not in ascending order?
MATLAB roots function returns roots in no guaranteed order; here roots are 1 and 2 as in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of y after step 3?
A2
B1
C0
D-1
💡 Hint
Check the 'Result' column in row with Step 3 in execution_table.
At which step are the roots of the polynomial calculated?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for root calculation.
If x was changed to 1, what would be the new value of y in the variable_tracker?
A1
B0
C2
DUndefined
💡 Hint
Evaluate polynomial at x=1: 1^2 - 3*1 + 2 = 0; relate to variable_tracker for y.
Concept Snapshot
Polynomial evaluation and roots in MATLAB:
- Define polynomial as vector of coefficients, highest power first.
- Use polyval(p, x) to evaluate polynomial at x.
- Use roots(p) to find polynomial roots.
- Roots may be real or complex.
- Example: p=[1 -3 2] means x^2 -3x +2.
Full Transcript
This example shows how to define a polynomial in MATLAB using a vector of coefficients. We then evaluate the polynomial at a specific value x using the polyval function. Next, we find the roots of the polynomial using the roots function. The execution table traces each step, showing variable values and results. Key moments clarify why the polynomial evaluates to zero at x=2 and why roots order is not guaranteed. The visual quiz tests understanding of these steps. The concept snapshot summarizes the key commands and behavior for polynomial evaluation and root finding in MATLAB.

Practice

(1/5)
1. What does the MATLAB function polyval(p, x) do when p is a vector of polynomial coefficients and x is a number?
easy
A. Evaluates the polynomial at the value x.
B. Finds the roots of the polynomial p.
C. Plots the polynomial curve over x.
D. Calculates the derivative of the polynomial at x.

Solution

  1. Step 1: Understand the function inputs

    The vector p contains polynomial coefficients, and x is the point where we want to evaluate the polynomial.
  2. Step 2: Identify the function purpose

    polyval calculates the polynomial value at x using the coefficients in p.
  3. Final Answer:

    Evaluates the polynomial at the value x. -> Option A
  4. Quick Check:

    polyval = evaluate polynomial [OK]
Hint: polyval means evaluate polynomial at given points [OK]
Common Mistakes:
  • Confusing polyval with roots function
  • Thinking polyval finds zeros of polynomial
  • Assuming polyval plots the polynomial
2. Which of the following is the correct syntax to find the roots of a polynomial with coefficients stored in vector p in MATLAB?
easy
A. polyval(p)
B. roots(p)
C. findroots(p)
D. solve(p)

Solution

  1. Step 1: Recall MATLAB root-finding function

    MATLAB uses the function roots to find zeros of a polynomial given its coefficients.
  2. Step 2: Check syntax correctness

    The correct syntax is roots(p), where p is the coefficient vector.
  3. Final Answer:

    roots(p) -> Option B
  4. Quick Check:

    roots function syntax = roots(p) [OK]
Hint: Use roots() to find polynomial zeros in MATLAB [OK]
Common Mistakes:
  • Using polyval instead of roots
  • Trying non-existent functions like findroots
  • Passing wrong arguments to roots
3. What is the output of the following MATLAB code?
p = [1 -3 2]; % Represents x^2 - 3x + 2
r = roots(p);
medium
A. [1 2]
B. [3 2]
C. [2 1]
D. [-1 -2]

Solution

  1. Step 1: Identify the polynomial

    The vector p = [1 -3 2] represents the polynomial x^2 - 3x + 2.
  2. Step 2: Find roots of the polynomial

    Factoring: (x-1)(x-2) = 0 so roots are 1 and 2. MATLAB returns a column vector [2; 1], matching option [2 1].
  3. Final Answer:

    [2 1] -> Option C
  4. Quick Check:

    Roots of x^2-3x+2 = 1, 2 [OK]
Hint: Roots are where polynomial equals zero [OK]
Common Mistakes:
  • Mixing up coefficient order
  • Forgetting roots are values of x
  • Assuming roots are negative
4. The following MATLAB code is intended to evaluate the polynomial p = [2 -5 3] at x = 4. What is the error?
p = [2 -5 3];
y = roots(p, 4);
medium
A. The syntax is correct; no error.
B. The variable y is not defined before use.
C. The vector p should be transposed before calling roots.
D. roots does not take a second argument; use polyval instead.

Solution

  1. Step 1: Understand function usage

    The function roots only takes one input: the coefficient vector p.
  2. Step 2: Identify correct function for evaluation

    To evaluate polynomial at a point, use polyval(p, x), not roots.
  3. Final Answer:

    roots does not take a second argument; use polyval instead. -> Option D
  4. Quick Check:

    roots(p) finds zeros; polyval(p,x) evaluates [OK]
Hint: Use polyval to evaluate, roots only finds zeros [OK]
Common Mistakes:
  • Passing extra arguments to roots
  • Confusing roots and polyval functions
  • Assuming roots evaluates polynomial
5. You have a 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?
hard
A. r = roots(p); y = polyval(p, 2);
B. r = polyval(p, 2); y = roots(p);
C. r = roots(2); y = polyval(p);
D. r = roots(p, 2); y = polyval(p, 2);

Solution

  1. Step 1: Find roots of polynomial

    Use roots(p) to get zeros of polynomial p.
  2. Step 2: Evaluate polynomial at x=2

    Use polyval(p, 2) to find polynomial value at 2.
  3. Final Answer:

    r = roots(p); y = polyval(p, 2); -> Option A
  4. Quick Check:

    roots(p) finds zeros, polyval(p,x) evaluates [OK]
Hint: roots() finds zeros; polyval() evaluates at points [OK]
Common Mistakes:
  • Swapping roots and polyval calls
  • Passing wrong arguments to roots
  • Using roots with two arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard