4
$\begingroup$

Consider these two functions which are almost the same:

 f[x_] := N@
  Evaluate[Integrate[Sin[z]*Cos[x - z], {z, 0, x}]]

g[x_] := 
Evaluate[Integrate[Sin[z]*Cos[x - z], {z, 0, x}]]

The first function has this extra command: N@

f[{1, 2}] returns:

  {{{0.420735, 1.22404}, {0.100612, 0.909297}}}

N@g[{1, 2}] returns:

{{0.420735, 0.909297}}

and,

N@Integrate[Sin[z]*Cos[1 - z], {z, 0, 1}] = 0.420735
N@Integrate[Sin[z]*Cos[1 - z], {z, 0, 2}] = 1.22404
N@Integrate[Sin[z]*Cos[2 - z], {z, 0, 1}] = 0.100612
N@Integrate[Sin[z]*Cos[2 - z], {z, 0, 2}] = 0.909297

it seems f uses a combination of 1 and 2 and put each in the x in Cos and the upper limit of integral independently. Does anybody know why it happens?

$\endgroup$
8
  • $\begingroup$ Maybe removing Table gives a simpler example without eliminating the issue you raise? $\endgroup$
    – kglr
    Nov 2, 2014 at 1:32
  • $\begingroup$ ... or more simply f[x_] := N@Evaluate[Integrate[x - z, {z, 0, x}]]; g[x_] := Evaluate[Integrate[x - z, {z, 0, x}]]. $\endgroup$
    – kglr
    Nov 2, 2014 at 1:52
  • $\begingroup$ @kguler. You are right here the Table does not an effect. I will correct that later. $\endgroup$
    – MOON
    Nov 2, 2014 at 2:02
  • 3
    $\begingroup$ Interesting ... Don't think I have seen this before: Integrate[z + {a, b}, {z, {r, s}, {t, u}}] threads over {{a,b},{r,s},{t,u}}. More generally, it seems to do so if the the integrand is Listable: i.e., if foo is Listable, then Integrate[foo[z, {a, b}], {z, {r, s}, {t, u}}] threads, not just over {a,b}, but over {{a,b},{r,s},{t,u}} $\endgroup$
    – kglr
    Nov 2, 2014 at 2:49
  • 2
    $\begingroup$ By the way, it's not the fault of N except insofar as it prevents Evaluate from doing anything (because Evaluate is no longer immediately under the :=). The real reason for the difference is that in g the integral is pre-evaluated while in f it is not. $\endgroup$
    – user484
    Nov 2, 2014 at 12:10

1 Answer 1

1
$\begingroup$

From the "Details" section of the documentation for Evaluate: "Evaluate only overrides HoldFirst, etc. attributes when it appears directly as the head of the function argument that would otherwise be held." and from the "Possible Issues" section: "Evaluate works only on the first level, directly inside a held function."

f[x_] := N@Evaluate[Integrate[Sin[z]*Cos[x - z], {z, 0, x}]]

?f

Global`f

f[x_] := N[Evaluate[Integrate[Sin[z]*Cos[x - z], {z, 0, x}]]]

f[{x1, x2}]

{{0.5 x1 Sin[x1], 0.25 (Cos[x1] - 1. Cos[x1 - 2. x2] + 2. x2 Sin[x1])}, {0.25 (-1. Cos[2. x1 - 1. x2] + Cos[x2] + 2. x1 Sin[x2]), 0.5 x2 Sin[x2]}}

 g[x_] := Evaluate[Integrate[Sin[z]*Cos[x - z], {z, 0, x}]]

?g

Global`g

g[x_] := (x*Sin[x])/2

g[{x1, x2}] == f /@ {x1, x2}

True

N@g[{x1, x2}] === f /@ {x1, x2}

True

$\endgroup$

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.