SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
Curve Fitting in Matlab
Shimi Machlufāˆ—
November 8, 2008
Contents
1 Introduction 2
2 The Curve Fitting Tool 5
2.1 Data... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Fitting... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Exclude..., Plotting... and Analysis... . . . . . . . . . . . . . . 9
2.4 Residuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3 Examples 9
3.1 Simple linear ļ¬t with error bars . . . . . . . . . . . . . . . . . 9
3.2 More advanced ļ¬t . . . . . . . . . . . . . . . . . . . . . . . . 12
List of Figures 16
āˆ—
Like any other document, this one can have mistakes or not be clear enough. I would
like to get comments regarding this text to my email: machlufs@bgu.ac.il.
1
1 Introduction
In this tutorial I assume you know the basics of working with Matlab. For
the ones who donā€™t know, there are many other tutorials, like the ones you
can ļ¬nd in Matlab help, Highlern or just search the internet. Still, Iā€™ll
explain the basic commands and will give examples.
Few more general comments:
ā€¢ The pictures of the computer screen dosnā€™t look clear. This problem is
due to the screen size relative to the paper size, zooming in will solve
this problem.
ā€¢ In the pictures that you see a window and the command window in
the background, usually you can see in the command window the com-
mands I used.
In Fig.1 we can see the Matlab window, where the big part is the
"Command Window" and on the left there are 3 tabs: "Current Directory",
"Workspace" and "Command History".
ā€¢ The "Command Window" is the main window and in it we insert/add
data into the Matlab Workspace or writing commands.
ā€¢ The "Current Directory" is where the Matlab path is set. You can
change this directory or open ļ¬les directly from this tab. If you want
Figure 1: An example of the Matlab window.
2
to run scripts or functions, they need to be in this directory or you
need to go to their directory.
ā€¢ The "Workspace" is a tab showing all the variables you already deļ¬ned
in this Matlab session. When you close Matlab the Workspace is being
deleted, but you can save the Workspace to ļ¬le and load it in diļ¬€erent
time with the File menu.
ā€¢ The "Command History" tab saves all the command you typed in the
"Command Window", also in old sessions of Matlab.
Now Iā€™ll explain very shortly how to insert data into the Matlab Workspace,
manipulate it and then plot it to ļ¬gure. In Fig.2, I typed 3 commands. The
ļ¬rst one:
>> x = [0:0.1:2*pi]
just deļ¬nes a vector (an array) of numbers. Starting at 0, to 2Ļ€ with jumps
of 0.1. After typing the command Matlab plots the result - the value of the
vector x. In the second command:
>> y = sin(x).āˆ§2;
I deļ¬ne a new variable and set its values to be sin(x)2. In this command I
Figure 2: An example to deļ¬ne 2 variables and plot them.
3
Figure 3: The result of the "plot(x,y)" command.
would like you to pay attention to 2 things:
1. First, to the fact that there was no output. This is because of the
semicolon (;) at the end of the command. This semicolon tells Matlab
that it shouldnā€™t print the output. You should put the semicolon at
the end of every line, unless you want to see the output, for 2 main
reasons: Itā€™s easier to see what commands you already inserted and
when there is a lot to print it can take some time.
2. The second thing is that when I wanted to use the "square" operator
I put a dot (.) before it. The reason is that Matlab, by default, works
with matrices and vectors, and in order to multiply 2 vectors at size n,
their size need to be [1Ɨn]Ā·[nƗ1] for scalar product, or [nƗ1]Ā·[1Ɨn]
for getting the square of each element. Since our vectors have the same
size, using the dot before the square operator tell Matlab to do this
operation (square in our case) on each element and not on the vector.
The last command just opens a new Figure window (Fig.3) and plots y
as a function of x.
I want to mention a few more things before we continue:
ā€¢ As you can see, the 2 variables were added to the Workspace, see Fig.2.
We can see their size and their nim/max values.
ā€¢ The variable Ļ€, is already deļ¬ned in the Matlab Workspace but we
donā€™t see it in the Workspace tab. Also the imaginary number ı is
4
Figure 4: The Curve Fitting window.
already deļ¬ned as
>> i=sqrt(-1);
ā€¢ The functions "sin" and "sqrt", square root, are Matlab functions. The
Matlab Help contains information about these functions and on any
other Matlab function. There are A LOT OF Matlab functions. Any
mathematical function like "sin" or "exp" or manipulation function (on
vector or matrices) like "max" or "mean" that you can think of, is
probably there.
2 The Curve Fitting Tool
to open the Curve Fitting tool just type:
>> cftool
and this tool will be open in a new window. See Fig.4. This window has 5
buttons:
Data...: Will open a new window that will allow us to import data from
the Matlab workspace into the Curve Fitting tool environment (called
data set).
5
Figure 5: Creating a new data set.
Fitting...: This one will set the ļ¬tting function and parameter.
Exclude...: With this window we can exclude some data points from the
data set.
Plotting...: If we have a few data set and/or a few graphs, here we can
decide which one we want to see, to plot. The rest are just not shown,
there are not deleted.
Analysis...: If we want to evaluate the ļ¬tted function we can use this win-
dow.
The Data... and Fitting... are the most important and Iā€™ll explain how to
use them in more details using the next example (y = sin(x)).
2.1 Data...
After pressing the Data... button, a new window will open, Fig.5, and we
could choose the x, y and weights variables1, set the data set name and click
the "Create data set" button. After that youā€™ll see your data set in the list
and you can close the window.
1
These variables MUST exist in the Matlab Workspace, otherwise they wonā€™t appear
in the list.
6
In this short example I didnā€™t set any value to the weight because I
created the x and y variables. When you want to analyze some data, usually
the x parameter is something you choose. Like measuring something every
second (or some other time), or every 10 cm. Thatā€™s the meaning of free
parameter. The y parameter is something you measure (the dependent
parameter) and since weā€™re in real life, the measurement has errors. Some
of the data points will have small error and some will have big error2. The
regular ļ¬t (without weights) consider all the data points as equal, but if
some of the data points have larger error bars (relative to the other points)
it means that they are less accurate and that the ļ¬t should consider these
points less in its calculations. Thatā€™s what the weights are for. The more
accurate data points get higher weights relative to the less accurate data
points.
The way to create the weights, according to the above logic, is that the
weight is the inverse of the square of the error bar. In a mathematical way:
if wi is the weight and Ļƒi is the error of the ith data point, then:
wi =
1
Ļƒ2
i
.
The Smooth tab is for smoothing noisy data, something we donā€™t need
for now and I wonā€™t explain.
2.2 Fitting...
To start ļ¬tting the data, we need to click the Fitting... button so the Fit
Editor will open. There we need to start "New ļ¬t", give it the name we want
(the default is ļ¬t and a number), choose the data set we want to ļ¬t, choose
the ļ¬tting function (Type of ļ¬t) and click Apply.
There are a lot of built in ļ¬tting function which are grouped, like "Sum of
Sin Function" (what I used because I want to ļ¬t the data to Sin function) or
"Polynomial" functions, where in every group you need to choose the speciļ¬c
function you want to ļ¬t to.
After clicking the Apply button, the ļ¬t is computed and the results of
the ļ¬t are printed in the "Result" part of the ļ¬t editor and also plotted as
a graph in the Curve Fitting Tool. In Fig.6 we can see the input I entered
the Fit Editor and the result of the ļ¬t. In this Figure I also adjusted the
Fit Editor so we could see all of the output. Just by looking on the plot we
can see that the ļ¬t is good and the R-square and Adjusted R-square (check
the Help for details) conļ¬rm that. We can also see that the amplitude (a1)
is 1, the frequency (or more precisely, Ļ‰ = 2Ļ€f, which is b1 if our x variable
2
If we took a few measurement for each x value, we can put all of them into the ļ¬t.
The error (or error bar) is for each measurement. It can happen that all the measurements
for the same x value will have the same error.
7
Figure 6: Fitting the data with y = a1 Ā· sin(b1 Ā· x + c1) function.
is the time) is also 1 and that the phase (c1) is 0, like the parameters we
choose when we created the y vector (y = sin(x)).
The numbers in parenthesis near the values of the parameters (Coeļ¬ƒ-
cients) are the conļ¬dence bounds - the error in the ļ¬t. Since our ļ¬t ļ¬ts
exactly the data set, these conļ¬dence bounds are zeros (the same value as
the parameter itself). In other examples, like in Sec. 3.1, where the ļ¬t wonā€™t
be so good, weā€™ll see that the conļ¬dence bounds are not zero any more.
In general, the ļ¬tting algorithm start from some values of the ļ¬tting
constants (a1, b1 and c1, in out example) that Matlab choose, and try to
converge from that initial parameter to the ļ¬nal ones. Sometimes the initial
parameters are not close enough to the ļ¬nal ones so the algorithm converge
to something else or doesnā€™t converge at all. Clicking the "Fit options..."
will open a window that allow as to change this initial parameter. I will use
that in one of the examples.
If the ļ¬t function that we need isnā€™t in the list of functions, we can choose
as the Type of ļ¬t "Custom Equations" and then "New equation" and to build
the needed function. I will show how to do that in example 3.2.
"Save to workspace..." will save the ļ¬t data to the workspace, and there
we can continue to work with this data in the Command Window. In ex-
ample 3.1, Iā€™ll show another way to save data to the Workspace and to plot
it with Error bars.
8
2.3 Exclude..., Plotting... and Analysis...
The ļ¬rst 2 options will open a new window that allows us to exclude some of
the data points from the ļ¬tting (they are still in the data set) or to remove
some data set or ļ¬ts from the plot. These 2 option are very intuitive and
Iā€™ll not elaborate on them.
The analysis will open a window that enable us to get the value of the
ļ¬tted function in some values that we choose (not related to the data set)
and the conļ¬dence bounds. We can also get the ļ¬rst 2 derivatives or to
integrate the function. Iā€™ll explain more when Iā€™ll show an example (Sec.
3.1) with conļ¬dence bounds.
2.4 Residuals
Going to the menu: View ā†’ Residuals ā†’ Scatter Plot, will open a new
graph that will show the residual - the ļ¬nal diļ¬€erence between our data
points and the ļ¬t. Fig.7 is what weā€™ll get for our example.
When looking on the residuals itā€™s important to see that they are scat-
tered randomly and evenly around the zero value. That they are not positive
in one part of the ļ¬t and negative in other part, or that their value isnā€™t
small or large in diļ¬€erent parts.
In our graph both of these criterions donā€™t exist, but if we look on the
value of the residuals, up to 5 Ā· 10āˆ’14, we can understand that these is just
rounding (or calculation of the sin function) error because the numbers in
the computer are not inļ¬nitely accurate.
3 Examples
3.1 Simple linear ļ¬t with error bars
In this example Iā€™ll ļ¬t 5 points to linear ļ¬t. The input is:3
>> x=[1 2 3 4 5];
>> y=[1 2 3 4 4.5];
and the error bars are:
>> err=[0.1 0.2 0.3 0.4 0.5];
This is clearly a strait line with slope of 1, just the last point looks to be
wrong. One option is to exclude this point from the graph but this option
is not the preferred one. Weā€™ll see that adding weights has similar result.
3
This is a way to create a vector with speciļ¬c values.
9
Figure 7: The plot with the residuals.
Figure 8: Fitting the data to linear line. The red (blue) ļ¬t is without (with)
weights
In Fig.8 we can see 2 ļ¬ts, without (red) and with (blue) weights. In the
Fit Editor we see only the result of the ļ¬t with the weights, and we see that
10
Figure 9: The Analysis window and the output.
the ļ¬tted parameters are almost 1 and 0, as we would expect. Now, when
the ļ¬t isnā€™t perfect, the conļ¬dence bound has deferent values. Clearly the
ļ¬t with the weights gives better results, it still not perfect but our data isnā€™t
perfect too. Between the 2 windows, the Matlab commands are also visible
(see the way I deļ¬ned the weights). The error is something that we need to
calculate separately - in this case itā€™s 10% of the x value.
Now weā€™ll go back to the Curve Fitting Tool and open the Analysis
window. In it weā€™ll choose "ļ¬t 2 (y vs. x with weights)", and set the analyze
points to be: 0:0.5:10 (between 0 to 10 in jumps of 0.5). After that weā€™ll
choose "Evaluate ļ¬t at Xi" and "For function". Weā€™ll also add "Plot Results"
and "Plot data set", and click Apply. The output is displayed in Fig.9.
We see that the ļ¬t is relatively accurate where we have data points, but
in the range without data points it start to be less accurate. If weā€™ll do the
same for the data set without the weights weā€™ll see that the prediction is less
accurate.
Plotting the data we have in an Error Bar plot with the ļ¬tting function
and the conļ¬dence bounds is a bit tricky and Iā€™ll explain how to do that.
Letā€™s analyze again the data set with weights and save it to Workspace
(using the button). A new variable will be created. We can change its name
but I used the default one: "analysisresults1". Double click on this variable
in the Workspace tab will open the "Array Editor" where we can see what
this variable contains.
In the Matlab Command Window type the following commands (every-
thing after % is comments and arenā€™t necessary):
>> ļ¬gure % open a new ļ¬gure window
>> errorbar(x, y, err, ā€™roā€™) % plots the data, as circles (small letter o - not
zero), with errorbars in a red color
11
>> hold on % Plotting the next plots without deleting the old ones
>> plot(analysisresults1.xi, analysisresults1.yļ¬t, ā€™b-ā€™, analysisresults1.xi, anal-
ysisresults1.lower, ā€™b:ā€™, analysisresults1.xi, analysisresults1.upper, ā€™b:ā€™) %
plots the ļ¬t in solid blue line and the conļ¬dence bounds in dotted blue line
>> hold oļ¬€ % release the ļ¬gure
(If you use copy/paste delete any new line in the Command Window if
you have one - all the commands are one line).
Fig.10 shows the result. If you want to adjust the ļ¬gure, like adding
titles, clicking the small icon below the help menu will open the needed
editors (or with the menu: View ā†’ Plot Browser and View ā†’ Property
Editor). Choosing the Axes in the Plot Browser will allow us to add titles
(in the Property Editor) to the plot and axes, and choosing one of the plots
will allow us to change the color, shape and more properties of the plot
lines themselves. We can also insert "Legend" or "Textbox" with the "Insert"
menu. In the text box we can copy the ļ¬t data, for example, see Fig.11.
3.2 More advanced ļ¬t
In this example Iā€™ll ļ¬t a data to sin2 function. This function isnā€™t a built
in function so weā€™ll need to create it. This time the ļ¬t wonā€™t work with the
initial parameters so weā€™ll also need to ļ¬x them. In this example Iā€™ll use a
noisy data but without errorbars - just because Iā€™ll already explain about
them and in this example I want to show something else, not because theyā€™re
not important.
Figure 10: A plot with error bars, the ļ¬tted function and the conļ¬dence
bounds.
12
Figure 11: The same plot as in Fig.10 but with titles and some data on the
ļ¬t.
In Fig.12 we can see the commands that create the data and add some
random value. The data is sin2 with amplitude of 8, frequency of 0.2 and
phase of 1, and we would like to get those numbers from the ļ¬t.
In order to get to the state of Fig.12, we need to create the variables
Figure 12: sin2 example.
13
Figure 13: sin2 example - after changing the initial parameters.
in the Matlab Workspace, open the Curve Fitting Tool and create the data
set, and then to open the Fit Editor, create New Fit, Custom Equation
Figure 14: sin2 example - the residuals of the ļ¬nal ļ¬t.
14
and New Equation. In the new window, "Create Custom Equation", go to
"General Equations" tab and type the equation (here we donā€™t need the dot
before the operators, like we need it in the Matlab Command Window).
Here we should also insert the initial parameters for the ļ¬t. I will leave the
one Matlab choose only for pedagogic reason, this way I could show how
to change them afterward. After clicking OK this equation will be added
to the "Custom Equations" in the Fit Editor. Clicking Apply will give the
result that is plotted in the Curve Fitting Tool in Fig.12, which is clearly
not the right ļ¬t. Also in the Result part of the Fit Editor there is a warning
that says the same.
Clicking the "Fit Options..." button will open a new window where we
can change the ļ¬t parameters, see Fig.13.
Achieving the right result can be a bit tricky and annoying, especially
for periodic functions, and it requires that after every change you apply
the changes and look on the ļ¬t to see if it looks right. After you ļ¬nd the
right one you need also to plot the residuals and see that they are randomly
distributed, see Fig.14. My logic here was that the amplitude is about 10,
we see that from the graph. The phase I set to 0, and since in the wrong ļ¬t
the frequency looks to high, I put less then what I got in the wrong ļ¬t.
15
List of Figures
1 An example of the Matlab window. . . . . . . . . . . . . . . . 2
2 An example to deļ¬ne 2 variables and plot them. . . . . . . . 3
3 The result of the "plot(x,y)" command. . . . . . . . . . . . . . 4
4 The Curve Fitting window. . . . . . . . . . . . . . . . . . . . 5
5 Creating a new data set. . . . . . . . . . . . . . . . . . . . . . 6
6 Fitting the data with y = a1 Ā· sin(b1 Ā· x + c1) function. . . . . 8
7 The plot with the residuals. . . . . . . . . . . . . . . . . . . . 10
8 Fitting the data to linear line. The red (blue) ļ¬t is without
(with) weights . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
9 The Analysis window and the output. . . . . . . . . . . . . . 11
10 A plot with error bars, the ļ¬tted function and the conļ¬dence
bounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
11 The same plot as in Fig.10 but with titles and some data on
the ļ¬t. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
12 sin2 example. . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
13 sin2 example - after changing the initial parameters. . . . . . 14
14 sin2 example - the residuals of the ļ¬nal ļ¬t. . . . . . . . . . . 14
16

Weitere Ƥhnliche Inhalte

Was ist angesagt?

Bab 4 dan bab 5 algebra and trigonometry
Bab 4 dan bab 5 algebra and trigonometryBab 4 dan bab 5 algebra and trigonometry
Bab 4 dan bab 5 algebra and trigonometry
CiciPajarakan
Ā 
Roots of equations
Roots of equationsRoots of equations
Roots of equations
Robinson
Ā 

Was ist angesagt? (20)

ML - Multiple Linear Regression
ML - Multiple Linear RegressionML - Multiple Linear Regression
ML - Multiple Linear Regression
Ā 
A Correlative Information-Theoretic Measure for Image Similarity
A Correlative Information-Theoretic Measure for Image SimilarityA Correlative Information-Theoretic Measure for Image Similarity
A Correlative Information-Theoretic Measure for Image Similarity
Ā 
Unit 1.3
Unit 1.3Unit 1.3
Unit 1.3
Ā 
Quantitative Analysis For Decision Making
Quantitative Analysis For Decision MakingQuantitative Analysis For Decision Making
Quantitative Analysis For Decision Making
Ā 
Some Engg. Applications of Matrices and Partial Derivatives
Some Engg. Applications of Matrices and Partial DerivativesSome Engg. Applications of Matrices and Partial Derivatives
Some Engg. Applications of Matrices and Partial Derivatives
Ā 
Bab 4 dan bab 5 algebra and trigonometry
Bab 4 dan bab 5 algebra and trigonometryBab 4 dan bab 5 algebra and trigonometry
Bab 4 dan bab 5 algebra and trigonometry
Ā 
APPLICATION OF NUMERICAL METHODS IN SMALL SIZE
APPLICATION OF NUMERICAL METHODS IN SMALL SIZEAPPLICATION OF NUMERICAL METHODS IN SMALL SIZE
APPLICATION OF NUMERICAL METHODS IN SMALL SIZE
Ā 
Monte carlo-simulation
Monte carlo-simulationMonte carlo-simulation
Monte carlo-simulation
Ā 
Unit 1.1
Unit 1.1Unit 1.1
Unit 1.1
Ā 
Roots of equations
Roots of equationsRoots of equations
Roots of equations
Ā 
03 notes
03 notes03 notes
03 notes
Ā 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
Ā 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
Ā 
Echo Function
Echo FunctionEcho Function
Echo Function
Ā 
Unit 7.2
Unit 7.2Unit 7.2
Unit 7.2
Ā 
Computer graphic
Computer graphicComputer graphic
Computer graphic
Ā 
Comparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesComparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategies
Ā 
Interpolation and its applications
Interpolation and its applicationsInterpolation and its applications
Interpolation and its applications
Ā 
Mathematical modeling models, analysis and applications ( pdf drive )
Mathematical modeling  models, analysis and applications ( pdf drive )Mathematical modeling  models, analysis and applications ( pdf drive )
Mathematical modeling models, analysis and applications ( pdf drive )
Ā 
Unit 7.1
Unit 7.1Unit 7.1
Unit 7.1
Ā 

Andere mochten auch

Polynomial functions
Polynomial functionsPolynomial functions
Polynomial functions
nicole379865
Ā 
Least square method
Least square methodLeast square method
Least square method
Somya Bagai
Ā 

Andere mochten auch (14)

Curve fitting
Curve fitting Curve fitting
Curve fitting
Ā 
Curve fitting
Curve fittingCurve fitting
Curve fitting
Ā 
Applied Numerical Methods Curve Fitting: Least Squares Regression, Interpolation
Applied Numerical Methods Curve Fitting: Least Squares Regression, InterpolationApplied Numerical Methods Curve Fitting: Least Squares Regression, Interpolation
Applied Numerical Methods Curve Fitting: Least Squares Regression, Interpolation
Ā 
Curvefitting
CurvefittingCurvefitting
Curvefitting
Ā 
Automating pKa Curve Fitting Using Origin
Automating pKa Curve Fitting Using OriginAutomating pKa Curve Fitting Using Origin
Automating pKa Curve Fitting Using Origin
Ā 
Curve fitting
Curve fittingCurve fitting
Curve fitting
Ā 
case study of curve fitting
case study of curve fittingcase study of curve fitting
case study of curve fitting
Ā 
Polynomial functions
Polynomial functionsPolynomial functions
Polynomial functions
Ā 
Numerical method (curve fitting)
Numerical method (curve fitting)Numerical method (curve fitting)
Numerical method (curve fitting)
Ā 
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLABPolynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLAB
Ā 
Instrumentation Engineering : Transducers, THE GATE ACADEMY
 Instrumentation Engineering : Transducers, THE GATE ACADEMY Instrumentation Engineering : Transducers, THE GATE ACADEMY
Instrumentation Engineering : Transducers, THE GATE ACADEMY
Ā 
Curve fitting - Lecture Notes
Curve fitting - Lecture NotesCurve fitting - Lecture Notes
Curve fitting - Lecture Notes
Ā 
Non linear curve fitting
Non linear curve fitting Non linear curve fitting
Non linear curve fitting
Ā 
Least square method
Least square methodLeast square method
Least square method
Ā 

Ƅhnlich wie Curve fitting

Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
Indrani Jangete
Ā 
Excel Training
Excel TrainingExcel Training
Excel Training
James Ramsey
Ā 
Microsoft mathmatics step-by-step_guide
Microsoft mathmatics step-by-step_guideMicrosoft mathmatics step-by-step_guide
Microsoft mathmatics step-by-step_guide
Anang Anang
Ā 
Matlab tut2
Matlab tut2Matlab tut2
Matlab tut2
Vinnu Vinay
Ā 

Ƅhnlich wie Curve fitting (20)

Mmc manual
Mmc manualMmc manual
Mmc manual
Ā 
An Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked ExamplesAn Introduction to MATLAB with Worked Examples
An Introduction to MATLAB with Worked Examples
Ā 
Basic matlab for beginners
Basic matlab for beginnersBasic matlab for beginners
Basic matlab for beginners
Ā 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
Ā 
Anything but simple Mathematica
Anything but simple MathematicaAnything but simple Mathematica
Anything but simple Mathematica
Ā 
interfacing matlab with embedded systems
interfacing matlab with embedded systemsinterfacing matlab with embedded systems
interfacing matlab with embedded systems
Ā 
Mathcad
MathcadMathcad
Mathcad
Ā 
Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62Machine Learning Guide maXbox Starter62
Machine Learning Guide maXbox Starter62
Ā 
Excel Training
Excel TrainingExcel Training
Excel Training
Ā 
How to make fewer errors at the stage of code writing. Part N3.
How to make fewer errors at the stage of code writing. Part N3.How to make fewer errors at the stage of code writing. Part N3.
How to make fewer errors at the stage of code writing. Part N3.
Ā 
EE6711 Power System Simulation Lab manual
EE6711 Power System Simulation Lab manualEE6711 Power System Simulation Lab manual
EE6711 Power System Simulation Lab manual
Ā 
Matlab 1 level_1
Matlab 1 level_1Matlab 1 level_1
Matlab 1 level_1
Ā 
How To Automate Part 3
How To Automate Part 3How To Automate Part 3
How To Automate Part 3
Ā 
Data Transformation
Data TransformationData Transformation
Data Transformation
Ā 
Me 443 4 plotting curves Erdi KaraƧal Mechanical Engineer University of Gaz...
Me 443   4 plotting curves Erdi KaraƧal Mechanical Engineer University of Gaz...Me 443   4 plotting curves Erdi KaraƧal Mechanical Engineer University of Gaz...
Me 443 4 plotting curves Erdi KaraƧal Mechanical Engineer University of Gaz...
Ā 
Microsoft mathmatics step-by-step_guide
Microsoft mathmatics step-by-step_guideMicrosoft mathmatics step-by-step_guide
Microsoft mathmatics step-by-step_guide
Ā 
A complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projectsA complete introduction on matlab and matlab's projects
A complete introduction on matlab and matlab's projects
Ā 
Matlab tut2
Matlab tut2Matlab tut2
Matlab tut2
Ā 
User rpl tut
User rpl tutUser rpl tut
User rpl tut
Ā 
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory optionStar Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Star Transformation, 12c Adaptive Bitmap Pruning and In-Memory option
Ā 

KĆ¼rzlich hochgeladen

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
Ā 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
Christopher Logan Kennedy
Ā 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
Ā 

KĆ¼rzlich hochgeladen (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
Ā 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Ā 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Ā 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
Ā 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Ā 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
Ā 
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls šŸ„° 8617370543 Service Offer VIP Hot Model
Ā 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
Ā 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Ā 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Ā 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
Ā 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
Ā 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
Ā 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
Ā 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Ā 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
Ā 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
Ā 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
Ā 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
Ā 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Ā 

Curve fitting

  • 1. Curve Fitting in Matlab Shimi Machlufāˆ— November 8, 2008 Contents 1 Introduction 2 2 The Curve Fitting Tool 5 2.1 Data... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.2 Fitting... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.3 Exclude..., Plotting... and Analysis... . . . . . . . . . . . . . . 9 2.4 Residuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 3 Examples 9 3.1 Simple linear ļ¬t with error bars . . . . . . . . . . . . . . . . . 9 3.2 More advanced ļ¬t . . . . . . . . . . . . . . . . . . . . . . . . 12 List of Figures 16 āˆ— Like any other document, this one can have mistakes or not be clear enough. I would like to get comments regarding this text to my email: machlufs@bgu.ac.il. 1
  • 2. 1 Introduction In this tutorial I assume you know the basics of working with Matlab. For the ones who donā€™t know, there are many other tutorials, like the ones you can ļ¬nd in Matlab help, Highlern or just search the internet. Still, Iā€™ll explain the basic commands and will give examples. Few more general comments: ā€¢ The pictures of the computer screen dosnā€™t look clear. This problem is due to the screen size relative to the paper size, zooming in will solve this problem. ā€¢ In the pictures that you see a window and the command window in the background, usually you can see in the command window the com- mands I used. In Fig.1 we can see the Matlab window, where the big part is the "Command Window" and on the left there are 3 tabs: "Current Directory", "Workspace" and "Command History". ā€¢ The "Command Window" is the main window and in it we insert/add data into the Matlab Workspace or writing commands. ā€¢ The "Current Directory" is where the Matlab path is set. You can change this directory or open ļ¬les directly from this tab. If you want Figure 1: An example of the Matlab window. 2
  • 3. to run scripts or functions, they need to be in this directory or you need to go to their directory. ā€¢ The "Workspace" is a tab showing all the variables you already deļ¬ned in this Matlab session. When you close Matlab the Workspace is being deleted, but you can save the Workspace to ļ¬le and load it in diļ¬€erent time with the File menu. ā€¢ The "Command History" tab saves all the command you typed in the "Command Window", also in old sessions of Matlab. Now Iā€™ll explain very shortly how to insert data into the Matlab Workspace, manipulate it and then plot it to ļ¬gure. In Fig.2, I typed 3 commands. The ļ¬rst one: >> x = [0:0.1:2*pi] just deļ¬nes a vector (an array) of numbers. Starting at 0, to 2Ļ€ with jumps of 0.1. After typing the command Matlab plots the result - the value of the vector x. In the second command: >> y = sin(x).āˆ§2; I deļ¬ne a new variable and set its values to be sin(x)2. In this command I Figure 2: An example to deļ¬ne 2 variables and plot them. 3
  • 4. Figure 3: The result of the "plot(x,y)" command. would like you to pay attention to 2 things: 1. First, to the fact that there was no output. This is because of the semicolon (;) at the end of the command. This semicolon tells Matlab that it shouldnā€™t print the output. You should put the semicolon at the end of every line, unless you want to see the output, for 2 main reasons: Itā€™s easier to see what commands you already inserted and when there is a lot to print it can take some time. 2. The second thing is that when I wanted to use the "square" operator I put a dot (.) before it. The reason is that Matlab, by default, works with matrices and vectors, and in order to multiply 2 vectors at size n, their size need to be [1Ɨn]Ā·[nƗ1] for scalar product, or [nƗ1]Ā·[1Ɨn] for getting the square of each element. Since our vectors have the same size, using the dot before the square operator tell Matlab to do this operation (square in our case) on each element and not on the vector. The last command just opens a new Figure window (Fig.3) and plots y as a function of x. I want to mention a few more things before we continue: ā€¢ As you can see, the 2 variables were added to the Workspace, see Fig.2. We can see their size and their nim/max values. ā€¢ The variable Ļ€, is already deļ¬ned in the Matlab Workspace but we donā€™t see it in the Workspace tab. Also the imaginary number ı is 4
  • 5. Figure 4: The Curve Fitting window. already deļ¬ned as >> i=sqrt(-1); ā€¢ The functions "sin" and "sqrt", square root, are Matlab functions. The Matlab Help contains information about these functions and on any other Matlab function. There are A LOT OF Matlab functions. Any mathematical function like "sin" or "exp" or manipulation function (on vector or matrices) like "max" or "mean" that you can think of, is probably there. 2 The Curve Fitting Tool to open the Curve Fitting tool just type: >> cftool and this tool will be open in a new window. See Fig.4. This window has 5 buttons: Data...: Will open a new window that will allow us to import data from the Matlab workspace into the Curve Fitting tool environment (called data set). 5
  • 6. Figure 5: Creating a new data set. Fitting...: This one will set the ļ¬tting function and parameter. Exclude...: With this window we can exclude some data points from the data set. Plotting...: If we have a few data set and/or a few graphs, here we can decide which one we want to see, to plot. The rest are just not shown, there are not deleted. Analysis...: If we want to evaluate the ļ¬tted function we can use this win- dow. The Data... and Fitting... are the most important and Iā€™ll explain how to use them in more details using the next example (y = sin(x)). 2.1 Data... After pressing the Data... button, a new window will open, Fig.5, and we could choose the x, y and weights variables1, set the data set name and click the "Create data set" button. After that youā€™ll see your data set in the list and you can close the window. 1 These variables MUST exist in the Matlab Workspace, otherwise they wonā€™t appear in the list. 6
  • 7. In this short example I didnā€™t set any value to the weight because I created the x and y variables. When you want to analyze some data, usually the x parameter is something you choose. Like measuring something every second (or some other time), or every 10 cm. Thatā€™s the meaning of free parameter. The y parameter is something you measure (the dependent parameter) and since weā€™re in real life, the measurement has errors. Some of the data points will have small error and some will have big error2. The regular ļ¬t (without weights) consider all the data points as equal, but if some of the data points have larger error bars (relative to the other points) it means that they are less accurate and that the ļ¬t should consider these points less in its calculations. Thatā€™s what the weights are for. The more accurate data points get higher weights relative to the less accurate data points. The way to create the weights, according to the above logic, is that the weight is the inverse of the square of the error bar. In a mathematical way: if wi is the weight and Ļƒi is the error of the ith data point, then: wi = 1 Ļƒ2 i . The Smooth tab is for smoothing noisy data, something we donā€™t need for now and I wonā€™t explain. 2.2 Fitting... To start ļ¬tting the data, we need to click the Fitting... button so the Fit Editor will open. There we need to start "New ļ¬t", give it the name we want (the default is ļ¬t and a number), choose the data set we want to ļ¬t, choose the ļ¬tting function (Type of ļ¬t) and click Apply. There are a lot of built in ļ¬tting function which are grouped, like "Sum of Sin Function" (what I used because I want to ļ¬t the data to Sin function) or "Polynomial" functions, where in every group you need to choose the speciļ¬c function you want to ļ¬t to. After clicking the Apply button, the ļ¬t is computed and the results of the ļ¬t are printed in the "Result" part of the ļ¬t editor and also plotted as a graph in the Curve Fitting Tool. In Fig.6 we can see the input I entered the Fit Editor and the result of the ļ¬t. In this Figure I also adjusted the Fit Editor so we could see all of the output. Just by looking on the plot we can see that the ļ¬t is good and the R-square and Adjusted R-square (check the Help for details) conļ¬rm that. We can also see that the amplitude (a1) is 1, the frequency (or more precisely, Ļ‰ = 2Ļ€f, which is b1 if our x variable 2 If we took a few measurement for each x value, we can put all of them into the ļ¬t. The error (or error bar) is for each measurement. It can happen that all the measurements for the same x value will have the same error. 7
  • 8. Figure 6: Fitting the data with y = a1 Ā· sin(b1 Ā· x + c1) function. is the time) is also 1 and that the phase (c1) is 0, like the parameters we choose when we created the y vector (y = sin(x)). The numbers in parenthesis near the values of the parameters (Coeļ¬ƒ- cients) are the conļ¬dence bounds - the error in the ļ¬t. Since our ļ¬t ļ¬ts exactly the data set, these conļ¬dence bounds are zeros (the same value as the parameter itself). In other examples, like in Sec. 3.1, where the ļ¬t wonā€™t be so good, weā€™ll see that the conļ¬dence bounds are not zero any more. In general, the ļ¬tting algorithm start from some values of the ļ¬tting constants (a1, b1 and c1, in out example) that Matlab choose, and try to converge from that initial parameter to the ļ¬nal ones. Sometimes the initial parameters are not close enough to the ļ¬nal ones so the algorithm converge to something else or doesnā€™t converge at all. Clicking the "Fit options..." will open a window that allow as to change this initial parameter. I will use that in one of the examples. If the ļ¬t function that we need isnā€™t in the list of functions, we can choose as the Type of ļ¬t "Custom Equations" and then "New equation" and to build the needed function. I will show how to do that in example 3.2. "Save to workspace..." will save the ļ¬t data to the workspace, and there we can continue to work with this data in the Command Window. In ex- ample 3.1, Iā€™ll show another way to save data to the Workspace and to plot it with Error bars. 8
  • 9. 2.3 Exclude..., Plotting... and Analysis... The ļ¬rst 2 options will open a new window that allows us to exclude some of the data points from the ļ¬tting (they are still in the data set) or to remove some data set or ļ¬ts from the plot. These 2 option are very intuitive and Iā€™ll not elaborate on them. The analysis will open a window that enable us to get the value of the ļ¬tted function in some values that we choose (not related to the data set) and the conļ¬dence bounds. We can also get the ļ¬rst 2 derivatives or to integrate the function. Iā€™ll explain more when Iā€™ll show an example (Sec. 3.1) with conļ¬dence bounds. 2.4 Residuals Going to the menu: View ā†’ Residuals ā†’ Scatter Plot, will open a new graph that will show the residual - the ļ¬nal diļ¬€erence between our data points and the ļ¬t. Fig.7 is what weā€™ll get for our example. When looking on the residuals itā€™s important to see that they are scat- tered randomly and evenly around the zero value. That they are not positive in one part of the ļ¬t and negative in other part, or that their value isnā€™t small or large in diļ¬€erent parts. In our graph both of these criterions donā€™t exist, but if we look on the value of the residuals, up to 5 Ā· 10āˆ’14, we can understand that these is just rounding (or calculation of the sin function) error because the numbers in the computer are not inļ¬nitely accurate. 3 Examples 3.1 Simple linear ļ¬t with error bars In this example Iā€™ll ļ¬t 5 points to linear ļ¬t. The input is:3 >> x=[1 2 3 4 5]; >> y=[1 2 3 4 4.5]; and the error bars are: >> err=[0.1 0.2 0.3 0.4 0.5]; This is clearly a strait line with slope of 1, just the last point looks to be wrong. One option is to exclude this point from the graph but this option is not the preferred one. Weā€™ll see that adding weights has similar result. 3 This is a way to create a vector with speciļ¬c values. 9
  • 10. Figure 7: The plot with the residuals. Figure 8: Fitting the data to linear line. The red (blue) ļ¬t is without (with) weights In Fig.8 we can see 2 ļ¬ts, without (red) and with (blue) weights. In the Fit Editor we see only the result of the ļ¬t with the weights, and we see that 10
  • 11. Figure 9: The Analysis window and the output. the ļ¬tted parameters are almost 1 and 0, as we would expect. Now, when the ļ¬t isnā€™t perfect, the conļ¬dence bound has deferent values. Clearly the ļ¬t with the weights gives better results, it still not perfect but our data isnā€™t perfect too. Between the 2 windows, the Matlab commands are also visible (see the way I deļ¬ned the weights). The error is something that we need to calculate separately - in this case itā€™s 10% of the x value. Now weā€™ll go back to the Curve Fitting Tool and open the Analysis window. In it weā€™ll choose "ļ¬t 2 (y vs. x with weights)", and set the analyze points to be: 0:0.5:10 (between 0 to 10 in jumps of 0.5). After that weā€™ll choose "Evaluate ļ¬t at Xi" and "For function". Weā€™ll also add "Plot Results" and "Plot data set", and click Apply. The output is displayed in Fig.9. We see that the ļ¬t is relatively accurate where we have data points, but in the range without data points it start to be less accurate. If weā€™ll do the same for the data set without the weights weā€™ll see that the prediction is less accurate. Plotting the data we have in an Error Bar plot with the ļ¬tting function and the conļ¬dence bounds is a bit tricky and Iā€™ll explain how to do that. Letā€™s analyze again the data set with weights and save it to Workspace (using the button). A new variable will be created. We can change its name but I used the default one: "analysisresults1". Double click on this variable in the Workspace tab will open the "Array Editor" where we can see what this variable contains. In the Matlab Command Window type the following commands (every- thing after % is comments and arenā€™t necessary): >> ļ¬gure % open a new ļ¬gure window >> errorbar(x, y, err, ā€™roā€™) % plots the data, as circles (small letter o - not zero), with errorbars in a red color 11
  • 12. >> hold on % Plotting the next plots without deleting the old ones >> plot(analysisresults1.xi, analysisresults1.yļ¬t, ā€™b-ā€™, analysisresults1.xi, anal- ysisresults1.lower, ā€™b:ā€™, analysisresults1.xi, analysisresults1.upper, ā€™b:ā€™) % plots the ļ¬t in solid blue line and the conļ¬dence bounds in dotted blue line >> hold oļ¬€ % release the ļ¬gure (If you use copy/paste delete any new line in the Command Window if you have one - all the commands are one line). Fig.10 shows the result. If you want to adjust the ļ¬gure, like adding titles, clicking the small icon below the help menu will open the needed editors (or with the menu: View ā†’ Plot Browser and View ā†’ Property Editor). Choosing the Axes in the Plot Browser will allow us to add titles (in the Property Editor) to the plot and axes, and choosing one of the plots will allow us to change the color, shape and more properties of the plot lines themselves. We can also insert "Legend" or "Textbox" with the "Insert" menu. In the text box we can copy the ļ¬t data, for example, see Fig.11. 3.2 More advanced ļ¬t In this example Iā€™ll ļ¬t a data to sin2 function. This function isnā€™t a built in function so weā€™ll need to create it. This time the ļ¬t wonā€™t work with the initial parameters so weā€™ll also need to ļ¬x them. In this example Iā€™ll use a noisy data but without errorbars - just because Iā€™ll already explain about them and in this example I want to show something else, not because theyā€™re not important. Figure 10: A plot with error bars, the ļ¬tted function and the conļ¬dence bounds. 12
  • 13. Figure 11: The same plot as in Fig.10 but with titles and some data on the ļ¬t. In Fig.12 we can see the commands that create the data and add some random value. The data is sin2 with amplitude of 8, frequency of 0.2 and phase of 1, and we would like to get those numbers from the ļ¬t. In order to get to the state of Fig.12, we need to create the variables Figure 12: sin2 example. 13
  • 14. Figure 13: sin2 example - after changing the initial parameters. in the Matlab Workspace, open the Curve Fitting Tool and create the data set, and then to open the Fit Editor, create New Fit, Custom Equation Figure 14: sin2 example - the residuals of the ļ¬nal ļ¬t. 14
  • 15. and New Equation. In the new window, "Create Custom Equation", go to "General Equations" tab and type the equation (here we donā€™t need the dot before the operators, like we need it in the Matlab Command Window). Here we should also insert the initial parameters for the ļ¬t. I will leave the one Matlab choose only for pedagogic reason, this way I could show how to change them afterward. After clicking OK this equation will be added to the "Custom Equations" in the Fit Editor. Clicking Apply will give the result that is plotted in the Curve Fitting Tool in Fig.12, which is clearly not the right ļ¬t. Also in the Result part of the Fit Editor there is a warning that says the same. Clicking the "Fit Options..." button will open a new window where we can change the ļ¬t parameters, see Fig.13. Achieving the right result can be a bit tricky and annoying, especially for periodic functions, and it requires that after every change you apply the changes and look on the ļ¬t to see if it looks right. After you ļ¬nd the right one you need also to plot the residuals and see that they are randomly distributed, see Fig.14. My logic here was that the amplitude is about 10, we see that from the graph. The phase I set to 0, and since in the wrong ļ¬t the frequency looks to high, I put less then what I got in the wrong ļ¬t. 15
  • 16. List of Figures 1 An example of the Matlab window. . . . . . . . . . . . . . . . 2 2 An example to deļ¬ne 2 variables and plot them. . . . . . . . 3 3 The result of the "plot(x,y)" command. . . . . . . . . . . . . . 4 4 The Curve Fitting window. . . . . . . . . . . . . . . . . . . . 5 5 Creating a new data set. . . . . . . . . . . . . . . . . . . . . . 6 6 Fitting the data with y = a1 Ā· sin(b1 Ā· x + c1) function. . . . . 8 7 The plot with the residuals. . . . . . . . . . . . . . . . . . . . 10 8 Fitting the data to linear line. The red (blue) ļ¬t is without (with) weights . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 9 The Analysis window and the output. . . . . . . . . . . . . . 11 10 A plot with error bars, the ļ¬tted function and the conļ¬dence bounds. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 11 The same plot as in Fig.10 but with titles and some data on the ļ¬t. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 12 sin2 example. . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 13 sin2 example - after changing the initial parameters. . . . . . 14 14 sin2 example - the residuals of the ļ¬nal ļ¬t. . . . . . . . . . . 14 16