Get Help with Defining Functions in Mathematica

In summary, the conversation discusses the possibility of defining a function in Mathematica with a specific domain and range, using an If statement instead of breaking it up into two separate functions. The individual also asks about deleting their post.
  • #1
Qyzren
44
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
  • #2
You can make use of the If statement.

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

What is Mathematica?

Mathematica is a powerful computational software program used for mathematical, scientific, and engineering calculations. It also has advanced features for data analysis, visualization, and programming.

How can I define a function in Mathematica?

To define a function in Mathematica, you can use the "Function" command or the "Set" command. For example, the function f(x) = x^2 can be defined as either f = Function[x, x^2] or f[x_] := x^2.

What is the syntax for defining a function in Mathematica?

The basic syntax for defining a function in Mathematica is "functionName [input parameters] := function definition". The input parameters can be a single variable or a list of variables, and the function definition can be an expression or a series of commands.

Can I use built-in functions within my own defined function?

Yes, you can use built-in functions within your own defined function in Mathematica. This allows for more complex and powerful functions to be created by combining multiple built-in functions.

How can I get help with defining functions in Mathematica?

You can get help with defining functions in Mathematica by using the built-in documentation or by searching online for tutorials and examples. There are also forums and communities dedicated to Mathematica where you can ask for help and advice from experienced users.

Similar threads

  • Calculus and Beyond Homework Help
Replies
1
Views
756
  • Calculus and Beyond Homework Help
Replies
10
Views
857
  • Calculus and Beyond Homework Help
Replies
1
Views
454
  • Calculus and Beyond Homework Help
Replies
3
Views
825
  • Calculus and Beyond Homework Help
Replies
7
Views
934
  • Calculus and Beyond Homework Help
Replies
1
Views
258
  • Calculus and Beyond Homework Help
Replies
8
Views
455
  • Calculus and Beyond Homework Help
Replies
4
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
264
  • Calculus and Beyond Homework Help
Replies
2
Views
531
Back
Top