0% found this document useful (0 votes)
59 views

Curve Fitting in MATLAB Notes

This document provides instructions for importing spreadsheet data into MATLAB and fitting curves to datasets. It describes how to import CSV files using the import tool or load command. Imported data is stored in a variable with multiple rows and columns resembling the spreadsheet. Specific values can be accessed using row and column indices. Whole rows or columns can be extracted using colon notation. The polyfit command is used to fit polynomials to datasets, returning the coefficients which define the curve. The r-squared value measures the goodness of fit, calculating the proportion of variance explained by the model.

Uploaded by

extramailm2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Curve Fitting in MATLAB Notes

This document provides instructions for importing spreadsheet data into MATLAB and fitting curves to datasets. It describes how to import CSV files using the import tool or load command. Imported data is stored in a variable with multiple rows and columns resembling the spreadsheet. Specific values can be accessed using row and column indices. Whole rows or columns can be extracted using colon notation. The polyfit command is used to fit polynomials to datasets, returning the coefficients which define the curve. The r-squared value measures the goodness of fit, calculating the proportion of variance explained by the model.

Uploaded by

extramailm2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MM114 Engineering Mathematics (Applications)

Curve Fitting in Matlab: Reference Notes

Importing data from spreadsheet files


Matlab is able to import data from spreadsheets such as Excel or OpenOffice. The
easiest way to do this is to use comma-separated value files. These files usually have
names that end with “.csv”. To import one of these files into Matlab, the easiest
method is to use the Import tool, which is started by double-clicking a file in the
Current Folder, or by clicking the “Import Data” button on the Home tab:

Import Data Button


Current Folder
If you used the Import Data button, you’ll be asked to pick the file that you want to
import.
Once you’ve chosen the file to use, the import tool itself will appear:

The sort of variable


Names of variable(s) where
that Matlab should
Matlab will put the data
use to import the
Selected column: imported data
Non-selected column: not imported
Import button

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.

Importing spreadsheet data from the command line


Occasionally (for example if you want to repeat an import-and-process task on
several files) you might want to use the command line to import data. To import a
CSV file from the command line, use the load command. For example:
>> load filename.csv

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.

>> load mm.doc


??? Error using ==> load
Unknown text on line number 1 of ASCII file M:\tmp\mm.doc "ÐÏࡱá".

Variables with rows and columns


Importing data from a spreadsheet results in a variable which has values in rows
and columns. In a later week, we will see that Matlab treats a variable like this as a
matrix. For this week, we will think of it as a number of sets of related values (like
corresponding voltage, current, speed and torque values for a motor driving
different loads).

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.

Fitting a curve to data


Matlab can easily fit a polynomial function to a set of data. The simplest type of
polynomial fit is a 1st order polynomial, in other words a straight line in the form:
y = mx+c.
The general form of a polynomial function is:
p(x) = p1xn + p2xn-1 + … + pn-1x2 + pnx + pn+1
The coefficients are p1, p2, p3, etc and the value of n (the highest power of x) is called
the order of the polynomial.

For a straight line, the polynomial is:


p(x) = p1x + p2
Thus, p1 is the gradient, m, and p2 is the intersection at the y axis, and the polynomial
is of order 1 (the highest power of x is 1).

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]

First, we fit a 1st order polynomial to the data.


>> p=polyfit(x,y,1);
p =
0.9517 0.5639
We can now obtain the gradient, m (or p1)
>> m=p(1)
m =
0.9517

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

Goodness of fit – the r-squared value


When fitting a line to asset of data it is important to also provide an indication of
how well the line fits the data. A standard measure of this “goodness of fit” is called
the r-squared error value.

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

i1

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

This is done to normalise the data.

c) Calculate the r-squared value using the following equation:


A
r 2  1
S

The closer the r-squared value is to 1, the better the fit.




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

Finally calculate the r-squared value


>> r2=1 – A/S
r2 =
0.9821

6
Index

Topic Term Week Page


Adding Data to a Graph 2 8 13
The Command Window 2 8 2
Equations using Sequences 2 8 7
Fitting a curve to data 2 9 3
Formatting Graphs 2 8 10
Functions and Equations 2 8 5
Goodness of fit – the r-squared value 2 9 4
Importing data from spreadsheet files 2 9 1
Importing spreadsheet data from the 2 9 2
command line
Loading and Saving Workspaces 2 8 3
The Matlab Window 2 8 1
Plotting Graphs 2 8 8
Saving and Loading Graphs 2 8 9
Sequences of values 2 8 6
Variables and Workspaces 2 8 3
Variables with rows and columns 2 9 2

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

You might also like