Functions

Mathematica has a large amount of functions already built in. The arguments of a function are put in between square brackets and separated by commas. Some basic functions are given below. Note that all built in functions and variables start with capital letters. To avoid potential conflicts it is a good idea to start your own functions and variables with lower case letters.

Sqrt[x]     square root (Sqrt[x])
                         Exp[x]     exponential (e^x)
                         Log[x]     natural logarithm (Subscript[log, e] x)
                     Log[b, x]     logarithm to base b (Subscript[log, b] x)
  Sin[x], Cos[x], Tan[x]     trigonometric functions 
   ArcSin[x], ArcTan[x]     inverse trigonometric functions                                    
                         Abs[x]     absolute value
                     Round[x]    closest integer to x

Most built-in Mathematica functions act on lists element - by - element. That is, Mathematica typically maps functions across lists. See for example the trigonometric functions.

Sin[Pi]
L={Pi, 1/2 Pi, 1/3Pi, 1/4 Pi}
Sin[L]

We can compose any number of built-in Mathematica functions, as in the following example:

Round[Sqrt[Abs[-5]]]

The symbol @ (slash) maps a function (of one argument) to its argument. In place of writing f[x], one could simply write f @ x. With @ you do not have to move to the end of the expression to match the square bracket. You can also use the // postfix notation to accomplish the same thing.

Round@Sqrt@Abs@-5

-5 // Abs // Sqrt // Round

The symbol @@ (slash - slash) applies a function (of several arguments) to its arguments. For instance, Log @@ {2, 32} is the same as Log[2,32] and it computes the logarithm of 32 in base 2.

Log @@ {2, 32}

Note that this is different from Log @ {2, 32}, which is equivalent to Log[{2, 32}] and returns a list of two elements, representing the natural logarithms (base e) of 2 and 32

User defined functions

We can define our own functions as follows:

f[x_]:=(x^2)+Sin[x]

In this expression the underscore represents a pattern. A single underscore will match a single expression (argument) and x is the name of the pattern (it can be used to refer to the matched expression on the right hand side). The colon tells Mathematica not to evaluate the right hand side. It will only be evaluated when you invoke the function. This is why you get no output. Now you can use your function:

f[angle]+f[2Pi]

Try to assign some value to x, then compute f[2Pi]. What do you notice? Does f use the global or the local values of x when computing f[2Pi]?

x=5;
f[2Pi]

Warning:  Do not omit the colon when defining functions!

x=5;
g[x_]=(x^2)+Sin[x]
g[2Pi]

Can you explain this result? You can look at the definition of a function by using a question mark:

?f
?g

To clear the function or variable definitions, use Clear[..]

Clear[x,f,g]

Now we can do a new definition for the function f.

f[i_,j_]:=i+j

The function f gives the sum of two variables. Let us evaluate f with some integer arguments, and then with some vector arguments. What do you notice?

f[2,3]
f[{1,2,3, a},{3,4,5, b}]

Plotting a function

Documentation available at  http://reference.wolfram.com/language/ref/Plot.html

Plot[f[x],{x,xmin,xmax}] generates a plot of f as a function of the variable x, where x ranges from xmin to xmax.

f[x_,c_]:=(x-c)^3

Plot[f[x,2], {x, -4,8}]

There are a number of options that can be used with the Plot command, see the Mathematica documentation for a comprehensive list of available options and examples. For instance, AxesLabel specifies labels for the x and y axes. PlotStyle sets the style of the curve. You can fill in the space between the curve and the x-axis using the option Filling.

Plot[f[x,2],{x,-4,8},AxesLabel->{"x-axis","f[x]"}, PlotStyle->{Red, Thick}, Filling->Axis]

We can plot several functions on the same graph, by enumerating them in a list given in the first argument of the plot command. The option PlotLegends specifies labels for each graph.

Plot[{f[x,0],f[x,-2],f[x,4]} , {x, -10,10} , AspectRatio->1/1, PlotLegends->"Expressions"]

Exercise :

Browse the documentation of the Plot command (more precisely, look in the list of Options at Filling and FillingStyle), then plot the graphs of Sin[x] and Cos[x] for x between 0 and 4Pi, on the same plot, with the region between the two graphs filled in as follows : when Sin[x] >= Cos[x], the filling color should be LightBlue, otherwise, it should be LightRed.

Answer :

Plot[{Sin[x], Cos[x]}, {x,0,4Pi}, Filling->{1->{2}}, FillingStyle->{LightRed, LightBlue}]

More plotting commands

ContourPlot[f[x, y] == g[x, y], {x, xmin, xmax}, {y, ymin, ymax}]
plots the contour of the region in the xy plane where f[x, y] is equal to g[x, y].

ContourPlot[x^2+y^2==1/4,{x,-1,1},{y,-1,1} ]

ContourPlot[{(x^2+y^2)^2==x^2-y^2,(x^2+y^2)^2==2 x y},{x,-1,1},{y,-1,1}, PlotLegends->{"First plot: ((x^2)+(y^2))^2 = (x^2)-(y^2)", "Second plot: ((x^2)+(y^2))^2 = 2xy"} ]

ParametricPlot[{x[t], y[t]}, {t, tmin, tmax}]
generates a parametric plot of a curve with x and y coordinates as a function of t.

ParametricPlot[{2Cos[t],Sin[t]},{t,0,2Pi}]

3D Plots

Plot3D[f, {x, xmin, xmax}, {y, ymin, ymax}]
generates a three dimensional plot of f as a function of x and y.

Plot3D[Sin[x+y^2],{x,-3,3},{y,-2,2}]

Let' s generate the same 3D plot as before, assign it to a variable P, and then export it as pdf.

P=Plot3D[Sin[x+y^2],{x,-3,3},{y,-2,2}, Mesh->None, ColorFunction->"Rainbow", Boxed->False, Axes->False, MaxRecursion->15]
(* we want to save the graphics P to a picture that we can use later *)
SetDirectory[NotebookDirectory[]];
Export["mypicture.pdf",P];