Skip to main content
added 109 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

why the error happened

Plus has higher precedence than ReplaceAll(see: Operator Input Forms >> Operator Precedence), so addition is performed before replacement.

{#, Precedence @ #} & /@ {Plus, Rule, ReplaceAll}

{{Plus, 310.}, {Rule, 120.}, {ReplaceAll, 110.}}

A simpler example that generates the same error is

a^5 /. sol[[1]] + 5 

enter image description here

how to fix it

You can parenthesize the terms to make sure that ReplaceAll is performed before Plus:

sum = (a^5 /. sol[[1, 1]]) + (b^5 /. sol[[1, 2]]) + (c^5 /. sol[[1, 3]]);
N[sum]

4138. + 9.99201*10^-16 I

Alternatively, you can use the following which avoids precedence issues:

sum2 = Total[{a, b, c}^5] /. sol[[1]];
N[sum2]

4138. + 9.99201*10^-16 I

why the error happened

Plus has higher precedence than ReplaceAll(see: Operator Input Forms >> Operator Precedence), so addition is performed before replacement.

A simpler example that generates the same error is

a^5 /. sol[[1]] + 5 

enter image description here

how to fix it

You can parenthesize the terms to make sure that ReplaceAll is performed before Plus:

sum = (a^5 /. sol[[1, 1]]) + (b^5 /. sol[[1, 2]]) + (c^5 /. sol[[1, 3]]);
N[sum]

4138. + 9.99201*10^-16 I

Alternatively, you can use the following which avoids precedence issues:

sum2 = Total[{a, b, c}^5] /. sol[[1]];
N[sum2]

4138. + 9.99201*10^-16 I

why the error happened

Plus has higher precedence than ReplaceAll(see: Operator Input Forms >> Operator Precedence), so addition is performed before replacement.

{#, Precedence @ #} & /@ {Plus, Rule, ReplaceAll}

{{Plus, 310.}, {Rule, 120.}, {ReplaceAll, 110.}}

A simpler example that generates the same error is

a^5 /. sol[[1]] + 5 

enter image description here

how to fix it

You can parenthesize the terms to make sure that ReplaceAll is performed before Plus:

sum = (a^5 /. sol[[1, 1]]) + (b^5 /. sol[[1, 2]]) + (c^5 /. sol[[1, 3]]);
N[sum]

4138. + 9.99201*10^-16 I

Alternatively, you can use the following which avoids precedence issues:

sum2 = Total[{a, b, c}^5] /. sol[[1]];
N[sum2]

4138. + 9.99201*10^-16 I

Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

why the error happened

Plus has higher precedence than ReplaceAll(see: Operator Input Forms >> Operator Precedence), so addition is performed before replacement.

A simpler example that generates the same error is

a^5 /. sol[[1]] + 5 

enter image description here

how to fix it

You can parenthesize the terms to make sure that ReplaceAll is performed before Plus:

sum = (a^5 /. sol[[1, 1]]) + (b^5 /. sol[[1, 2]]) + (c^5 /. sol[[1, 3]]);
N[sum]

4138. + 9.99201*10^-16 I

Alternatively, you can use the following which avoids precedence issues:

sum2 = Total[{a, b, c}^5] /. sol[[1]];
N[sum2]

4138. + 9.99201*10^-16 I