# Calculate local minima and maxima of a periodic function

**URL:** <https://root-forum.cern.ch/t/calculate-local-minima-and-maxima-of-a-periodic-function/40052>\
**Category:** Newbie\
**Created:** [June 23, 2020, 5:00pm UTC](https://root-forum.cern.ch/t/calculate-local-minima-and-maxima-of-a-periodic-function/40052 "2020-06-23T17:00:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nyx](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Nyx](https://root-forum.cern.ch/u/Nyx)\
**Post date:** [June 23, 2020, 5:00pm UTC](https://root-forum.cern.ch/t/calculate-local-minima-and-maxima-of-a-periodic-function/40052/1 "2020-06-23T17:00:33Z")

</div>

I have a periodic function for x(t) very similar to a cos function. I want to calculate the time at which local maxima and minima occur. I know how to do this with if loops checking each time that `x(t1)>x(t0)`, etc, but I wonder if there is a ROOT function to help me with this, or a better approach.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 24, 2020, 1:51pm UTC](https://root-forum.cern.ch/t/calculate-local-minima-and-maxima-of-a-periodic-function/40052/2 "2020-06-24T13:51:15Z")

</div>

In what kind of object is stored your function ? TGraph ? TF1 ?

---

<div class="post-metadata">

**Author:** ![Nyx](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Nyx](https://root-forum.cern.ch/u/Nyx)\
**Post date:** [July 8, 2020, 3:45pm UTC](https://root-forum.cern.ch/t/calculate-local-minima-and-maxima-of-a-periodic-function/40052/3 "2020-07-08T15:45:47Z")

</div>

Sorry for the delay in my response. The object is stored in a TTree, and I make a TGraph with the Draw option

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 8, 2020, 4:04pm UTC](https://root-forum.cern.ch/t/calculate-local-minima-and-maxima-of-a-periodic-function/40052/4 "2020-07-08T16:04:47Z")

</div>

So you have a TGragh. You can easily loop on the TGraph points and use [GetPoint](https://root.cern/doc/master/classTGraph.html#aef2de9b0f38f49a21b4a0d2b422a648b) to retrieve the X and Y values of each points

---

<div class="post-metadata">

**Author:** ![Nyx](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Nyx](https://root-forum.cern.ch/u/Nyx)\
**Post date:** [July 8, 2020, 4:20pm UTC](https://root-forum.cern.ch/t/calculate-local-minima-and-maxima-of-a-periodic-function/40052/5 "2020-07-08T16:20:51Z")

</div>

Right, I was just wondering if there was a function that could give me the maxima or minima in some defined range

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [July 9, 2020, 6:07am UTC](https://root-forum.cern.ch/t/calculate-local-minima-and-maxima-of-a-periodic-function/40052/6 "2020-07-09T06:07:57Z")

</div>

Not for TGraph. GetMaximum() and GetMinimum() work on the whole range. But the method is really easy to write. Something like:

```auto
double GetMaximumRange(TGraph *g, double x1, double x2) {
   double n = g->GetN();
   double max = 0;
   double x,y;
   for (int i=0; i<n; i++) {
      g->GetPoint(i,x,y);
      if (x>x1 && x <x2) {
         if (y>max) max=y
      }
   }
   return max;
}

```
