1

newbie to MMA.

After computing the following two integrals:

min1 = 0;
min2 = 0;
max1 = 1.00;
max2 = 1.00;
density = (1/(max1 - min1)) (1/(max2 - min2));


Integrate[(2 x1 - max1) density Boole[ 
    x1 + x2 >= s >= 0 && x1 >= p1 >= 0 && min1 <= x1 <= max1 && 
     min2 <= x2 <= max2], {x1, min1, max1}, {x2, min2, max2}];
b1 = Part[%, 1];
g1[s_] := Piecewise[b1, 0];


Integrate[(2 x2 - max2) density Boole[ 
    x1 + x2 >= s >= 0 && x2 >= p2 >= 0 && min1 <= x1 <= max1 && 
     min2 <= x2 <= max2], {x1, min1, max1}, {x2, min2, max2}];
b2 = Part[%, 1];
g2[s_] := Piecewise[b2, 0];

It returns "Nonatomic expression expected at position 1 in Last[0]. " when I try to add them up:

d1 = Simplify[g1[s_] + g2[s_]]

The sum works if I eliminate x1 >= p1 >= 0 and x2 >= p2 >= 0 respectively, but I cannot figure out why it does not work after including these two conditions.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Yun
  • 23
  • 5

1 Answers1

1

By using SetDelayed (short form :=) to define the function g1 or g2 you prevent the evaluation of b1 or b2 respectively into the assigned expression, so e.g. g1 has for its complete definition:

?? g1

Global`g1

g1[s_]:=Piecewise[b1,0]

Parameter substitutions into the right-hand-side are made before further evaluation, and since there is no literal s in the unevaluated form of Piecewise[b1,0] your function does not work correctly.

Using instead Set (short form =) the expressions b1 and b2 are expanded in the definition of g1 and g2.

g1[s_] = Piecewise[b1, 0];
g2[s_] = Piecewise[b2, 0];

Simplify[g1[s] + g2[s]]   (* outputs large Piecewise expression and no errors *)

Recommended reading:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371