Professional Documents
Culture Documents
SYMBOLIC CALCULATION
Simplify
The commands "sym" and "syms" are Matlab's reserved words. When "syms" is used
by itself at the command prompt, all defined symbolic values will be listed.
>> simplify(z)
ans =
You may also specify the format of the output in symbolic calculation by adding the
option as shown in the example below.
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.
http://edu.levitas.net/Tutorials/Matlab/symbolic.html 16/11/2010
MATLAB TUTORIAL - by T. Nguyen: Symbolic Calculation Page 2 of 5
Example 2: Derivative.
>> syms x
>> f=x^3-cos(x);
>> g=diff(f)
Matlab returns:
g=
3*x^2+sin(x)
Note that the command "diff" was 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 following format:
> diff(f, x)
>
Matlab command entries:
>> syms x y
>> f=x^2+(y+5)^3;
>> diff(f,y)
Matlab returns:
ans =
3*(y+5)^2
Example 3: Integral
To integrate function f(x,y) as shown in Example 2, we will use the command "int" as
shown below.
>> int(f,x)
Matlab returns:
ans =
http://edu.levitas.net/Tutorials/Matlab/symbolic.html 16/11/2010
MATLAB TUTORIAL - by T. Nguyen: Symbolic Calculation Page 3 of 5
1/3*x^3+(y+5)^3*x
The syntax of the integral command can be viewed by typing >> help int in Matlab
command window.
>> int(f,y,0,10)
Matlab returns:
ans =
12500+10*x^2
Suppose we 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:
Matlab returns:
ans =
-3.2361
1.2361
Note that the results from both approaches are the same.
>> syms a b c d e f g h
http://edu.levitas.net/Tutorials/Matlab/symbolic.html 16/11/2010
MATLAB TUTORIAL - by T. Nguyen: Symbolic Calculation Page 4 of 5
>> A=[a b; c d]
Matlab's echo:
A=
[ a, b]
[ c, d]
Matlab's echo:
B=
[ e, f]
[ g, h]
>> C=A+B
C=
[ a+e, b+f]
[ c+g, d+h]
>> D=A*B
D=
[ a*e+b*g, a*f+b*h]
[ c*e+d*g, c*f+d*h]
>> 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
http://edu.levitas.net/Tutorials/Matlab/symbolic.html 16/11/2010
MATLAB TUTORIAL - by T. Nguyen: Symbolic Calculation Page 5 of 5
>> 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
>> De=eval(D)
De =
-2.0000 1.0000
1.5000 -0.5000
>> Df=inv(eval(A))
___________________________
© T. Nguyen MMIII, MMIV, MMV. All rights reserved. Users may not download and print extracts of content
from this website without written permission from the author. Republication or redistribution of the content in
this site, including by framing or similar means, is expressly prohibited without the prior written consent of
the author.
http://edu.levitas.net/Tutorials/Matlab/symbolic.html 16/11/2010