How can I find the Y value on an X–Y plot that corresponds to the tangent of the flattest part of a curve?
Show older comments

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.
8 Comments
Turlough Hughes
on 22 Aug 2021
Can we depend on that initial pattern always happening? i.e. initially flat, then accelerates then decelerates again to the "flattest" point on the plateau? Can you provide some representative data please.
Srh Fwl
on 22 Aug 2021
Srh Fwl
on 22 Aug 2021
dpb
on 22 Aug 2021
For the example, the only "flat" portion I see is for X<~3; is that the area of interest or is it the inflection region between ~3 < X < ~5?
If it's the latter, looking for the change in slope to locate the "hump" would seem to be the starting point.
If you plot the slope of the line (see gradient) you'll see a dip toward y=0 at the area around ~3.5 but it doesn't quite reach 0 so it's not technically flat. You may want to set a threashold (slope ~2?) and identify the area I think you're refering to by searching for slopes that fall below the threshold after the initial rise of the slope curve. That means you'll exclude all slope values prior to the first peak of the slope curve which can be found by a change in sign of the second derivative of the raw data.
Srh Fwl
on 23 Aug 2021
Accepted Answer
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);
subplot(2,1,1)
plot(x,y,'LineWidth',3)
ylabel('f(x)'), xlabel('x')
set(gca,'fontSize',12)
subplot(2,1,2)
ypp = gradient(gradient(y,x),x); % second derivative of y w.r.t. x
plot(x,ypp,'LineWidth',3);
ylabel('f''''(x)')
xlabel('x')
set(gca,'fontSize',12)
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.
plot(x(iInflect),ypp(iInflect),'sk','MarkerSize',10,'LineWidth',2)
subplot(2,1,1)
hold on, plot(x(iInflect),y(iInflect),'sk','MarkerSize',10,'LineWidth',2)
x(1:iInflect-1) = [];
y(1:iInflect-1) = [];
plot(x,y,'--r','LineWidth',2)
legend('Original Dataset','Second Inflection Point','New Dataset','Location','NorthWest')
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

