source: MML/trunk/machine/SOLEIL/common/toolbox/ezyfit/ezyfit/demo/efdemo.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 3.9 KB
Line 
1%% Discover Ezyfit: A free curve fitting toolbox for Matlab
2% F. Moisy, 19 nov 2008.
3%
4% Laboratory FAST, University Paris Sud.
5
6%% About the Ezyfit Toolbox
7% The EzyFit toolbox for Matlab enables you to perform simple curve fitting
8% of one-dimensional data using arbitrary fitting functions. It provides
9% command-line functions and a basic graphical user interface for interactive
10% selection of the data.
11
12%% Simple fit: exponential decay
13%
14% First plot some data, say, an exponential decay.
15
16plotsample exp nodisp
17
18%%
19% A predefined fit called 'exp' allows you to fit your data:
20
21showfit exp
22
23%%
24% Suppose now you want to use your own variable and function names.
25% Let's fit this data with the function f(t)=a*exp(-t/tau), and show the
26% fit with a bold red line:
27
28undofit  % deletes the previous fit
29showfit('f(t)=a*exp(-t/tau)','fitlinewidth',2,'fitcolor','red');
30
31%%
32% Note that showfit recognizes that t is the variable, and the coefficients
33% of the fit are named a and tau.
34%
35% If you want to use the values of the coefficients t and tau into Matlab,
36% you need to creates these variables into the base workspace:
37
38makevarfit
39a
40tau
41
42
43%% Initial guesses
44% Now suppose you want to fit more complex data, like a distribution
45% showing two peaks. Let's try to fit these peaks with two gaussians, each
46% of height a, mean m and width s.
47
48plotsample hist2 nodisp
49showfit('a_1*exp(-(x-x_1)^2/(2*s_1^2)) + a_2*exp(-(x-x_2)^2/(2*s_2^2))');
50
51%%
52% The solver obviously get lost in our 6-dimensional space. Let's help it,
53% by providing initial guesses
54
55undofit
56showfit('a_1*exp(-(x-m_1)^2/(2*s_1^2)) + a_2*exp(-(x-m_2)^2/(2*s_2^2)); a_1=120; m_1=7; a_2 = 100; m_2=15', 'fitcolor','blue','fitlinewidth',2);
57
58%%
59% The result seems to be correct now. Note that only 4 initial guesses are
60% given here; the two other ones, s_1 and s_2, are taken as 1 -- which is
61% close to the expected solution.
62
63
64%% Fitting in linear or in log scale
65% Suppose you want to fit a power law in logarithmic scale:
66
67plotsample power nodisp
68showfit power
69
70%%
71% would you have obtained the same result in linear scale? No:
72swy    % this shortcut turns the Y-axis to linear scale
73showfit('power','fitcolor','red');
74
75%%
76% The value of the coefficients have changed. In the first case, LOG(Y) was
77% fitted, whereas in the second case Y was fitted, because the Y-axis has
78% been changed.
79%
80% You may however force showfit to fit LOG(Y) or Y whatever the Y axis, by
81% specifying 'lin' or 'log' in the first input argument:
82
83rmfit % this removes all the fits
84showfit('power; lin','fitcolor','red');
85showfit('power; log','fitcolor','blue');
86
87%%
88% In the equation information, it is specified (lin) or (log) after the R
89% coefficient.
90
91
92
93
94%% Using the fit structure f
95
96%%
97% You can fit your the data without displaying it:
98x=1:10;
99y=[15 14.2 13.6 13.2 12.9 12.7 12.5 12.4 12.4 12.2];
100f = ezfit(x,y,'beta(rho) = beta_0 + Delta * exp(-rho * mu);  beta_0 = 12');
101
102%%
103% f is a structure that contains all the informations about the fit:
104
105f
106
107%%
108% From this structure, you can plot the data and the fit:
109
110clf
111plot(x,y,'r*');
112showfit(f)
113
114%%
115% you can also display the result of the fit
116
117dispeqfit(f)
118
119%%
120% or create the variables in the base workspace
121
122makevarfit(f)
123beta_0
124mu
125Delta
126
127%% Weigthed fit
128% Suppose now we want to fit data with unequal weights, shown here as error
129% bars of different lengths:
130
131x =  1:10;
132y =  [1.56 1.20 1.10 0.74 0.57 0.55 0.31 0.27 0.28 0.11];
133dy = [0.02 0.02 0.20 0.03 0.03 0.10 0.05 0.02 0.10 0.05];
134clf, errorbar(x,y,dy,'o');
135
136%%
137% In order to perform a weighted fit on this data, the vectors y and dy
138% have to be merged into a 2-by-N matrix and given as the second input
139% argument to ezfit. Compare the results for the usual and weighted fits:
140
141fw = ezfit(x, [y;dy], 'exp');
142showfit(fw,'fitcolor','red');
143f = ezfit(x, y, 'exp');
144showfit(f,'fitcolor','blue');
145
146%%
147% The red curve (weighted fit) tends to go through the data with smaller
148% error bars.
Note: See TracBrowser for help on using the repository browser.