For those familiar with the trendline feature in Microsoft Excel, you may have wondered how we can accomplish the same purpose, that is, constructing a mathematical model to fit a set of data using MATLAB. This is a considerably more involved process in MATLAB, but what you lose in ease of use is made up for in far greater flexibility than one can achieve with Excel. Excel. We will consider only three different model types in this course, linear, power, and exponential. For all three of these, we will use the same MATLAB function, polyfit, to determine the mathematical model, although how we use it will be a bit different for the three different models.
First, we will consider the simplest case, the linear model, where the data is described relatively accurately by the well-known equation y equals mx plus b. The real purpose of polyfit is to determine the coefficients of a polynomial model to fit a data set, and a linear model is just a first-order polynomial. The independent variable is not raised to a power other than 1, the mx part, and 0. the constant, b. To contrast this, a quadratic model, or second-order polynomial, would have both of these terms as well as an x-squared term. To review, a linear model appears as a straight line on a standard, or rectilinear, graph, and the coefficient m is the slope of the line, or the rise over the run.
The coefficient b is the intercept with the vertical axis or the value of y when x equals 0. To use polyfit to determine a linear model, we have to send it the values of the independent data, normally plotted on the horizontal axis, the data for the dependent variable, and we need to tell polyfit that we wish to fit a first order polynomial to that data. The format is C equals polyfit , where X is a vector containing the independent data, Y is an equal length vector containing the dependent data in the corresponding order, and the 1 tells polyfit to fit a first order polynomial to the data. It returns a two element vector containing M and B in that order.
There is nothing magical about the variable c. This can be any valid MATLAB name, but c is convenient to stand for coefficients. To extract the two values for the model, m equals c1 and b equals c2. We will see later how to use these values to actually add a trend line to a graph along with the data points. Next, we will look at power functions, those in which the independent variable is raised to a power, but only one such term is present and may or may not be an integer, although in most cases the power is an integer or fraction of small integers such as 1 half or 2 thirds.
In other words, this is not a general polynomial model in which different powers of the variable are present. And all such powers are integers. The general form of the power model is y equals bx to the m.
To review, if m is positive, the curve crosses through the origin. For negative m, the curve is asymptotic to the vertical axis. In both cases, b is the value of y when x equals 1. Unless M is 1, this is not a linear model, so we cannot just use polyfit like we did for linear models. The trick is to make this look like a linear model. We will not go into the gory details here, but it is strongly recommended that you refer to chapter 13 in the text to understand how an why this linearization works.
Taking the base 10 log of both sides of the equation, we get log of y equals m log of x plus log of b. Therefore, if we plot the log of y versus the log of x, we get a straight line. Now, to use polyfit to obtain the coefficients m and b, we first have to send the base 10 logarithms of the data, not the data itself, along with a 1 for first order polynomial. Note that the base 10 logarithm in MATLAB is not just log, but must be written as log10. The coefficients stored in the two element vector C are m and log b.
So we have to do a minor calculation to get b, basically raising 10 to the power log of b. Thus the code for getting m and b is slightly different. Note that if we plot a power model on a full logarithmic graph, It appears to be a straight line.
The final model we will consider with polyfit is the exponential model given by y equals b e raised to the mx. Note that the fundamental difference between this model and the power model is the location of the independent variable. Here in the exponential model, x appears in the exponent.
On the other hand, in the power model, x is being raised to a power. It is not part of the power. Also note that in an exponential model, m can be essentially any value and is not typically restricted to integers or quotients of small integers, as is the power model. The appearance of the exponential model is also different. The coefficient b is the intercept of the curve with the vertical axis.
In other words, where x equals 0. If m is positive, the curve approaches 0 as x approaches negative infinity. For negative m, this asymptote is for positive x. Again, we need to linearize this equation in order to use polyfit and again it is recommended that you refer to chapter 13 in the book for more detail.
This time we will take the natural log of both sides yielding natural log of y equals mx plus natural log of b thus we need to send polyfit the vector x, but the natural log of the y values. Again, we need to send a 1 to specify a first order polynomial. Note that in MATLAB, the natural log function is log, not ln. When polyfit returns the two coefficients, it stores m and the natural log of b. Thus, when we extract m and b from the vector, we have to raise the value e to the second element of the vector to get b.
Finally, note that if we plot exponential data on a semi-log graph with the vertical axis logarithmic, it will appear to be a straight line.