Curve Fitting in MATLAB Notes
Curve Fitting in MATLAB Notes
The tool allows you to select the data you want to import from the spreadsheet, and
to tell Matlab how to format and store it.
You can import the data into a number of different forms, which can be selected in
the IMPORTED DATA section of the Import tab. The most useful is likely to be
‘Column vectors’, which will import each column of the spreadsheet into an
individual variable. Selecting ‘Matrix’ will import all of the data you select into a
single variable with multiple rows and columns.
1
Select all of the columns you want to import (in the picture above, columns A and C
are selected), and check that Matlab has correctly detected the type of data in each –
it will usually be ‘number’, but could also be ‘text’ or one of a variety of date format.
You can change the type if Matlab gets it wrong. You will usually want to change the
variable names which Matlab proposed (VarName1 etc.) to something more
meaningful (like time, length, etc.).
Finally, press the ‘Import Selection’ button to import the spreadsheet data you have
selected into Matlab’s workspace. This button will also allow you to generate Matlab
scripts to repeat the process if required, but for the moment, you will usually just
want to import the data.
will import data from the file called “filename.csv” in the working directory. If the
file is not in the working directory, you will see an error:
>> load file.csv
??? Error using ==> load
Unable to read file file.csv: No such file or directory.
Note that Matlab imports the data into a single variable with the same name as the
file. Hence the variable for the data above would become filename. Inspecting this
variable in the Variable Editor shows that it looks like the table in the original
spreadsheet – it has both rows and columns.
If the file you specify is not a comma-separated value file, Matlab will try to import it
anyway. This may appear to succeed but with unexpected results such as variables
with strange values, or it may cause an error:
>> load vco180.pdf
??? Error using ==> load
Number of columns on line 1 of ASCII file M:\tmp\vco180.pdf must be the
same as previous lines.
2
We can refer to a particular value in the variable by its row and column:
variable(row,column)
For example:
x(4,2)
is the value in row 4, column 2 of the variable called x. Row and column values start
at 1.
More often though, we will want to extract a complete row or column of data – for
example to draw a graph. To do this, we use the same approach, but specify the
number of the row or column we want, and replace the other number by the :
symbol (which we can think of here as meaning “all”). For example:
measurements(3,:)
will give us the third row of the variable named measurements. If we assign this to
another variable, it will be a sequence having one row. Also,
prices(:,2)
will give the second column of the variable called prices. We can assign this to a
variable which will then be a sequence having one column.
The polyfit command in Matlab is used for fitting a polynomial function to a set of
data. The general form of the polyfit function is:
>> p=polyfit(x,y,n)
where x is the set of x data values, y is the set of y data values and n is the order of
the polynomial equation to be fitted. The polyfit function will return the coefficients
of the polynomial in the order p1, p2, etc. There must be an equal number of values
in the x and y data sets:
3
>> polyfit(x,z,1)
??? Error using ==> polyfit at 48
X and Y vectors must be the same size.
If you try to fit too complex a polynomial (i.e. one of too high an order) to your data,
Matlab will warn you that the results are unreliable:
>> polyfit(x,y,8)
Warning: Polynomial is badly conditioned. Add points with distinct X
values, reduce the degree of the polynomial, or try centering
and scaling as described in HELP POLYFIT.
> In polyfit at 80
ans =
1.0e+003 *
0.0000 -0.0003 0.0045 -0.0422 0.2342 -0.7815 1.5121 -1.5225 0.5971
>> polyfit(x,y,9)
Warning: Polynomial is not unique; degree >= number of data points.
> In polyfit at 72
ans =
0.0013 -0.0516 0.8589 -7.6762 39.3760 -113.0523 151.5839 0 -198.8450 129.2050
Although Matlab has still produces a best-fit line, you probably should not trust it.
Simple Example
Assume that we have the following variables:
>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
>> y = [1.4, 1.5, 6, 3, 6, 5, 6.9, 9 ,9.1]
We can also get the intersection with the y axis, c (or p2)
>> c=p(2)
c =
0.5639
To obtain a set of data to plot the best fit line, we use the original x values and
calculate the equivalent y values for the polynomial:
>> w = m*x + c
4
The r-squared value is calculated by the following process:
a) Find the sum of the squares of the residuals using the following formula:
N
A f (x i ) t i
2
i1
This formula takes each original data point ti in turn and calculates how far away it
is from the equivalent position f(xi) on the fitted line. Squaring the term ensures
that those points above the line do not cancel out those below the line.
b) Find the sum of the squares of the deviation of the data from the mean t
using the following equation:
N 2
S ti t
i 1
5
To calculate the r-squared value in Matlab, using our previous example, first
calculate the sum of residuals. You can use Matlab’s sum function to calculate the
sum of all of the values in a sequence variable:
>> A=sum((w-y).^2)
A =
1.0746
Calculate the sum of the squares of the deviation of the data from the mean. Note
you can use the built in Matlab mean function to calculate the average of all of the
values in a sequence variable.
>>S=sum((y-mean(y)).^2)
S =
60
6
Index
Errors
The table below lists the text of some common errors, and will help you to find the
worksheet which mentions them
Error Term Week Page
Matrix must be square. 2 8 7
Number of columns on line 1 of ASCII file x must 2 9 2
be the same as previous lines.
Polynomial is badly conditioned. 2 9 4
Polynomial is not unique; degree >= number of 2 9 4
data points.
Unable to read file x: No such file or directory. 2 8 4
2 9 2
Undefined function or variable 'A'. 2 8 4
Unknown text on line number 1 of ASCII file x 2 9 2
Vectors must be the same lengths. 2 8 7
2 8 8
X and Y vectors must be the same size. 2 9 4