       Re: Integrate piecewise with Assumptions

• To: mathgroup at smc.vnet.net
• Subject: [mg44099] Re: Integrate piecewise with Assumptions
• Date: Thu, 23 Oct 2003 07:14:40 -0400 (EDT)
• References: <bn2iop\$o1e\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```"Chia-Ming" <yucalf at mail.educities.edu.tw> wrote in message news:<bn2iop\$o1e\$1 at smc.vnet.net>...
> Excuse me,
>
> First, I define a piecewise function to discribe the shortest distance
> on the edge of a circle.
>
> dis[x_, xi_] :=
>   Which[1/2 >= x - xi >= 0, x - xi, x - xi > 1/2, 1 - (x - xi),
>     0 > x - xi >= -1/2, xi - x, x - xi < -1/2, 1 - (xi - x)]
>
> And it does works that it can automatically judge to go in a clockwise
> or a conter-clockwise direction.
>
> dis[2/3, 1/10]
> 13/30
>
> Furthermore, dis[] can be applied in the Integrate[] command:
>
> Integrate[(a - 5 dist[1/6, x])^2, {x, 0, 1}]// Simplify
> 25/12 - (5 a)/2 + a^2
>
> However, it can not work with the option Assumptions:
>
> Integrate[(a - 5 dist[y, x])^2, {x, 0, 1}, Assumptions->0<y<1/2]//Simplify
[...]

Dear Chia-Ming  (or should I write Dear Yu ?)

You better use UnitStep functions to describe your function.
to build piecewise constant functions,
or piecewise linear functions, expressed
in terms of UnitStep functions.

In:=(* 'pcf' is piecewise constant function constructor.
Function is expressed in terms of UnitStep functions.
call format : pcf[y0, listOfPoints, x]
input :
y0 = leftmost value
listOfPoints = {{x1,y1},...,{xn,yn}} where  yi are right values
at points of discontiuity
x = variable used to generate output expression
output :
piecewise constant function expressed in terms of UnitStep
functions
using 'x' as variable
*)

pcf[y0_, listOfPoints_List, x_]:=
Module[{lp = Length[listOfPoints], f, a, b, eq, vars},
f[u_] := Sum[a[i]UnitStep[u-listOfPoints[[i,1]]],{i,1,lp}]+b;
eq = Append[f[#[]] == #[]& /@ listOfPoints,f[-Infinity] ==
y0];
vars = Append[Table[a[i],{i,1,lp}],b];
First[f[x] /. Solve[eq,vars]]
];

(* example of a square bump *)
pcf[0,{{0,1},{1,0}},x]
Out=-UnitStep[-1+x]+UnitStep[x]

In:=(* a stairs function : *)
pcf[0,{{0,1},{1,2},{2,3},{3,4},{4,5},{5,0}},x]
Out=-5 UnitStep[-5+x]+UnitStep[-4+x]+UnitStep[-3+x]+UnitStep[-2+x]+UnitStep[-1+x]+
UnitStep[x]

In:=(* plf is a piecewise linear function constructor.
(function may be discontinuous)
call format : plf[leftSlope0_, listOfPoints_List, x_]
input :
leftSlope0 : leftmost slope
listOfPoints format :
{{x1, lefty1, righty1, rightSlope1},...,
{xn, leftyn, rightyn, rightSlopen}}
x : variable
output :
piecewise linear function expressed in terms of UnitStep functions
using 'x' as variable
*)

plf[leftSlope0_, listOfPoints_List, x_]:=
Module[{deltaFunctions, pcfPoints, tempPlf, k, sol},
deltaFunctions = (#[]-#[])DiracDelta[x-#[]]&/@listOfPoints;
pcfPoints = {#[],#[]}& /@ listOfPoints;
tempPlf = Integrate[pcf[leftSlope0,pcfPoints,x]+Plus@@deltaFunctions,x]+k;
sol = Solve[(tempPlf/.x -> (listOfPoints[[1,1]]+
listOfPoints[[2,1]])/2)==(listOfPoints[[1,3]]+listOfPoints[[2,2]])/2,k];
tempPlf/.sol[]
];

(* example of a sawtooth function *)
plf[0,{{0,0,0,1},{1,1,0,1},{2,1,0,1},{3,1,0,0}},x] //Simplify

Out=-(-2+x) UnitStep[-3+x]-UnitStep[-2+x]-UnitStep[-1+x]+x
UnitStep[x]

In:=(* the bump function may be expressed two ways : *)
plf[0,{{0,0,1,0},{1,1,0,0}},x] == pcf[0,{{0,1},{1,0}},x]//Simplify
Out=True