Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
3  
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? –  Peltio Nov 9 '13 at 18:17
    
@Peltio That sounds like an answer to me. –  Szabolcs Nov 9 '13 at 18:24
    
@Peltio it's reasonable, but I'd like that the function g(x) was undefined when f(x) was negative. –  Fabrizio Nov 9 '13 at 18:27
    
Try Piecewise function, if it's works for you. –  Hinstein Nov 9 '13 at 18:32
    
@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. –  Fabrizio Nov 9 '13 at 18:54
show 2 more comments

1 Answer

up vote 3 down vote accepted

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]

share|improve this answer
    
@Szabolcs, in fact it was what I suggested as comment but it was not what OP asked for. –  s.s.o Nov 10 '13 at 19:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.