Jasymca can handle polynomials with symbolic variables. In this chapter, however, we work with the Matlab/Octave/Scilab-approach of using vectors as list of polynomial coefficients: A polynomial of degree n is represented by a vector having n+1 elements, the element with index 1 being the coefficient of the highest exponent in the polynomial. With poly(x) a normal polynomial is created, whose zeros are the elements of x, polyval(a,x) returns function values of the polynomial with coefficients a in the point x, roots(a) calculates the zeros, and polyfit(x,y,n) calculates the coefficients of the polynomial of degree , whose graph passes through the points x and y. If their number is larger than n+1 a least square estimate is performed. The regression analysis in exercise 8 was performed using this method.
The roots of a 4th-degree polynomial are -4,-2,2,4 and it intersects the y-axis at (y=-64). Calculate its coefficients:
>> a=poly([-4,-2,2,4]) a = [ 1 0 -20 0 64 ] >> a = -64/polyval(a,0) * a a = -1 0 20 0 -64 >> a = polyfit([-4,-2,2,4,0],[0,0,0,0,-64],4) a = -1 0 20 0 -64
Example 2: On a grid with -Koordinaten we have the following greyvalues of a digital image:
y\x 1 2 3 4 1 98 110 122 136 2 91 112 131 141 3 73 118 145 190 4 43 129 170 230
Which greyvalue do you expect at position ? Calculate the bicubic interpolation.
>> Z=[98,110,122,136; > 91,112,131,141; > 73,118,145,190; > 43,129,170,230]; >> i=1; p=polyfit(1:4,Z(i,:),3); z(i)=polyval(p,2.35); >> i=2; p=polyfit(1:4,Z(i,:),3); z(i)=polyval(p,2.35); >> i=3; p=polyfit(1:4,Z(i,:),3); z(i)=polyval(p,2.35); >> i=4; p=polyfit(1:4,Z(i,:),3); z(i)=polyval(p,2.35); >> p=polyfit(1:4,z,3); zp=polyval(p,2.74) zp = 124.82
We have learnt that polynomials may be represented by the vector of their coefficient. Using a symbolic variable 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.
The command allroots functions with variable coefficients also, but only, if the polynomials degree in the main variable is smaller than 3, or it is biquadratic. If roots of other variables x are searched, one should use the more general solve(p,x), which will be discussed in more detail later.
The decomposition of p in linear, quadratic, cubic etc factors is accomplished by sqfr(p). Returned is a vector of factors sorted in ascending order of the exponents.
>> syms x >> y=(x-1)^3*(x-2)^2*(x-3)*(x-4) y = x^7-14*x^6+80*x^5-242*x^4+419*x^3-416*x^2+220*x-48 >> z=sqfr(y) z = [ x^2-7*x+12 x-2 x-1 ]
The division of two polynomials p and q in one polynomial and remainder is calculated using divide(p,q). If the polynomials have more than one variable, an optional variable can be specified, which will be used for division. gcd(p,q) returns the greatest common denominator of two expressions. Both functions also work with numbers as arguments.