I am attempting to evaluate this double integral:

        Exp[-R (t1 - t2)] s[t1] s[t2], {t2, 0, t1}
    ], {t1, 0, t}

where s(t) is a piecewise-constant function. The final t value will be within a portion of the domain of s(t) is constant. The s(t) function is determined first by generating an array, lArray, that contains the domain for the piecewise-function, and ModArray, that contains the function values for those domains. I then define the following identifying function that returns the value of ModArray when fed an arbitrary l value:

IdenS[lArray_, ModArray_, Nlen_, l_] :=
 For[i = 1, i < Nlen + 1, i++,
  TempSum = Sum[lArray[[1, j]], {j, 1, i}];
  TotalSum = Sum[lArray[[1, j]], {j, 1, Nlen}];
   ((TempSum > l) && (l < TotalSum) && (l > 0)),
   (Return[ModArray[[1, i]]];
   l < 0,
   l > TotalSum,

When trying to evaluate the integral using the IdenS function defined above, the integral returns something in terms of Null values. The IdenS function is having a problem with accepting the t1 and t2 values before evaluation. Is there some workaround that will result in an analytic result in terms of t and R for t values greater than the sum of all the t values in lArray? Perhaps some usage of Hold, Evaluate, and Assumptions?

Example result:

Nlen = 5;
lArray = ConstantArray[1, {1, Nlen}];
ModArray = ConstantArray[0, {1, Nlen}];
For[j = 1, j < Nlen + 1, j++,
 ModArray[[1, j]] = (-1)^(j + 1);

  Exp[-R (t1 - t2)] IdenS[lArray, ModArray, Nlen, t1] IdenS[lArray, 
    ModArray, Nlen, t2], {t2, 0, t1}
  ], {t1, 0, 1}

(* Result: *)
(E^-R Null^2 (1 + E^R (-1 + R)))/R^2
  • $\begingroup$ Shouldn't s(t1) and s(t2) actually be s[t1] and s[t2] instead? Also, you have shown how you are trying to implement your solution. But could you comment on what exactly you are trying to accomplish? It is not that clear to me what the underlying problem is. $\endgroup$ – MarcoB Jan 30 '17 at 16:43
  • $\begingroup$ Sorry, it should be s[t1] and s[t2], but I never actually execute that code, so that shouldn't be a problem. Thanks for pointing that out. My goal is generally to calculate something that looks like an autocorrelation of the s(t) function convolved with an exponential decay. The general problem is that the IdenS function is having a difficult time dealing with the symbolic t1 and t2 values. $\endgroup$ – Aaron Ross Jan 30 '17 at 16:45
  • $\begingroup$ Aaron, I wonder if IdenS is correct. That function returns 1 throughout the $[0,1[$ domain, except at 1, where its value is -1. Is that correct? See Plot[IdenS[lArray, ModArray, Nlen, t], {t, 0, 1}]. $\endgroup$ – MarcoB Jan 30 '17 at 16:55
  • $\begingroup$ Hi MarcoB: this is correct! The domain of the s(t) function here is intended to be [0,5], oscillating back and forth between 1 and -1, and 0 outside of those bounds. $\endgroup$ – Aaron Ross Jan 30 '17 at 17:02
  • $\begingroup$ In your example, the IdenS function generates a square wave. Is that always the case, or is it peculiar to the example? If it is always the case, then you can replace it with the built-in function SquareWave. $\endgroup$ – m_goldberg Jan 31 '17 at 8:13

I believe your IdenS can be replaced with a function defined by much simpler code.

f[lArray_, ModArray_, l_] :=
    l <= 0, ModArray[[1]],
    l >= Total[lArray], ModArray[[-1]],
      Module[{val = Pick[ModArray, l <= # & /@ Accumulate[lArray]][[1]]},
        If[NumericQ[val], val, 1, 1]]]

Using the example parameters

n = 5;
lArray = ConstantArray[1, n];
modArray = Table[(-1)^(j + 1), {j, n}];

f generates a square wave.

Plot[f[lArray, modArray, t] , {t, 0, 5}]


Evaluating your double integral with f replacing IdesS

  Exp[-R (t1 - t2)] f[lArray, modArray, t1]  f[lArray, modArray, t2],
  {t1, 0, 1}, {t2, 0, t1}]



The above integral is equivalent to

  Exp[-R (t1 - t2)] SquareWave[t1/2]  SquareWave[t2/2], {t1, 0, 1}, {t2, 0, t1}]
| improve this answer | |
  • $\begingroup$ this is perfect! Thanks for working through this. I am not entirely sure that I understand the syntax in the Module portion of the function though. I need to dig into the # % /@ usage a bit. The f function is necessary for me to be able to input more complicated modulation functions other than the square wave. $\endgroup$ – Aaron Ross Jan 31 '17 at 15:36

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