# Symbolic Math in Matlab

Post on 07-Apr-2018

219 views

Embed Size (px)

TRANSCRIPT

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

Recommended

View more >