0
$\begingroup$

Below is a sample program that shows that Integrate is giving different results depending on whether I name the variable duration1 or duration[1]. Anyone know why? Am I better off not using subscripts?

Remove["Global`*"]
$Assumptions = 
 duration[1] > 0 && duration1 > 0 && duration2 > 0 && t > 0 && 
  t < duration[1] + duration2 && t < duration1 + duration2
jer1a = Sin[(Pi*t)/duration[1]] ;
jer1b = Sin[(Pi*t)/duration1] ;

jer2 = 1;
constants = {duration[1] -> 6, duration1 -> 6, duration2 -> 5};

jerks1a = {jer1a, jer2};
jerks1b = {jer1b, jer2};
boundaries1a = {t <= duration[1], True};
boundaries1b = {t <= duration1, True};

jer1a = Piecewise[Transpose[{jerks1a, boundaries1a}]]
jer1b = Piecewise[Transpose[{jerks1b, boundaries1b}]]
acc1a = Integrate[jer1a /. t -> $t, {$t, 0, t}, 
    Assumptions -> {t \[Element] Reals}] // Simplify;
acc1b = Integrate[jer1b /. t -> $t, {$t, 0, t}, 
    Assumptions -> {t \[Element] Reals}] // Simplify;
Print["Original: ", jer];
Print["Integrate with duration[1]: ", acc1a];
Print["Integrate with duration1: ", acc1b];

Here's the output:

enter image description here

I know the results are equivalent mathematically. But why would the use of indexing cause one result to specially handle the case of t == duration[1] while the other does not?

$\endgroup$
2
$\begingroup$

that Integrate is giving different results

It is the same result. Just written different. I suppose due to lexical ordering difference between a[1] and a? and may be some different internal code path was used when variable was indexed vs. not? But both final results are equivalent.

Plot[acc1a /. duration[1] -> 2, {t, -3, 3}]

Mathematica graphics

Plot[acc1b /. duration1 -> 2, {t, -3, 3}]

Mathematica graphics

Table[(acc1a - acc1b) /. {duration[1] -> n, duration1 -> n, 
    t -> 1}, {n, -3, 3}] // N

Mathematica graphics

Update

To answer comment

it is surprising and counter-intuitive (to me at least) that one piecewise function explicitly handles the case of t== duration[1] while the other doesn't.

It is the same. In the case of duration1 it says

 (duration1 - duration1*Cos[(Pi*t)/duration1])/Pi, duration1 >= t   (*1*)

It combined the = with the > in one statement. While in the case of duration[1] it handled the = on its own, and > on its own.

For = it said

     (2*duration[1])/Pi, t == duration[1]    (*2*)

And if we plug in duration1=t in (1) it gives

 (duration1 - duration1*Cos[(Pi*t)/t])/Pi

Which simplifies to

 (duration1 - duration1*Cos[Pi])/Pi

Which simplifies to

 (duration1 + duration1)/Pi

Which simplifies to

 2 * duration1 /Pi   

Which is the same as (2) for duration1=t

So they are the same Piecewise in both cases.

Why are they written differently? Who knows. We do not have the source code of Mathematica internals to look and find out. It is clearly related to using index variable, which made it go to different code path.

But as long as the result is the same Mathematically, why is it so important they are written differently?

| improve this answer | |
$\endgroup$
  • $\begingroup$ Yeah, I didn't mean that the results were different mathematically. I should have been more specific -- it is surprising and counter-intuitive (to me at least) that one piecewise function explicitly handles the case of t== duration[1] while the other doesn't. I'll edit the question to be more specific. $\endgroup$ – Dan Sandberg Apr 21 at 22:28
  • $\begingroup$ I'm doing a complex integration of integration of integration, and so keeping the results as simple as possible is important to keep things performant. My understanding was that symbols were treated the same as an indexed symbol, and the fact that this isn't true is surprising and good to know. Ideally there'd be some way to get the results both ways, so that I could keep using symbols since they make the code a little cleaner. $\endgroup$ – Dan Sandberg Apr 22 at 8:45

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.