Functions and procedures in Mathematica
Definition and rules for working with user functions and organization of procedures
All functions in Mathematica have a name, arguments and formula or procedure for calculation. Analogically  the user can also define his/her own functions by obeying the rules of the system. It is recommended that the name of the user defined function begins with a non-capital latin letter as opposed to system functions. After that in square brackets the formal parameters are set, ending with the symbol  _   and separated with comma. After that an appropriation symbol   :=  must follow, as well as the expression of  the formal arguments, which are written without the symbol     at the end.  Using these functions is the same as using all other functions in Mathematica.

Example 1. Definition of a function of one argument. The function can also be called upon itself, in other words  functions are recursive.

f[x_] := x^3/(1 + x) + Cos[x] RowBox[{f, [, 0., ]}] RowBox[{a, =, RowBox[{RowBox[{5, RowBox[{f, [, 3.5, ]}]}], -, RowBox[{f, [, RowBox[{f, [, 1.2, ]}], ]}]}]}]

1.

41.8421

Example 2. If we want to check what is the exact definition the system has saved we can use the command '? name of function or variable'  to show its meaning. All operations on the function are allowed.

?f
a=.
f[a+1]

Global`f

f[x_] := x^3/(1 + x) + Cos[x]

(1 + a)^3/(2 + a) + Cos[1 + a]

Example 3. Development of the expression from example 2, grouping of multipliers and simplification.

Expand[f[a + 1]] Factor[%] Simplify[%]

1/(2 + a) + (3 a)/(2 + a) + (3 a^2)/(2 + a) + a^3/(2 + a) + Cos[1 + a]

(1 + 3 a + 3 a^2 + a^3 + 2 Cos[1 + a] + a Cos[1 + a])/(2 + a)

((1 + a)^3 + (2 + a) Cos[1 + a])/(2 + a)

Example 4. Graphics of the function from example 1 in the interval [-2, 2]. The same graphics but with values of f[x] in the interval [0,10] by applying option PlotRange.

g1 = Plot[f[x], {x, -2, 2}] g2 = Plot[f[x], {x, -2, 2}, PlotRange {0, 10}]

[Graphics:HTMLFiles/index_11.gif]

⁃Graphics⁃

[Graphics:HTMLFiles/index_13.gif]

⁃Graphics⁃

Example 5. Definition of a function of two variables. The variables for formal parameters can be duplicated from the ones used earlier. We deduce the formula and build a two dimensional graphics. Although the function is not defined at  y=0, Mathematica shows the graphics with an obvious jump in that area.

huhu[x_, y_] := (x - y)^3/y ? huhu Plot3D[huhu[x, y], {x, -10, 10}, {y, -1, 1}]

Global`huhu

huhu[x_, y_] := (x - y)^3/y

[Graphics:HTMLFiles/index_17.gif]

⁃SurfaceGraphics⁃

Example 6.  We can define expressions appropriated to simple variables by using already defined functions without limitations and use in combination with system functions. Here in variable ' r ' we store a formula of a variable x by using huhu, drawing the graphics, calculate with a temporary tangible value x=3 and solve the equation r==0 with respect to x.

r = 2 * 10^2huhu[x, 2] Plot[r, {x, -10, 10}] RowBox[{%%, /., RowBox[{x, , 3.}]}] Solve[r2, x] %//N

100 (-2 + x)^3

[Graphics:HTMLFiles/index_21.gif]

⁃Graphics⁃

100.

{{x2 + 1/(2^(1/3) 5^(2/3))}, {x2 - (1 -  3^(1/2))/(2 2^(1/3) 5^(2/3))}, {x2 - (1 +  3^(1/2))/(2 2^(1/3) 5^(2/3))}}

RowBox[{{, RowBox[{RowBox[{{, RowBox[{x, , 2.27144}], }}], ,, RowBox[{{, RowBox[{x, &# ...  , RowBox[{RowBox[{1.86428, }], -, RowBox[{0.235075,  , }]}]}], }}]}], }}]

Example 7.  Mixed use of the functions from the previous example.  We define a new function, calculate value at z=3,  t=6 and obtain the Taylor expansion to the third member.

p1[z_, t_] := (1 - f[z] * huhu[z, t])^2 p1[3, 6] %//N Series[p[x, y], {x, 0, 3}, {y, 0, 3}]

(1 + 9/2 (27/4 + Cos[3]))^2

724.688

(1 + 2 y^2 + O[y]^4) + (-6 y - 6 y^3 + O[y]^4) x + (6 + 14 y^2 + O[y]^4) x^2 + (-2/y - 17 y + 2 y^2 + 6 y^3 + O[y]^4) x^3 + O[x]^4

Example 8.  We must consider,  that variables used in functions in the right part of the definition are local to that operator and have no value outside of it. Here us an example in which the variable ' i ' is local to an operator for the defined function ss.  Calculating a sum with 1 000 000 members takes approximately 20 seconds, after which the system cuts off the calculation with a double large closing bracket on the right of the current cell.

ss[n_] := Underoverscript[∑, i = 1, arg3] i^2 ss[1000000] i

333333833333500000

i

Example 9.  Definition of procedures is done with operators seperated with ; (semicolon) and all included in brackets ( ). To find out which variables are global and which local we print them. The type of the variable (integer number, rational, complex etc.) automatically leads to work over the corresponding field of numbers.

Clear[aa, bb, n, k] pro1[n_, k_] := (aa = ∫_0^k (1 + u)^(1/2) u ; bb = a ... ;    (* local variable *)n    (* local variable *)

 For n=1 and  k=π the integral is = 2/3 (-1 + (1 + π)^(3/2))

 For n=3 and  k= -2 the integral is =  -2/3 - (2 )/3

u

-2/3 - (2 )/3

-2/9 - (2 )/9

n

Example 10.  Formal substitution allows for different symbolic calculations with formulas.

Clear[f]
r=f[x]+2f[y]
r /. x→2
r /. {f[x]→p,f[y]→6q}
r/. f[t_] →t^2

f[x] + 2 f[y]

f[2] + 2 f[y]

p + 12 q

x^2 + 2 y^2


Created by Mathematica  (December 21, 2007)