# DMelt:JMathlab/Polynomials

## Contents

# Polynomials

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 $n$, 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 $x,y$-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 $x=2,35; y=2,74$? Calculate the bicubic interpolation.

Solution:

```
>> 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
```

## Polynomial functions. Summary table

No access to this part. Use Member area to request membership. If you are already a member, login to Member area and come back to this wiki. |

No access to this part. Use Member area to request membership. If you are already a member, login to Member area and come back to this wiki. |

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.

```
>> 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
```

## Roots

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.

```
>> syms x,z
>> y = x*z^2-3*x*z+(2*x+1);
>> allroots(y)
ans = [ sqrt((1/4*x-1)/x)+3/2 -sqrt((1/4*x-1)/x)+3/2 ]
>> solve(y,x)
ans = -1/(z^2-3*z+2)
```

## Squarefree Decomposition

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 ]
```

## Division and Common Denominator

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.

```
>> divide(122344,7623)
ans = [ 16 376 ]
>> divide(2+i,3+2*i)
ans = [ 1/2 1/2 ]
>> syms x,z
>> divide(x^3*z-1,x*z-x,x)
ans = [ x^2*z/(z-1) -1 ]
>> divide(x^3*z-1,x*z-x,z)
ans = [ x^2 x^3-1 ]
>> gcd(32897397,24552502)
ans = 377
>> gcd(z*x^5-z,x^2-2*x+1)
ans = x-1
```

## Real- and Imaginary Part

realpart(expression) and imagpart(expression) is used to decompose complex expressions. Symbolic variables in these expressions are assumed to be real-valued.

```
>> syms x
>> y=(3+i*x)/(2-i*x)
y = (-x+3i)/(x+2i)
>> realpart(y)
ans = (-x^2+6)/(x^2+4)
>> imagpart(y)
ans = 5*x/(x^2+4)
```

More information on this topic is in DMelt books |