I want to perform the following series summation: $$\sum_{j=1}^5 \exp\left[{-\beta \, \left(\text{En} + \frac{\Delta \, X}{Num} \right)}\right] \cos{\left(\text{En} + \frac{\Delta \, X \, T}{Num} \right)},$$

where $X$ is a random real number, which takes a different value for each of the 5 terms in the series, in the interval $[0,1]$. I am trying to implement the same using the following code:

Sum[ E^{-\[Beta] {En + \[CapitalDelta]/ Num RandomReal[]} } {Cos[{En + \[CapitalDelta]/ Num RandomReal[]} t]}, {J, 1, 5}]

Here the problem I am encountering is that the random number takes different values for the argument of the exponential and the argument of the cosine, while my case involves the same random number for both. How do I enforce that the argument X for a term in the series is the same, and not two different random reals within the same term in the series?

  • 3
    $\begingroup$ Use a With and set the RandomReal[] in there Sum[With[{X = RandomReal[]},E^{-\[Beta] {En + \[CapitalDelta]/ Num X}} {Cos[{En + \[CapitalDelta]/Num X} t]}], {J, 1, 5}] $\endgroup$
    – flinty
    Aug 10, 2020 at 14:20
  • $\begingroup$ @flinty Thanks, that works. $\endgroup$
    – Learner
    Aug 10, 2020 at 14:24

1 Answer 1

f[beta_, en_, x_, delta_, num_, t_] :=
 Exp[-beta (en + x*delta)]*Cos[en + delta*x*t/num]
Total[f[β, En, X, Δ, #, t] & /@ RandomReal[{0, 1}, 5]]
  • 1
    $\begingroup$ Yes. Use the procedural Total rather than the analytic Sum. Sometimes Sum figures out that you are using it procedurally, but it can get lost attempting symbolic analysis. $\endgroup$
    – John Doty
    Aug 10, 2020 at 16:29
  • 1
    $\begingroup$ @John, indeed Total[] is appropriate here (especially if you use Method -> "CompensatedSummation"), but Sum[] can also be coerced to do no symbolic analysis at all via Method -> "Procedural". $\endgroup$ Aug 10, 2020 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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