Reputation: 2225
I have many different data sets which are discrete data. The local minimum is not necessary the smallest data but it is the valley around the first peak. I am trying to find the indices of the first valley around the the first peak. My idea is to search the difference between two neighbor points and when the difference is less than some critical value and when the forward point larger than the backward point, then that's the point we wanted. e.g.
for k=PEAK_POS:END_POS
if ( (abs(y(k)-y(k-1))<=0.01) && (y(k-1)>y(k)) )
expected_pos = k;
break;
end
end
this works for some data set but not for all since some dataset might have different sample step so we might change the critical condition but I have so many data set to analyse, I don't think I can analyze each set manually. I am looking for any better way to find that minimum. Thanks.
Upvotes: 0
Views: 7166
Reputation: 4732
I would use the condition that to the left of a minimum its derivation <0 and to the right >0.
Like in this example:
x = cumsum(rand(1,100)); % nonuniform distance
y = 5*sin(x/10)+randn(size(x)); % example data
dd = diff(y);
ig = [false (dd(1:end-1)<0 & dd(2:end)>0) false]; % patch to correct length
plot(x,y)
line(x(ig),y(ig),'Marker','+','LineStyle','none','Color','r')
and as you stat you'd like the first after a peak:
x_peak = 15;
candidates = x(ig);
i_min=find(candidates>x_peak,1,'first');
candidates(i_min)
Upvotes: 0
Reputation: 38032
As mentioned by @JakobS., optimization theory is a large field in Mathematics, with its own journals and conferences and everything.
Your problem does not sound complicated enough to justify the optimization toolbox (correct me if I'm wrong). It sounds like fminsearch
is sufficient for your needs. Here's a brief tutorial for it. Type help fminsearch
or doc fminsearch
for more info.
% example cost function to minimize
z = @(x) sin(x(:,1)).*cos(x(:,2));
% Make a plot
x = -pi:0.01:pi;
y = x;
[x,y] = meshgrid(x,y);
figure(1), surf(x,y, reshape(z([x(:) y(:)]), size(x)), 'edgecolor', 'none')
% find local minimum
[best, fval] = fminsearch(z, [pi,pi])
The result is
best =
1.570819831365890e+00 3.141628097071647e+00
fval =
-9.999999990956473e-01
Which is obviously a very reasonable approximation to the expected local optimum.
Upvotes: 1
Reputation: 1891
Optimization problems are a very broad topic and there has been done a lot already, it's not necessarily a good idea to start coding your own algorithms. For matlab there is the optimization toolbox, which might help: http://www.mathworks.de/products/optimization/
Upvotes: 0