Вы находитесь на странице: 1из 2

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 round off 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 Variable


To declare a variable as symbolic in Matlab , use the sym command, e.g. w = sym('w'); makes a symbolic variable called w. If you would like to declare several symbolic variables, the syms 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 example c = 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 Variables


Many Matlab commands, such as det, inv, and rref, are overloaded to work with symbolic variables. The symbolic version of the command will automatically be used when the command is called with symbolic arguments, e.g. det([w x; 3 y]) returns w*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 example f=x^2+3*x+4 g=sin(x)+cos(w)^2 M=[x y; z 1]

To check installed toolboxes, use the ver command. The standard Clemson install of Matlab includes the symbolic toolbox.
R. Groff ECE409 09/22/2009

Some Useful Symbolic Commands diff symbolic differentiation diff(f) % derivative of f with respect to a variable diff(g,x) % derivative specifying with respect to which variable gd=diff(g,w) int symbolic integration int(f) % indefinite integral of f int(g,w) %indefinite integral, specifying variable of integration int(g,w,a,b) % definite int w/ respect to w, going from a to b pretty print result in an easier to read format pretty(f) simplify simplify an expression. Try a limited number of methods to simply an expression simplify( (s^2+2*s+1)/(s+1) ) simple try a more extensive search to find the simplest expression simple(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 differentiation dsolve('Dx = -a*x') dsolve('D2x = -a*x','x(0)=3,Dx(0)=4') subs substitute symbolic expressions in other symbolic expressions syms s M g L kd p=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 t laplace(exp(a*t)) ilaplace inverse Laplace transform of a function syms s ilaplace( (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) ) )

R. Groff ECE409 09/22/2009

Вам также может понравиться