1
$\begingroup$

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.

tt [1]

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

$\endgroup$
  • $\begingroup$ 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}]. $\endgroup$ – Alx Aug 9 at 3:43
  • $\begingroup$ Doing like that, I get the same value of data[[Micro]] for each [Micro], i.e a horizontal line. $\endgroup$ – Lucas Lopes Aug 9 at 3:56
  • $\begingroup$ 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. $\endgroup$ – Alx Aug 9 at 4:03
  • $\begingroup$ 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}} $\endgroup$ – Lucas Lopes Aug 9 at 8:22
  • $\begingroup$ 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)]} ] $\endgroup$ – Lucas Lopes Aug 9 at 8:23
1
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Lucas Lopes Aug 9 at 6:32
  • 1
    $\begingroup$ @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] $\endgroup$ – Rom38 Aug 9 at 6:37
  • $\begingroup$ f[wr] is obtained from an iterative method. The code is: pastebin.com/7SwVvTen $\endgroup$ – Lucas Lopes Aug 9 at 6:46
  • $\begingroup$ @LucasLopes, It works well on my PC with your f[wr]. The result is: pastebin.com/mTQW0AhE $\endgroup$ – Rom38 Aug 9 at 7:06
  • $\begingroup$ Try now with the definition of f[wr] $\endgroup$ – Lucas Lopes Aug 9 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.