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?
Suppose we are given a polynomial function, let’s say:
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!
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:
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:
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:
Do you find the exact red elements of the equation above in the original polynomial equation? Well, that’s because:
–> = =
–> = =
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”:
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:
Of degree 6:
and so on…
But what about our very first original equation?
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:
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 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:
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:
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:
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.
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:
Both lines will give us the same value:
Not convinced yet? Let’s try another example. We have to get the value (evaluate) the following polynomial equation:
when x = 8. What’s its value? Polyval knows:
p = [6 0 0 0 0 5.5 0 0]
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:
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:
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]
Summary with Examples
And finally I wanna summarize all this info with two examples. In a first example we are given the function:
evaluate it when x = 2.
Using polyval we write:
polyval([2 -5 0.1 0 -2], 2)
And the result should be:
In a second example we have the function:
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:
Do you see your cubic function? It should look something like this:
Great, right? I hope this post was useful for you. Play around and leave a comment of your results or doubts.