Appendix E
Symbolic Computations with MATLAB

E.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 follows:

>>a=sym('a'); x=sym('x'); y=sym('y'); t=sym('t'); n=sym('n');
>>syms a x y t n %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) % easy plot of f for [-π,+π]
>>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
>>simplify(cos(x)^2-sin(x)^2) % simple expression
   ans= cos(2*x)
>>simplify(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 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)^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 + 3 y) y + (-1 + 3 y + x) x) x

If you need to substitute numeric values or other expressions for some symbolic variables in an expression or take the limit of an expression, you can use subs( ) and limit( ) as follows:

>>subs(eq1,x,0) % substitute numeric value x=0 into eq1=(x+y-1)*(x+y)^2
    ans= -y^2+y^3
>>subs(eq1,{x,y},{0,x-1}) % substitute numeric values x=0 and y=x-1
    ans= (x-1)^3-(x-1)^2
>>limit((1+x/n)^n,n,inf) % images
    ans= exp(x)

E.2 Solving Algebraic Equations

We can use the backslash() operator to solve a set of linear equations written in a matrix‐vector form.

>>syms R11 R12 R21 R22 b1 b2
>>R=[R11 R12; R21 R22]; b=[b1; b2];
>>x=R % or R^-1*b or inv(R)*b
  x= [ (R12*b2-b1*R22)/(-R11*R22+R21*R12)]
     [ (-R11*b2+R21*b1)/(-R11*R22+R21*R12)]

MATLAB has many commands and functions that can be very helpful in dealing with complex analytic(symbolic) expressions and equations as well as in getting numerical solutions. One of them is ‘solve()’, which can be used for obtaining the symbolic or numeric roots of equations. According to what we could see by typing ‘help solve’ into the MATLAB Command Window, its usages are as follows:

>>syms a b c x
>>fx=a*x^2+b*x+c;
>>solve(fx) % Formula for roots of 2nd-order polynomial eq
   ans= [ 1/2/a*(-b+(b^2-4*a*c)^(1/2))]
        [ 1/2/a*(-b-(b^2-4*a*c)^(1/2))]
>>syms x1 x2 b1 b2
>>fx1=x1+x2-b1; fx2=x1+2*x2-b2; % a system of simultaneous algebraic eq.
>>[x1o,x2o]=solve(fx1,fx2) %
   x1o= 2*b1-b2
   x2o= -b1+b2
>>solve('p*sin(x)=r') %regarding x as an unknown variable and p as a   parameter
   ans = asin(r/p) %sin−1(r/p)
>>[x1,x2]=solve('x1^2+4*x2^2-5=0','2*x1^2-2*x1-3*x2-2.5=0')
   x1 = [                  2.]  x2 = [                0.500000]
        [           -1.206459]       [                0.941336]
        [0.603229 -0.392630*i]       [-1.095668 -0.540415e-1*i]
        [0.603229 +0.392630*i]       [-1.095668 +0.540415e-1*i]
>>S=solve('x^3-y^3=2','x=y') %returns the solution in a structure.
  S = x: [3x1 sym]
  y: [3x1 sym]
>>S.x
  ans = [                  1]
        [ -1/2+1/2*i*3^(1/2)]
        [ -1/2-1/2*i*3^(1/2)]
>>S.y
  ans = [                -1]
        [ 1/2-1/2*i*3^(1/2)]
        [ 1/2+1/2*i*3^(1/2)]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.122.11