Use Assertions

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.

If you rely on correctness of particular statements for your code, then consider including these statements in the code. Such statements are called assertions. Assertions help you remember specific conditions under which you expected to execute your code. They can also help other developers who might need to review or update your code.

MuPAD® lets you use checked assertions. If you switch to a special mode of executing your code, the system evaluates assertions during run time. If an assertion does not evaluate to TRUE, the system stops code execution and throws an error.

For example, this procedure solves the equation sin(x) + cos(x) = a2. Suppose you get the parameter a as a result of some computations, and you expect the condition to be always valid. Relying on this condition, you expect the solutions to be real. Specify this condition as an assertion by using assert:

f := proc(a)
  assert(a^2 <= sqrt(2));
  s := solve(sin(x) + cos(x) = a^2)

Assertions are checked only when you run your code in a special mode called the argument checking mode. Otherwise, the system ignores all assertions. For example, in this procedure call, MuPAD skips the assertion and returns the following complex result:


To switch to the argument checking mode, set the value of testargs to TRUE:


Now when you call the procedure f, MuPAD checks the assertion. For a = 4/3, the assertion evaluates to FALSE, and the procedure execution stops with the error:

Error: Assertion 'a^2 <= sqrt(2)' has failed. [f]

For a = 1, the assertion evaluates to TRUE. The procedure call runs to completion, and returns the following set of real solutions:


The argument checking mode can slow down your computations. Use this mode only for debugging your code. Always restore testargs to its default value FALSE after you finish debugging: