Functions

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Call Existing Functions

If you want to execute the same code repeatedly, create a procedure and use it. As a shortcut for simple procedures, create and use functions. Compared to procedures, functions require less complicated syntax. Like procedures, functions let you use the same code for different arguments as many times as you need. For example, you can always calculate sine and cosine of a particular value:

sin(30.0), sin(-1.0), sin(0.5);
cos(10.0), cos(-0.8), cos(3.0)

Create Functions

To define your own functions in MuPAD®, use the arrow operator:

f := x -> x^2

After defining a function, call it in the same way you call system functions:

f(1), f(x), f(sin(x))

The arrow operator also can create a multivariate function:

g := (x, y) -> x^2 + y^3

Call the multivariate function with numeric or symbolic parameters:

g(5, 2); g(x, 2*x); g(a, b)

Evaluate Expressions While Creating Functions

If you use an arrow operator to define a function, MuPAD does not evaluate the right-side expression:

f1 := x -> int(x^2, x)

To evaluate the right-side expression when defining a function, use the double arrow operator:

f2 := x --> int(x^2, x)

Use Functions with Parameters

Besides symbolic variables, functions can contain symbolic parameters. To evaluate such a function for particular values of symbolic parameters, use evalAt or the vertical bar | as a shortcut:

fa := x -> x^2 + a:
fa(2);
fa(2) | a = 10

Functions with symbolic parameters serve best for interactive use in a notebook. In your regular code, avoid unnecessary creation of such functions. When using a symbolic parameter, you use a global variable even though you do not explicitly declare it. See Global Variables for information on global variables and recommendations on their use.