MATLAB Basic Fitting Tool: Limitations and Solutions
Image Courtesy: Mathworks.com

MATLAB Basic Fitting Tool: Limitations and Solutions

Regression analysis is a powerful technique to identify relationship between independent and dependent variables in a dataset. The purpose of regression analysis is to study the relationship between variables and come up with a model/equation. The model is useful to calculate or predict values of dependent variables given independent variables not present in the dataset. 

Software tools are often used to identify the best fit regression curve or equation for a given dataset. MATLAB basic fitting tool is one of the commonly used tools for regression analysis. This article identifies two major limitations of MATLAB basic fitting tool and discusses solutions to these problems. 

Two major limitations of basic fitting are: 

(1) Not able to fit multiple dataset at the same time

(2) Limitation in number of available functions

Let’s discuss these limitations with examples and one possible solution for each. 

Limitation 1: Issue with Multiple Dataset

Basic fitting works fine with a single dataset. When we have multiple datasets plotted on the same graph or in a subplot, basic fitting GUI can only display the fit curve for any one of the dataset. This is a major limitation when we want to compare regression analysis of two or more datasets at the same time. 

Figure 1: Limitation of Basic Fitting in multiple dataset plotted as a Subplot


Figure 1 represents a subplot where 2 datasets are plotted side by side. When we use Select data option of Basic Fitting (top right corner of Figure 1), it only allows to select one of the dataset at a time. Upon selecting one of the dataset, the fit curve for the other one disappears. As a result, it is not possible to save the plot with both regression curves. 

Solution:

One possible solution for this problem is saving fit data for the datasets individually into workspace and use that data to plot the regression curve manually using MATLAB Script. 

Figure 2: Saving Fit Data to Workspace


Figure 2 shows that fit data for two datasets are saved as fit1 and fit2 in the workspace. The fit data is a structure with coefficients of the equation. 

Next, we use the coefficients to plot the regression curve. We use MATLAB polyval function to evaluate the equation at different values for the independent variable (x). Example MATLAB Script is shown in Figure 3. 

Figure 3: MATLAB Script for plotting regression line from fit data


Figure 4 shows the two regression curves plotted manually. 

Figure 4: Regression curves plotted manually


Note: As we have very few data points, evaluating regression equations for only the values of independent variables may result in a not-so-smooth curve. To solve that, we can keep the range of independent variable same, but use more points within that range. Figure 5 shows the modified regression curve with more data points. 

Figure 5: Smoother regression curves with more data points


As we have plotted the regression curves manually, it was not possible to use the show equation tool of basic fitting. Only way to show equation on these plots is to type them manually by adding a text box. Similarly, legends for data points and regression curve can be added manually.

Note: To reuse the script to generate the regression plots, the workspace must be saved along with the script. Otherwise fit data will disappear when any other program erases the workspace or when MATLAB is closed. 



Limitation 2: Available Functions

The version of MATLAB I’m using right now is R2017a. The basic fitting tool in this version has the following functions listed:

  • Spline Interpolant
  • Shape Preserving Interpolant
  • Linear
  • Quadratic
  • Cubic
  • Polynomial (4th degree to 10th degree)

As the name suggests, basic fitting covers all the basic functions for regression analysis. What if you need exponential, logarithmic, or sinusoidal functions? Or maybe a custom function? Basic fitting doesn’t provide the capability to support any other function than the listed ones. 

For example, let’s consider the following dataset:

We want to use basic fitting tool in MATLAB to find a best fit curve for this dataset. Figure 6 shows linear, quadratic, and cubic regression function and corresponding equation for the dataset. 

Figure 6: Linear, Quadratic and Cubic Regression plot and equation for data in Table


Among the three regression curves, cubic gives best fit for the dataset. However, the cubic regression curve does not contain more than 50% of the points on it and residual error is significant. Using polynomial regression of 4th degree doesn’t improve the scenario much. Using polynomial regression of 5th and 6th degree with only 6 data points will create over-fitting. It will reduce the residual error significantly but the model becomes complex and not a good predictor of y values given x values absent in the dataset. 

Solution for this limitation is to use curve fitting app of MATLAB to try other functions not available in basic fitting. Some of functions available in curve fitting app are:

  • Exponential
  • Gaussian
  • Interpolant
  • Sum of sine
  • Fourier
  • Custom Equation

We are going to use Custom Equation of a logarithmic function with our dataset. The curve fitting app allows us to select x data and y data, then type a custom equation to generate fit plot. 

Figure 7: Select data and Custom Equation menu of Curve fitting app in MATLAB


Note, before using the curve fitting app, make sure you have your data saved in your workspace. Without the data in workspace, the curve fitting app won’t be able to generate fit. As shown in Figure 7, select the X-data and Y-data from your workspace on the left panel. In our example, they are x and y vectors. On the right side, type the equation. The equation 

  • Must have your independent variable (x)
  • Must have at least one constant

If the written equation is not correct, an error message is displayed in the result section. 

We used the equation y=a*log10(x) to see how it fits with the given data point. Figure 8 shows the regression curve. 

Figure 8: Logarithmic Regression Curve generated from MATLAB Curve Fitting App


The logarithmic regression curve touches 4 out of 6 points closely and the residual error is minimized that the error we had in cubic fitting. The result section of curve fitting app displays the values of constants used in the custom equation. Note that, there is no way of knowing which custom equation will fit best for a given data. It is always trial and error. It is better to try the basic fitting tool first. It will provide an overall idea about possible best fit function. Then switching to curve fitting app and playing with equations becomes easier. 

Curve fitting app comes with a code generation feature which creates MATLAB function automatically from the fit session. This feature is useful if user plans to reuse fit session and play with the parameters. The GUI session can also be saved as .sfit file and loaded later using the app. Curve fitting app has one drawback compared to basic fitting . Basic fitting tool generates equation of the fit and displays it on the graph. In curve fitting app, it only displays the constants. The equation in Figure 8 is written manually on the figure using text box. 

If you have read the article so far, thank you for your patience! Any suggestions, feedback or alternate solutions will be highly appreciated.

Reference:

The Mathworks Inc. www.mathworks.com





I have multiple data for 20 curves how to find curve fitting for all. How fit multiple data sets using the Basic Fitting Tool in MATLAB.

Like
Reply

how fit multiple data sets using the Basic Fitting Tool in MATLAB

Like
Reply

To view or add a comment, sign in

Others also viewed

Explore content categories