Why are the results different when I integrate an expression in different ways?

When I integrate the expression like,

A1=(0.310314 ((0.00564116 - 4.25452 I) + (0.149772 - 55.6062 I) \[Beta] + \[Beta]^2 - (0. + 0.0765116 I) \[Omega] - I \[Beta] \[Omega]))/(236.578 + 3092.07 \[Beta] + 0.223032 \[Beta]^2 + \[Beta]^3 + 8.50904 \[Omega] + 111.212 \[Beta] \[Omega] +  0.0765116 \[Omega]^2 + \[Beta] \[Omega]^2) - (0.0239123 ((0.000620872 - 0.471257 I) + 0.0084749 \[Beta] - (0. + 0.0084749 I) \[Omega]))/((236.578 + 3092.07 \[Beta] + 0.223032 \[Beta]^2 + \[Beta]^3 + 8.50904 \[Omega] + 111.212 \[Beta] \[Omega] + 0.0765116 \[Omega]^2 + \[Beta] \[Omega]^2) (0.0732601 -I (55.7334 + \[Omega])))


Three ways

((Integrate[ A1 \[Rho][\[Omega]], \[Omega]] /. {\[Omega] -> \[Omega]1}) -
(Integrate[ A1 \[Rho][\[Omega]], \[Omega]] /. {\[Omega] -> \[Omega]2})) {\[Beta] -> 0.6295615718735122 + 0.07944775685111569 I}

((Integrate[A1 \[Rho][\[Omega]] /. {\[Beta] -> 0.6295615718735122 + 0.07944775685111569 I}, \[Omega]] /. {\[Omega] -> \[Omega]1}) - (Integrate[A1 \[Rho][\[Omega]] /. {\[Beta] -> 0.6295615718735122 + 0.07944775685111569 I}, \[Omega]] /. {\[Omega] -> \[Omega]2}))

Integrate[A1 \[Rho][\[Omega]] /. {\[Beta] ->0.6295615718735122 + 0.07944775685111569 I}, {\[Omega], \[Omega]2, \[Omega]1}]


give three different results

  0.356811 + 5.38461*10^-6 I

-0.407398 + 0.228095 I

0.355888 + 0.0000238939 I


where \[Rho][\[Omega]]=0.758333 + 1/3 (-55.6062 - \[Omega]) and \[Omega]1=-54.9468,\[Omega]2=-56.2655.

What is the problem here? And how to determine which one is right?

• Did you try A1 = Rationalize[(0.310314 ((0.00564116 - 4.25452 I) + (0.149772 - 55.6062 I) \[Beta] + \[Beta]^2 - (0. + 0.0765116 I) \[Omega] - I \[Beta] \[Omega]))/(236.578 + 3092.07 \[Beta] + 0.223032 \[Beta]^2 + \[Beta]^3 + 8.50904 \[Omega] + 111.212 \[Beta] \[Omega] + 0.0765116 \[Omega]^2 + \[Beta] \[Omega]^2) - (0.0239123 \ ((0.000620872 - 0.471257 I) + 0.0084749 \[Beta] - (0. + ... + \[Beta] \[Omega]^2) (0.0732601 - I (55.7334 + \[Omega]))), 0]; to avoid roundoff errors? Commented Jul 7, 2022 at 13:10
• Pay your attention to ... in the above (A part of the expression is omitted to be presented here.). Commented Jul 7, 2022 at 14:00

Clear["Global*"]


The order of operations affects the precision. Use exact values until the final result and then use arbitrary precision rather than machine precision.

ρ[ω_] = 0.758333 + 1/3 (-55.6062 - ω) //
Rationalize[#, 0] & // Simplify;
ω1 = -54.9468 // Rationalize;
ω2 = -56.2655 // Rationalize;

βrule = {β -> Rationalize[
0.6295615718735122 + 0.07944775685111569 I, 0]};

A1 = (0.310314 ((0.00564116 - 4.25452 I) +
(0.149772 - 55.6062 I) β + β^2 -
(0. + 0.0765116 I) ω - I β ω))/
(236.578 + 3092.07 β + 0.223032 β^2 +
β^3 + 8.50904 ω + 111.212 β ω +
0.0765116 ω^2 + β ω^2) -
(0.0239123
((0.000620872 - 0.471257 I) + 0.0084749 β -
(0. + 0.0084749 I) ω))/
((236.578 + 3092.07 β + 0.223032 β^2 +
β^3 + 8.50904 ω + 111.212 β ω +
0.0765116 ω^2 + β ω^2)
(0.0732601 - I (55.7334 + ω))) //
Rationalize[#, 0] & // Simplify;


Method 1

N[(
(Integrate[A1 ρ[ω], ω] /. {ω -> ω1}) -
(Integrate[A1 ρ[ω], ω] /. {ω -> ω2})) /.
βrule, 15]

(* 0.342963552432318 + 0.002442574599151 I *)


Method 2

N[(
(Integrate[
A1 ρ[ω] /. βrule, ω] /. {ω -> ω1}) -
(Integrate[
A1 ρ[ω] /. βrule, ω] /. {ω -> ω2})),
15]

(* 0.342963552432318 + 0.002442574599151 I *)


Method 3

N[Integrate[A1 ρ[ω] /. βrule, {ω, ω2, ω1}], 15]

(* 0.342963552432318 + 0.002442574599151 I *)


Comparing results,

% == %% == %%%

(* True *)

• It is enough to rationalize only A1 to obtain close results. Commented Jul 7, 2022 at 16:15
• No, if you will remove Rationalize from ω1 and ω2 it will break N[, 15] it will not work for N[, 30]. This is a regression, see: mathematica.stackexchange.com/questions/172425/… Same here: mathematica.stackexchange.com/questions/160695/… Commented Jul 7, 2022 at 18:18
• Thank you all. And how can I set every number Rationalize globally? Commented Jul 8, 2022 at 8:30
• You probably do not want to automatically rationalize all numbers. This would notably impact performance for all numeric calculations whether or not there were any precision issues. However, to temporarily do this, use $Pre = (# /. {val_Real | val_Complex :> Function[{x}, Rationalize[x, 0]][val]} &); then after the relevant calculations are done, use $Pre=.` to reset normal behavior. Commented Jul 8, 2022 at 13:37
• Thank you! @BobHanlon Commented Jul 8, 2022 at 17:14