**Symbolic Expressions and Algebra**

The sym function can be used to create “symbolic objects” in MATLAB. If the input argument to sym is a string, the result is a symbolic number or variable. If the input argument is a numeric scalar or matrix, the result is a symbolic representation of the given numeric values. For example, typing x = sym ( , x ‘ ) creates the

symbolic variable with name x, and typing y = sym ( ‘v ‘ ) creates a symbolic variable named y. Typing x = sym ( , x ‘ , ‘rea 1 ‘ ) tells MATLAB to assume that x is real. Typing x = sym ( ‘x’ , ‘unreal’) tells MATLAB to assume

that x is not real. The syms function enables you to combine more than one such statement

into a single statement. For example, typing syms x is equivalent to typing x = sym ( , x ‘ ) , and typing syms x y u v creates the four symbolic variables x, y, u, and v. When used without arguments, syms lists the symbolic objects in

the workspace. The syms command, however, cannot be used to create symbolic constants; you must use sym for this purpose. The ms command enables you to specify that certain variables are real.

For example, »syms x y real

You can use the sym function to create symbolic constants by using a SYMBOLIC numerical value for the argument. For example, typing pi = sym ( ‘pi’ ), CONSTANT fraction = sym(‘1!3’),andsqroot2 = sym(‘sqrt(2)’) create

symbolic constants that avoid the floating-point approximations inherent in the values of n, 1/3, and ../2. If you create the symbolic constant n this way, it temporarily replaces the built-in numeric constant, and you no longer obtain a numerical value when you type its name. For example,

»pi = sym( ‘pi’)

pi =

pi

»sqroot2 sym(‘sqrt(2) ‘)

sqroot2 =

sqrt(2)

»a = 3*sqrt(2) % This gives a numeric result.

a =

4.2426

»b = 3*sqroot2

b =

% This gives a symbolic result.

3*2″(1/2)

The advantage of using symbolic constants is that they need not be evaluated (with the accompanying round-off error) until a numeric answer is required. Symbolic constants can look like numbers but are actually symbolic expressions. Symbolic expressions can look like character strings but are a different

sort of quantity. You can use the c 1as s function to determine whether or not a

quantity is symbolic, numeric, or a character string. We will give examples of the c 1 ass function later.

The effect of round-off error needs to be considered when converting MATLAB floating-point values to symbolic constants in this way. You can use an optional second argument with the sym function to specify the technique for converting floating-point numbers. Refer to the online help for more information.

**Symbolic Expressions**

You can use symbolic variables in expressions and as arguments of functions. You use the operators + – * / A and the built-in functions just as you use them with numerical calculations. For example, typing

»syms x y

»s = x + Yi

»r = sqrt(x”2 + y”2)i

creates the symbolic variables sand r. The terms s = x + y and r = sqrt (x”2 + y”2) are examples of symbolic expressions. The variables s and r created this way are not the same as user-defined function files. That is, if

you later assign x and y numeric values, typing r will not cause MATLAB to evaluate the equation r = vix2 + y2. We will see later how to evaluate symbolic expressions numerically. The syms command enables you to specify that expressions have certain characteristics. For example, in the following session MATLAB will treat the

expression w as a nonnegative number:

»syms x y real

»w = x”2 + y”2i

To clear x of its real property, type syms x unreal. Note that typing clear x eliminates x from the workspace and does not make x a nonreal variable. The vector and matrix notation used in MATLAB also applies to symbolic

variables. For example, you can create a symbolic matrix A as follows:

1, 1, 1, 1]

1, x, x”2, x”3]

1, x”2, x”4, x”6]

1, x”3, x”6, x”9]

»n = 3i

»syms Xi

»A = x.”((O:n) ‘*(O:n))

A =

Note that it was not necessary to use sym or syms to declare A to be a symbolic variable beforehand. It is recognized as a symbolic variable because it is created with a symbolic expression.

In MATLAB the variable x is the default independent variable, but other DEFAULT variables can be specified to be the independent variable. It is important to VARIABLE know which variable is the independent variable in an expression. The function findsym (E) can be used to determine the symbolic variable used by MATLAB

in a particular expression E. The function findsym (E) finds the symbolic variables in a symbolic expression

or matrix, where E is a scalar or matrix symbolic expression, and returns a string containing all of the symbolic variables appearing in E. The variables are returned in alphabetical order and are separated by commas. If no symbolic variables are found, findsym returns the empty string. By contrast, the function findsym (E,n) returns the n symbolic variables in E closest to x, with the tie breaker going to the variable closer to z. The following

session shows some examples of its use: »syms b xl y »findsym(6*b+y)

ans =

b,y

»findsym(6*b+y+x) %Note: x has not been declared symbolic. ??? Undefined function or variable ‘x’.

»findsym(6*b+y,l) %Find the one variable closest to x

ans =

y

»findsym(6*b+y+xl,l) %Find the one variable closest to x

ans =

xl

indsym(6*b+y*i) %i is not symbolic

=

b, y

**Manipulating Expressions**

The following functions can be used to manipulate expressions by collecting coefficients oflike powers, expanding powers, and factoring expressions, for example. The function collect (E) collects coefficients of like powers in the expression E. If there is more than one variable, you can use the optional form

collect (E,v) ,which collects all the coefficients with the same power of v.

»syms x y

»E = (x-S)A2+(y-3)A2;

»collect (E)

ans =

xA2-l0*x+2S+(y-3)A2

»collect (E,y)

ans =

y 2-6+Y+y+(x-5) ^ 2+9

The function expand (E) expands the expression E by carrying out powers. For example,

»syms x y

»expand((x+y)A2) % applies algebra rules

ans =

xA2+2*x*y+yA2

»expand(sin(x+y» % applies trig identities

ans =

sin(x)*cos(y)+cos(x)*sin(y)

»simplify(6*((sin(x»A2+(cos(x»A2» % applies another trig identity

ans

6

The function factor (E) factors the expression E.For example,

»syms X Y

»factor(xA2-1)

ans =

(x-1)*(x+1)

The function simplify (E) simplifies the expression E, using Maple’s simplification rules. For example,

»syms x y

»simplify(x*sqrt(xA8*yA2»

ans =

x*(xA8*yA2)A(1/2)

The function simp 1e (E) searches for the shortest form of the expression E in terms of number of characters. When called, the function displays the results of each step of its search. When called without the argument, simple acts on the previous expression. The form [r, how] = simple (E) does not display intermediate steps, but saves those steps in the string how. The shortest form found is stored in r. You can use the operators + – * I and A with symbolic expressions to obtain new expressions. The following session illustrates how this is done.

»syms

»E1

»E2

»81 =

81 =

xA2+3+yA3

»82 = E1*E2

x Y

xA2+5;

yA3-2;

E1 + E2

% define two expressions

% add the expressions

% multiply the expressions

82 =

(xA2+5)*(yA3-2)

»expand(82) % expand the product

ans =

xA2*yA3-2*xA2+5*yA3-10

»E3 = xA3+2*xA2+5*x+10; % define a third expression

»83 = E3/E1 % divide two expressions

83 = (xA3+2*xA2+5*x+10)/(xA2+5)

»simplify(S3) % see if some terms cancel

ans

x+2

The function [num den] = nurnden (E) returns two symbolic expressions that represent the numerator nurnand denominator den for the rational representation of the expression E.

»syms x

»E1 = xA2+5;

»E4 = .1/(x+6);

»[num, den) = numden(E1+E4)

num

den

x+6

The function double (E) converts the expression E to numeric form. The expression E must not contain any symbolic variables. The term double stands for floating-point, double precision. For example,

»sqroot2 = sym(‘sqrt(2) ‘);

»y = 6*sqroot2

y

6*2A(1/2)

z = double(y)

z

8.4853

The function poly2syrn (p) converts a coefficient vector p to a symbolic polynomial. The form poly2sym (p, ,v ‘ ) generates the polynomial in terms of the variable v. For example,

»poly2sym([2,6,4])

ans =

2*XA2+6*x+4

»poly2sym([5,-3,7), ‘y’)

ans

5*y^2-3*y+7

The function sym2poly (E) converts the expression E to a polynomial coefficient vector.

»syms x

»sym2poly(9*xA2+4*x+6)

ans

946

The function pret ty (E) displays the expression E on the screen in a form that resembles typeset mathematics.

The function subs (E,old, new) substitutes new for old in the expression E,where old can be a symbolic variable or expression and new can be a symbolic variable, expression, or matrix, or a numeric value or matrix. For example.

»syms x y

»E xA2+6*x+7;

»F = subs(E,x,y)

F

yA2+6*y+7

If old and new are cell arrays of the same size, each element of old i replaced by the corresponding element of new. If E and old are scalars and new is an array or cell array, the scalars are expanded to produce an array result. If you want to tell MATLAB [hat f is a function of the variable t, type f = sym (,f (t) ,).Thereafter, f behaves like a function of t,and you can manipulate it with the toolbox commands. For example, to create a new function

get) = f(t + 2) – f(t), the session is

»syms t

»f sym(‘f(t) ‘);

»g = subs(f,t,t+2)-f

g =

f(t+2)-f(t)

Once a specific function is defined for f(t), the function get) will be available. We will use this technique with the Laplace transform in Section 10.5. MATLAB does not have a symbolic factorial function, but you can use

the sym and subs functions to access the Maple factorial function to compute

(n – I)! as follows:

»kfac = sym(‘k!’);

»syms k n

»E = subs(kfac,k,n-I)

E =

(n-I) !

»expand (El

To compute a numeric factorial, say, 5!, type factorial (5), or use the prod function and type prod ( 1 ! 5 ) .

To perform multiple substitutions, enclose the new and old elements in braces. For example, to substitute a = x and b = 2 into the expression E = a sin b, the session is

»syms a b x

»E a*sin(b);

»F = subs (E, {a, b}, {x, 2})

x*sin(2)

**Evaluating Expressions**

In most applications we eventually want to obtain numerical values or a plot from the symbolic expression. Use the subs and double functions to evaluate an expression numerically. Use subs (E, old, new) to replace old with a numeric ·alue new in the expression E. The result is of class double. For example,

»syms X

»E x”2+6*x+7;

»G = subs(E,x,2)

23

»class·(G)

ans

double

Sometimes MATLAB will display all zeros as the result of evaluating an expression, whereas in fact the value can be nonzero but so small that you need to evaluate the expression with more accuracy to see that it is nonzero. You can use the di g its and the vpa functions to change the number of digits MATLAB uses

for calculating and evaluating expressions. The accuracy of individual arithmetic operations in MATLAB is 16 digits, whereas symbolic operations can be carried out to an arbitrary number of digits. The default is 32 digits. Type di g its (d) to change the number of digits used to d. Be aware that larger values of d will

require more time and computer memory to perform operations. Type vpa (E) to compute the expression E to the number of digits specified by the default value of 32 orthe current setting of digi ts. Type vpa (E, d) to compute the expression E using d digits. (The abbreviation vpa stands for “variable precision arithmetic.”)

**Plotting Expressions**

The MATLAB function e zp 1 ot (E) generates a plot of a symbolic expression E, which is a function of one variable. The default range of the independent variable is the interval [- 2rr, 2rr] unless this interval contains a singularity. The optional form ezplot (E, [xmin xmax]) generates a plot over the range from xmin

to xmax. Of course, you can enhance the plot generated by ezplot by using the plot format commands discussed in Chapter 5; for example, the axi s,xlabel, and y 1 abe 1 commands.

For example,

»syms x

»E = xA2-6*x+7;

»ezplot (E, [-2 6])

The plot is shown in Figure 10.1-1. Note that the expression is automatically placed at the top of the plot and that the axis label for the independent variable is automatically placed. You can use other plot-enhancement functions with ezplot to modify its appearance. For example, sometimes the automatic selection

of the ordinate scale is not satisfactory. To obtain an ordinate scale from -5 to 25 and to place a label on the ordinate, you would type »ezplot(E),axis([-2 6 -5 25]) ,ylabel(‘E’)

**Order of Precedence**

MATLAB does not always arrange expressions in a form that we normally would

use. For example, MATLAB might provide an answer in the form – c +b, whereas

we would normally write b- c. The order of precedence used by MATLAB must be constantly kept in mind to avoid misinterpreting the MATLAB output (see pages 9 and 10for the order of precedence). MATLAB frequently expresses results in the form 1/ a *b, whereas we would normally write b/ a. MATLAB sometimes

writes x ” (1/2) =v: (1/2) instead of grouping the terms as (x*y) ‘” (1/2) , and often fails to cancel negative signs where possible, as in -a/ (-b*c-d), instead of a/ (b*c+d). Forexample,

»syrns X

»E x”‘2-6*x+7

»F = -E/3

-1/3*x”‘2+2*x-7/3

The answer is F = -( I/3)x2 +2x – 7/3, which we would normally express

as F = _(x2 – 6x +7)/3.

Tables 10.1-1 and 10.1-2 summarize the functions for creating, evaluating, and manipulating symbolic

expressions.

**Test Your Understanding**

110.1-1 Given the expressions: E1 = x3 – 15×2 + 75x – 125 and E2 =

(x +5f – 20x, use MATLAB to

a. Find the product E 1E2 and express it in its simplest form.

b. Find the quotient E 1/E2 and express it in its simplest form.

c. Evaluate the sum E 1+E2 at x = 7.1 in symbolic form and in numeri

form.

(Answers: a. (x – 5)5; b. x – 5; c. 13,671/1000 in symbolic form,

13.6710 in numeric form.)