Regression analysis with MATLAB is easy. The MATLAB Basic Fitting GUI allows us to interactively to do "curve fitting" which is a method to arrive at the best "straight line" fit for linear equations or the best curve fit for a polynomial up to the tenth degree. The procedure to perform a curve fitting with MATLAB is as follows:
Using the data set above, determine the relationship between velocity and time.
First, let us input the variables (Workspace > New variable) as shown in the following figures.

Figure \(\PageIndex{1}\). A new variable is created in the Workspace.

Figure \(\PageIndex{2}\). New variables are entered in the Variable Editor.
Second, we will plot the data by typing in plot(time,velocity)
at the MATLAB prompt. The following plot is generated, select Tools > Basic Fitting:

Figure \(\PageIndex{3}\). A plot is generated in Figure 1. The Basic Fitting tool can be initialized from Tools > Basic Fitting.
In the "Basic Fitting" window, select "linear" and "Show equations". The best fitting linear line along with the corresponding equation are displayed on the plot:

Figure \(\PageIndex{4}\). Basic Fitting window is used to select the desired regression analysis parameters.
Now let us do another curve fitting and obtain an equation for the function. Using that equation, we can evaluate the function at a desired value with polyval
.
The following is a collection of data for an iron-constantan thermocouple (data available for download). 1
Temperature [C] |
Voltage [mV] |
50 |
2.6 |
100 |
6.7 |
150 |
8.8 |
200 |
11.2 |
300 |
17.0 |
400 |
22.5 |
500 |
26 |
600 |
32.5 |
700 |
37.7 |
800 |
41 |
900 |
48 |
1000 |
55.2 |
Temperature [C] vs Voltage [mV]
- Plot a graph with Temperature as the independent variable.
- Determine the equation of the relationship using the Basic Fitting tools.
- Estimate the Voltage that corresponds to a Temperature of 650 C and 1150 C.
We will input the variables first
Voltage=[2.6;6.7;8.8;11.2;17;22.5;26;32.5;37.7;41;48;55.2]
Voltage=[2.6;6.7;8.8;11.2;17;22.5;26;32.5;37.7;41;48;55.2]
To plot the graph, type in:
We can now use the Plot Tools and Basic Fitting settings and determine the equation:

Figure \(\PageIndex{5}\). Basic Fitting window is used to select the desired regression analysis parameters.
By clicking the right arrow twice at the bottom right corner on the Basic Fitting window, we can evaluate the function at a desired value. See the figure below which illustrates this process for the temperature value 1150 C.

Figure \(\PageIndex{6}\). Estimating the Voltage that corresponds to a Temperature of 1150 C.
Now let us check our answer with a technique we learned earlier. As displayed on the plot, we have obtained the following equation:\(y=0.052831 x+0.67202\) This equation can be entered as polynomial and evaluated at 650 and 1150 as follows:
>> p=[0.052831,0.67202]
p =
0.0528 0.6720
>> polyval(p,1150)
ans =
61.4277