Can someone please explain how to write functions in Wolfram Mathematica ?

In summary, the conversation discusses the difficulty of writing functions in Mathematica compared to Matlab, specifically the challenge of writing a function that returns a [1,0,-1] for different input values. The use of patterns is mentioned as a way to refine the function's behavior. The conversation also mentions the built-in function Sign and suggests looking at the tutorial on patterns for more information.
  • #1
Physics_rocks
12
0
I'm good in writing functions in Matlab , however since moving to Mathematica , I find it pretty hard to write them . For instance I need to write a function that returns a [1,0,-1] to integers, meaning x>0 , x==0 , x<0

with a single If , but I didn't find a way with one (only two)
sigNum[x_] := If[x < 0, -1, If[x > 0, 1, 0]]

and then using with PATTERNS . what are patterns and how do I use them ? tnx
 
Physics news on Phys.org
  • #2
There is a built in function for this called Sign.

Regarding patterns, you may want to start with the tutorial on patterns. In the search field of the help window type in: tutorial/PatternsOverview

Basically, you are already using patterns in the above definition, specifically, in sigNum[x_] the x_ is a pattern named x. You can refine your patterns to only apply in certain conditions by using the /; operator and the ? operator and you can also specify the head of the pattern. So x_Integer/;(x<0) would be a pattern that would match any negative integer.
 
  • #3


Sure, I'd be happy to explain how to write functions in Wolfram Mathematica. Functions in Mathematica are defined using the syntax "FunctionName[arguments]:= expression", where FunctionName is the name of the function, arguments are the variables that the function takes as input, and expression is the code that the function evaluates and returns as output.

In your specific case, if you want to write a function that returns -1 for x<0, 1 for x>0, and 0 for x=0, you can use the built-in function "Piecewise" which allows you to define functions with different behaviors for different input values. The syntax for using Piecewise is "Piecewise[{{value1, condition1}, {value2, condition2},...}]", where value1, value2, etc. are the values that the function returns for different conditions.

For your function, you can write it as "sigNum[x_] := Piecewise[{{-1, x < 0}, {1, x > 0}, {0, x == 0}}]". This will return -1 if x<0, 1 if x>0, and 0 if x=0. You can then use this function by simply calling "sigNum[x]" with your desired value for x.

Now, let's talk about patterns. Patterns in Mathematica are used to match specific types of input. For example, if you want your function to only accept integers as input, you can define it with the pattern "FunctionName[x_Integer]". This will ensure that the function only evaluates for integer values of x.

In your case, you can use patterns to make your function more robust and handle different types of input. For example, you can define your function as "sigNum[x_Integer] := Piecewise[{{-1, x < 0}, {1, x > 0}, {0, x == 0}}]". This will ensure that your function only evaluates for integer values of x, and returns an error message if a non-integer value is passed as input.

I hope this explanation helps you in writing functions in Mathematica. If you have any further questions, please feel free to ask.
 

Related to Can someone please explain how to write functions in Wolfram Mathematica ?

1. What is a function in Wolfram Mathematica?

A function in Wolfram Mathematica is a self-contained piece of code that takes in one or more inputs, performs a set of operations, and returns an output. It is a fundamental building block of the Mathematica programming language and is used to organize and streamline complex calculations.

2. How do I define a function in Wolfram Mathematica?

To define a function in Wolfram Mathematica, use the syntax "functionName[parameters_]:= expression". This creates a function named "functionName" that takes in the specified parameters and evaluates the given expression. For example, "square[x_]:= x^2" defines a function named "square" that takes in one input (x) and returns its square.

3. Can I have multiple inputs and outputs in a function?

Yes, you can have multiple inputs and outputs in a function in Wolfram Mathematica. Simply list the parameters within the square brackets, separated by commas. For example, "sum[x_,y_]:= x + y" defines a function named "sum" that takes in two inputs (x and y) and returns their sum.

4. How do I call a function in Wolfram Mathematica?

To call a function in Wolfram Mathematica, simply enter the function name followed by the necessary inputs within square brackets. For example, "square[3]" would call the "square" function defined in question 2 and return the value 9.

5. Can I use built-in functions within my own functions?

Yes, you can use built-in functions within your own functions in Wolfram Mathematica. This allows you to combine multiple operations and create more complex functions. For example, "sumOfSquares[x_,y_]:= square[x] + square[y]" calls the previously defined "square" function to calculate the sum of squares of two inputs.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
78
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
Back
Top