Page 492 - Applied Numerical Methods Using MATLAB
P. 492

APPENDIX G















                SYMBOLIC COMPUTATION













            G.1  HOW TO DECLARE SYMBOLIC VARIABLES AND HANDLE
            SYMBOLIC EXPRESSIONS

            To declare any variable(s) as a symbolic variable, you should use the sym or
            syms command as below.

            >>a = sym(’a’); t = sym(’t’); x = sym(’x’);
            >>symsaxyt %or, equivalently and more efficiently

              Once the variables have been declared as symbolic, they can be used in expres-
            sions 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 = 2*cos(x)^2-1
            >>simple(cos(x)^2 - sin(x)^2) %simple expression
              ans = cos(2*x)
            >>simple(cos(x) + i*sin(x)) %simple expression
              ans = exp(i*x)
            >>eq1 = expand((x + y)^3 - (x + y)^2) %expand
             eq1 = x^3 + 3*x^2*y + 3*x*y^2 + y^3 - x^2 - 2*x*y - y^2
            >>collect(eq1,y) %collect similar terms in descending order with respect to y
              ans = y^3 + (3*x - 1)*y^2 + (3*x^2 - 2*x)*y + x^3 - x^2

                                          
            Applied Numerical Methods Using MATLAB , by Yang, Cao, Chung, and Morris
            Copyright  2005 John Wiley & Sons, Inc., ISBN 0-471-69833-4
                                                                            481
   487   488   489   490   491   492   493   494   495   496   497