Video: Polynomials in MATLAB (7.08)

 

 

Transcript

We all are familiar with the polynomials. Most commonly we use linear, quadratic and cubic polynomials.

And sometimes higher order polynomials.

 

In MATLAB, a polynomial is represented by an array. An array of its coefficients in desending order of the powers. The cooefficient of highest power comes first, then the next highest power and so on. At the end it is

a place for the constant term because it is the lowest powre term. Power is zero.

 

For example polynomial x4 squared minus 3x plus 2 - in MATLAB it is written as an array 4, minus 3 and 2.

Let us name it - say p. So this respresents a polynomial. 4 x squared minus 3 x plus 2. Let me enter it so it is saved in the Workspace.

 

Now How to write the polynomial, say 2x cubed -27 in MATLAB. Well it will be an array - so let's write it. So

I want to write 2 x cubed minus 27. Now the highest power is x cubed, and the coefficient of x cubed is

2. So the first number is 2. Next highest power is x squared, but there is no x squared term. So zero. There is no x term either so zero for that. The last term is minus 27, the constant.

 

So this is a polynomial 2 x cubed minus 27. Let's name it say q equal to this and  save in the workspace.

 

Now how to multiply two polynomials? There is a command called conv to multiply two polynomials. Conv for

convolution actually, so conv. So if I want to multiply p and q I will type conv, and in the bracket, p comma q. And this will return me the coefficient array of the resulting polynomial.

So, this is what we got.

 

So the last place is reserved for the constant term. then 1, 2, 3, 4, 5. So the highest power is x to the power of 5. So the polynomial is 8 x to the power of 5, minus 6 x to the power 4, plus 4 x cubed, minus 108 x squared, plus 81 x minus 54.

 

And I would like to show you something. Of course you will learn about symbolic toolbox very soon. So this is something to do with Symbolic Toolbox. If you type in the command window poly2sym then this command will convert any polynomial array in a symbolic polynomial.

 

So I want to know what this last polynomial looks like. So while we didn't name it but here is the name ans

Lets just type ans. And see what it is. Yes - this is the polynomial.

 

So to multiply 2 polynomials you can use conv function, and to divide 2 polynomials you can use deconv function and just follow the syntax. You can use p and q straight away in multiplication and division. However, if you have to add 2 polynomials, you might need to do some extra work.

 

For example, if I want to add p and q, I can not add them straight away. So if I write p plus q, it going to give me an error. Because p got 3 members in it and q got 4. So we should have same size of the arrays. So what I am going to do I am adding an extra zero to the beginning of p.

So I am wrhiting p is equal to zero in the beginning and p and this new array p is actually the same polynomial, but now it got size 4. Now you can do p plus q. And that is the result.

 

There are some other useful commands for polynomials - one of them is polyval.

Suppose you are given a polynomial say 1, 2, 3, which means polynomial is x squared plus 2 x plus 3.

And suppose you want to calculate its value at x equal to 5. So you will just type polyval, the polynomial array, and comma 5 close the bracket and this will evaluate the polynomial at x equal to 5.

 

Roots command can be used to find roots of a polynomial.

So suppose you want to know roots of the polynomial x squared plus 2 x plus 3, you just type roots and the polynomial array as input, it will give you both the roots. Of course they are complex numbers this time. Okay let's do some other poynomial that has real root. So maybe this - 1, minus 5 - 14, I think it has real roots, Yes. 7 and minus 2.

 

You can then recreate the polynomials from the roots. So there is a command poly. So if you want to

what poly nomial has these roots, 7 and minus 2, so I just type poly, 7 minus 2 in a square bracket, no comma enter, you have got the coefficient of the polynomial which has these roots. See this is a double check as well.

 

 

Last modified: Monday, 10 August 2015, 10:20 AM