0
$\begingroup$

Suppose that I wish to use "Piecewise" to merge two different interpolating functions (obtained via a NDSolve of some ODE), defined over negative and positive arguments respectively, like:

rule1 = f -> InterpolatingFunction["stuff"]
rule2 = f -> InterpolatingFunction["other stuff"]
rule1and2[xcoord_] := Piecewise[{{rule1, xcoord < 0},{rule2,xcoord > 0}}]

with this, I can then define a function over the real line in one go:

myfunc[x_] = f[x] /. rule1and2[x] 

Now, myfunc works well. The problem is when I want to use rule1and2 independently. For example, writing

rule1and2[-1]

gives the "left-hand side" of the rule as expected, but it is a bit misleading since one gets the impression that one top of it, on also evaluates the interpolating function at that specific point, which is of course not the case.

Instead, I would like to define rule1and2 in such a way that:

rule1and2[left]

gives the "left-hand" rule and

rule1and2[right]

gives the right one, and possible outputs a specific error message if the argument is none of the above two. This seems like an easy thing to do, but I am puzzled by how to do it?

$\endgroup$
  • $\begingroup$ Why can't you use f[x_] := Piecewise[{{InterpolatingFunction["stuff"][x], x < 0}, {InterpolatingFunction["other stuff"][x], x > 0}}]? $\endgroup$ – m_goldberg Jan 31 at 19:42
  • $\begingroup$ Hi. The reason is because I need to have the rules separately to plug them into other functions, which themselves depend on f. I suppose I could then define a new rule through the function f and go from there, but it seems a bit convoluted, to start from a rule, define a function, and then define a rule from it again. $\endgroup$ – Patrick.B Jan 31 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.