# Best way to integrate a function

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

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:

``````double func(double* x, double* p) {
double xx = x;
double a = p;
double b = p;

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...
}

``````

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)

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.:

``````double func(double* x, double* p) {
double xx = x;
double a = p;
double b = p;

return a + b * xx;
}

double integral(double* x, double* p) {
double b = x;
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();
}

``````

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.

Hmmm, I don’t think it’s doing the integration right What concerns “intergration”, see for example:

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;
double a = par;
return a*sin(y);
}

double Integral(double *x, double *par){
double a = x;
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.

Isn’t a straight line correct in this case? If you integrate f = asin(x) you get F = -acos(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.

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

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