# Best way to integrate a function

**URL:** <https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638>\
**Category:** ROOT\
**Created:** [April 11, 2018, 9:42am UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638 "2018-04-11T09:42:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![amytee](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c6cbf5/32.png) [@amytee](https://root-forum.cern.ch/u/amytee)\
**Post date:** [April 11, 2018, 9:42am UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/1 "2018-04-11T09:42:13Z")

</div>

Hey,

I want to integrate the function below:

```
  (4*lambda*(1-z*z)) /( ((lambda+1)*(lambda+1) - (lambda-1)*(lambda-1)*z*z)*((lambda+1)*(lambda+1) - (lambda-1)*(lambda-1)*z*z));     

```

I want to integrate over various z values, but plot as a function of lambda. I’m not sure how best to go about this. I have a range of z values i.e 0.2, 0.4, 0.6, 0.8 and 1.0. and Lambda is between 1.0001 and 1000.1. I’m not sure how best to approach this. Any advice would be appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![maxisacson](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxisacson/32/13173_2.png) [@maxisacson](https://root-forum.cern.ch/u/maxisacson)\
**Post date:** [April 11, 2018, 10:11am UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/2 "2018-04-11T10:11:01Z")

</div>

Hi,

you can create a TF1 and then use the TF1::Integrate method. An example integrating f = a + b\*x for different values of a and b:

```auto
double func(double* x, double* p) {
    double xx = x[0];
    double a = p[0];
    double b = p[1];

    return a + b * xx;
}

void root_int() {
    TF1 f("func", func, 0, 1, 2);
    f.SetParameters(0, 1); // Set a = 0, b = 1
    auto i = f.Integral(0, 1); // integrate between 0 and 1
    std::cout << i << std::endl; // i = 0.5

    f.SetParameters(0, 2); // Set a = 0, b = 2
    i = f.Integral(0, 1); // integrate between 0 and 1
    std::cout << i << std::endl; // i = 1

    // etc...
}

```

---

<div class="post-metadata">

**Author:** ![amytee](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c6cbf5/32.png) [@amytee](https://root-forum.cern.ch/u/amytee)\
**Post date:** [April 11, 2018, 10:51am UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/3 "2018-04-11T10:51:07Z")

</div>

Hey,

I don’t quite follow this. I had initially thought of using a TF1 and figured integrating over z would be easy, but I wasn’t sure then if it was possible to plot as a function of lambda. I don’t see how this is achieved via your example? Unless I’m missing something…

Edit: I realised I wasn’t that clear. I want to take my function, and integrate over a particular z value and then plot that integrated function over the whole range of lambda (between 1.001 and 1000.1)

---

<div class="post-metadata">

**Author:** ![maxisacson](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxisacson/32/13173_2.png) [@maxisacson](https://root-forum.cern.ch/u/maxisacson)\
**Post date:** [April 11, 2018, 11:20am UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/4 "2018-04-11T11:20:28Z")

</div>

The easiest would probably be to save the values of lambda and the corresponding integral value in two vectors, and then make a plot using TGraph. I suppose you can also create a second function that computes the value of the integral, and plot that directly, e.g.:

```auto
double func(double* x, double* p) {
    double xx = x[0];
    double a = p[0];
    double b = p[1];

    return a + b * xx;
}

double integral(double* x, double* p) {
    double b = x[0];
    TF1 f("func", func, 0, 1, 2);
    f.SetParameters(0, b); // Set a = 0, b = b
    return f.Integral(0, 1);
}

void root_int() {

    TF1* g = new TF1("integral", integral, 0, 5, 0);
    g->Draw();
}

```

---

<div class="post-metadata">

**Author:** ![amytee](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c6cbf5/32.png) [@amytee](https://root-forum.cern.ch/u/amytee)\
**Post date:** [April 11, 2018, 12:15pm UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/5 "2018-04-11T12:15:41Z")

</div>

Hey,

Thanks, I will try and adapt this to my needs as I hope to plot all the integrated z values as a function of lambda on the same plot.

---

<div class="post-metadata">

**Author:** ![amytee](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c6cbf5/32.png) [@amytee](https://root-forum.cern.ch/u/amytee)\
**Post date:** [April 11, 2018, 1:56pm UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/6 "2018-04-11T13:56:31Z")

</div>

Hmmm, I don’t think it’s doing the integration right 😕

---

<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:** [April 11, 2018, 2:00pm UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/7 "2018-04-11T14:00:39Z")

</div>

What concerns “intergration”, see for example:

> [@IntegratorMultiDim accuracy](https://root-forum.cern.ch/t/integratormultidim-accuracy/27827):
>
> Hello, I was trying to learn how to use IntegratorMultiDim by calculating a volume of ball with a following macro: [testIntegral.C](https://root-forum.cern.ch/uploads/default/original/2X/0/059bc9c0b5f489a997adc72e6e8fb73d7188a59f.C) (727 Bytes) On ROOT v6-12-04 it gives the following result: exact : 4.18879 kDEFAULT : 4.17793 , error: 0 kADAPTIVE : 4.17793 , error: 0 kVEGAS : 4.18659 , error: 0 kMISER : 4.20576 , error: 0 kPLAIN : 4.20984 , error: 0 Why with tolerance settings at 1e-9 I have accuracy worse than 1e-3? Best regards, Antoni

---

<div class="post-metadata">

**Author:** ![amytee](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c6cbf5/32.png) [@amytee](https://root-forum.cern.ch/u/amytee)\
**Post date:** [April 11, 2018, 2:04pm UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/8 "2018-04-11T14:04:05Z")

</div>

I changed the example code, to something of my own to understand exactly what is going on:

```
 double fsin(double *x, double *par){
 double y = x[0]; 
 double a = par[0];
 return a*sin(y);
}

double Integral(double *x, double *par){
 double a = x[0];
 TF1 f("fsin", fsin, 0, 10, 1); //min, max, no.parameters
  f.SetParameter(0,a); //setting the variable to be integrated
  return f.Integral(0,5); //Integrate 
  }

void Integrations2(){
 
  TF1* g = new TF1("Integral", Integral, 0, 10, 0);
  g->Draw();

   }

```

The graph that I got for the integration doesn’t seem right. It shouldn’t be a straight line but a curve. So this is why I don’t think it’s working.

---

<div class="post-metadata">

**Author:** ![maxisacson](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/maxisacson/32/13173_2.png) [@maxisacson](https://root-forum.cern.ch/u/maxisacson)\
**Post date:** [April 11, 2018, 2:10pm UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/9 "2018-04-11T14:10:53Z")

</div>

Isn’t a straight line correct in this case? If you integrate f = a_sin(x) you get F = -a_cos(x), so if you integrate between x1 and x2 you will get I = -a\*(cos(x2) - cos(x1)). So if x1 and x2 are the same in all cases you will get a line with slope cos(x1) - cos(x2) as a function of a.

---

<div class="post-metadata">

**Author:** ![amytee](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/c6cbf5/32.png) [@amytee](https://root-forum.cern.ch/u/amytee)\
**Post date:** [April 11, 2018, 2:13pm UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/10 "2018-04-11T14:13:45Z")

</div>

Ahhh, massive maths fail on my part! Yes, you are correct. Thank you for this!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [April 25, 2018, 2:13pm UTC](https://root-forum.cern.ch/t/best-way-to-integrate-a-function/28638/11 "2018-04-25T14:13:46Z")

</div>

Closed on April 25, 2018, 2:13pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
