How to Do Curve Fitting in MatLab

Curve fitting is an important tool when it comes to developing equations that best describes a set of given data points. It is also very useful in predicting the value at a given point through extrapolation. In MATLAB, we can find the...

Getting MATLAB Ready

  1. How to Do Curve Fitting in MatLab Picture 1How to Do Curve Fitting in MatLab Picture 1
    Open MATLAB and click on the New Script button on the left side of the Home tab. Creating the script will help to store your work in a program and will allow reusability.[1]
  2. How to Do Curve Fitting in MatLab Picture 2How to Do Curve Fitting in MatLab Picture 2
    Type commands 'clc' and 'clear all' in the command window. These commands are used to clear the command window and the workspace before executing the script program.

Getting Coefficients of the Equation

  1. How to Do Curve Fitting in MatLab Picture 3How to Do Curve Fitting in MatLab Picture 3
    Choose variable and type the data. Choose your independent variable for example 'x' and dependent variable for example 'y'. You can choose any letter for these variables. Write the data points in the square brackets in following format: x = [ ], y = [ ]. Both of these variables are followed by a semicolon (;) if you want to suppress them from appearing in the command window.
  2. How to Do Curve Fitting in MatLab Picture 4How to Do Curve Fitting in MatLab Picture 4
    Import the file if data is in an excel sheet. If you have your data in excel file import the data into MATLAB. You can select the columns from the data that are independent or dependent.
    1. Click on 'Import Data' from the home tab.
  3. How to Do Curve Fitting in MatLab Picture 5How to Do Curve Fitting in MatLab Picture 5
    Type the file name given file and then click open.
  4. How to Do Curve Fitting in MatLab Picture 6How to Do Curve Fitting in MatLab Picture 6
    Choose the Output type to be 'Column Vector'. This will allow you to choose the independent or dependant vector in the form of a column.
    1. Select the Columns from the data set.
    2. At last Click on 'Import Selection' from the tab. Once imported the data columns will show up in the workspace.
  5. How to Do Curve Fitting in MatLab Picture 7How to Do Curve Fitting in MatLab Picture 7
    Choose the independent and dependent variable for the selected data points. The variables chosen should contain the same title as of imported data points. The syntax will be: x = [Column Title]. This same rule applies to the other selected column. Once you have the dependant and independent variable data points we can then use polyfit to find the coefficients.
  6. How to Do Curve Fitting in MatLab Picture 8How to Do Curve Fitting in MatLab Picture 8
    Use Polyfit command to get the coefficients of the equation. Polyfit command not only gives coefficients but also lets us choose the highest power of the equation.
    1. Use the following syntax for the polyfit command, p = polyfit(x,y,n); where x is the independent variable, y is the dependent variable, and n is the degree of the polynomial.

Plotting the Line of Best Fit

  1. How to Do Curve Fitting in MatLab Picture 9How to Do Curve Fitting in MatLab Picture 9
    Use 'polyval' to get the values at the given interval. The syntax of the polyval command is yfit = polyval(p,x), where p is the coefficients of the equation, and x is a vector of independent data points.[2]
  2. How to Do Curve Fitting in MatLab Picture 10How to Do Curve Fitting in MatLab Picture 10
    Plot the line of best fit. Use the syntax plot (m,yfit) to plot the line of the best fit. You can also add the color of the line.
    1. Add the title and axis labels in the plot.
    2. You can also add the previous plot to the same graph by using function hold on.
  3. How to Do Curve Fitting in MatLab Picture 11How to Do Curve Fitting in MatLab Picture 11
    Save your work. Click on Save as from the drop-down menu under Save from the editor tab. Name your file and choose the destination file. Then click Save.
  4. How to Do Curve Fitting in MatLab Picture 12How to Do Curve Fitting in MatLab Picture 12
    Get the results. Click run to see the result.
4.5 ★ | 2 Vote