-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfindcorners.m
67 lines (58 loc) · 2 KB
/
findcorners.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
% Copyright 2010 Andrew Leifer et al <[email protected]>
% This file is part of Mindcontrol-analysis.
%
% Mindcontrol-analysis is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Mindcontrol-analysis is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with mindcontrol-analysis. If not, see <http://www.gnu.org/licenses/>.
function [tanhCoef,tanhGOF, c1, c2]= findcorners(data);
ymax=max(data);
ymin=min(data);
xmax=length(data);
xmin=0;
%Let's fit to a hyperbolic tangent
%B is the halfway between forward and backwords
%B+A is one steady state velocity
%B-A is the other steady state velocity
%x0 is the center of the change in speed
%x0 +/- 1/beta is the start and end of the change in speed
s=fitoptions('Method','NonlinearLeastSquares',...
'Lower',[ymin, ymin, -Inf, xmin],...
'Upper',[ymax, ymax, Inf, xmax],...
'MaxFunEvals',1200,...
'Startpoint',[(ymax-ymin)/2, (ymax-ymin)/2,1,xmin+0.6*(xmax-xmin)]);
fitTanh=fittype('A*tanh(beta*(x-x0))+B','options',s);
x= 1:length(data);
[tanhCoef,tanhGOF]=fit(x',data',fitTanh);
A=tanhCoef.A;
beta=tanhCoef.beta;
x0=tanhCoef.x0;
B=tanhCoef.B;
y=A*tanh(beta.*(x-x0))+B;
c1=[x0+1/beta; A+B];
c2=[x0-1/beta; B-A];
% %Plot Output:
%
% figure; hold on;
% A=tanhCoef.A;
% beta=tanhCoef.beta;
% x0=tanhCoef.x0;
% B=tanhCoef.B;
% y=A*tanh(beta.*(x-x0))+B;
%
% plot(x,data)
% plot(x,y,'m','linewidth',2)
% plot([x0+1/beta x0-1/beta],[A+B B-A],'ro','linewidth',3);
% figure; hold on;
% plot(data,x)
% plot(y,x,'m','linewidth',2)
% plot([A+B B-A],[x0+1/beta x0-1/beta],'ro','linewidth',3);
%