# C++ doubts in global variables and numerical integration

**URL:** <https://root-forum.cern.ch/t/c-doubts-in-global-variables-and-numerical-integration/25124>\
**Category:** ROOT\
**Created:** [June 14, 2017, 4:58am UTC](https://root-forum.cern.ch/t/c-doubts-in-global-variables-and-numerical-integration/25124 "2017-06-14T04:58:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabiokopp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/a587f6/32.png) [@fabiokopp](https://root-forum.cern.ch/u/fabiokopp)\
**Post date:** [June 14, 2017, 4:58am UTC](https://root-forum.cern.ch/t/c-doubts-in-global-variables-and-numerical-integration/25124/1 "2017-06-14T04:58:08Z")

</div>

I’m trying to integrate a simple function (x\*y) using the Romberg method. But I’m having 2 problems.  
Problem 1:  
I want to integrate only x and maintain the argument y present in the rest of calculation, like a global variable. In fortran 77 I would use common.  
Question 2:  
How to integrate using arguments in the limits. For example:  
f(x,y)=x\*y  
intervals-y → [x,1]  
intervals-x → [0,1]

PS: Integration using values in the limits are okay.

Routine obtained from [nintlib](https://people.sc.fsu.edu/~jburkardt/cpp_src/nintlib/nintlib.html)

```cpp
// Just a part of the code
double f1(int dim_num,double x[]){
double *a;
double *b;
int dim;
int eval_num;
int ind;
int it_max = 3;
double result;
int *sub_num;
double tol;
//
// Set the integration limits.
//

a = new double[dim_num];
b = new double[dim_num];
sub_num = new int[dim_num];

a[0]=0.0; <- x[0] or x[1]?
b[0]=1.0;
tol = 0.0001;
for ( dim = 0; dim < dim_num; dim++ )
{
sub_num[dim] = 20;
}
result = romberg_nd ( f0, a, b, dim_num, sub_num, it_max, tol,
&ind, &eval_num );

delete [] a;
delete [] b;
delete [] sub_num;
// cout << result << endl;
return result;
}

double f0(int dim_num,double x[]){
double value; 
value=x[0]*x[1];
return value;
}

```

---

<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:** [June 14, 2017, 6:17am UTC](https://root-forum.cern.ch/t/c-doubts-in-global-variables-and-numerical-integration/25124/2 "2017-06-14T06:17:55Z")

</div>

See the old “[Boundary integral](https://root-forum.cern.ch/t/boundary-integral/17872/6)” and "[Non-rectangular region integration](https://root-forum.cern.ch/t/non-rectangular-region-integration/23765)" threads.

---

<div class="post-metadata">

**Author:** ![fabiokopp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/a587f6/32.png) [@fabiokopp](https://root-forum.cern.ch/u/fabiokopp)\
**Post date:** [June 14, 2017, 1:13pm UTC](https://root-forum.cern.ch/t/c-doubts-in-global-variables-and-numerical-integration/25124/3 "2017-06-14T13:13:46Z")

</div>

Thanks again, Wile!🙂

---

<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:** [June 28, 2017, 1:13pm UTC](https://root-forum.cern.ch/t/c-doubts-in-global-variables-and-numerical-integration/25124/4 "2017-06-28T13:13:50Z")

</div>

Closed on June 28, 2017, 1:13pm UTC

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