## Symbolic Objects to Represent Mathematical Objects

To solve mathematical problems with Symbolic Math Toolbox™, symbolic objects are defined to represent various mathematical objects. This example discusses the usage of symbolic numbers, variables, functions, expressions, vectors, and symbolic matrices to perform symbolic computations that solve mathematical problems.

### Symbolic Number

Use a symbolic number to represent the argument of an inverse trigonometric function $\theta ={\mathrm{sin}}^{-1}\left(1/\sqrt{2}\right)$. Defining $\text{1/}\sqrt{2}$ as a symbolic number instructs MATLAB® to treat the number as an exact form instead of a numeric approximation. Create the symbolic number $\text{1/}\sqrt{2}$ using `sym`, and assign it to `a`.

`a = 1/sqrt(sym(2))`
```a = 2^(1/2)/2```

Find the inverse sine of `a`. The result is the symbolic number `pi/4`.

`thetaSym = asin(a)`
```thetaSym = pi/4```

You can convert a symbolic number to variable-precision arithmetic by using `vpa`. The result is a decimal number with 32 significant digits.

`thetaVpa = vpa(thetaSym)`
```thetaVpa = 0.78539816339744830961566084581988```

To convert the symbolic number to numeric double data type, use `double`. For more information about whether to use numeric or symbolic arithmetic, see Choose Numeric or Symbolic Arithmetic.

`thetaDouble = double(thetaSym)`
```thetaDouble = 0.7854```

### Symbolic Variable, Function, and Expression

Use a symbolic variable, function, and expression to represent the quadratic function $f\left(x\right)={x}^{2}+x-2$. Defining variables, functions, and expressions as symbolic objects enables you to perform algebraic operations with those symbolic objects, including simplifying formulas and solving equations. Create a symbolic variable `x` using `syms`. For more information about whether to use `syms` or `sym`, see Choose syms or sym Function. Define a symbolic expression `x^2 + x - 2` to represent the right side of the quadratic equation, and assign it to `f(x)`. The identifier `f(x)` is a symbolic function that represents the quadratic function.

```syms x f(x) = x^2 + x - 2```
```f(x) = x^2 + x -2```

You can then evaluate the quadratic function by providing its input argument inside the parenthesis. For example, evaluate `f(2)`.

`fVal = f(2)`
```fVal = 4```

Next, solve the quadratic equation $f\left(x\right)=0$. Use `solve` to find the roots of the quadratic equation. `solve` returns the two solutions as a vector of two symbolic numbers.

`sols = solve(f)`
```sols = -2 1```

### Symbolic Equation

Use a symbolic equation to solve the trigonometric problem $2\mathrm{sin}\left(t\right)\mathrm{cos}\left(t\right)=1$. Create a symbolic function `g(t)` using `syms`. Assign the symbolic expression `2*sin(t)*cos(t)` to `g(t)`.

```syms g(t) g(t) = 2*sin(t)*cos(t)```
```g(t) = 2*cos(t)*sin(t)```
To define the equation, use the `==` operator and assign the expression `g(t) == 1` to `eqn`. The identifier `eqn` is a symbolic equation that represents the trigonometric problem.
`eqn = g(t) == 1`
```eqn = 2*cos(t)*sin(t) == 1```

Use `solve` to find the solution of the trigonometric problem.

`sol = solve(eqn)`
```sol = pi/4```

### Symbolic Vector and Matrix

Use a symbolic vector and matrix to represent and solve a system of linear equations.

`$\begin{array}{c}x+2y=u\\ 4x+5y=v\end{array}$`

You can cast the system of equations as a vector of two symbolic equations. You can also cast the system of equations as a matrix problem involving a symbolic matrix and a vector. Create two symbolic equations `eq1` and `eq2`. Combine the two equations into a vector of symbolic equations. For brevity, any vector of symbolic objects is called a symbolic vector and any matrix of symbolic objects is called a symbolic matrix.

```syms u v x y eq1 = x + 2*y == u; eq2 = 4*x + 5*y == v; eqns = [eq1, eq2]```
```eqns = [x + 2*y == u, 4*x + 5*y == v]```

Use `solve` to find the solutions of the system of equations `eqns`. `solve` returns a structure `S` with fields named after each of the variables in the equations. You can access the solutions using the dot notation `S.x` and `S.y`.

```S = solve(eqns); S.x```
```ans = (2*v)/3 - (5*u)/3```
`S.y`
```ans = (4*u)/3 - v/3```

Another alternative to solve the system of linear equations is to convert it to a matrix form. Use `equationsToMatrix` to convert the system of equations to a matrix form and assign the output to `A` and `b`. `A` is a symbolic matrix and `b` is a symbolic vector. Solve the matrix problem by using the matrix division \ operator.

`[A,b] = equationsToMatrix(eqns,x,y)`
```A = [1, 2] [4, 5] b = u v```
`sols = A\b`
```sols = (2*v)/3 - (5*u)/3 (4*u)/3 - v/3```

### Comparisons of Symbolic Objects

This table compares various symbolic objects that are available in Symbolic Math Toolbox.

Symbolic ObjectsExamples of MATLAB CommandsSize of Symbolic ObjectsData Type
symbolic number
```a = 1/sqrt(sym(2)) theta = asin(a)```
```a = 2^(1/2)/2 theta = pi/4```
`1`-by-`1``sym`
symbolic variable
`syms x y u v`
`1`-by-`1``sym`
symbolic function
```syms x f(x) = x^2 + x - 2 syms g(t) g(t) = 2*sin(t)*cos(t)```
```f(x) = x^2 + x - 2 g(t) = 2*cos(t)*sin(t)```
`1`-by-`1``symfun`
symbolic equation
```syms u v x y eq1 = x + 2*y == u eq2 = 4*x + 5*y == v```
```eq1 = x + 2*y == u eq2 = 4*x + 5*y == v```
`1`-by-`1``sym`
symbolic expression
```syms x expr = x^2 + x - 2 expr2 = 2*sin(x)*cos(x)```
```expr = x^2 + x - 2 expr2 = 2*cos(x)*sin(x)```
`1`-by-`1``sym`
symbolic vector
```syms u v b = [u v]```
```b = [u, v]```
`1`-by-`n` or `m`-by-`1``sym`
symbolic matrix
```syms A x y A = [x y; x*y y^2]```
```A = [ x, y] [x*y, y^2]```
`m`-by-`n``sym`
symbolic multidimensional array
```syms A [2 1 2] A```
```A(:,:,1) = A1_1 A2_1 A(:,:,2) = A1_2 A2_2```
`sz1`-by-`sz2`-...-`szn``sym`