Group Abstract Group Abstract

Message Boards Message Boards

0
|
132 Views
|
2 Replies
|
6 Total Likes
View groups...
Share
Share this post:

Efficiently Computing Piecewise Function Definitions Over an Interval Without Manual Steps

Posted 6 days ago

Piecewise function: when 1 <= x < 2, f[x] = 1 - Abs[2 x - 3]; when x >= 2, f[x] = 1/2 f[x/2].

I want to compute all the specific analytical expressions of the piecewise function in the interval 1 <= x < 8.

I used the following code to solve it:

f[x_] := Piecewise[{{1 - Abs[2 x - 3], 1 <= x < 2}, {g[x], x >= 2}}]
Reduce[{PiecewiseExpand[f[x] == 1/2 f[x/2], 8 > x >= 2]}, g[x]]

After running the code, the result obtained was: when x>=4, the analytical expression for f[x] was not solved.

(x < 3 && g[x] == -1 + x/2) || (3 <= x < 4 && 
   g[x] == (4 - x)/2) || (x >= 4 && g[x] == 1/2 g[x/2])

Therefore, I manually added all the analytical expressions obtained for the interval 2≤x<4 from the previous code into the original code and ran it again, finally obtaining all the analytical expressions for the interval 1≤x<8.

f[x_] := 
 Piecewise[{{1 - Abs[2 x - 3], 1 <= x < 2}, {-1 + x/2, 3 > x >= 2}, {(
    4 - x)/2, 3 <= x < 4}, {g[x], x >= 4}}]
Reduce[{PiecewiseExpand[f[x] == 1/2 f[x/2], x >= 2]}, g[x]]

x < 3 || 
 3 <= x < 
  4 || (4 <= x < 6 && g[x] == 1/8 (-4 + x)) || (6 <= x < 8 && 
   g[x] == (8 - x)/8) || (x >= 8 && g[x] == 1/2 g[x/2])

The problem I'm facing is how to automatically generate all the explicit expressions over a given interval in a single run, avoiding manual code adjustments and repeated executions. This manual process is tedious.

POSTED BY: Jim Clinton
2 Replies

Perhaps the following. The uses of Simplify[] are important, especially in the definition of f[]. It requires some nuanced understanding of the evaluation process and the use of $Assumptions by Simplify[], though.

f // ClearAll;
f[x_] /; Simplify[x >= 1] := Piecewise[{{1 - Abs[2 x - 3], 1 <= x < 2}, {f[x/2]/2, x >= 2}}];

This is equivalent to Gianluca's example:

Assuming[2^4 <= x, f[x]] // PiecewiseExpand
Plot[%, {x, 1, 32}]

The following gives an expression strictly defined over 1 <= x < 8:

k = 3;
Assuming[1 <= x < 2^k,
  ConditionalExpression[
   Assuming[2^(k - 1) <= x, f[x]] // PiecewiseExpand,
   $Assumptions]] // Simplify
Plot[%, {x, 1, 8}]
POSTED BY: Michael Rogers

I suppose there are smarter ways, but this one seems to work:

f0 = ConditionalExpression[1 - Abs[2 x - 3], 1 <= x < 2];
NestList[1/2 # /. x -> x/2 &, f0, 4]
Piecewise[% /. ConditionalExpression -> List]
Plot[%, {x, 1, 32}]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard