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

7 visualizaciones (últimos 30 días)
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 comentarios
dpb
dpb el 23 de Ag. de 2021
@Srh Fwl -- You've still not precisely answered the Q? of just what it is you're after here...is it the end of the "real" initial floor or an inflection point later that is the subject?
Adam Danz
Adam Danz el 23 de Ag. de 2021
@Srh Fwl, looks like Star Strider demonstrated that idea in a comment below the answer. Notice that the dip in the red curve is at the location I think you're refering to. If you need additional specific help, you may want to clarify the foggy areas pointed out by dpb.

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 22 de Ag. de 2021
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/718334/exampleData.txt')
T1 = 101×2 table
Var1 Var2 ____ ____ NaN 0 0.15 0 0.25 0 0.35 0 0.45 0 0.55 0 0.65 0 0.75 0 0.85 0 0.95 0 1.05 0 1.15 0 1.25 0 1.35 0 1.45 0 1.55 0
T1 = rmmissing(T1); % Remove 'NaN' Values
h = mean(diff(T1.Var1))
h = 0.1000
d2d1 = gradient(T1.Var2, h); % Numerical Derivative
flatidx = find(abs(d2d1)<1E-14); % Zero Slope (With Tolerance)
yflat = T1.Var2(flatidx)
yflat = 19×1
0 0 0 0 0 0 0 0 0 0
figure
plot(T1.Var1, T1.Var2)
hold on
plot(T1.Var1(flatidx), T1.Var2(flatidx), 'vr')
hold off
grid
legend('Data','Flat Section', 'Location','best')
This should also work with other data sets, although obviously I cannot test it with them.
.
  2 comentarios
Srh Fwl
Srh Fwl el 23 de Ag. de 2021
@Star Strider, thank you very much. I apologize because my question wasn't clear enough. I need the y value of the flattest area at y >0. The curves start off with y close to 0 and it's where they again flatten (in my example, at y between 3 and 5) that I need the y value. Apologies.
Star Strider
Star Strider el 23 de Ag. de 2021
Set the conditions in the find call to match what you want to define.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/718334/exampleData.txt')
T1 = 101×2 table
Var1 Var2 ____ ____ NaN 0 0.15 0 0.25 0 0.35 0 0.45 0 0.55 0 0.65 0 0.75 0 0.85 0 0.95 0 1.05 0 1.15 0 1.25 0 1.35 0 1.45 0 1.55 0
T1 = rmmissing(T1); % Remove 'NaN' Values
h = mean(diff(T1.Var1))
h = 0.1000
d2d1 = gradient(T1.Var2, h); % Numerical Derivative
flatidx = find((abs(d2d1)<1.0) & (T1.Var2 > 0)); % Define Slope Criteria (With Tolerance)
yflat = T1.Var2(flatidx)
yflat = 6×1
0.0000 0.0004 0.0030 0.0123 0.0395 0.0982
figure
plot(T1.Var1, T1.Var2)
hold on
plot(T1.Var1, d2d1)
plot(T1.Var1(flatidx), T1.Var2(flatidx), '.r')
hold off
grid
legend('Data', 'Numeircal Derivative', 'Flat Section', 'Location','best')
Make appropriate changes to get the result you want.
.

Iniciar sesión para comentar.

Más respuestas (1)

Turlough Hughes
Turlough Hughes el 23 de Ag. de 2021
Editada: Turlough Hughes el 23 de Ag. de 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')
  1 comentario
Srh Fwl
Srh Fwl el 24 de Ag. de 2021
Thank you very much, Turlough. Your solution does work but unfortunately I can only accept one answer and I put the first answerer through more inconvenience so chose that one. I appreciate your help.

Iniciar sesión para comentar.

Productos


Versión

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by