-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiveMeFit.m
114 lines (100 loc) · 3.21 KB
/
GiveMeFit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function [f_handle,Stats,c] = GiveMeFit(xData,yData,fitType,suppressFigure)
% ------------------------------------------------------------------------------
% Ben Fulcher, 2014-11-20
% ------------------------------------------------------------------------------
if nargin < 4
suppressFigure = false;
end
switch fitType
case 'decayEta0'
s = fitoptions('Method','NonlinearLeastSquares','StartPoint',[200,1]);
f = fittype('A*x.^-n','options',s);
try
[c, Stats] = fit(xData,yData,f);
catch
error('Fit to decayEta0 failed')
end
f_handle = @(x) c.A.*x.^(-c.n);
case 'decayEta'
s = fitoptions('Method','NonlinearLeastSquares','StartPoint',[2,1,-1]);
f = fittype('A*x.^-n + B','options',s);
try
[c, Stats] = fit(xData,yData,f);
catch
error('Fit to decayEta failed')
end
f_handle = @(x) c.A.*x.^(-c.n) + c.B;
case 'exp'
s = fitoptions('Method','NonlinearLeastSquares','StartPoint',[1,0.2,0]);
f = fittype('A*exp(-x*n) + B','options',s);
try
[c, Stats] = fit(xData,yData,f);
catch
error('Fit to exp failed')
end
f_handle = @(x,c) c.A.*exp(-x*c.n) + c.B;
case 'exp0'
s = fitoptions('Method','NonlinearLeastSquares','StartPoint',[1,1]);
f = fittype('A*exp(-x/n)','options',s);
try
[c, Stats] = fit(xData,yData,f);
catch
error('Fit to exp0 failed')
end
f_handle = @(x) c.A.*exp(-x/c.n);
case 'exp1'
s = fitoptions('Method','NonlinearLeastSquares','StartPoint',[1,0]);
f = fittype('exp(-x/n) + B','options',s);
try
[c, Stats] = fit(xData,yData,f);
catch
error('Fit to exp1 failed')
end
f_handle = @(x) exp(-x/c.n) + c.B;
case 'exp_1_0'
s = fitoptions('Method','NonlinearLeastSquares','StartPoint',1);
f = fittype('exp(-x/n)','options',s);
try
[c, Stats] = fit(xData,yData,f);
catch
error('Fit to exp_1_0 failed')
end
f_handle = @(x) exp(-c.n*x);
if ~suppressFigure
figure('color','w'); box('on');
plot(xData,yData,'.k'); hold on; plot(xData,f_handle(xData),'xk');
title(sprintf('Fit exponential to %u points with eta = %.4g\n',length(xData),c.n),'interpreter','none')
end
fprintf(1,'Fit exponential with eta = %.4g\n',c.n);
case 'decay0'
s = fitoptions('Method','NonlinearLeastSquares','StartPoint',200);
f = fittype('A/x','options',s);
try
[c, Stats] = fit(xData,yData,f);
catch
error('Fit to decay0 failed')
end
f_handle = @(x) c.A./x;
case 'decay' % 1/x decay
% f = fittype('A*exp(B*x) + C','options',s);
s = fitoptions('Method','NonlinearLeastSquares','StartPoint',[500, min(yData)]);
f = fittype('A/x + B','options',s);
try
[c, Stats] = fit(xData,yData,f);
catch
error('Fit to decay failed')
end
f_handle = @(x) c.A./x + c.B;
case 'linear'
[c,Stats] = fit(xData,yData,'poly1');
% [p, Stats] = polyfit(xData,yData,1);
Gradient = c.p1; Intercept = c.p2;
f_handle = @(x) Gradient*x + Intercept;
case 'robust_linear'
[p, Stats] = robustfit(xData,yData);
Gradient = p(2); Intercept = p(1);
f_handle = @(x) Gradient*x + Intercept;
otherwise
error('Unknown fit type: ''%s''',fitType);
end
end