# Appendix GSymbolic Computation

## Chapter Outline

## G.1 How to Declare Symbolic Variables and Handle Symbolic Expressions

To declare any variable(s) as a symbolic variable, you should use the command ‘
`sym`

’ or ‘
`syms`

’ as follows:

`>a=sym('a'); t=sym('t'); x=sym('x');`

`>syms a x y t % or, equivalently and more efficiently`

Once the variables have been declared as symbolic, they can be used in expressions and as arguments to many functions without being evaluated as numeric:

`>f=x^2/(1+tan(x)^2);`

`>ezplot(f,[-pi pi])`

`>simplify(cos(x)^2+sin(x)^2) % simplify an expression`

`ans = 1`

`>simplify(cos(x)^2-sin(x)^2) % simplify an expression`

`ans = cos(2*x)`

`>simplify(2*sin(x)*cos(x)) % simple expression`

`ans = sin(2*x)`

`>simplify(sin(x)*cos(y)+cos(x)*sin(y)) % simple expression`

`ans = sin(x+y)`

`>eq1=expand((x+y)^3-(x+y)^2) % expand`

`eq1 = x^3 + 3*x^2*y - x^2 + 3*x*y^2 - 2*x*y + y^3 - y^2`

`>collect(eq1,y) % collect similar terms in descending order w.r.t. y`

`ans = y^3 + (3*x-1)*y^2 + (3*x^2-2*x)*y + x^3 - x^2`

`>factor(eq1) % factorize`

`ans = [ x + y - 1, x + y, x + y]`

`>horner(eq1) % nested multiplication form`

`ans = x*(x*(x + 3*y - 1) - 2*y + 3*y^2) - y^2 + y^3`

`>pretty(ans) % pretty form`

`2 2 3`

`x (x (x ...`

Get *Applied Numerical Methods Using MATLAB, 2nd Edition* now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.