Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Math Review with Matlab: Symbolic Math Toolbox Fundamentals S. Awad, Ph.D. M. Corless, M.S.E.E. E.C.E. Department University of Michigan Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Fundamentals of Matlab’s Symbolic Toolbox Creating Symbolic Variables Defining Symbolic Expressions Defining Numerical Representation Converting Symbolic Variables to Doubles Creating Real Symbolic Variables Creating Complex Symbolic Variables Manipulating Abstract Functions 2 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Defining Symbolic Variables Use sym to create a symbolic variable x: Use syms to create several symbolic variables at one time Use who to view al variables in the workspace » x=sym('x'); » syms y a b » who Your variables are: a b x y 3 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Viewing Workspace Variables Use whos to view all workspace variables with their associated size, bytes, and class information » n=1.0;t=[1.1 2.2 3.3]; » whos Name Size Bytes Class a 1x1 126 sym object b 1x1 126 sym object n 1x1 8 double array t 1x3 24 double array x 1x1 126 sym object y 1x1 126 sym object Grand total is 12 elements using 536 bytes 4 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Symbolic Expressions Symbolic Expressions: » f = 2*x^2 + x + 1; » g = a*x^2 + b*x + 5 g = a*x^2+b*x+5 Symbolic and Numerical Conversions to perform a mathematical operation and create a new symbolic variable delta: » delta = sym('1+sqrt(2)/2'); » f = delta^2 + delta; f = (1+1/2*2^(1/2))^2+1+1/2*2^(1/2) 5 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Numerical Representation The command sym(A,flag) converts a numeric scalar or matrix, A, to symbolic form The flag argument specifies the technique for converting floating point numbers 'f' Exactly represents Floating Point values 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 form may not be convenient for subsequent manipulation) 'd' Represents Decimal numbers where the number of digits is taken from the current setting of DIGITS (described later) 6 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Symbolic Representation Example » rho=(1+sqrt(5)/2) rho = 2.1180 Double-Precision Floating Point Variable » rho_float = sym(rho,'f') rho_float = '1.0f1bbcdcbfa54'*2^(1) Symbolic Variables » rho_decimal = sym(rho,'d') rho_decimal = 2.1180339887498949025257388711907 7 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Digits Command The digits command is used to set the number of digits of accuracy used for future numeric computations on symbolic variables digits(n) sets accuracy to n digits for subsequent calculations. Where n represents an integer digits, by itself, displays the current accuracy (default = 32 digits) 8 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Digits Example » digits Default Precision (32 Digits) Digits = 32 » rho=(1+sqrt(5)/2); » rho_decimal = sym(rho,'d') rho_decimal = 2.1180339887498949025257388711907 » digits(7) » rho_decimal_7=sym(rho,'d') rho_decimal_7 = 2.118034 Adjusted Precision (7 Digits) 9 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Double Command The double command coverts a symbolic variable to a general Matlab double floating point number » x=sym(3);y=sym(4); » z_sym = x/y z_sym = Symbolic Variable 3/4 » z_float = double(z_sym) z_float = Double Float Variable 0.7500 10 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Declaring Real Variables To declare real symbolic variables: » x = sym('x','real'); » y = sym('y','real'); Or use shorthand notation: » syms x y real » who Your variables are: x y 11 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Declaring Complex Variables To construct a complex number use i or j to represent the imaginary part » syms x y » z=x+i*y; % or z=x+j*y z= x+i*y Use real to find the real part » z_real = real(z) z_real = x Use imag to find the imaginary part » z_imag = imag(z) z_imag = y 12 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Unreal The 'unreal' argument to sym can be used to convert a real variable to a purely formal variable with no additional properties If x is real, the complex conjugate of x will be x » x=sym('x','real'); » conj(x) ans = x If x is unreal, the complex conjugate of can not be further simplified » x=sym('x','unreal'); » conj(x) ans = conj(x) 13 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Abstract Functions A symbolic variable can represent an abstract function: f=sym('f(x)')where the input argument is a string Abstract functions are useful for solving algebraic and differential equations » f=sym('2*x+2') f = 2*x+2 14 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Abstract Function Example Find the determinant and inverse of the matrix z: a 0 0 z 0 b 0 0 0 c » » z [ [ [ syms a b c z=[ a 0 0; 0 b 0; 0 0 c] = a, 0, 0] 0, b, 0] 0, 0, c] » determinant = det(z) determinant = a*b*c » inverse = inv(z) inverse = [ 1/a, 0, 0] [ 0, 1/b, 0] [ 0, 0, 1/c] 15 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Matrix Manipulation Example Change the first element of the matrix from a to g: ag 0 0 0 b 0 0 0 c » z(1,1)='g' z = [ g, 0, 0] [ 0, b, 0] [ 0, 0, c] 16 Symbolic Toolbox: Fundamentals Math Review with Matlab U of M-Dearborn ECE Department Summary Matlab can be used to create and manipulate symbolic variables and expressions Symbolic variables representing numbers can be displayed with adjustable accuracy The double command converts symbolic variables into Matlab double precision floating point variables Symbolic variables can be declared as real, complex, or converted to the default unreal state Abstract functions can be created and manipulated symbolically 17