5
$\begingroup$

I have a function f(x) and I want to define a new function g(x) that is equal to f(x) only when f(x) is positive.

What is the right way to do that?

Sorry for this silly question, but I cannot find the proper way to accomplish this simple task in Mathematica.

$\endgroup$
7
  • 3
    $\begingroup$ Maybe g[x_]:=Max[f[x],0] ? (Provided I correctly remember the syntax of Max). But what should be the value of your new function when f is not positive? Is the zero value important? $\endgroup$
    – Peltio
    Nov 9, 2013 at 18:17
  • $\begingroup$ @Peltio That sounds like an answer to me. $\endgroup$
    – Szabolcs
    Nov 9, 2013 at 18:24
  • $\begingroup$ @Peltio it's reasonable, but I'd like that the function g(x) was undefined when f(x) was negative. $\endgroup$
    – psmith
    Nov 9, 2013 at 18:27
  • $\begingroup$ Try Piecewise function, if it's works for you. $\endgroup$
    – Hanoch
    Nov 9, 2013 at 18:32
  • $\begingroup$ @s.s.o In this way g(x) is defined only for positive x, instead I want that it is defined only when f(x) is positive. $\endgroup$
    – psmith
    Nov 9, 2013 at 18:54

1 Answer 1

4
$\begingroup$

You can check the Mathematica documentation about Condition /;

here is a simple example :

f[x_] := x + 5;
g[x_] := f[x] /; f[x] > 0

g[5]

10

g[-9]

g[-9]

$\endgroup$
1
  • $\begingroup$ @Szabolcs, in fact it was what I suggested as comment but it was not what OP asked for. $\endgroup$
    – s.s.o
    Nov 10, 2013 at 19:39

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.