EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab fit
 

Matlab fit

Priya Pedamkar
Article byPriya Pedamkar

Updated March 3, 2023

Matlab fit

 

 

Introduction to Matlab fit

MATLAB fit method can be used to fit a curve or a surface to a data set. Fitting a curve to data is a common technique used in Artificial intelligence and Machine learning models to predict the values of various attributes.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

For example, if we compare the weight of an item like rice with its price; ideally, it should increase linearly (Price will increase as the weight of rice will increase). If we fit a curve to this data of weight and price, we will get mostly a linear curve. Now someone looking at this linear curve can easily interpret the relation between the 2 attributes (weight and price in our example), without looking at the data.

Syntax:

  1. fitobject = fit (a, b, fitType) is used to fit a curve to the data represented by the attributes ‘a’ and ‘b’. The type of model or curve to be fit is given by the argument ‘fitType’
  2. Various values which the argument ‘fitType’ can take are given in the table below:
Model Name Description
  1. ‘poly1’
A polynomial curve with linear nature
  1. ‘poly11’
A polynomial surface with linear nature
  1. ‘poly2’
A polynomial curve with quadratic nature
  1. ‘linearinterp’
Linear piecewise interpolation
  1. ‘cubicinterp’
Cubic piecewise interpolation
  1. ‘smoothingspline’
A curve of the nature smoothing spline

Table 1

Let us now understand how to fit a curve or a surface to data in MATLAB:

We will need some data to which we will fit the curve, for our examples, we will use some inbuilt data sets provided by MATLAB like ‘carsmall’ and ‘census’.

Examples of Matlab fit

Let us discuss examples of Matlab fit.

Example #1

In this example, we will use the ‘carsmall’ data provided by MATLAB. The data is of various attributes of cars manufactured over the years 1970, 1976, and 1982. It has attributes like ‘Acceleration’, ‘Cylinders’, ‘Horsepower’ etc. which represent various features of a car. We will load this data to our workspace and will fit a curve to its attributes ‘Acceleration’ and ‘Displacement’. The steps to be followed for this example are:

  1. Load the ‘carsmall’ data to the workspace
  2. View the file loaded above to understand its attributes
  3. Use the ‘fit’ function to fit a curve to the loaded data
  4. Plot the model created in above step

Code:

1. load carsmall

[Using the ‘load’ command to load the ‘carsmall’ data set to our workspace]

2. whos -file carsmall

[Using ‘whos’ command to view the file loaded above]

3. C = fit(Acceleration, Displacement, ‘poly2’)

[Using the ‘fit’ command to fit a curve to the data. The first 2 parameters represent the attributes to which we want to fit the curve and the 3rd parameter represents the type of curve which we want to fit (please refer to Table 1 for this)]

4. plot(C, Acceleration, Displacement)

[Using ‘plot’ command to plot the model created in above step]

This is how our input and output will look like in MATLAB command window:

Input 1:

Loading the carsmall data set:

Matlab fit 1

Input 2:

Fitting the curve to the data:

Matlab fit 2

Input 3:

Plotting the model created above:

Matlab fit 3

Output 1:

Matlab fit 4

Output 2:

Matlab fit 5

Output 3:

Matlab fit 6

As we can see in Output 3, we have obtained a curve that fits our data. Output 1 and Output 2 represent the data attributes and the model respectively.

In the same example, we can also fit a different type of curve as per our requirement. Let us try to fit ‘smoothingspline’ curve to the above data.

The code will be similar as in the above example with a change in line 3

Code:

 1. load carsmall

[Using the ‘load’ command to load the ‘carsmall’ file to our workspace]

2. whos -file carsmall

[Using ‘whos’ command to view the file loaded above]

 3. C = fit(Acceleration, Displacement, ‘smoothingspline’)

[Please note that the 3rd argument is now ‘smoothingspline’]

4. plot(C, Acceleration, Displacement)

[Using ‘plot’ command to plot the model created in above step]

This is how our input and output will look like in the Matlab command window:

Input 1:

Loading the carsmall data set:

example 2

Input 2:

Fitting the curve to the data:

example 2-1

Input 3:

Plotting the model created above:

example 2-2

Output 1:

example 2-3 

Output 2:

example 2-4

Output 3:

example 2-5

As we can see in Output 3, we have obtained a smoothing spline curve that fits our data.

Example #2

In this example, we will use the ‘census’ data provided by MATLAB. The data is of the US and gives the population of the country in a particular year. It has 2 attributes ‘cdate’ and ‘pop’ representing ‘census date’ and ‘population’. We will load this data to our workspace and will fit a curve to it. The steps to be followed for this example are:

  1. Load the census data to the workspace
  2. View the file loaded above to understand its attributes
  3. Use the ‘fit’ function to fit a curve to the loaded data
  4. Plot the model created in above step

Code:

load census

[Using the ‘load’ command to load the census file to our workspace]

whos -file census

[Using ‘whos’ command to view the file loaded above]

C = fit(cdate, pop, 'poly2')

[Using the ‘fit’ command to fit a curve to the data. The first 2 parameters represent the attributes to which we want to fit the curve and the 3rd parameter represents the type of curve which we want to fit (please refer to Table 1 for this)]

plot(C, cdate, pop)

[Using ‘plot’ command to plot the model created in above step]

This is how our input and output will look like in MATLAB command window:

Input 1:

Loading the census data set:

Matlab fit 7 

Input 2:

Fitting the curve to the data:

Matlab fit 8

Input 3:

Plotting the model created above:

Matlab fit 9

Output 1:

output 1

Output 2:

output 2

Output 3:

output 3

As we can see in Output 3, we have obtained a curve that fits our data. Output 1 and Output 2 represent the data attributes and the model respectively.

Conclusion

  1. We use ‘fit’ function in MATLAB to fit a curve to our data set
  2. Fitting a curve is very useful technique used in Machine learning
  3. We can control the type of curve that we want to fit to our data by using the ‘fitType’ argument.

Recommended Articles

This is a guide to Matlab fit. Here we also discuss the introduction, syntax, and different examples with code implementation. You may also have a look at the following articles to learn more –

  1. Matlab Forms
  2. Matlab plot title
  3. Matlab fplot()
  4. Matlab Syms
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW