Matlab's Symbolic Toolbox: Symbolic Objects, Calculus, and Linear Algebra, Study notes of Mathematical Methods for Numerical Analysis and Optimization

An overview of matlab's symbolic toolbox, which allows users to create symbolic objects, perform symbolic operations such as differentiation, integration, and analytical solution techniques for equations, and work with symbolic matrices for calculus and linear algebra. Creating symbolic objects using the sym function and syms command, calculus functions like diff, int, taylor, limit, and jacobian, and linear algebra functions like diag, triu, tril, inv, det, rank, rref, null, colspace, eig, svd, jordan, poly, and expm.

Typology: Study notes

Pre 2010

Uploaded on 09/17/2009

koofers-user-xwd-1
koofers-user-xwd-1 🇺🇸

10 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 6
Computer Algebra
6.1 Matlab’s Symbolic Toolbox
Matlab’s Symbolic Toolbox is a collection of functions that provide access
to some of Maple’s functionality. Using this toolbox, one can create sym
objects, which represent expressions, and perform symbolic operations on
them, such as differentiation, integration, or analytical solution techniques
for equations.
6.1.1 Creating Symbolic objects
The sym function creates a new Matlab object that represents an expression,
similar to an expression in Maple. It accepts as an argument a string repre-
senting the expression, and returns an ob ject whose value is that expression.
An optional second argument can be used to set properties of the ex-
pression, such as whether the expression is assumed to be a real number, or
a positive number.
Instead of a string, one may pass a matrix to sym, in which case a sym-
bolic representation of the matrix is created. An optional second argument
indicates how the floating-point entries are to be represented symbolically.
The argument ’f’ is used to create a floating-point representation, ’r’ for a
rational representation (the default), ’d’ for decimal, and ’e’ for “estimate
error”, which implies ’r’ but also provides an estimate of the error in the
rational approximation.
>> myexpr=sym(’x’)
myexpr =
201
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Matlab's Symbolic Toolbox: Symbolic Objects, Calculus, and Linear Algebra and more Study notes Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

Chapter 6

Computer Algebra

6.1 Matlab’s Symbolic Toolbox

Matlab’s Symbolic Toolbox is a collection of functions that provide access to some of Maple’s functionality.objects, which represent expressions, and perform symbolic operations on Using this toolbox, one can create sym them, such as differentiation, integration, or analytical solution techniquesfor equations.

6.1.1 Creating Symbolic objects

The sym function creates a new Matlab object that represents an expression, similar to an expression in Maple. It accepts as an argument a string repre-senting the expression, and returns an object whose value is that expression.

pression, such as whether the expression is assumed to be a real number, orAn optional second argument can be used to set properties of the ex- a positive number. bolic representation of the matrix is created. An optional second argumentInstead of a string, one may pass a matrix to^ sym, in which case a sym- indicates how the floating-point entries are to be represented symbolically.The argument ’f’ is used to create a floating-point representation, ’r’ for a rational representation (the default),error”, which implies ’r’ but also provides an estimate of the error in the ’d’ for decimal, and ’e’ for “estimate rational approximation.

myexpr=sym(’x’) myexpr =

x

myexpr2=sym(’x^2’) myexpr2 = x^ sym([ 1/3 ]) ans = 1/ sym([ 1/3 ],’f’) ans = ’1.5555555555555’*2^(-2) sym([ 1/3 ],’d’) ans = . sym([ 1/3 ],’e’) ans = 1/3-eps/

The syms command is used as a shortcut for creating several symbolic variables. The arguments to syms are each passed to sym. For each argu- ment, a new object is created with the same name as the argument, and itsvalue equal to the symbolic variable of that name. syms cannot be used to create symbolic objects representing arbitrary expressions.ments, syms lists all symbolic variables. With no argu-

limit(1/x,x,0,’left’) ans = -inf syms y; jacobian([ exp(xy) x^2y ],[x y]) ans = [ yexp(xy), xexp(xy)][ 2xy, x^2]

6.1.3 Linear Algebra

The following functions, many of which are familiar, work with symbolic matrices.

  • diag creates a symbolic diagonal matrix, or extracts a diagonal of a symbolic matrix
  • triu returns the upper triangular part of a symbolic matrix.
  • tril returns the lower triangular part of a symbolic matrix.
  • inv computes the inverse of a symbolic matrix
  • det computes the determinant of a symbolic matrix.
  • rank computes the rank of a symbolic matrix.
  • rref computes the reduced row echelonn form of a symbolic matrix.
  • null computes a basis for null space of a symbolic matrix A, i.e. the set of vectors {x} for which Ax = 0.
  • colspacevectors {y (^) }computes a basis for the column space of for which y = Ax for some vector x. A, i.e. the set of
  • eigtrix. computes the eigenvalues and/or eigenvectors of a symbolic ma-
  • svdSingular vectors can only be computed for matrices with numeric val- computes the singular value decomposition of a symbolic matrix. ues.
  • jordan computes the Jordan canonical form of a symbolic matrix.
  • polyi.e. det( computes the characteristic polynomial of a symbolic matrixλI − A). A,
  • expmexp(A computes the). matrix exponential of a symbolic matrix A, i.e.

6.1.4 Simplification of Expressions

The following functions may be used to rearrange expressions, with the goal of simplifying them. They accept an expression or a matrix of expressions.

  • simplify uses a variety of techniques to try to reduce the size of an expression.
  • expand rewrites an expression as a sum of terms, distributing whenever possible.
  • collect rewrites an expression as a polynomial in given variables.
  • simple not only simplifies an expression, but indicates the sequence of steps used to simplify it.
  • numden converts an expression to a rational form.
  • hornersentation. converts a symbolic polynomial to its nested (Horner) repre-
  • subexpr rewrites an expression in terms of common subexpressions.
  • subs substitutes variables in an expression with values obtained from the workspace where subs is called.

simplify(cos(x)^2+sin(x)^2) ans = 1

S=solve(’u+v=1’,’u-v=0’,’u’,’v’) S = u: [1x1 sym]v: [1x1 sym]

S.u ans = 1/ dsolve(’D2y=-y’,’y(0)=1’,’Dy(0)=1’) ans = cos(t)+sin(t) compose(exp(x),sin(x)) ans = exp(sin(x)) finverse(exp(x)) ans = log(x)

6.1.6 Variable Precision Arithmetic

Matlab can use Maple’s variable precision arithmetic.uates each element of its argument using variable precision floating point vpa numerically eval- arithmetic, withreturned by digits D decimal digits of accuracy, where. digits, with no arguments, returns the current preci- D is the current value sion. It accepts an integer argument, which becomes the new precision.

digits

Digits = 32

vpa(1/3) ans = . digits(16);>> vpa(1/3)

ans = .

6.1.7 Data Conversion

Often, it is desirable to convert between data used in symbolic operations and data used with Matlab’s own functions.

  • doublemust not contain any symbolic variables except for converts a symbolic matrix to an ordinary Matlab matrix. It ’eps’.
  • poly2sym and sym2poly convert between a symbolic polynomial in a given variable (default ’x’) to Matlab’s representation of a polyno- mial, a row vector of the coefficients.
  • char converts a symbolic expression to a string.

6.1.8 Basic Operations on Symbolic Objects

  • findsym returns a list of independent variables in a given expression.
  • pretty formats an expression for more readable display
  • latex, ccode, and fortran return LaTeX, C, and FORTRAN repre- sentations, respectively, of a given expression.