       Re: Re: Wrong Integral result for a Piecewise function

• To: mathgroup at smc.vnet.net
• Subject: [mg58641] Re: [mg58614] Re: Wrong Integral result for a Piecewise function
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Mon, 11 Jul 2005 04:19:26 -0400 (EDT)
• References: <200507100912.FAA06529@smc.vnet.net> <4F43F6EE-C6BF-40C1-A9B1-58F07EDE700A@gmail.com>
• Sender: owner-wri-mathgroup at wolfram.com

```Actually, we can do even better (I did not try it earlier as I did
not believe it would work but it does).

f[x_] = Limit[FullSimplify[Integrate[
UnitStep[2*y + 2*z - (t - 1)]*
UnitStep[t - 2*y - 2*z], {y, 0, 1}, {z, 0, 1}]],
t -> x]

Piecewise[{{(1/8)*(9 - 2*x), Inequality[3, LessEqual, x,
Less, 4]}, {(1/8)*(x - 5)^2, Inequality[4,
LessEqual, x, Less, 5]},
{x^2/8, Inequality[0, LessEqual, x, Less, 1]},
{(1/8)*(2*x - 1), Inequality[1, LessEqual, x, Less,
2]}, {(1/8)*(-2*x^2 + 10*x - 9),
Inequality[2, LessEqual, x, Less, 3]}}]

It seems that this is the way to get the right answer using the
UnitStep approach.

Andrzej Kozlowski

Chiba, JAPAN

On 10 Jul 2005, at 22:50, Andrzej Kozlowski wrote:

>
> On 10 Jul 2005, at 18:12, Maxim wrote:
>
>
>> On Thu, 7 Jul 2005 09:42:42 +0000 (UTC), Andrzej Kozlowski
>> <akozlowski at gmail.com> wrote:
>>
>>
>>>
>>>
>>>
>>> g[x_] = FullSimplify[Integrate[UnitStep[2y + 2z - (x - 1)]
>>> *UnitStep[x
>>> - 2y - 2z], {y, 0, 1}, {z, 0, 1}]]
>>>
>>> (this takes a while to complete) then
>>>
>>> Plot[g[x],{x,0,5}]
>>>
>>> looks correct. Also
>>>
>>> In:=
>>> NIntegrate[g[x], {x, 0, 5}]
>>>
>>> Out=
>>> 1.
>>>
>>> In:=
>>> Integrate[g[x], {x, 0, 5}]
>>>
>>> Out=
>>> 1
>>>
>>> This, of course, is the pre-Mathematica 5 way of doing these things
>>> which only goes to confirm that progress is not always
>>> improvement ;-)
>>>
>>> Andrzej Kozlowski
>>> Chiba, Japan
>>>
>>>
>>>
>>
>> In version 5.1.0 this gives an answer which is correct everywhere
>> except
>> at the integer points:
>>
>> In:=
>> g[x_] = Integrate[UnitStep[2*y + 2*z - x + 1]*UnitStep[x - 2*y -
>> 2*z],
>>    {y, 0, 1}, {z, 0, 1}]
>>
>> Out=
>> (1/8)*(2*(-1 + (-2 + x)*x)*UnitStep[1 - x] + 2*(-2 + x)^2*UnitStep
>> [2 - x]
>> + 2*(-2 + x)*x*UnitStep[2 - x] - 2*(7 + (-6 + x)*x)*UnitStep[3 -
>> x] - (-5
>> + x)*UnitStep[5 - x]*(6 - 2*x + (-1 + x)*UnitStep[-3 + x]) +
>> UnitStep[4 -
>> x]*(-4*UnitStep[1 - x/2] + (-4 + x)*(4 - 2*x + x*UnitStep[-2 +
>> x])) + (-3
>> + x)*UnitStep[3 - x]*(2 - 2*x + (1 + x)*UnitStep[-1 + x]) - 2*(-2
>> + x^2)*UnitStep[-x] - (-4 + x^2)*UnitStep[2 - x, x])
>>
>> In:=
>> Reduce[g[x] != If[x == 3, 3/8, 0] + If[0 < x < 1, x^2/8, 0] + If[1
>> <= x <=
>> 2, (1/8)*(-1 + 2*x), 0] + If[2 < x < 3, (1/8)*(-9 + 10*x - 2*x^2), 0]
>> + If[Inequality[3, Less, x, LessEqual, 4], (1/8)*(9 - 2*x), 0] + If
>> [4 < x
>> < 5, (1/8)*(-5 + x)^2, 0]]
>>
>> Out=
>> x == 0 || x == 1 || x == 2 || x == 3
>>
>> This is always a potential pitfall when the answer is returned as
>> a sum of
>> UnitStep terms, e.g. as UnitStep[-x] + (x + 1)*UnitStep[x]. It is
>> likely
>> that the value at x = 0 will be incorrect, because both terms are
>> equal to
>> 1 at zero, not just one of them. Thus Limit[g[x], x -> 0] is
>> correct but
>> g isn't.
>>
>> Maxim Rytin
>> m.r at inbox.ru
>>
>>
>>
>
>
> This suggests a simple but rather curious remedy. First define gg
> as above:
>
> gg[x_] = FullSimplify[Integrate[UnitStep[2y + 2z - (x - 1)]*UnitStep[x
> - 2y - 2z], {y, 0, 1}, {z, 0, 1}]];
>
> and then define g simply as:
>
> g[x_] := Limit[gg[t], t -> x]
>