# Extracting the 'critical point'

I have a discrete function (or list of ordered pairs) with respect to a discrete integer n, f[n]. In the format {f[n],n}, the function values in this case would be {{25,1},{10,2},{9,3},{1,4},{0.999,5},{0.998,6},{0.997,7}). It looks like this when plotted:

If I specify Joined-> True then it looks like a linear piecewise function. We see that after $n=4$, the slope of this graph is the highest (or its absolute value lowest). For all $n>=4$, the slope will be highest. Given this kind of function (linear piecewise once it's joined up), How do I extract the number $n=4$ (the point after which joined gradient is highest)?

• What do you mean by "I think this is a statistical phenomenon"? And when you say "highest gradient", I suppose you mean that the derivative is largest there in the sense that the slope there is zero but it's negative before that? (I'm used to think of gradients as vectors, and so "highest gradient" to me means that largest rate of change.) In any case, can you provide sample data for us to work with, perhaps that data you used to generate the plot? – march Oct 28 '15 at 2:56
• Yes I mean slope. And the slope after n=4 is not zero by the way. Its absolute value is very small. The data was from another question so I don't have it, but it's essentially perfectly linear data (piecewise) at every integer n. – minusatwelfth Oct 28 '15 at 8:18
• You write: "I have a function". I do not see any function in your question. Why do not you give this function? Then you write: "If I specify Joined-> True..." This sounds for me not as something about a function, but something about ListPlot  . It seems that you mix up the terms. I suggest that you check Menu/Help/WolframDocumentation/tutorial/DefiningFunctions and Menu/Help/WolframDocumentation/ListPlot and refine your question after that. – Alexei Boulbitch Oct 28 '15 at 8:34
• @march Please see my edit for the datapoints. Please also forget I mentioned statistical phenomenon – minusatwelfth Oct 28 '15 at 8:34
• @AlexeiBoulbitch well we don't have to call it a function, it will essentially just be a list of ordered pairs {y,x} such as the ones I just added to the question – minusatwelfth Oct 28 '15 at 8:41

Trying to keep this as general as possible, I take the data in the OP:

pts = SortBy[First]@(Reverse /@
{{25, 1}, {10, 2}, {9, 3}, {1, 4}, {0.999, 5}, {0.998, 6}, {0.997, 7}})
(* {{1, 25}, {2, 10}, {3, 9}, {4, 1}, {5, 0.999}, {6, 0.998}, {7, 0.997}} *)


because I prefer {n, f[n]}, not the other way round. SortBy[First] is included just in case the data aren't sorted in order of increasing n. Now some nice approaches might be available in v10, such as MovingMap or rather complicated expressions can do this with ListCorrelate, but I'll try to keep this as simple as possible instead, using stuff like Apply and Partition.

MaximalBy[({#1, (#4 - #2)/(#3 - #1)} &) @@@ (Flatten /@
Partition[pts, 2, 1]), Last] // First // First
(* 4 *)


Partition[pts, 2, 1] gives a list like {{point1, point2},{point2, point3}..., Flatten /@ flattens all the sublists like {point1, point2} to flat lists of 4 numbers. Then a pure function is applied to each of those lists. It is written here as an anonymous function.

The full form of the expression above (Flatten /@ ...) looks like

List[List[x1, y1, x2, y2], List[x2, y2, x3, y3], ...]


And the operator @@@ replaces each List inside the main outer List with some anonymous function (let's call it f here) like so:

List[f[x1, y1, x2, y2], f[x2, y2, x3, y3], ...]


The body of the function is {#1, (#4 - #2)/(#3 - #1)} where #n stands for the n-th argument of this function. So we get a list of tuples where the first element is the n value and the second is the slope between the n-th and n+1-th point. The purpose of MaximalBy should be quite obvious.

There are shorter ways to do this. For example, as we know, that the data are evenly spaced, we can do

Join @@@ Transpose[{Most[pts], Differences[pts]}] // MaximalBy[Last] // First // First


I suggest looking at the result of the innermost expression (that is, the Transpose), then adding the application (Join @@@), then adding the postfix operators one-by-one and looking at each result to see how this works.

• That sure was a weird format. Which fields put the ordinate before the abscissa, anyway? – J. M. will be back soon Oct 28 '15 at 12:30
• @J.M. Josephson junctions voltage-current plots prefer to have current as a function of voltage, not the other way round, even though one truly drives a current through a junction and measures the voltage. – LLlAMnYP Oct 28 '15 at 12:33
• Ah, so in that, they're sampling the function, but they're actually interested in the inverse. Interesting. – J. M. will be back soon Oct 28 '15 at 12:46
• @J.M. It's a matter of convention. The functions used to be plotted by 2-channel oscilloscopes. I guess someone just mixed up the inputs some time back :) More seriously, for non-superconducting circuits one can easily apply a certain voltage, rather than a current. So that's how the axes are ordered in non-SC devices. – LLlAMnYP Oct 28 '15 at 12:55
• Does this work for a dataset of any size? For example if I had 100 points instead of 7 – minusatwelfth Oct 28 '15 at 13:02