Page 493 - Applied Numerical Methods Using MATLAB
P. 493
482 SYMBOLIC COMPUTATION
>>factor(eq1) %factorize
ans=(x+y- 1)*(x + y)^2
>>horner(eq1) %nested multiplication form
ans = (-1 + y)*y^2 + ((- 2 + 3*y)*y + (-1 + 3*y + x)*x)*x
>>pretty(ans) %pretty form
2
(-1+y)y +((-2+3y)y+ (-1+3y+x)x)x
If you need to substitute numeric values or other expressions for some sym-
bolic variables in an expression, you can use the subs function as below.
>>subs(eq1,x,0) %substitute numeric value
ans = -y^2 + y^3
>>subs(eq1,{x,y},{0,x - 1}) %substitute numeric values
ans = (x - 1)^3 - (x - 1)^2
The sym command allows you to declare symbolic real variables by using the
‘real’ option as illustrated below.
>>x = sym(’x’,’real’); y = sym(’y’,’real’);
>>syms x y real %or, equivalently
>>z=x+ i*y; %declare z as a symbolic complex variable
>>conj(z) %complex conjugate
ans=x-i*y
>>abs(z)
ans = (x^2 + y^2)^(1/2) %equivalently
The sym function can be used to convert numeric values into their symbolic
expressions.
>>sym(1/2) + 0.2
ans = 7/10 %symbolic expression
On the other hand, the double command converts symbolic expressions into
their numeric (double-precision floating-point) values and the vpa command finds
the variable-precision arithmetic (VPA) expression (as a symbolic representation)
of a numeric or symbolic expression with d significant decimal digits, where d
is the current setting of DIGITS that can be set by the digits command. Note
that the output of the vpa command is a symbolic expression even if it may look
like a numeric value. Let us see some examples.
>>f = sym(’exp(i*pi/4)’)
f = exp(i*pi/4)
>>double(f)
ans = 0.7071 + 0.7071i %numeric value
>>vpa(ans,2)
ans = .71 + .71*i %symbolic expression with 2 significant digits

