You are a guest. Restricted access. Read more

Polynomials

One can handle polynomials with symbolic variables. In this chapter, however, we work with the Octave 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]); printf('%f',a)
b=polyval(a,1)
a=-64/polyval(a,0) * a; printf('%f',a) 
a = polyfit([-4 -2 2 4 0],[0 0 0 0 -64],4)

One can use alternative engine requiring symbolic calculations:


a=poly([-4,-2,2,4]); printf('%f\n',a) 
a = -64/polyval(a,0) * a; printf('%f\n',a) 
a = polyfit([-4,-2,2,4,0],[0,0,0,0,-64],4)

Example 2: On a grid with $x,y$-Coordinates we have the following grey values 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 grey value do you expect at position $x=2,35; y=2,74$? Calculate the bi-cubic 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) 
printf('p=%f\n',p)
zp=polyval(p,2.74)
printf('zp=%f',zp)

Polynomial functions. Summary table

You are not full member and have a limited access to this section. One can unlock advanced pages after becoming a full member. You can also request to edit this manual and insert comments.

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  
ans=coeff(y,x,3)     % get one coefficient   
b=coeff(y,x,4:-1:0)  % or all at once
printf('%f\n',b)
ans=allroots(y)      % same as roots(a)
printf('%f\n',ans)

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 non-constant 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); printf('%f\n',y)
ans=coeff(y,x,2); printf('%f\n',ans)
ans=coeff(y,z,2); printf('%f',ans)

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);
ans=allroots(y); printf('All roots=%f\n',ans)
ans=solve(y,x);  printf('Solves=%f',ans)

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); printf('Input=%f\n',y)
z=sqfr(y);                     printf('sqfr=%f\n',z)

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.


ans=divide(122344,7623); printf('ans=%f',ans)


ans=divide(2+i,3+2*i);   printf('ans=%f\n',ans)


syms x,z
ans=divide(x^3*z-1,x*z-x,x); printf('ans=%f\n',ans)
ans=divide(x^3*z-1,x*z-x,z); printf('ans=%f',ans)


syms x,z
ans=gcd(32897397,24552502); printf('ans=%f\n',ans)
ans=gcd(z*x^5-z,x^2-2*x+1); printf('ans=%f',ans)

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); printf('Input=%f\n',y)
ans=realpart(y);   printf('Real=%f\n',ans)
ans=imagpart(y);   printf('Imag=%f',ans)

jmathlab/polynomials.txt · Last modified: 2013/04/14 16:52 (external edit)
Back to top
CC Attribution-Share Alike 3.0 Unported
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0