Download - Symbolic Math in Matlab
-
8/4/2019 Symbolic Math in Matlab
1/2
R. Groff
ECE409
09/22/2009
Symbolic Math in Matlab
Matlab was designed for numerical computation, i.e. computation performed using standard arithmetic
operations (addition, subtraction, division, multiplication) on floating point representations of numbers,
with a focus on numerical linear algebra. In numerical mathematics, variable names always represent
specific numerical (floating point) values. Floating point numbers approximate real numbers, and roundoff errors may accumulate during computation.
The Symbolic Toolbox1 adds the ability to perform symbolic computation, in which a variable may truly
represent unknown values and arithmetic is performed with infinite precision, e.g. 1/3 and 2 are
represented as is rather than being replaced by a floating point approximation. Other popular symbolic
computational tools are Maple and Mathematica. In fact, the Matlab Symbolic Toolbox uses the Maple
kernel.
Declaring a Symbolic VariableTo declare a variable as symbolic in Matlab , use the symcommand, e.g.
w = sym('w');makes a symbolic variable called w. If you would like to declare several symbolic variables, thesyms
command may be used as shorthand:syms x y z a b s
declares x, y, z, a, b, an s as symbolic. The sym command may be used to provide further information
about constraints on the variables. For examplec = sym('c', 'real')k = sym('k', 'positive')
declares c as a real number and k to be positive. This information is used while simplifying expressions
involving these variables.
Working with Symbolic VariablesMany Matlab commands, such asdet, inv, and rref, are overloaded to work with symbolic
variables. The symbolic version of the command will automatically be used when the command is calledwith symbolic arguments, e.g.
det([w x; 3 y])
returnsw*y-3*x
the determinant of the specified matrix. When using help from the Matlab command line, e.g.help det
Matlab will by default provide help on the numerical version of the function. The help entry will also
indicate overloaded versions. The symbolic version is generally prepended by sym/ To get help on the
symbolic version, e.g.help sym/det
Symbolic variables may be used to define symbolic expressions. For examplef=x^2+3*x+4g=sin(x)+cos(w)^2M=[x y; z 1]
1 To check installed toolboxes, use the ver command. The standard Clemson install of Matlab includes the
symbolic toolbox.
-
8/4/2019 Symbolic Math in Matlab
2/2
R. Groff
ECE409
09/22/2009
Some Useful Symbolic Commands
diff symbolic differentiationdiff(f) % derivative of f with respect to a variablediff(g,x) % derivative specifying with respect to which variablegd=diff(g,w)
int symbolic integrationint(f) % indefinite integral of fint(g,w) %indefinite integral, specifying variable of integrationint(g,w,a,b) % definite int w/ respect to w, going from a to b
pretty print result in an easier to read formatpretty(f)
simplify simplify an expression. Try a limited number of methods to simply an expressionsimplify( (s^2+2*s+1)/(s+1) )
simple try a more extensive search to find the simplest expressionsimple(gd)
solve solve algebraic equations
solve('a*x^2+b*x+c=0',x) % solve equation for x[x,y] = solve('x^2+2*x+5+y^2=0','y^2+3*y-4+x^2=0')
% Simultaneously solve pair of equations for x and y
dsolve solve ordinary differential equations, using D to represent time differentiationdsolve('Dx = -a*x')dsolve('D2x = -a*x','x(0)=3,Dx(0)=4')
subs substitute symbolic expressions in other symbolic expressionssyms s M g L kdp=solve('a*s^2+b*s+c=0','s')subs(p,{a,b,c},{M*L^2,kd,M*g*L})
laplace Laplace transform of a function (assumes function is zero for t less than zero)syms a tlaplace(exp(a*t))
ilaplace inverse Laplace transform of a functionsyms silaplace( (3*s+1)/(s^2+2*s+5))
vpa, double commands for converting a symbolic expression (involving only numbers) into a numerical
value.
Partial fraction expansion The partial fraction expansion of a rational function F is given by diff(int(F)).
The symbolic toolbox does not include a command for directly determining the partial fraction expansion,
but this trick works because the partial fraction expansion is used to integrate a rational function.
diff(int( (s+2)/(s+4)/(s+5) ) )