Today we will be covering the following topics:
1. Polynomial Operations – Roots, Poly, fminsearch
2. Roots of Non-Linear Functions – Fzero
2. Fitting Data to a Polynomial
3. Evaluating how well the data fit
VIDEO COMING SOON
Polynomial Operations – Roots, Poly
To find the roots (aka the zeroes) of a polynomial function, use the command roots.
Example – Finding roots of a quadratic polynomial
The syntax for this is roots(p), where p is the polynomial you want to find the roots of. Say we want to find the roots of a quadratic equation 4x^2 – 2x -5. These will be put in order from the highest exponent to the lowest, in a vector p (it can be named any variable, but that will be your input to the roots command). I already have the equation in order, with the first term being that of the x^2, the second term with the x (aka x^1) and the last term is just a number (-5, which means x^0). Creating a vector p with these coefficients looks like this:
Using the roots command with p looks like this:
It gives this output as the roots. These have the letter i in them because they are imaginary numbers, meaning there are no real roots.
Example – Finding roots of a quartic polynomial
Let’s find the roots of the quartic polynomial (quartic meaning the highest exponent is a 4, as in x^4), x^4 – 1:
The way I have set this up above is in order of the coefficients in front of the term with the x^4, then x^3, then so on until x^0 (which is the constant value, -1). Because we have an x^4, the coefficient is a 1 as the first value in the vector. There are no other terms with x, so all the other coefficients are 0. The last term is a -1, because the term at the end of the brackets is the one without an x attached to it. This is why I used the vector [1 0 0 0 -1].
Now to find the roots of this polynomial, which are also imaginary as the first example was:
Example – Finding polynomial given the roots using Poly command
To go backwards and find which polynomial gave you specific roots, use the poly command. Let’s do this with roots -2 and 2. You will need to use square brackets to define the zeroes as I have used [-2 2] within round brackets and the command poly.
The answer it gave above is 1 0 -4. Remember that MATLAB gives the answer of the polynomial whose roots are -2 and 2, as a vector with the coefficients above. Looking at this output, it means that the polynomial has the highest degree of 2 because there are three terms (1, 0, and -4). Take the number of terms and subtract 1 to get the degree of your polynomial. Knowing the highest degree is 2, the first number on the left of the output (1) has an x^2 attached. Then it goes in order of the next number down has an x^1, then the last number has an x^0, which means it has no variable x attached with it. This gives x^2 – 4 as the polynomial whose roots would be -2 and 2.
Polynomial Operations – Minimum of a Function
To find the minimum point of a function, use fminbnd(function,x1,x2). Replace function with the name of the function you want to find the minimum of, and x1,x2 is the x-value range of the values you want to find the minimum between (x1 is the lower value, x2 is the higher one).
Example – Using fminbnd
I want to find the minimum of the following function, which I am defining as an anonymous function:
Now I will use it within fminbnd to find the minimum of the function between x-values of -5 to 5. Putting these values into the fminbnd syntax gives the following answer of 2. This means the function hits a minimum point at x = 2.
We can also find what y-value corresponds to that x-value, by calling the anonymous function with the value we obtained, x = 2. To call an anonymous function, remember that we use the name of the function with the number beside it in brackets. Our anonymous function is y, and the value we are finding the y-value at is x = 2. This is why I have used y(2).
How do we know this is correct? Well, we can check the graph! This is what the graph of the function looks like, and we can see at x = 2 and about close to y = 1, the graph is at a minimum.
Roots of Non-Linear Functions – Fzero
To find the roots of a function that is not a polynomial, you can use fzero. You can also use this with polynomials, but you don’t have to. It will only solve for one variable though, so you can’t use an equation that has more than one unknown!
To find the roots of a non-linear function (linear functions will work as well, but it is usually reserved for non-linear functions) use the fzero(function name, x0) command. This command works by finding where the function changes sign as well as where it equals 0. If you have a piecewise function (one that is not continuous), note that using this command may return points at which the function is discontinuous.
For the syntax mentioned above, function name will be replaced by the name of a function you want to take the roots of. x0 will be a starting point for MATLAB to begin finding the roots of the function from. It is important to choose initial guess values that are fairly close to the real roots. Plot the function using the fplot command (click here if you missed the lesson on the fplot command) and choose x-values that are close to the real roots. We will cover this in the examples. You may have heard of another command for function plotting – ezplot – but Mathworks does not recommend to use it.
To make this command work, the function must actually cross the x-axis, and not just touch it (in the case of x^2, for example). If you have a function that does follow the rule, then you may want to try fsolve instead. Check out this lesson on how to use fsolve. Now to use fzero, we can either define the function we want to solve as an inline function or an m-file function. Let’s check out examples with both.
Example – Finding roots of a non-linear function defined by an inline function, using Fzero
Say we want to find the roots of a function using fzero, and we want to define the function as an inline function. We will need to define the inline function using the @ symbol (click here if you missed the lesson on inline functions) first.
I have defined a random function and assigned it to variable y. Now we will use fzero(function name, x0) to get MATLAB to solve for x, which is the unknown variable. fzero takes in two arguments – function name, and x0. How do we choose x0? We will need to make sure this initial guess is relatively close to the real root (or roots) – plot the function beforehand to see what it looks like. Here I defined the unknown variable x using syms, which will treat any unknowns as variables rather than undefined values. I also used grid on to be able to easily see what values the graph is crossing the axis at:
Here is a separate script just to check the graph to identify the root (or roots if your graph has more than one). Then we will go back to the main code where we use fzero. I didn’t put these in one script together, because we want to identify the root with fplot before we use fzero – not have them run within the same code:
This is the graph that comes up. I have drawn the axis in red, and the green area is where the graph is crossing the x-axis. Now I am not sure on the exact x-value where it crosses, so we can use a range of 0 to 2 as the initial guess, x0.
Let’s use the fzero command with the 2 arguments – the function name (y, as I had assigned the function to this variable) and the initial guess ([0 2]).
The output is the following. This means that the graph is crossing the x-axis at x = 1, which is the root (aka the zero) of the function:
Example – Finding roots of a non-linear function defined by an m-file function, using Fzero
Here I have defined a function (m-file function) called fun (Click here if you missed the lesson m-file functions). Then I have defined an equation y, which is the same variable as the output of the function defined in the first line.
I won’t use fplot to check where the nearest zeroes are, because for x^3 I know that it passes the x-axis at x = 0. If I didn’t know this for sure though, I could use a vector to narrow the search limit, which is what I do below (just to show you how it is used). Now I will fzero in the Command Window to call the function (because we can’t run functions without “calling” them in the Command Window with an input). I use the name of my function, fun, in single quotes, and [-1 1] is the range within which I want MATLAB to search for the zero:
This is the output – MATLAB has correctly found that the root of the function x^3 is at x = 0.
Example – Finding multiple roots with fzero
What if there is more than one root in a function? We should first graph the function with wide limits, to see how many times it crosses the axis (maybe something from -500 to 500 would cover it). Then we can use fzero to identify each root. Let’s try the following example. I am going to use a parabola, and I know that a parabola only has 2 roots at most.
I am going to try graphing it on a smaller interval to see whether it covers all the roots. I will use ezplot to graph here, although it isn’t recommended – it still works.
The output graph shows that both the zeroes are covered here, meaning I don’t have to change the limits to make them wider to be able to see the other possible roots. This can be seen at the line y = 0, as I have highlighted it in red – the graph crosses twice over the x-axis, so both roots are covered.
It looks like the graph crosses the x-axis at around -4 and 1, but we can either use these as our initial guesses x0, or we can guess values very close to these instead – I am going to try this instead. Let’s try using -3 and 2 instead, and see if MATLAB can figure out that the real roots are at -4 and 1. I have made two separate fzero commands, one with each guess.
The output shows both -4 and 1, so it looks like MATLAB has correctly figured out the roots!
Fitting Data to a Polynomial
When we have a data set, it is useful to find a curve of best fit to the data points. The line of best fit may be linear, quadratic, cubic, or so on. We do this using the built-in function polyfit. Your function does not have to be a polynomial – you can still get a fit with a non-polynomial function such as with exponential (eg. e^x), trigonometric (eg. sin(x), cos(x), tan(x)), logarithmic (eg. ln(x)), or other functions.
To make MATLAB work with the polyfit function, you will need to take a guess as to what the order of the function is – the order means whether it’s linear (1st order), quadratic (2nd order), cubic (3rd order), and so on. The syntax for polyfit is polyfit(x,y,n). x will be the data points, y will be the function, and n is your guess as to what the order is. n can usually be one less than the number of data points, if you don’t know what to guess Polyfit will generate coefficients of the function, which will be used to fit the data to a curve.
Example – Using polyfit
Here we have a vector created from 0 to 2*pi using linspace. Then we create a vector for y using cos(x) so the values of x will be plugged into y. To get MATLAB to create coefficients of a function that can be fit to this data, use polyfit with the guess for the order. The syntax polyfit(x,y,n) as I mentioned above is used here. You will replace x and y with the variables that hold your vectors (for me, these variables are just called x and y). I have used 5 as the guess for the order of the function.
Polyfit will create the coefficients of the polynomial that will best fit to the data, and there are 6 coefficients because you guessed that the order was 5. From high school math, that means that there can be up to 6 terms in the fit equation (n+1).
Evaluating how well the data fit
Once we use polyfit, we need to use the polyval command to determine how well the fit is, using polyval. This will fit the data defined earlier in your code based on the coefficients that were found with polyfit.
Example – Using Polyval then graphing
After using polyval, the data points need to be plotted using plot. This will help to determine with a visual how well MATLAB had fit a curve to the data on the graph and compare polyval (which had used polyfit) with the actual data points.
Use the hold on command to plot x with y, then x with f1, and f1 was the variable I had stored the polyval command in. I plotted the polyval graph using red dashes to tell the difference between both plots on the same graph.
This is the graph that comes up – the red dashed plot makes up the graph that was made to fit the data, and the black circles plot is the original function. The 5th order polynomial fit pretty well to the sin(x+2) function.
Let’s try guessing a 2nd order polynomial fit instead.
Now notice how the polynomial of 2nd order did not fit well to the graph: