4
$\begingroup$

By running the following code:

q[y_] = y;

For[i = 1, i <= 4, i++, q[y_] = q[If[1 - i <= x <= 2 + i, i, y]]];

f[x_] = q[If[7 <= x <= 6, 10 x, x]];

f[x]

I get:

If[0 <= x <= 3, i, 
 If[-1 <= x <= 4, i, If[-2 <= x <= 5, i, If[-3 <= x <= 6, i, x]]]]

and unfortunately I just can not figure out how to get the value of "i" and not "i" ultimate expression!


For example:

f[x_] = If[0 <= x <= 1 - 1/5, x,  If[1 + 1/5 <= x <= 2, -x + 2, 1 - 1/5]];

Plot[f[x], {x, 0, 2}]

enter image description here

So, in short, I'm interested in building a function f with the characteristics described above and can not find an effective way to do this (what I have shown is the one thing that is the closest). The actual setting is really complicated to explain, all will be placed in a much larger program.

$\endgroup$
4
  • 1
    $\begingroup$ Please clarify what you're attempting to accomplish, and why you choose this approach. $\endgroup$
    – kirma
    Jul 27, 2016 at 10:16
  • 1
    $\begingroup$ Have you seen Nest[]? $\endgroup$ Jul 27, 2016 at 10:38
  • $\begingroup$ @J.M. Once I understood what the For construct was trying to accomplish, I definitely ran into the same conclusion. :) - That is, imperative redeclaration of q is not very easy to understand and not very Mathematica-like. $\endgroup$
    – kirma
    Jul 27, 2016 at 10:44
  • $\begingroup$ @Manu, why not describe your actual problem and then maybe show what you expect to get from, say, two or three nestings? $\endgroup$ Jul 27, 2016 at 10:46

2 Answers 2

5
$\begingroup$

If has attribute HoldRest, which you can clear, but I don't recommend that.

I think the right solution is to replace a[HoldedArgument] by a[#] &[ToDoBeforeHold], i.e.

q[y_] = y;
For[i = 1, i <= 4, i++, q[y_] = q[If[1 - i <= x <= 2 + i, #, #2] &[i, y]]];
f[x_] = q[If[7 <= x <= 6, 10 x, x]];
f[x]
$\endgroup$
5
$\begingroup$

You may use Piecewise.

For your For loop function

g[x_] = Piecewise[{#, 1 - # <= x <= 2 + #} & /@ Range[4]]

Plot[g[x], {x, -3, 6}]

enter image description here

For your example function

f[x_] = Piecewise[{
   {x       ,  0 <= x <= 1 - 1/5},
   {-x + 2  , 1 + 1/5 <= x <= 2},
   {1 - 1/5 , True}
   }]

Plot[f[x], {x, 0, 2}]

enter image description here

Hope this helps.

$\endgroup$
4
  • $\begingroup$ @Manu See update. $\endgroup$
    – Edmund
    Jul 27, 2016 at 11:58
  • $\begingroup$ @Manu, Edmund's methods ought to be easily extendible by changing the appropriate numbers. $\endgroup$ Jul 27, 2016 at 11:59
  • $\begingroup$ @Manu The key is Piecewise. Generate your list of value-condition pairs with your algorithm and enclose that list in Piecewise. $\endgroup$
    – Edmund
    Jul 27, 2016 at 12:08
  • $\begingroup$ @Manu Piecewise evaluates its conditions in the order they are given and returns the value of the first condition that is true. Therefore you can Join your exclusion value-condition pairs to the list of value-condition pairs generated by your algorithm. Then enclose this list in Piecewise. For example Piecewise[Join[{{x, 0 < x < .5}, {-x + 1, .5 <= x < 1}}, {#, 1 - # <= x <= 2 + #} & /@Range[4]]]. $\endgroup$
    – Edmund
    Jul 27, 2016 at 12:33

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.