Curve Fitting in Excel

I’ve discussed linear regression on this blog before, but quite often a straight line is not the best way to represent your data. For these specific situations, we can take advantage of some of the tools available to perform nonlinear regression or curve fitting in Excel.

Curve Fitting in Excel with Charts

Excel charts are a convenient way to fit a curve to experimental data.

First, create a scatter chart.

Then right click on the data series and select “Add Trendline…”

curve fitting in excel

In the Format Trendline pane, select the options to Display Equation on chart and Display R-Squared value on chart.

display equation and display r-squared value on chart

Try different types of curves to see which one maximizes the value of R-squared.

For this data set, a logarithmic equation fits the curve with an R-squared value of 0.7992. From the image below, we can also clearly see that it is not a good fit.

logarithmic curve fitting in excel

However, a second-order polynomial fits the data with an R-squared value of 0.998. That means the polynomial equation fits the data better.

polynomial curve fitting in excel

Although the trendline option is convenient, it may not always be the best option when you want to know the equation that best fits the data.

In some cases, it may provide only 1 or 2 significant digits for the equation coefficients. This will make any formula that uses these coefficients only accurate to the same number of significant digits.

Also, there is no way to reference the coefficients of the equation in the spreadsheet without manually typing them into cells. This can cause problems if the data is updated, and the coefficients are not updated in the spreadsheets.

See also  Multiple Linear Regression in Excel

However, there is an option that provides a robust way to curve fit in Excel using the LINEST function.

Curve Fitting in Excel

Remember our old friend LINEST? Although LINEST is short for “linear estimation”, we can also use it for nonlinear data analysis with a few simple tweaks.

LINEST has the following syntax:

= LINEST(known_y’s, [known_x’s], [const], [stats])

where

  • known_y’s are the y-values corresponding to the x-values which you are trying to fit (or dependent variable)
  • known_x’s are the known x-values (or independent variable)
  • const (optional) is set to TRUE for the y-intercept to be calculated normally and set to FALSE if the y-intercept should be set to ZERO
  • stats (optional) is an argument that tells whether to return regression statistics. TRUE will return the statistics and FALSE will return no statistics.

Polynomial Curve Fitting in Excel

Let’s say we have some experimental data of pressure drop vs. flow rate through a water valve, and after plotting the data on a chart and trying different trendlines we see that the data is quadratic, as in the example above.

nonlinear regression excel

Even though this data is nonlinear, the LINEST function can also be used here to find the best fit curve for this data. For a polynomial equation, we do that by using array constants .

An advantage to using LINEST to get the coefficients that define the polynomial equation is that we can return the coefficients directly to cells. That way we don’t have to manually transfer them out of the chart.

Since the equation is quadratic, or a second order polynomial, there are three coefficients, one for x squared, one for x, and a constant. So we’ll need to start by creating a space to store the three coefficients for the equation.

See also  Using Excel Solver for Nonlinear Regression

Using LINEST for Nonlinear Regression in Excel

The LINEST function returns an array of coefficients, and optional regression statistics. So we’ll need to enter it as an array formula by selecting all three of the cells for the coefficients before entering the formula.

If the cells containing the flow and pressure data are named “flow” and “pressure”, the formula looks like this:

=LINEST(pressure, flow^{1,2},TRUE, FALSE)

nonlinear curve fitting in excel

The known y’s in this case are the pressure measurements, and the known x’s are the flow measurements raised to the first and second power. The curly brackets, “{” and “}”, indicate an array constant in Excel. Basically, we are telling Excel to create two arrays: one of flow and another of flow-squared, and to fit the pressure to both of those arrays together.

Finally, the TRUE and FALSE arguments tell the LINEST function to calculate the y-intercept normally (rather than force it to zero) and not to return additional regression statistics, respectively.

Since it’s an array formula, we need to enter it by typing Ctrl+Shift+Enter .

The function then returns the coefficients of x 2 and x as well as a constant (because we chose to allow LINEST to calculate the y-intercept).

The coefficients are identical to those generated by the chart trendline tool, but they are in cells now which makes them much easier to use in subsequent calculations.

For any polynomial equation, LINEST returns the coefficient for the highest order of the independent variable on the far left side, followed by the next highest and so on, and finally the constant.

A similar technique can be used for Exponential, Logarithmic, and Power function curve fitting in Excel as well.

See also  Linear Regression in Excel: 4 Alternative Methods

Fitting a Logarithmic Curve to Data

logarithmic function fit to excel data

A logarithmic function has the form:

We can still use LINEST to find the coefficient, m , and constant, b , for this equation by inserting ln(x) as the argument for the known_x’s:

=LINEST(y_values,ln(x_values),TRUE,FALSE)

Of course, this method applies to any logarithmic equation, regardless of the base number. So it could be applied to an equation containing log10 or log2 just as easily.

Exponential Curve Fitting in Excel

curve fitting exponential function in excel

An exponential function has the form:

It’s a little trickier to get the parameter values a and b for this equation because first we need to do a little algebra to make the equation take on a “linear” form. First, take the natural log of both sides of the equation to get the following:

Now we can use LINEST to get ln(a) and b by entering ln(y) as the argument for the y_values:

=LINEST(ln(y_values),x_values,TRUE,FALSE)

The second value returned by this array formula is ln(a), so to get just “a” , we would simply use the exponential function:

Which, in an Excel spreadsheet, translates to:

=EXP(number)

Fitting a Power Function to Data

curve fitting power function

A power function curve can be fit to data using LINEST in much the same way that we do it for an exponential function. A power function has the form:

Again, we can “linearize” it by taking the base 10 log of both sides of the equation to obtain:

With the equation in this form, the LINEST function to return b and log 10 (a) can be set up like this:

=LINEST(LOG10(yvalues),LOG10(xvalues),TRUE,FALSE)

Since the LINEST function returns b and log10(a) , we’ll have to find a with the following formula:

In Excel, that formula is:

=10^(number)

That’s it for now. As you can see, there are a number of ways to use the LINEST function for nonlinear curve fitting in Excel.

Scroll to Top