Symbolic Math Toolbox
 Go to function:
  Search    Help Desk 
sym See Also

Construct symbolic numbers, variables and objects.

Syntax

Description

S = sym(A) constructs an object S, of class 'sym', from A. If the input argument is a string, the result is a symbolic number or variable. If the input argument is a numeric scalar or matrix, the result is a symbolic representation of the given numeric values.

x = sym('x') creates the symbolic variable with name 'x' and stores the result in x. x = sym('x','real') also assumes that x is real, so that conj(x) is equal to x. alpha = sym('alpha') and r = sym('Rho','real') are other examples. x = sym('x','unreal') makes x a purely formal variable with no additional properties (i.e., ensures that x is not real). See also the reference pages on syms.

Statements like pi = sym('pi') and delta = sym('1/10') create symbolic numbers that avoid the floating-point approximations inherent in the values of pi and 1/10. The pi created in this way temporarily replaces the built-in numeric function with the same name.

S = sym(A,flag) converts a numeric scalar or matrix to symbolic form. The technique for converting floating-point numbers is specified by the optional second argument, which can be 'f', 'r', 'e' or 'd'. The default is 'r'.

'f' stands for "floating-point." All values are represented in the form '1.F'*2^(e) or '-1.F'*2^(e) where F is a string of 13 hexadecimal digits and e is an integer. This captures the floating-point values exactly, but may not be convenient for subsequent manipulation. For example, sym(1/10,'f') is '1.999999999999a'*2^(-4) because 1/10 cannot be represented exactly in floating-point.

'r' stands for "rational." Floating-point numbers obtained by evaluating expressions of the form p/q, p*pi/q, sqrt(p), 2^q, and 10^q for modest sized integers p and q are converted to the corresponding symbolic form. This effectively compensates for the roundoff error involved in the original evaluation, but may not represent the floating-point value precisely. If no simple rational approximation can be found, an expression of the form p*2^q with large integers p and q reproduces the floating-point value exactly. For example, sym(4/3,'r') is '4/3', but sym(1+sqrt(5),'r') is 7286977268806824*2^(-51)

'e' stands for "estimate error." The 'r' form is supplemented by a term involving the variable 'eps', which estimates the difference between the theoretical rational expression and its actual floating-point value. For example, sym(3*pi/4) is 3*pi/4-103*eps/249.

'd' stands for "decimal." The number of digits is taken from the current setting of digits used by vpa. Fewer than 16 digits loses some accuracy, while more than 16 digits may not be warranted. For example, with digits(10),
sym(4/3,'d') is 1.333333333, while with digits digits(20), sym(4/3,'d') is 1.3333333333333332593, which does not end in a string of 3's, but is an accurate decimal representation of the floating-point number nearest to 4/3.

See Also

digits, double, syms

eps in the online MATLAB Function Reference



[ Previous | Help Desk | Next ]