I want to find :
- all local maxima in range
- all local minima in range
From those points I can interpolate and combine functions upper and lower boundary. What I am really interested in, is the mean function of those boundaries.

Data model for this plot:
GetRLine3[MMStdata_, IO_: 1][x_: x] := ListInterpolation[#, InterpolationOrder -> IO,
Method -> "Spline"][x] & /@ (({{#[[1]]}, #[[2]]}) & /@ # & /@ MMStdata);
data = Transpose[{# + RandomReal[]*0.1 & /@ Range[-10, 30, 0.4],
Tanh[#] + (Sech[2 x - 0.5]/1.5 + 1.5) /. x -> # & /@ Range[-4, 4, 0.08]}];
xLimits = {Min@#1, Max@#1} & @@ Transpose[data];
f = D[GetRLine3[{data}, 3][x], x];
Edit: As my effort:
minimums = DeleteDuplicates[Round[x /. Last[FindMinimum[f, {x, #}]] & /@ Transpose[data][[1]], 0.0001]]
minimumvalues = (f /. x -> #)[[1]] & /@ minimums;
minimumData := Transpose[{minimums, minimumvalues}];
maximums = DeleteDuplicates[Round[x /. Last[FindMaximum[f, {x, #}]] & /@ Transpose[data][[1]], 0.0001]];
maximumsvalues = (f /. x -> #)[[1]] & /@ maximums;
maximumsData := Transpose[{maximums, maximumsvalues}];
maxf = Max[{GetRLine3[{maximumsData}, 3][x], f}]
minf = Min[{GetRLine3[{minimumData}, 3][x], f}]
mf = Mean[{maxf, minf}]
This was what I was trying to make: 
I still get quite few warnings and I'm sure it's not the best solution. I don't like the DeleteDuplicates@Round@ part, but it was necessarily to get the interpolation function working.
Answer
This can be done using event location within NDSolve. I start off as below (note f is slightly modified from what you have, mostly to rescale it).
GetRLine3[MMStdata_, IO_: 1][x_: x] :=
ListInterpolation[#, InterpolationOrder -> IO, Method -> "Spline"][
x] & /@ (({{#[[1]]}, #[[2]]}) & /@ # & /@ MMStdata);
data = Transpose[{# + RandomReal[]*0.1 & /@ Range[-10, 30, 0.4],
Tanh[#] + (Sech[2 x - 0.5]/1.5 + 1.5) /. x -> # & /@
Range[-4, 4, 0.08]}];
xLimits = {Min@#1, Max@#1} & @@ Transpose[data];
f = First[100*D[GetRLine3[{data}, 3][x], x]];
We'll recapture f using NDSolve, and locate the points where the derivative vanishes in the process.
vals = Reap[
soln = y[x] /.
First[NDSolve[{y'[x] == Evaluate[D[f, x]],
y[-9.9] == (f /. x -> -9.9)}, y[x], {x, -9.9, 30},
Method -> {"EventLocator", "Event" -> y'[x],
"EventAction" :> Sow[{x, y[x]}]}]]][[2, 1]];
Visual check:
Plot[f, {x, -9.9, 30},
Epilog -> {PointSize[Medium], Red, Point[vals]}]

Comments
Post a Comment