# Why different output is obtained for the same code (Mathematica 10.1)?

I wrote some code in Mathematica:

ClearAll["Global*"]
w = 1/H[x] D[q[x], x] (\[Eta][x] - z) +
q[x]/H[x] D[H[x], x] (1 - (\[Eta][x] - z)/(\[Eta][x] - Zb[x])) +
q[x]/H[x] D[Zb[x], x];
der = Simplify[\!$$\*SubscriptBox[\(\[PartialD]$$, $$x$$]w\)];
dpdz = -(q[x]/H[x] der + w D[w, z] + g);
int = Integrate[dpdz, {z, Z0, Z1}];
Simplify[int] /. {Z0 -> z, Z1 -> \[Eta], q[x] -> q,
H[x] -> H, \[Eta][x] -> \[Eta], Zb[x] -> Zb}


After I execute it for a first time (after turning on the Mathematica) I get some output. But when I press shift+enter multiple times, then after third time I get different output. Can anybody confirm such behaviour?

• It gives me the same output in V10.1
– Kuba
May 21, 2015 at 11:38
• I don't know what's your output but I get: 1/2 (z-\[Eta]) (2 g+(1/(H^3 (Zb-\[Eta])^2))(q (Zb-\[Eta]) (H^\[Prime])[x] ((z-\[Eta]) (Zb-\[Eta]) (q^\[Prime])[x]+q ((z-2 Zb+\[Eta]) (H^\[Prime])[x]+2 (-Zb+\[Eta]) (Zb^\[Prime])[x]))+H ((z-\[Eta]) (Zb-\[Eta])^2 (q^\[Prime])[x]^2+q (Zb-\[Eta]) ((z-\[Eta]) (H^\[Prime])[x] (q^\[Prime])[x]+(Zb-\[Eta]) (2 (q^\[Prime])[x] (\[Eta]^\[Prime])[x]-(z-\[Eta]) (q^\[Prime]\[Prime])[x]))+q^2 ((z-2 Zb+\[Eta]) ((H^\[Prime])[x] ((H^\[Prime])[x]+(Zb^\[Prime])[x]-(\[Eta]^\[Prime])[x])+(-Zb+\[Eta]) (H^\[Prime]\[Prime])[x])+2 (Zb-\[Eta])^2 (Zb^\[Prime]\[Prime])[x]))))
– Kuba
May 21, 2015 at 11:42
• I seem to get the same output no matter how many times I evaluate this code (starting in a fresh session). I am using 10.1 under Windows. Start by turning of the predictive interface (suggestions bar) if you have not done so already. May 21, 2015 at 11:46
• How long does that Simplify take on your computer? If your computer is very slow it could happen that you run into the TimeConstraint that is set for Simplify when evaluating the first time. Following evaluations will then take advantage of cached intermediate results and get further in the evaluation and thus return a different result. If that's the case I think it is a known problem but not "broken-code"... May 21, 2015 at 11:52
• @Misery: well, then it looks like a different problem. Unfortunately I also don't see the problem so it is difficult to help... May 21, 2015 at 11:57

On my MacBook Pro with version 10.1, two forms of the same answer are returned, i.e., Simplify is timing out. Rather than guessing whether your system is fast enough, capture your "different" results and test whether they are equivalent. If so, then it is a time-out related issue.

\$Version


"10.1.0 for Mac OS X x86 (64-bit) (March 24, 2015)"

ClearAll["Global*"]

w = 1/H[x] D[q[x], x] (\[Eta][x] - z) +
q[x]/H[x] D[H[x], x] (1 - (\[Eta][x] - z)/(\[Eta][x] - Zb[x])) +
q[x]/H[x] D[Zb[x], x];

der = Simplify[\!$$\*SubscriptBox[\(\[PartialD]$$, $$x$$]w\)];

dpdz = -(q[x]/H[x] der + w D[w, z] + g);

int = Integrate[dpdz, {z, Z0, Z1}];

Simplify[int] /. {Z0 -> z, Z1 -> \[Eta], q[x] -> q,
H[x] -> H, \[Eta][x] -> \[Eta], Zb[x] -> Zb};

firstCalc =
g z - g \[Eta] + (1/(
2 H^2 (Zb - \[Eta])^2))(z - \[Eta]) (q Derivative[1][H][
x] + (Zb - \[Eta]) Derivative[1][q][
x]) ((z - \[Eta]) (Zb - \[Eta]) Derivative[1][q][x] +
q ((z - 2 Zb + \[Eta]) Derivative[1][H][x] +
2 (-Zb + \[Eta]) Derivative[1][Zb][x])) -
1/(2 H^3)q (-((q (z - \[Eta]) (z - 2 Zb + \[Eta]) Derivative[1][H][x]^2)/(
Zb - \[Eta])) - (z - \[Eta])^2 Derivative[1][H][x] Derivative[1][q][
x] + (H (z - \[Eta]) (z - 2 Zb + \[Eta]) Derivative[1][H][
x] Derivative[1][q][x])/(Zb - \[Eta]) +
2 q z Derivative[1][H][x] Derivative[1][Zb][x] -
2 q \[Eta] Derivative[1][H][x] Derivative[1][Zb][x] +
2 H (-z + \[Eta]) Derivative[1][q][x] Derivative[1][Zb][x] +
2 H (-z + \[Eta]) Derivative[1][q][x] Derivative[1][\[Eta]][x] - (
H q (z - \[Eta]) Derivative[1][H][
x] ((z - \[Eta]) Derivative[1][Zb][
x] - (z - 2 Zb + \[Eta]) Derivative[1][\[Eta]][
x]))/(Zb - \[Eta])^2 + (
H q (z - \[Eta]) (z - 2 Zb + \[Eta]) (H^\[Prime]\[Prime])[x])/(
Zb - \[Eta]) + H (z - \[Eta])^2 (q^\[Prime]\[Prime])[x] -
2 H q z (Zb^\[Prime]\[Prime])[x] +
2 H q \[Eta] (Zb^\[Prime]\[Prime])[x]);

subsequentCalc =
1/2 (z - \[Eta]) (2 g +
1/(H^3 (Zb - \[Eta])^2) (q (Zb - \[Eta]) Derivative[1][H][
x] ((z - \[Eta]) (Zb - \[Eta]) Derivative[1][q][x] +
q ((z - 2 Zb + \[Eta]) Derivative[1][H][x] +
2 (-Zb + \[Eta]) Derivative[1][Zb][x])) +
H ((z - \[Eta]) (Zb - \[Eta])^2 Derivative[1][q][x]^2 +
q (Zb - \[Eta]) ((z - \[Eta]) Derivative[1][H][x] Derivative[1][q][
x] + (Zb - \[Eta]) (2 Derivative[1][q][x] Derivative[
1][\[Eta]][x] - (z - \[Eta]) (q^\[Prime]\[Prime])[x])) +
q^2 ((z -
2 Zb + \[Eta]) (Derivative[1][H][
x] (Derivative[1][H][x] + Derivative[1][Zb][x] -
Derivative[1][\[Eta]][x]) + (-Zb + \[Eta]) (
H^\[Prime]\[Prime])[x]) +
2 (Zb - \[Eta])^2 (Zb^\[Prime]\[Prime])[x]))));

firstCalc == subsequentCalc // Simplify


True