## Perform Computations

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.

### Compute with Numbers

#### Types of Numbers

Using MuPAD®, you can operate on the following types of numbers:

• Integer numbers

• Rational numbers

• Floating-point numbers

• Complex numbers

By default, MuPAD assumes that all variables are complex numbers.

#### Compute with Integers and Rationals

When computing with integers and rational numbers, MuPAD returns integer results

`2 + 2`
` `

or rational results:

`(1 + (5/2*3))/(1/7 + 7/9)^2`
` `

If MuPAD cannot find a representation of an expression in an integer or rational form, it returns a symbolic expression:

`56^(1/2)`
` `

#### Compute with Special Mathematical Constants

You can perform exact computations that include the constants `=exp(1)=2.718...` and π`=3.1415...`:

`2*(exp(2)/PI)`
` `

#### Approximate Numerically

By default, MuPAD performs all computations in an exact form. To obtain a floating-point approximation to an expression, use the `float` command. For example:

`float(sqrt(56))`
` `

The accuracy of the approximation depends on the value of the global variable `DIGITS`. The variable `DIGITS` can assume any integer value between 1 and 229 + 1. For example:

`DIGITS:=20: float(sqrt(56))`
` `

The default value of the variable `DIGITS` is 10. To restore the default value, enter:

`delete DIGITS`

When MuPAD performs arithmetic operations on numbers involving at least one floating-point number, it automatically switches to approximate numeric computations:

`(1.0 + (5/2*3))/(1/7 + 7/9)^2`
` `

If an expression includes exact values such as or sin(2) and floating-point numbers, MuPAD approximates only numbers:

`1.0/3*exp(1)*sin(2)`
` `

To approximate an expression with exact values, use the `float` command:

`float(1.0/3*exp(1)*sin(2))`
` `

or use floating-point numbers as arguments:

`1.0/3*exp(1.0)*sin(2.0)`
` `

You also can approximate the constants π and :

`DIGITS:=30: float(PI); float(E); delete DIGITS`
` `
` `

#### Work with Complex Numbers

In the input regions MuPAD recognizes an uppercase I as the imaginary unit . In the output regions, MuPAD uses a lowercase i to display the imaginary unit:

`sqrt(-1), I^2`
` `

Both real and imaginary parts of a complex number can contain integers, rationals, and floating-point numbers:

`(1 + 0.2*I)*(1/2 + I)*(0.1 + I/2)^3`
` `

If you use exact expressions, for example, , MuPAD does not always return the result in Cartesian coordinates:

`1/(sqrt(2) + I)`
` `

To split the result into its real and imaginary parts, use the `rectform` command:

`rectform(1/(sqrt(2) + I))`
` `

The functions `Re` and `Im` return real and imaginary parts of a complex number:

`Re(1/(2^(1/2) + I))`
` `
`Im(1/(2^(1/2) + I))`
` `

The function `conjugate` returns the complex conjugate:

`conjugate(1/(2^(1/2) + I))`
` `

The function `abs` and `arg` return an absolute value and a polar angle of a complex number:

```abs(1/(2^(1/2) + I)); arg(1/(2^(1/2) + I))```
` `
` `

### Differentiation

#### Derivatives of Single-Variable Expressions

To compute the derivative of a mathematical expression, use the `diff` command. For example:

`f := 4*x + 6*x^2 + 4*x^3 + x^4: diff(f, x)`
` `

#### Partial Derivatives

You also can compute a partial derivative of a multivariable expression:

`f := y^2 + 4*x + 6*x^2 + 4*x^3 + x^4: diff(f, y)`
` `

#### Second- and Higher-Order Derivatives

To find higher order derivatives, use a nested call of the `diff` command

`diff(diff(diff(sin(x), x), x), x)`
` `

or, more efficiently:

`diff(sin(x), x, x, x)`
` `

You can use the sequence operator `\$` to compute second or higher order derivatives:

`diff(sin(x), x \$ 3)`
` `

#### Mixed Derivatives

`diff(f, x1, x2, ...)` is equivalent to ```diff(...diff(diff(f, x1), x2)...)```. The system first differentiates `f` with respect to `x1`, and then differentiates the result with respect to `x2`, and so on. For example

`diff(diff((x^2*y^2 + 4*x^2*y + 6*x*y^2), y), x)`
` `

is equivalent to

`diff(x^2*y^2 + 4*x^2*y + 6*x*y^2, y, x)`
` `

### Note

To improve performance, MuPAD assumes that all mixed derivatives commute. For example, .

This assumption suffices for most of engineering and scientific problems.

For further computations, delete `f`:

`delete f:`

#### Derivatives of a Function

MuPAD provides two differentiation functions, `diff` and `D`. The `diff` function serves for differentiating mathematical expressions, such as `sin(x)`, `cos(2y)`, `exp(x^2)`, ```x^2 + 1```, `f(y)`, and so on.

To differentiate a standard function, such as `sin`, `exp`, `heaviside`, or a custom function, such as `f:= x -> x^2 + 1`, use the differential operator `D`:

`D(sin), D(exp), D(heaviside)`
` `
`f := x -> x^2 + 1: D(f)`
` `

`'` is a shortcut for the differential operator `D`:

`sin', sin'(x), f'`
` `

The command `D(f)(x)` assumes that `f` is a univariate function, and represents the derivative of `f` at the point `x`. For example, the derivative of the sine function at the point x2 is:

`D(sin)(x^2)`
` `

Note that in this example you differentiate the `sin` function, not the function `f := x -> sin(x^2)`. Differentiating `f` returns this result:

`f := x -> sin(x^2): D(f)`
` `

For details about using the operator `D` for computing second- and higher-order derivatives of functions, see Differentiating Functions.

### Integration

#### Indefinite Integrals

To compute integrals use the `int` command. For example, you can compute indefinite integrals:

`int((cos(x))^3, x)`
` `

The `int` command returns results without an integration constant.

#### Definite Integrals

To find a definite integral, pass the upper and lower limits of the integration interval to the `int` function:

`int((cos(x))^3, x = 0..PI/4)`
` `

You can use infinity as a limit when computing a definite integral:

`int(sin(x)/x, x = -infinity..infinity)`
` `

#### Numeric Approximation

If MuPAD cannot evaluate an expression in a closed form, it returns the expression. For example:

`int(sin(x^2)^2, x = -1..1)`
` `

You can approximate the value of an integral numerically using the `float` command. For example:

`float(int(sin(x^2)^2,(x = -1..1)))`
` `

You also can use the `numeric::int` command to evaluate an integral numerically. For example:

`numeric::int(sin(x^2)^2, x = -1..1)`
` `

### Linear Algebra

#### Create a Matrix

To create a matrix in MuPAD, use the `matrix` command:

```A := matrix([[1, 2], [3, 4], [5, 6]]); B := matrix([[1, 2, 3], [4, 5, 6]])```
` `
` `

You also can create vectors using the `matrix` command:

`V := matrix([1, 2, 3])`
` `

You can explicitly declare the matrix dimensions:

```C := matrix(3, 3, [[-1, -2, -3], [-4, -5, -6], [-7, -8, -9]]); W := matrix(1, 3, [1, 2, 3])```
` `
` `

If you declare matrix dimensions and enter rows or columns shorter than the declared dimensions, MuPAD pads the matrix with zero elements:

`F := matrix(3, 3, [[1, -1, 0], [2, -2]])`
` `

If you declare matrix dimensions and enter rows or columns longer than the declared dimensions, MuPAD returns the following error message:

`matrix(3, 2, [[-1, -2, -3], [-4, -5, -6], [-7, -8, -9]])`
```Error: Number of columns does not match. [(Dom::Matrix(Dom::ExpressionField()))::mkSparse] ```

You also can create a diagonal matrix:

`G := matrix(4, 4, [1, 2, 3, 4], Diagonal)`
` `

#### Operate on Matrices

To add, subtract, multiply and divide matrices, use standard arithmetic operators. For example, to multiply two matrices, enter:

```A := matrix([[1, 2], [3, 4], [5, 6]]); B := matrix([[1, 2, 3], [4, 5, 6]]); A*B```
` `
` `
` `

If you add number `x` to a matrix `A`, MuPAD adds `x` times an identity matrix to `A`. For example:

```C := matrix(3, 3, [[-1, -2, -3], [-4, -5, -6], [-7, -8, -9]]); C + 10```
` `
` `

You can compute the determinant and the inverse of a square matrix:

`G := matrix([[1, 2, 0], [2, 1, 2], [0, 2, 1]]); det(G); 1/G`
` `
` `
` `

#### Linear Algebra Library

The MuPAD `linalg` library contains the functions for handling linear algebraic operations. Using this library, you can perform a wide variety of computations on matrices and vectors. For example, to find the eigenvalues of the square matrices G, F, and (A*B), use the linalg::eigenvalue command:

```linalg::eigenvalues(G); linalg::eigenvalues(F); linalg::eigenvalues(A*B)```
` `
` `
` `

To see all the functions available in this library, enter `info(linalg)` in an input region. You can obtain detailed information about a specific function by entering `?functionname`. For example, to open the help page on the eigenvalue function, enter `?linalg::eigenvalues`.

### Solve Equations

#### Solve Equations with One Variable

To solve a simple algebraic equation with one variable, use the `solve` command:

`solve(x^5 + 3*x^4 - 23*x^3 - 51*x^2 + 94*x + 120 = 0, x)`
` `

#### Solving Equations with Parameters

You can solve an equation with symbolic parameters:

`solve(a*x^2 + b*x + c = 0, x)`
` `

If you want to get the solution for particular values of the parameters, use the `assuming` command. For example, you can solve the following equation assuming that `a` is positive:

`solve(a*x^2 + b*x + c = 0, x) assuming a > 0`
` `

#### Solve Systems of Equations

You can solve a system of equations:

`solve([x^2 + x*y + y^2 = 1, x^2 - y^2 = 0], [x, y])`
` `

or you can solve a system of equations containing symbolic parameters:

`solve([x^2 + y^2 = a, x^2 - y^2 = b], [x, y])`
` `

#### Solve Ordinary Differential Equations

You can solve different types of ordinary differential equations:

```o := ode(x^2*diff(y(x), x, x) + 2*x*diff(y(x), x) + x, y(x)): solve(o)```
` `

#### Solve Inequalities

Also, you can solve inequalities:

`solve(x^4 >= 5, x)`
` `

If you want to get the result over the field of real numbers only, `assume` that x is a real number:

`assume(x in R_); solve(x^4 >= 5, x)`
` `

You can pick the solutions that are positive:

`solve(x^4 >= 5, x) assuming x > 0`
` `

### Manipulate Expressions

#### Transform and Simplify Polynomial Expressions

There are several ways to present a polynomial expression. The standard polynomial form is a sum of monomials. To get this form of a polynomial expression, use the `expand` command:

```expand((x - 1)*(x + 1)*(x^2 + x + 1)* (x^2 + 1)*(x^2 - x + 1)*(x^4 - x^2 + 1))```
` `

You can factor this expression using the `factor` command:

`factor(x^12 - 1)`
` `

For multivariable expressions, you can specify a variable and collect the terms with the same powers in this variable:

```collect((x - a)^4 + a*x^3 + b^2*x + b*x + 10*a^4 + (b + a*x)^2, x)```
` `

For rational expressions, you can use the `partfrac` command to present the expression as a sum of fractions (partial fraction decomposition). For example:

`partfrac((7*x^2 + 7*x + 6)/(x^3 + 2*x^2 + 2*x + 1))`
` `

MuPAD also provides two general simplification functions: `simplify` and `Simplify`. The `simplify` function is faster and it can handle most of the elementary expressions:

```simplify((x - 1)*(x + 1)*(x^2 + x + 1)*(x^2 + 1)* (x^2 - x + 1)*(x^4 - x^2 + 1))```
` `

The `Simplify` function searches for simpler results deeper than the `simplify` function. The more extensive search makes this function slower than `simplify`. The `Simplify` function allows you to extend the simplification rule set with your own rules and serves better for transforming more complex expressions. For the elementary expressions it gives the same result as simplify:

```Simplify((x - 1)*(x + 1)*(x^2 + x + 1)*(x^2 + 1)* (x^2 - x + 1)*(x^4 - x^2 + 1))```
` `

For the following expression the two simplification functions give different forms of the same mathematical expression:

```f := exp(wrightOmega(-ln(3/5)))*exp(ln(5) - ln(3)): simplify(f); Simplify(f)```
` `
` `

Note that there is no universal simplification strategy, because the meaning of the simplest representation of a symbolic expression cannot be defined clearly. Different problems require different forms of the same mathematical expression. You can use the general simplification functions `simplify` and `Simplify` to check if they give a simpler form of the expression you use.

#### Transform and Simplify Trigonometric Expressions

You also can transform and simplify trigonometric expressions. The functions for manipulating trigonometric expressions are the same as for polynomial expressions. For example, to expand a trigonometric expression, use the `expand` command:

`expand(sin(5*x))`
` `

To factor the trigonometric expression, use the `factor` command:

```factor(cos(x)^4 + 4*cos(x)^3*sin(x) + 6*cos(x)^2*sin(x)^2 + 4*cos(x)*sin(x)^3 + sin(x)^4)```
` `

You can use the general simplification functions on trigonometric expressions:

`simplify(cos(x)^2 + sin(x)^2)`
` `
`simplify(cos(x)^4 + sin(x)^4 + sin(x)*cos(x))`
` `
`Simplify(cos(x)^4 + sin(x)^4 + sin(x)*cos(x))`
` `

### Use Assumptions in Your Computations

#### Solve Expressions with Assumptions

By default, all variables in MuPAD represent complex numbers. When solving equations or simplifying expressions, the software considers all possible cases for complex numbers. If you are solving an equation or simplifying an expression, this default assumption leads to the exact and complete set of results including complex solutions:

`solve(x^(5/2) = 1, x)`
` `

To obtain real solutions only, pass the assumption to MuPAD using the `assuming` command:

`solve(x^(5/2) = 1, x) assuming x in R_`
` `

You can make various assumptions on the values that a variable represents. For example, you can solve an equation assuming that the variable x represents only positive values:

`solve(x^4 - 1 = 0, x) assuming x > 0`
` `

You can make multiple assumptions:

`solve(x^4 - a = 0, x) assuming a = 16 and x in R_`
` `

#### Integrate with Assumptions

You can use assumptions when integrating mathematical expressions. For example, without an assumption on the variable `x`, the following integral depends on the sign of the expression x2 - 1:

`int(1/abs(x^2 - 1), x)`
` `

If you know that x > 1, you can pass the assumption to the integral:

`int(1/abs(x^2 - 1), x) assuming x > 1`
` `

#### Simplify Expressions with Assumptions

Using assumptions along with the simplification functions narrows down the possible values that variables represent and can provide much shorter results than the simplification functions alone. For example:

```simplify(sqrt(x^2 + 2*x + 1) + sqrt(x^2 - 2*x + 1) + sqrt(x^2 + 4*x + 4) + sqrt(x^2 - 4*x + 4))```
` `

versus

```simplify(sqrt(x^2 + 2*x + 1) + sqrt(x^2 - 2*x + 1) + sqrt(x^2 + 4*x + 4) + sqrt(x^2 - 4*x + 4)) assuming x > 2```
` `

You can pass assumptions to the following functions: `expand`, `simplify`, `limit`, `solve`, and `int`. The `Simplify` function does not allow assumptions on variables.

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos