Functions > Data Analysis > Curve Fitting > Example: Fitting a Model Function to Data
  
Example: Fitting a Model Function to Data
Test how well your data is modeled by a linear, quadratic, or exponential function.
1. Define a data set.
Click to copy this expression
2. Capture column 0 and column 1 into separate vectors.
Click to copy this expression
Click to copy this expression
Linear Fit
1. Use the intercept and slope functions to get the intercept and slope values.
Click to copy this expression
Click to copy this expression
2. Plot the linear fitting function LF along with X and Y.
Click to copy this expression
Click to copy this expression
Quadratic Fit
1. Set the polynomial order.
Click to copy this expression
2. Use the polyfit function to calculate the fitting function.
Click to copy this expression
Previous versions of this example used the deprecated regress function to obtain a matrix of coefficients which is then passed to the interp function to obtain the fitting function.
The polyfit function, recommended as an alternate to the deprecated regress function, returns the fitting function and thus there is no longer a need for using the interp function.
As an experiment, change the value of the polynomial order to see its effect on the fitting function as seen in the below plot.
3. Plot the quadratic fitting function QF.
Click to copy this expression
Exponential Fit
The exponential fitting function is represented by:
Click to copy this expression
1. Provide guess values for parameters a, b, and c.
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. Define the guess vector.
Click to copy this expression
3. Use the expfit function to calculate the three coefficients of the exponential curve.
Click to copy this expression
4. Plot the exponential fitting function EF.
Click to copy this expression
Click to copy this expression
5. View the three plots to see how well each fitting model fits the data.
Click to copy this expression