Get Help with Defining Functions in Mathematica

  • Thread starter Thread starter Qyzren
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
SUMMARY

The discussion focuses on defining piecewise functions in Mathematica, specifically how to create a single function that behaves differently based on the input value. The user initially attempts to define two separate functions, g[x_] and h[x_], but seeks a more efficient solution. The final solution provided is f[x_]:=If[x>0,x,2x], which successfully combines the conditions into one function that operates over the entire domain of real numbers.

PREREQUISITES
  • Familiarity with Mathematica syntax
  • Understanding of conditional statements in programming
  • Basic knowledge of piecewise functions
  • Experience with function definitions in Mathematica
NEXT STEPS
  • Explore advanced conditional constructs in Mathematica
  • Learn about defining more complex piecewise functions using Which or Switch
  • Investigate optimization techniques for function definitions in Mathematica
  • Study the implications of function domain and range in mathematical modeling
USEFUL FOR

Mathematica users, mathematicians, and educators looking to enhance their skills in defining and utilizing piecewise functions effectively.

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:
Physics news 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
11K
Replies
8
Views
1K
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K