x
we will
now create a symbolic polynomial p
. Conversely, we can extract the
coefficients from a symbolic polynomial using the function
coeff(p, x, exponent)
. The command allroots(p)
returns the zeros.
>> a=[3 2 5 7 4]; % coefficients >> syms x >> y=polyval(a,x) % symbolic polynomial y = 3*x^4+2*x^3+5*x^2+7*x+4 >> coeff(y,x,3) % get one coefficient ans = 2 >> b=coeff(y,x,4:-1:0) % or all at once b = [ 3 2 5 7 4 ] >> allroots(y) % same as roots(a) ans = [ 0.363-1.374i 0.363+1.374i -0.697-0.418i -0.697+0.418i ]
Up to this point there is little advantage of using symbolic calculations,
it is just another way of specifying a problem.
The main benefit of symbolic calculations emerges when
we are dealing with more than one symbolic variable, or,
meaning essentially the same, when our polynomial has
nonconstant coefficients. This case can be treated efficiently
only with symbolic variables. Notice in the example
below how the polynomial y is automatically multiplied through,
and brought into a canonical form. In this form the symbolic variables
are sorted alphabetically, i.e. z
is main variable compared to x
.
The coefficients can be calculated for each variable separately.
>> syms x,z >> y=(x-3)*(x-1)*(z-2)*(z+1) y = (x^2-4*x+3)*z^2+(-x^2+4*x-3)*z+(-2*x^2+8*x-6) >> coeff(y,x,2) ans = z^2-z-2 >> coeff(y,z,2) ans = x^2-4*x+3