Get Help with Defining Functions in Mathematica

  • Thread starter Thread starter Qyzren
  • Start date Start date
  • Tags Tags
    Mathematica
Qyzren
Messages
41
Reaction score
0
Hi, not sure where to put this, so i'll just put it here.
i was wondering if there is a way to define functions of this sort in mathematica.
For example.
f(x)=x if x>0 and f(x)=2x if x<=0.

I know you can break it up into 2 functions and go something like
g[x_]:=x;
h[x_]:=2x;
but those are 2 different functions with different domain ranges (0,Inf), (-Inf,0].
what i want is just 1 function in mathematica with domain (-Inf, Inf) that knows to use g if x>0, and h if x<=0 in my example.

Thank you.

*edit i figured it out,
PS how do i delete my post?
 
Last edited:
on Phys.org
You can make use of the If statement.

f[x_]:=If[x>0,x,2x]
 
Last edited:

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
Replies
6
Views
3K
Replies
2
Views
2K
  • · Replies 105 ·
4
Replies
105
Views
14K
Replies
8
Views
1K
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K