How can I find the Y value on an X–Y plot that corresponds to the tangent of the flattest part of a curve?
8 views (last 30 days)
I have plots like the one attached. At Y >0, the curve plateaus (flattens) before it increases sharply. I need to find the Y value at which the plateau/flat area is flattest.
Does anyone know how to do this? I can't figure out a solution that gets the part of the curve that I want. Thank you.
Star Strider on 22 Aug 2021
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/718334/exampleData.txt')
T1 = rmmissing(T1); % Remove 'NaN' Values
h = mean(diff(T1.Var1))
d2d1 = gradient(T1.Var2, h); % Numerical Derivative
flatidx = find(abs(d2d1)<1E-14); % Zero Slope (With Tolerance)
yflat = T1.Var2(flatidx)
plot(T1.Var1(flatidx), T1.Var2(flatidx), 'vr')
legend('Data','Flat Section', 'Location','best')
This should also work with other data sets, although obviously I cannot test it with them.
More Answers (1)
Turlough Hughes on 23 Aug 2021
Edited: Turlough Hughes on 23 Aug 2021
How robust this is depends on the consistency of that initial pattern, i.e. the initial acceleration followed by a period of deceleration (starting to plateau) until the "flattest" point where it then begins to accelerate again. This point between the initial deceleration and acceleration is also known as an inflection point, as mentioned by @dpb. It's also the point where where y is closest to being parallel to the x-axis in the region (where it is initially plateauing).
To find the inflection point we find the location where . I understand you want the second one as follows:
T = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/718334/exampleData.txt');
x = T(:,1);
y = T(:,2);
ypp = gradient(gradient(y,x),x); % second derivative of y w.r.t. x
idx = ypp > 0;
hold on, plot(x(idx),ypp(idx),'or','LineWidth',2)
iFlat = find(diff(idx)==1)+1; % y is most linear when it's second derivate, ypp, is equal to 0
iInflect = iFlat(2); % the second time ypp becomes > 0 approximates the second inflection point.
hold on, plot(x(iInflect),y(iInflect),'sk','MarkerSize',10,'LineWidth',2)
x(1:iInflect-1) = ;
y(1:iInflect-1) = ;
legend('Original Dataset','Second Inflection Point','New Dataset','Location','NorthWest')