# How is the best way to integrate using loops

I'm using Mathematica, and I want to integrate a function f[wr] in wr using some method that works with a Table/Do/For in the variable wr.

I tried to use something like the Riemann's sum, to evaluate the function in some point wr_0 times the spacing (in this case 0.01). The problem is that wr depends on "[Micro]" and for each value of Micro, the integral in wr would change its limits (from [Micro] - 50*kt to [Micro] + 50*kt).

kb = 8.61*10^-5;
Te = 300;
kt = kb*Te;
sum = 0;
sum1 = 0;
data = Table[
{
Table[
{
sum = sum + (f[wr]*0.01);
sum1 = sum1 + (f[wr]*(wr - \[Micro])*0.01);

}
, {wr, \[Micro] - 50*kt, \[Micro] + 50*kt, 0.01}];

\[Micro], sum1/(Te*sum)
}

, {\[Micro], -0.4, 0.4, 0.1}];



I think this code, is not doing what I want. That is, for each value of Micro, calculate the integral (sum1 and sum) for a range of wr's and store, then for other value of Micro, do it all again, at the end i want the output of the quotient of these integrals. How could I implement that?

To illustrate the idea. For each [Micro] I have one integral to evaluate. At the last [Micro] we need to sum all the other.

If you are interested in who is f[wr]: https://pastebin.com/7SwVvTen

• You may define a helper function of \[Micro]: data[\[Micro]_]:=Module[{sum=0,sum1=0},Do[sum = sum + (f[wr]*0.01); sum1 = sum1 + (f[wr]*(wr - \[Micro])*0.01);, {wr, \[Micro] - 50*kt, \[Micro] + 50*kt, 0.01}]; sum1/(Te*sum)]. Then use it as a function itself data[0.4] or make a table Table[data[\[Micro]],{\[Micro],-0.4,0.4,0.1}].
– Alx
Aug 9, 2019 at 3:43
• Doing like that, I get the same value of data[[Micro]] for each [Micro], i.e a horizontal line. Aug 9, 2019 at 3:56
• Try setting for f some function, e.g. Sin and see this plot: ListLinePlot[Table[data[x] /. f -> Sin, {x, -0.4, 0.4, 0.1}],PlotRange -> All]. It is far from a horizontal line.
– Alx
Aug 9, 2019 at 4:03
• I dont understand why I get the same value in the quotient for different values of [Micro]. In[27]:= Table[data[[Micro]], {[Micro], -0.4, 0.4, 0.1}] Out[27]= {{-0.4, 0.0042950}, {-0.3, 0.0042950}, {-0.2, 0.0042950}, {-0.1, 0.0042950}, {0., 0.0042950}, {0.1, 0.0042950}, {0.2, 0.0042950}, {0.3, 0.0042950}, {0.4, 0.0042950}} Aug 9, 2019 at 8:22
• Where I used: data[[Micro]_] := Module[ {kb = 8.61*10^-5, Te = 300, soma = 0, soma1 = 0}, Do[ soma = soma + (E^((wr - [Micro])/kbTe)/kb Te*((E^((wr - [Micro])/kbTe) + 1.)^2)*cond[wr]*0.01); soma1 = soma1 + (E^((wr - [Micro])/kbTe)/kb* Te*((E^((wr - [Micro])/kbTe) + 1.)^2) cond[wr]*(wr - [Micro])*0.01); , {wr, [Micro] - 50*kbTe, [Micro] + 50*kbTe, 0.01}]; {[Micro], N[soma1/(Te*soma)]} ] Aug 9, 2019 at 8:23

As I understood, you need something like this:

data = Table[
sum = sum + (f[wr]*0.01);
sum1 = sum1 + (f[wr]*(wr - \[Micro])*0.01);
{\[Micro], sum1/(Te*sum)},

{\[Micro], -0.4, 0.4, 0.1}, {wr, \[Micro] - 50*kt, \[Micro] + 50*kt, 0.01}]


It calculates sum, sum1 and adds the {\[Micro], sum1/(Te*sum)} into the resulting list at the each step in \[Micro].

• I got: $RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of 2.97942*10^-22+sum.$RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of -3.84793*10^-22+sum1. $RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of -3.84793*10^-22+sum1. General::stop: Further output of$RecursionLimit::reclim2 will be suppressed during this calculation. Aug 9, 2019 at 6:32
• @LucasLopes, I don't have your f[wr] and due to this I tested this solution on the simple linear f[wr_]:=2 wr. It was working without any recursive loops. Look at my code, do you see there any reasons for looping? It is trivial. So, problem is in f[wr] Aug 9, 2019 at 6:37
• f[wr] is obtained from an iterative method. The code is: pastebin.com/7SwVvTen Aug 9, 2019 at 6:46
• @LucasLopes, It works well on my PC with your f[wr]. The result is: pastebin.com/mTQW0AhE Aug 9, 2019 at 7:06
• Try now with the definition of f[wr] Aug 9, 2019 at 21:04