Sie sind auf Seite 1von 5

29-01-2011 MATLAB TUTORIAL - by T.

Nguyen: Sym…

SYMBOLIC CALCULATION

Based on Maple kernel, symbolic Math Toolbox performs calculation symbolically in


Matlab environment. The following examples introduce some basic operations
available in Basic Symbolic Math toolbox version 2.1.3.

Example 1: Simplifying an expression.

Simplify

> - In Matlab command w indow , we w ill first need to define alpha as a symbolic
expression.
M
E >> alpha = sym('alpha')
N alternately, you may also enter:
U
>> syms alpha
>
The commands "sym" and "syms" are Matlab's reserved w ords. When "syms" is
used by itself at the command prompt, all defined symbolic values will be listed.

- Next, we w ill enter the expression of z.

>> z = sin(alpha)^2 + cos(alpha)^2;

Note that ";" is used to suppress echo.

>> simplify(z)

Matlab will yield "1", as expected:

ans =

You may also specify the format of the output in symbolic calculation by adding the
option as show n in the example below .

>> syms rho


>> rho=0.25
>> sym(rho,'r')

Matlab returns:

ans =

1/4

'r' stands for rational form. Similarly, you may use 'e', 'd' format. Please refer to
Matlab's help files or click here for more info on format.

Example 2: Derivative.

W e wish to take the derivative of function f(x):

Matlab command entries:


edu.levitas.net/Tutorials/…/symbolic.html 1/5
29-01-2011 MATLAB TUTORIAL - by T. Nguyen: Sym…
>> syms x
>> f=x^3-cos(x);
>> g=diff(f)

Matlab returns:

g=

3*x^2+sin(x)

Note that the command "diff" w as used to obtain the derivative of function f.

Since function f has only one independent variable, the diff command performed the
calculation based on x. If there are more than one independent variable in a function,
you should include the "intended" variable in the follow ing format:

diff(f, x)

w here x is the "intended" variable. For example,

W e wish to obtain the derivative of the following function

Matlab command entries:

>> syms x y
>> f=x^2+(y+5)^3;
>> diff(f,y)

Matlab returns:

ans =

3*(y+5)^2

Note that in this case, the command diff(f,y) is equivalent to

Example 3: Integral

To integrate function f(x,y) as shown in Example 2, w e w ill use the command "int" as
show n below.

>> int(f,x)

Matlab returns:

ans =

1/3*x^3+(y+5)^3*x

The syntax of the integral command can be view ed by typing >> help int in Matlab
command w indow.

If we w ish to perform the following definite integral:

Matlab command entry:

>> int(f,y,0,10)

Matlab returns:

ans =

12500+10*x^2
edu.levitas.net/Tutorials/…/symbolic.html 2/5
29-01-2011 MATLAB TUTORIAL - by T. Nguyen: Sym…

Example 4: Finding roots.

Consider the following polynomial:

Suppose w e wish to find the roots of this polynomial. In Matlab Command window:

>> syms x
>> f=2*x^2 + 4*x -8;
>> solve(f,x)

Matlab returns:

ans =

5^(1/2)-1
-1-5^(1/2)

Alternately, you may use the following lines in Matlab to perform the same
calculation:

>> f=[2 4 -8];


>> roots(f)

Matlab returns:

ans =

-3.2361
1.2361

Note that the results from both approaches are the same.

Example 5: Matrix Symbolic Calculation

This example demonstrates how Matlab handles matrix calculation symbolically.

First w e need to define the symbolic variables:

>> syms a b c d e f g h

Matrix A is then defined as:

>> A=[a b; c d]

Matlab's echo:

A=

[ a, b]
[ c, d]

Next, matrix B is defined as:

>> B=[e f;g h]

Matlab's echo:

B=

[ e, f]
[ g, h]

The addition of these tw o matrices yields:

>> C=A+B

C=

[ a+e, b+f]
[ c+g, d+h]
edu.levitas.net/Tutorials/…/symbolic.html 3/5
29-01-2011 MATLAB TUTORIAL - by T. Nguyen: Sym…
and the product of A and B is:

>> D=A*B

D=

[ a*e+b*g, a*f+b*h]
[ c*e+d*g, c*f+d*h]

If we w ish to evaluate a specific matrix numerically, we simply assign the numeric


values to the appropriate variable then use the command eval as demonstrate
below.

>> a=1;b=2;c=3;d=4;e=5;f=6;e=7;f=8;g=9;h=0;

>> eval(A)

ans =

12
34

>> eval(B)

ans =

78
90

>> eval(C)

ans =

8 10
12 4

The inverse of A can be expressed symbolically:

>> D=inv(A)

D=

[ d/(a*d-b*c), -b/(a*d-b*c)]
[ -c/(a*d-b*c), a/(a*d-b*c)]

Numerically, D is expressed by

>> Dn=eval(inv(A))

Dn =

-2.0000 1.0000
1.5000 -0.5000

As a verification, w e may evaluate D directly:

>> De=eval(D)

De =

-2.0000 1.0000
1.5000 -0.5000

You may also try

>> Df=inv(eval(A))

to verify if you get the same result.

___________________________

© T. Nguye n MMIII, MMIV, MMV. All rights re se rve d. Use rs m a y not downloa d a nd print e x tra cts of
conte nt from this we bsite without writte n perm ission from the a uthor. Re publica tion or redistribution
of the conte nt in this site , including by fra m ing or sim ilar m e a ns, is e x pre ssly prohibite d without

edu.levitas.net/Tutorials/…/symbolic.html 4/5
29-01-2011 MATLAB TUTORIAL - by T. Nguyen: Sym…
the prior writte n conse nt of the a uthor.

edu.levitas.net/Tutorials/…/symbolic.html 5/5

Das könnte Ihnen auch gefallen