0
0
MATLABdata~10 mins

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

Choose your learning style9 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.