How to use polyval

One of the easiest and most useful tools in Matlab is polyval, a very nice function that evaluates a polynomial function given its parameters and the range to evaluate… huh? All right, all right, we wanna be clear here, right?

polyval_matlab

Suppose we are given a polynomial function, let’s say:

f(x) = 3x^3 + 4x + 2

and we would like to represent it in Matlab like that. Well, we cannot just write it like that. Remember Matlab is a numerical computation programm, which means, that it won’t compute any symbol. So forget it if you wanna computate something writting letters. Matlabs wants only numbers. You might put names to the variables, but still, Matlab computes only with numbers.

Now, what to do? There’s where our great friend polyval comes to the rescue!

Mathematical background

Skip this section if you already know how a Polynomial Equation works.

As you probably have noticed (you’re readinf this because you use them), any polynomial equation is written as the form:

a_nx^n + a_{n-1}x^{n-1} + \dotsb + a_2x^2 + a_1x + a_0

That is, for every variable x, there is a constant a that multiplies it. And they all sum up. Remeber that the values for a can be any real value.

To be practical (or just seem fancy), some guys write it with a sumation as:

\sum_{i=0}^{n} a_ix^i

Now have a look at the last two terms of the long equation. Notice that there are no powers… or are there?

Actually yes, there are, but follow their logic. You can see that the variable n is decreasing until it becomes 0. Check this:

a_nx^n + a_{n-1}x^{n-1} + \dotsb + a_2x^2 + a_1x^1+ a_0x^0

Do you find the exact red elements of the equation above in the original polynomial equation? Well, that’s because:

x^1 = x   –>   a_1x^1a_1xa_1x

x^0 = 1   –>   a_0x^0 = a_01a_0

In the first case it’s not necessary to write that an element has power 1, we all know that.

In the second case, any number times 1 is the same number: a * 1 = a.

So why to write redundant powers and multiplications? Keep it simple. “The shorter the better” is the battle cry when you write any scientific paper. Therefore we keep it “short”:

a_nx^n + a_{n-1}x^{n-1} + \dotsb + a_2x^2 + a_1x + a_0

So yes, every sub-index of a matches the power of its corresponding x, and they decrease from n to 0. You can start from any value of n, provided that n is a positive integer.

Too much mathematical formality? Ok, give n a value greater than 0 without decimals. We need numbers like 3, 10, 100 or 249583745. AND don’t put numbers after the point, i.e. no 3.1415 nor 7.5, etc.

The highest power in an x, defines the degree of the polynomial. So, if you say you have a polynomial of degree 4, it will look like:

a_4x^4 + a_3x^3 + a_2x^2 + a_1x + a_0

Of degree 6:

a_6x^6 + a_5x^5 + a_4x^4 + a_3x^3 + a_2x^2 + a_1x + a_0

and so on…

But what about our very first original equation?

f(x) = 3x^3 + 4x + 2

Its highest power is 3, thus is a polynomial of degree 3 (a.k.a. cubic). It is also defined as a polynomial equation like the others, even if it doesn’t have any squared power written:

f(x) =3x^3 + 0x^2 + 4x + 2

You see how x2 is multiplying a zero? Our equation also had its corresponding constant a2, but it is equal to 0. A very decent way to identify a friend-zoned variable.

And the last element is a_0 equal to 2.

If you enumerate the parameters (the constants a) of our equation, it would look like:

a = 3, 0, 4, 2

From now on, get use to the fact that names of lists are noted with bolded letters, because we are gonna treat them as vectors.

How to write that list as a vector?

a = (3  0  4  2) = (3, 0, 4, 2)

That is a list that runs from left to right. In other words, is a row-vector. With or without commas, it is already understood that we’re handling a list of elements, especially with Matlab.

If we want to convert it into a column-vector, we can simply transpose it and write an upperscripted T at the end of the vector:

a = (3, 0, 4, 2)T

Did you get the idea so far? Great. Now that you know how polynomial equations are built and represented, we can see how to use them in Matlab.

Going to Matlab

Polyval is a very simple function that asks mainly for two things: the parameters and the variables. I like to think about it as in a block diagram:

polyval(block)

You can see it demands two inputs and gives you back a value. What are those?

The parameters are expressed as a vector (a list if you want it), where we numerate them from the left-most parameter to the right-most. That is from an to a0. For our equation it would look like:

polyval parameters

Written in Matlab, it is:

p = [3 0 4 2]

That line of code will take the list of parameters and put it in a vector named p (I chose p because it’s easier to relate it to “parameters”). When you write an array of elements in Matlab, a comma and a simple space are equally used to separate the elements. So

p = [3 0 4 2]

is the same as to write

p = [3, 0, 4, 2]

The variables are the quantities we want to evaluate with. For example, we want to know what is the value of our function when x = 5. It is written as:

f(5) = 3*5^3 + 4*5 + 2

The x in the equation was substituted by 5 in each element. Now we just do simple first grade math and we discover that our function is equal to 397 when x is equal to 5.

f(5) = 397

Using polyval we can simply write it as:

polyval([3 0 4 2], 5)

As you can see, we give the list of parameters (inside brackets) and then the variable. Don’t forget to separate the inputs with a coma.

But hey! We already have those values in the vector p, right? So, we can then write:

polyval(p, 5)

Both lines will give us the same value:

397

Not convinced yet? Let’s try another example. We have to get the value (evaluate) the following polynomial equation:

f(x) = 6x^7 + 5.5x^2

when x = 8. What’s its value? Polyval knows:

p = [6 0 0 0 0 5.5 0 0]
polyval(p, 8)

Did you see what I did here? First put the parameters of the equation in p, where only a7 and a2 have non-zero values. Then evaluate when the variable is equal to 8 with polyval to get:

12583264

And it is as simple as that. Yuhuuuu!

More than one variable

But hey! What happens if I want to evaluate my equation with more than one variable? Should I write polyval many times? Should I use a loop? Absolutely… not!

A good thing with polyval is that you can use another list of variables in the second input, instead of a single number. Like this:

polyval(p, [1.5 2.8 3.4])

In this case, polyval will evaluate our equation with the values 1.5, 2.8 and 3.4 (given our fist list of parameters p = [3 0 4 2]):

18.1250   79.0560  133.5120

Now it gives 3 values back, each one is the result of evaluating our function with the three given values for x:

f(x) = 3x^3 + 4x + 2

f(1.5) = 18.125

f(2.8) = 79.056

f(3.4) = 133.512

And it becomes even bigger (or shorter). We now want to evaluate it with more values… yes, a list stored in a vector is also used if desired. First store it and then write it in polyval:

x = [1.5 2.8 3.4 5.6 8.7]
polyval(p, x)

That’s all!

Summary with Examples

And finally I wanna summarize all this info with two examples. In a first example we are given the function:

f(x) = 2x^4 - 5x^3 + 0.1x^2 - 2

evaluate it when x = 2.

Using polyval we write:

polyval([2 -5 0.1 0 -2], 2)

And the result should be:

-9.6000

In a second example we have the function:

f(x) = 4x^3 + 1.1x^2 - 8

We want to evaluate it in the range from -5 to 8 and store all the values in the vector y. For this evaluation, we better use vectors containing the lists of elements:

p = [4 1.1 0 -8];
x = -5:8;
y = polyval(p, x);

Boom! You have it! Now you can use polyval to evaluate any polynomial function with any variable. Try visualizing your data. Write:

plot(x,y)

Do you see your cubic function? It should look something like this:

cubic function

Great, right? I hope this post was useful for you. Play around and leave a comment of your results or doubts.

Advertisements

2 thoughts on “How to use polyval

  1. Pingback: A fancy visualization of planes intersecting – Part 2 | Mayitzin

  2. I think everything wrote was actually very logical. But, think about this, what if you
    were to write a killer headline? I mean, I don’t want to
    tell you how to run your website, but suppose you added a
    post title that makes people want more? I mean How to use
    polyval | Mayitzin is a little vanilla. You could glance at Yahoo’s home page and see how they create article titles
    to get people to click. You might add a related video or a picture or two to get people interested about everything’ve got to say.
    Just my opinion, it might make your blog a little livelier.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s