If I have the following data:


and the following code:

peakpositions1 = RankedMax[FindPeaks[data[[All, 2]]][[All, 2]], 
   3];(*Finds peak position of first peak in y*)
xval1 = Select[data, #[[2]] == peakpositions1 &][[All, 1]][[
   1]];(*Finds peak position of second peak in x*)
peakpositions2 = 
  Max[FindPeaks[data[[All, 2]]][[All, 
    2]]];(*Finds peak position of second peak in y*)
xval2 = Select[data, #[[2]] == peakpositions2 &][[All, 1]][[
   1]];(*Finds peak position of second peak in x*)
finalpeak1 = {xval1, peakpositions1};
finalpeak2 = {xval2, peakpositions2};

start = 40;
end = 95;
region = Select[data, start <= #[[1]] <= end &];
peak = Interpolation[region];
f = peak;

aspect = 2/3;
l = 50;
a = 3;
b = 70;
Manipulate[m := l/Sqrt[a^2 + aspect^2 b^2 f'[p]^2]; 
 Show[Plot[f'[p] (x - p) + f[p], {x, p - m, p + m}, 
   PlotStyle -> {Thick, Orange}, PlotRange -> {{60, 95}, {-0.5, All}},
    AspectRatio -> aspect], ListPlot[{finalpeak1}, PlotStyle -> Red], 
  ListPlot[{finalpeak2}, PlotStyle -> Red], 
  Plot[f[x], {x, 65, 95}, PlotRange -> {{60, 95}, {-0.5, All}}, 
   PlotStyle -> {Blue}], 
  Epilog -> {Point[{p, f[p]}], 
    Text["Slope: " <> ToString[f'[p]], Scaled[{0.05, 0.95}], {-1, 1}],
     Text["Intecept: " <> ToString[-(f[p] - p*f'[p])/f'[p]], 
     Scaled[{0.05, 0.85}], {-1, 1}]}], {p, 65, 95, 0.05}]

which gives a manipulable sliding tangent around my data that shows the following:

1)the slope of the tangent 2)the x-intercept 3) the peak position of the two peaks of interest (in red)

such as in the picture below:


My question is:

1) How can I automatically get the intercept at x when the tangent intercepts the peak of both curves (red dots in the figure) and at the same time the slope is maximum?. I only need this for when the tangent is located from the left side of the curve or peak. For example visually it seems that the maximum slope that intercepts the peak is 0.267636 and the x-intercept is 71.3824 (as in the picture 1) for the first peak. For the second peak the maximum slope that intercepts the peak seems to be 5.03089 and the x-intercept 88.2961 such as the picture below:

enter image description here

This does not have to be with Manipulate. The only reason I used Manipulate is so that the question is more clear.

BONUS: This question is an attempt to solve a question I posted here: Find onset and peak temperatures , which has a bounty of 50 points. If you post your answer here and also in the link I will give you also the bounty of the link.

I appreciate in advanced your help.

  • $\begingroup$ From the look of the code, it seems that the problem is to find a point on the curve, such that a tangent line at it also intersects another given point. This requirement alone uniquely determines the solution, and the additional requirement of maximizing the derivative makes the system overdetermined. In general, it's logically impossible to satisfy both. Alternatively, you can draw a line through two chosen points, inflection and peak, but it won't be tangent at any of them. $\endgroup$
    – aooiiii
    Commented Jun 14, 2020 at 7:25

1 Answer 1


Naively I think you could do something like this:

t = 0.01; (* threshold *)
s = 0.01; (* step size *)

{x, y} = finalpeak1;

Table[{p, y - (f'[p] (x - p) + f[p])}, {p, 65, x, s}];

FirstCase[%, {p_, v_ /; v <= t} :> p]
oldx = x;
{x, y} = finalpeak2;

Table[{p, y - (f'[p] (x - p) + f[p])}, {p, oldx, 94, s}];

FirstCase[%, {p_, v_ /; v < t} :> p]
  • $\begingroup$ Hi Mr. Wizard! Thank you very much. I am analyzing your code. I have one question: It seems that the answer of 72.27 and 88.477 are the "p values" rather than the x-intercept with the tangent line. How could I also obtain the intercept of the x-axis with the tangent line? $\endgroup$
    – John
    Commented Jun 14, 2020 at 0:38
  • $\begingroup$ @John Yes, this is a search for p values. I don't understand your question; don't you calculate everything else from p in your own Manipulate code? $\endgroup$
    – Mr.Wizard
    Commented Jun 14, 2020 at 1:05
  • $\begingroup$ Mr.Wizard, yes you are absolutely right!. I was placing your code in the wrong position of my own code. Thank you so much! If you copy and paste everything together in the other link I can give you the bounty if you want. $\endgroup$
    – John
    Commented Jun 14, 2020 at 1:32
  • $\begingroup$ @John I'm glad I could help and that this appears useful. It's hardly enough to deserve a Bounty however. If I were you I would wait to see if you get any other answers to the original question. If the time ends and you don't, MarcoB put a lot more effort into this than I did. $\endgroup$
    – Mr.Wizard
    Commented Jun 14, 2020 at 4:19
  • $\begingroup$ Mr. Wizard sounds good! Thanks again for your help ! $\endgroup$
    – John
    Commented Jun 14, 2020 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.