# Improve fit by trigonometric function

**URL:** <https://root-forum.cern.ch/t/improve-fit-by-trigonometric-function/32604>\
**Category:** Newbie\
**Created:** [February 5, 2019, 5:43pm UTC](https://root-forum.cern.ch/t/improve-fit-by-trigonometric-function/32604 "2019-02-05T17:43:10Z")\
**Posts on this page:** 5\
**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:** [February 5, 2019, 5:43pm UTC](https://root-forum.cern.ch/t/improve-fit-by-trigonometric-function/32604/1 "2019-02-05T17:43:12Z")

</div>

I need to fit around 10^5 TGraph that are basically trigonometric functions with different amplitude and phase. I am specifically interested in the frequency. For that, I define:

`TF1 *Cosine = new TF1("Cosine", "[0]*cos([1]*x*1.e6 + [2])", 0., tmax)`

and then before the fit

```
Cosine->SetParameters(1., omega_0*1.e-6, 0.); 
Cosine->SetParLimits(0, 0., 1.);
Cosine->SetParLimits(2, -M_PI/2, M_PI/2);

```

I fit with the option `M`:

> gr\_Projection-\>Fit(“Cosine”, “M”);

omega\_0 is around 1.5, and most particles give a reasonable fit, but some of them give something completely wrong, like this one:

> Minimizer is Minuit / MigradImproved  
> Chi2 = 4678.99  
> NDf = 9997  
> Edm = 1.61618e-08  
> NCalls = 129  
> p0 = 0.216015 +/- 0.00963817 (limited)  
> p1 = 2.3494 +/- 0.00935233  
> p2 = -0.0734677 +/- 0.0731648 (limited)

If I look at the plot of this particle that I am trying to fit, I can see easily that the amplitude is very close to 1, and the frequency is similar to omega\_0, but for some reason, the result of the fit is not good (Chi2 is very large also).

Is there a way to improve this result, maybe forcing the minimizer to do more iterations?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 5, 2019, 6:10pm UTC](https://root-forum.cern.ch/t/improve-fit-by-trigonometric-function/32604/2 "2019-02-05T18:10:43Z")

</div>

Try with “WM” fit options.

BTW. I assume, before every fit, you always “SetParameters” (so that every fit “starts” from well defined reasonable initial values).

---

<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:** [February 5, 2019, 7:41pm UTC](https://root-forum.cern.ch/t/improve-fit-by-trigonometric-function/32604/3 "2019-02-05T19:41:42Z")

</div>

I tried the “WM” option, but I keep getting similar results.

And yes, I always set the parameters before each fit.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 5, 2019, 8:26pm UTC](https://root-forum.cern.ch/t/improve-fit-by-trigonometric-function/32604/4 "2019-02-05T20:26:36Z")

</div>

I guess you would need to attach one or two graphs which fail so that one can try to fit them.

---

<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:** [February 12, 2019, 9:07pm UTC](https://root-forum.cern.ch/t/improve-fit-by-trigonometric-function/32604/5 "2019-02-12T21:07:22Z")

</div>

Sorry, I think It would be very complicated to reproduce the graphs that I am trying to fit. I tried the verbose option, because I realized that some fits gave me the error " Invalid FitResult (status = 4 )".

The error messages are now too long, and I don’t think I need to copy the entire thing here. However, it is interesting to notice the lines:

MIGRAD FAILS TO FIND IMPROVEMENT  
MIGRAD TERMINATED WITHOUT CONVERGENCE.  
FCN=1503.8 FROM MIGRAD STATUS=FAILED 151 CALLS 152 TOTAL

As I suspected, for some particles, the Minimizer does not converge. I believe that my initial guess for the parameters, as well as the limits, is accurate (most of the particles have a nice fit).

How can I force TMinuit to converge, even if it needs more computing time? I have tried with

TVirtualFitter::SetMaxIterations( 100000 );  
TVirtualFitter::SetPrecision(1e-4);

as suggested in other forum questions, but it doesn’t seem to change significantly. Please let me know if you need more information from the verbose output.
