# Using N gives strange result

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?

• Maybe removing Table gives a simpler example without eliminating the issue you raise?
– kglr
Nov 2, 2014 at 1:32
• ... or more simply f[x_] := N@Evaluate[Integrate[x - z, {z, 0, x}]]; g[x_] := Evaluate[Integrate[x - z, {z, 0, x}]].
– kglr
Nov 2, 2014 at 1:52
• @kguler. You are right here the Table does not an effect. I will correct that later.
– MOON
Nov 2, 2014 at 2:02
• 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}}
– kglr
Nov 2, 2014 at 2:49
• 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.
– user484
Nov 2, 2014 at 12:10

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


Globalf

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

Globalg

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

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


True

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


True