# How to find the x element of a list for which the y element has smallest difference with its neighbouring y element?

I have a list of data with two columns, $$x$$ and $$y$$. I want to find the first $$x$$ element in this list for which the $$y$$ element has smallest difference with its adjacent $$y$$ element. In other words, I want to find the $$x$$ element for which $$y$$ has zero (or very small) derivative.

• Can you share the data and your desired output? Commented May 1, 2020 at 16:40
• data={{0, 0}, {1, 0.139366}, {2, 0.236317}, {3, 0.29733}, {4, 0.334718}, {5, 0.357199}, {6, 0.370556}, {7, 0.378447}, {8, 0.383104}, {9, 0.385857}, {10, 0.387491}, {11, 0.388465}, {12, 0.389048}, {13, 0.389399}, {14, 0.38961}, {15, 0.389738}, {16, 0.389816}, {17, 0.389863}, {18, 0.389892}, {19, 0.389909}, {20, 0.38992}, {21, 0.389926}, {22, 0.38993}, {23, 0.389933}, {24, 0.389934}, {25, 0.389935}, {26, 0.389935}, {27, 0.389936}, {28, 0.389936}, {29, 0.389936}, {30, 0.389936}} Commented May 1, 2020 at 23:32
• Here is an example of a data i have. The point is that i create this data in a part of a bigger loop and in each time of repeating the loop i want to know the first x element for which the difference of the adjacent y elements are for example less than 0.0001. Commented May 1, 2020 at 23:35
• The desired x value for the data i shared above will be 16. Commented May 1, 2020 at 23:49

Update

Using data from comment

yDelta = data // Transpose // MapAt[Differences, #, 2] &;
yDeltaMinPos = yDelta[[2]] // FirstPosition[x_ /; x < 0.0001]
(* {16} *)


(* Generate random data *)
SeedRandom[1]; list = RandomReal[{0, 5}, {10, 2}];

(* y value differences *)
yDelta = list // Transpose // MapAt[Differences, #, 2] &

(* Position of min y delta *)
yDeltaMinPos = yDelta // Position[#, (Min[#[[2]]])] &
(* {{2, 6}} *)

(* x value at position 6 *)
yDelta // Extract[{{1, 6}}]
(* {1.05913} *)

• Since what i want to do is a part of a bigger loop and i should get the x value at each time of repeating the loop, i do not see which position do i get for "yDeltaMinPos", therefore i tried your suggestion as "yDelta // Extract[{{ 1,yDeltaMinPos [[1]]}}]", using "yDeltaMinPos[[1]]" to indicate the position, but this did not work! Commented May 1, 2020 at 23:58
• Try this yDelta // Extract[{yDeltaMinPos[[1, 1]] - 1, yDeltaMinPos[[1, 2]]}]. Commented May 3, 2020 at 2:11
• Updated answer to use data and limit you provided in the comment. Commented May 3, 2020 at 2:23