8.1 Introduction

Until now, we have been dealing with numeric computation where variables store numeric values. In Chapter 7, you learned that numerical computation involves working with approximate solutions. On the other hand, an analytical solution is not an approximation since one uses symbols rather than numbers. MATLAB provides the means to perform symbolic computations, too.

8.2 Defining a Symbolic Variable

The keyword syms is used to define single or multiple symbolic variable(s). The key feature of a symbolic variable is that it just stores a symbol to perform symbolic calculations.

1  >> syms x y z 2  >>

After executing the command, inspect the Workspace window (see Figure 8-1) and note that three new variables—x, y, and z have been created.

Figure 8-1
figure 1

New symbolic variables appearing in the workspace

8.3 Defining a Symbolic Equation

Once the variables have been defined, you can define an equation:

$$ z={x}^2+y $$
(Equation 8-1)

using these variables as follows:

1  >> z = xˆ2+y 2  z = 3  xˆ2 + y

In the present example, z was predefined as a symbolic variable. The output variable is created by MATLAB and becomes a symbolic variable by default. Its inputs have been defined as symbolic variables. For example, suppose you want to define this equation:

$$ a={x}^3+{y}^2+z $$
(Equation 8-2)

This results in the creation of a new symbolic variable (which can be verified by checking the Workspace window). The following MATLAB code performs this task:

1  >> syms x y z 2  >> a = xˆ3+yˆ2+z 3  a = 4  xˆ3 + yˆ2 + z

8.4 Performing Symbolic Computations

Symbolic computations are same as what we are used to doing by hand on paper. You define a variable and use mathematical rules of algebra as well as calculus to perform calculations. For example, two roots (r 1 and r 2) of a quadratic equation :

$$ y=a{x}^2+ bx+c $$
(Equation 8-3)

can be written as follows:

$$ {r}_1=\frac{-b+\sqrt{b^2-4 ac}}{2a} $$
(Equation 8-4)
$$ {r}_1=\frac{-b-\sqrt{b^2-4 ac}}{2a} $$
(Equation 8-5)

This can be performed using the following MATLAB code :

1  >> syms a b c x 2  >> y = a*(xˆ2)+(b*x)+c 3  y = 4  a*xˆ2 + b*x + c 5  >> solve(y) 6  ans = 7  −(b + (bˆ2 − 4*a*c)ˆ(1/2))/(2*a) 8  −(b − (bˆ2 − 4*a*c)ˆ(1/2))/(2*a)

Similarly, a symbolic mathematical expression can be integrated and differentiated as follows:

 1  >> syms a b c x  2  >> y = a*(xˆ2)+(b*x)+c  3  y =  4  a*xˆ2 + b*x + c  5  >> int(y)  6  ans =  7  (a*xˆ3)/3 + (b*xˆ2)/2 + c*x  8  >> diff(y)  9  ans = 10  b + 2*a*x

This can be verified using paper-based calculation by hand, where we know the following:

$$ y=a{x}^2+ bx+c $$
(Equation 8-6)
$$ \int y=\frac{a{x}^3}{3}+\frac{b{x}^2}{2}+ cx $$
(Equation 8-7)
$$ \frac{dy}{dx}=2 ax+b $$
(Equation 8-8)

8.4.1 Arithmetic Expressions

Simple arithmetic expressions can be dealt with using symbols. For example, two polynomials can be used to define a new polynomial.

 1  >> syms x y z  2  >> a1 = xˆ2+2*y+z  3  a1 =  4  xˆ2 + 2*y + z  5  >> a2 = xˆ(−2)−2*y+3*z  6  a2 =  7  3*z − 2*y + 1/xˆ2  8  >> a3 = a1/a2  9  a3 = 10  (xˆ2 + 2*y + z)/(3*z − 2*y + 1/xˆ2) 11 >> a4 = a1*a2 12  a4 = 13  (xˆ2 + 2*y + z)*(3*z − 2*y+1/xˆ2)

8.4.2 Trigonometric Expressions

Trigonometric variables defined using symbolic variables can also be used in mathematical calculations, as follows:

$$ y=\sin (x) $$
(Equation 8-9)
$$ \frac{dy}{dx}=-\cos (x) $$
(Equation 8-10)

1  >> syms a b c x 2  >> y = sin(x) 3  y = 4  sin(x) 5  >> int(y) 6  ans = 7  −cos(x)

Even more complicated calculations can be performed by a click of a button.

1  >> z = cos(xˆ(1/2)) − (sin(y))ˆ(1/3) 2  z = 3  cos(xˆ(1/2)) − sin(y)ˆ(1/3) 4  >> int(z) 5  ans = 6  2*cos(xˆ(1/2)) − x*sin(y)ˆ(1/3) + 2*xˆ(1/2)*sin(xˆ(1/2)) 7  >> diff(z) 8  ans = 9  −sin(xˆ(1/2))/(2*xˆ(1/2))

8.4.3 Expanding and Factorizing an Expression

The expand() function can be used to write equations with individual terms of expanded polynomials. The most important use of expand() is the application of the distributivity law to rewrite products of sums as sums of products. If f represents a symbolic expression, then expand(f) is calculated using the following set of rules:

  • $$ {x}^{a+b}={x}^a\times {x}^b $$
  • $$ {(xy)}^b={x}^b\times {y}^b\forall x,y\ge 0,b\in I $$
  • $$ {\left({x}^a\right)}^b={x}^{a+b} $$

It is also important to note that the expand() function will work recursively on the subexpressions of a given expression.

 1  >> syms x y z  2  >> a1 = xˆ2+2*y+z  3  a1 =  4  xˆ2 + 2*y + z  5  >> a2 = xˆ(−2)−2*y+3*z  6  a2 =  7  3*z − 2*y + 1/xˆ2  8  >> a3 = a1/a2  9  a3 = 10  (xˆ2 + 2*y + z)/(3*z − 2*y + 1/xˆ2) 11  >> a4 = a1*a2 12  a4 = 13  (xˆ2 + 2*y + z)*(3*z − 2*y + 1/xˆ2) 14  >> a5 = expand(a3) 15  a5 = 16  (2*y)/(3*z − 2*y + 1/xˆ2) + z/(3*z − 2*y + 1/xˆ2) + xˆ2/(3*z − 2*y + 1/xˆ2) 17  >> a6 = expand(a4) 18  a6 = 19  4*y*z + (2*y)/xˆ2 − 2*xˆ2*y + z/xˆ2 + 3*xˆ2*z − 4*yˆ2 + 3*zˆ2 + 1

The function named factor produces factors of an expression such that multiplying all factors results in the final expression. Let’s try to factorize the values stored in symbolic variable a5 and a6.

1  >> a7 = factor(a5) 2  a7 = 3  [−1,x,x, xˆ2 + 2*y + z, −1/(3*xˆ2*z − 2*xˆ2*y + 1)] 4  >> a8 = factor(a5) 5  a8 = 6  [−1,x,x, xˆ2 + 2*y + z, −1/(3*xˆ2*z − 2*xˆ2*y + 1)]

The factors are present as elements of an array, which can be accessed using their index. This comes in handy when extracting a factor and its usage in mathematical analysis.

1  >> a7[2] = x 2  >> a7[4] = xˆ2 + 2*y + z 3  >> a8[3:5] = [x, xˆ2 + 2*y + z, −1/(3*xˆ2*z − 2*xˆ2*y + 1)]

In the previous example, a7[2] extracts the second element of variable a7, a7[4] extracts the fourth element of variable a7, and a8[3:5] extracts all elements from the third to fifth element and stores them as a list of symbolic expressions.

When an expression is written as a power of another expression, expand() works just like mathematical rules. For example, consider the case when an expression:

$$ a=x{y}^{\left(z+y\right)} $$

is defined. Its expansion is given as:

$$ x\times {y}^y\times {y}^z $$

Each term is clearly a factor of the expression. This can be verified with the following MATLAB code :

 1  >> syms x y z  2  >> a = x*yˆ(z+y)  3  a =  4  x*yˆ(y + z)  5  >> b = expand(a)  6  b =  7  x*yˆy*yˆz  8  >> c = factor(b)  9  c = 10  [x,yˆy,yˆz]

When an expression is powered by another expression, the expand() function works recursively.

1  >> a=((x+y)ˆ(x+z+2)) 2  a = 3  (x + y)ˆ(x + z + 2) 4  >> expand(a) 5  ans = 6  xˆ2*(x + y)ˆx*(x + y)ˆz + yˆ2*(x + y)ˆx*(x + y)ˆz + 2*x*y*(x + y)ˆx*    (x + y)ˆz

It can be used to check out trigonometric identities:

 1  >> expand(sin(x+y))  2  ans =  3  cos(x)*sin(y) + cos(y)*sin(x)  4  >> expand(cos(x+y))  5  ans =  6  cos(x)*cos(y) − sin(x)*sin(y)  7  >> expand(tan(x+y))  8  ans =  9  −(tan(x) + tan(y))/(tan(x)*tan(y) − 1) 10  >> expand(sec(x+y)) 11  ans = 12  1/(cos(x)*cos(y) − sin(x)*sin(y)) 13  >> a = cosh(x+y) 14  a = 15  cosh(x+y) 16  >> expand(a) 17  ans = 18  cosh(x)*cosh(y) + sinh(x)*sinh(y) 19  >> a = cosh(2*x) 20  a = 21  cosh(2*x) 22  >> expand(a) 23  ans = 24  2*cosh(x)ˆ2 − 1 25  >> a = coth(x+y) 26  a = 27  coth(x + y) 28  >> expand(a) 29  ans = 30  (coth(x)*coth(y) + 1)/(coth(x) + coth(y))

8.5 Summary

This chapter illustrated the usage of symbols to solve mathematical equations. Symbolic computation proves useful when error-prone numerical computing is not acceptable, but it has its limits. A limited set of built-in functions must be appended by user-defined functions, and this requires experience with writing MATLAB packages. But it is definitely worth exploring.

This book has illustrated the use of MATLAB as a tool for efficient scientific computing. It first illustrated the basic usage using single-line commands and then illustrated writing multi-line commands as an .m file. Arrays for the fundamental blocks of scientific computing and thus matrix-based calculations can be performed using arrays. Plotting graphs is simplified to the extent that even a beginner can easily plot a equation to visualize a graph. Using loops and functions, programs can be made modular and information flow can be controlled in an efficient fashion. You also saw some basic examples of numerical computing. The book should enable any beginner to enter the world of scientific computing with ease. Its widely popular usage has rightly coined the phrase “MATLAB is the language of engineering”.