0
$\begingroup$

I get two different results depending on when I apply the concrete parameter values for Integrate. First consider the following code:

Clear["Global`*"];
v = (1 - r) y + r x;
FullSimplify[Integrate[(d - (v - c))/(d s^2), {x, d (1 + i), (d (1 + i))/(1 - r)}, {y, (d (1 + i))/r - (1 - r)/r x, (d (1 + i))/(1 - r) - r/(1 - r) x}] + Integrate[(d - (v - c))/(d s^2), {x, (d (1 + i))/(1 - r), (d (1 + i))/r}, {y, 
0, (d (1 + i))/(1 - r) - r/(1 - r) x}] + Integrate[(d - (v - c q))/(d s^2), {x, 0, d (1 + i)}, {y, 0, (d (1 + i))/(1 - r) - r/(1 - r) x}] + Integrate[(d - (v - c q))/(d s^2), {x, d (1 + i), (d (1 + i))/(1 - r)}, {y, 0, (d (1 + i))/r - (1 - r)/r x}]]

When run the code, the output is:

-((d (1 + i)^2 (d - 2 d i + c (3 + 6 (-1 + q) r)))/(6 (-1 + r) r s^2))

To see what it would become under specific parameter values, I did

c = 0.5; d = 0.8; q = 1.5; s = 4;-((d (1 + i)^2 (d - 2 d i + c (3 + 6 (-1 + q) r)))/(6 (-1 + r) r s^2))

which yielded

-((0.00833333 (1 + i)^2 (0.8 - 1.6 i + 0.5 (3 + 3. r)))/((-1 + r) r))

Now, if I apply the same parameter values at the start I get a different result! Here is my code for this:

Clear["Global`*"];
c = 0.5; d = 0.8; q = 1.5; s = 4;
v = (1 - r) y + r x; FullSimplify[
Integrate[(d - (v - c))/(d s^2), {x, d (1 + i), (d (1 + i))/(1 - r)}, {y, (d (1 + i))/r - (1 - r)/r x, (d (1 +  i))/(1 - r) - r/(1 - r) x}] + Integrate[(d - (v - c))/(d s^2), {x, (d (1 + i))/(1 - r), (d (1 + i))/r}, {y, 0, (d (1 + i))/(1 - r) - r/(1 - r) x}] + Integrate[(d - (v - c q))/(d s^2), {x, 0, d (1 + i)}, {y, 0, (d (1 + i))/(1 - r) - r/(1 - r) x}] + Integrate[(d - (v - c q))/(d s^2), {x, d (1 + i), (d (1 + i))/(1 - r)}, {y, 0, (d (1 + i))/r - (1 - r)/r x}]]

The output is:

enter image description here

which is different from the first. Why? Please help.

$\endgroup$
1
  • $\begingroup$ Those coefficients with 10^-17 and 10^-18 are decimal approximation "floating point noise." Try using Chop[..yourresult..] which will make most things less than 10^-10 equal to zero and they disappear. That makes the second result somewhat closer to the first. OR you can use exact rationals for c,d,q,s and sometimes avoid the problem. $\endgroup$
    – Bill
    Commented Jan 16 at 16:49

1 Answer 1

3
$\begingroup$

The results are different only due to numerical errors that creeped their way because you are using approximate numbers instead of exact ones. First, use Chop to remove small numerical errors (terms like $10^{-17}$), then use Rationalize to convert approximate numbers into exact ones. You will see that the results match.

Clear["Global`*"];

params = {c -> 0.5, d -> 0.8, q -> 1.5, s -> 4};
v = (1 - r) y + r x;

int = Hold[FullSimplify[
    Integrate[(d - (v - c))/(d s^2), {x, d (1 + i), (d (1 + i))/(1 - r)}, {y, 
       (d (1 + i))/r - (1 - r)/r x, (d (1 + i))/(1 - r) - r/(1 - r) x}] + 
     Integrate[(d - (v - c))/(d s^2), {x, (d (1 + i))/(1 - 
        r), (d (1 + i))/r}, {y, 0, (d (1 + i))/(1 - r) - r/(1 - r) x}] + 
     Integrate[(d - (v - c q))/(d s^2), {x, 0, d (1 + i)}, {y, 
       0, (d (1 + i))/(1 - r) - r/(1 - r) x}] + 
     Integrate[(d - (v - c q))/(d s^2), {x, 
       d (1 + i), (d (1 + i))/(1 - r)}, {y, 0, (d (1 + i))/r - (1 - r)/r x}]]];

(* Integrate -> insert parameters *)
res1 = ReleaseHold[int] /. params;

(* Insert parameters -> integrate *)
res2 = ReleaseHold[int /. params];

Rationalize[res1 /. params] // FullSimplify
(* ((1 + i)^2 (-23 + 16 i - 15 r))/(1200 (-1 + r) r) *)

Rationalize[Chop[res2] /. params] // FullSimplify
(* ((1 + i)^2 (-23 + 16 i - 15 r))/(1200 (-1 + r) r) *)

A general advice: try using exact numbers. In this way, the results will be exactly the same without any post-processing.

paramsExact = {c -> 1/2, d -> 8/10, q -> 3/2, s -> 4};
(ReleaseHold[int] /. paramsExact) == ReleaseHold[int /. paramsExact]
(* True *)
$\endgroup$
1
  • $\begingroup$ This is amazing! Thanks so much, Domen!! $\endgroup$
    – ppp
    Commented Jan 16 at 22:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.