symbolic math toolbox
Post on 10-Jan-2016
67 Views
Preview:
DESCRIPTION
TRANSCRIPT
S. Awad, Ph.D.
M. Corless, M.S.E.E.
E.C.E. Department
University of Michigan
Math Review with Matlab:
Fundamentals
Symbolic Math Toolbox
U of M-Dearborn ECE DepartmentMath Review with Matlab
2
Symbolic Toolbox: Fundamentals
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
U of M-Dearborn ECE DepartmentMath Review with Matlab
3
Symbolic Toolbox: Fundamentals
Defining Symbolic Variables Use sym to create a
symbolic variable x:
» x=sym('x');
Use syms to create several symbolic variables at one time
Use who to view al variables in the workspace
» who
Your variables are:
a b x y
» syms y a b
U of M-Dearborn ECE DepartmentMath Review with Matlab
4
Symbolic Toolbox: Fundamentals
Use whos to view all workspace variables with their associated size, bytes, and class information
Viewing Workspace Variables
» 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
U of M-Dearborn ECE DepartmentMath Review with Matlab
5
Symbolic Toolbox: Fundamentals
Symbolic Expressions Symbolic Expressions:
Symbolic and Numerical Conversions to perform a mathematical operation and create a new symbolic variable delta:
» f = 2*x^2 + x + 1;» g = a*x^2 + b*x + 5g =a*x^2+b*x+5
» delta = sym('1+sqrt(2)/2');» f = delta^2 + delta;f =(1+1/2*2^(1/2))^2+1+1/2*2^(1/2)
U of M-Dearborn ECE DepartmentMath Review with Matlab
6
Symbolic Toolbox: Fundamentals
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
Numerical Representation
'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)
U of M-Dearborn ECE DepartmentMath Review with Matlab
7
Symbolic Toolbox: Fundamentals
» rho=(1+sqrt(5)/2)
» rho_float = sym(rho,'f')rho_float = '1.0f1bbcdcbfa54'*2^(1)
» rho_decimal = sym(rho,'d')rho_decimal =2.1180339887498949025257388711907
Symbolic Representation Example
Symbolic
Variables
rho = 2.1180
Double-Precision
Floating Point
Variable
U of M-Dearborn ECE DepartmentMath Review with Matlab
8
Symbolic Toolbox: Fundamentals
digits, by itself, displays the current accuracy (default = 32 digits)
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
U of M-Dearborn ECE DepartmentMath Review with Matlab
9
Symbolic Toolbox: Fundamentals
Digits Example» digitsDigits = 32» rho=(1+sqrt(5)/2);» rho_decimal = sym(rho,'d')
Default Precision (32 Digits)
Adjusted Precision (7 Digits)
» digits(7)» rho_decimal_7=sym(rho,'d')
rho_decimal_7 =2.118034
rho_decimal =2.1180339887498949025257388711907
U of M-Dearborn ECE DepartmentMath Review with Matlab
10
Symbolic Toolbox: Fundamentals
» x=sym(3);y=sym(4);» z_sym = x/yz_sym =3/4 » z_float = double(z_sym)z_float = 0.7500
Double Command The double command coverts a symbolic variable to a
general Matlab double floating point number
Symbolic Variable
Double Float Variable
U of M-Dearborn ECE DepartmentMath Review with Matlab
11
Symbolic Toolbox: Fundamentals
Declaring Real Variables To declare real symbolic variables:
Or use shorthand notation:
» x = sym('x','real');» y = sym('y','real');
» syms x y real» whoYour variables are:x y
U of M-Dearborn ECE DepartmentMath Review with Matlab
12
Symbolic Toolbox: Fundamentals
Declaring Complex Variables
To construct a complex number use i or j to represent the imaginary part
Use real to find the real part
» syms x y
» z=x+i*y; % or z=x+j*yz=x+i*y
Use imag to find the imaginary part
» z_real = real(z)z_real =x
» z_imag = imag(z) z_imag = y
U of M-Dearborn ECE DepartmentMath Review with Matlab
13
Symbolic Toolbox: Fundamentals
Unreal The 'unreal' argument to sym can be used to convert
a real variable to a purely formal variable with no additional properties
» x=sym('x','real');» conj(x)ans =x
If x is real, the complex conjugate of x will be x
If x is unreal, the complex conjugate of can not be further simplified
» x=sym('x','unreal');» conj(x)ans =conj(x)
U of M-Dearborn ECE DepartmentMath Review with Matlab
14
Symbolic Toolbox: Fundamentals
Abstract functions are useful for solving algebraic and differential equations
Abstract Functions A symbolic variable can represent an abstract
function: f=sym('f(x)')where the input argument is a string
» f=sym('2*x+2') f =2*x+2
U of M-Dearborn ECE DepartmentMath Review with Matlab
15
Symbolic Toolbox: Fundamentals
Abstract Function Example Find the
determinant and inverse of the matrix z:
c
b
a
z
00
00
00
» syms a b c» z=[ a 0 0; 0 b 0; 0 0 c]z =[ 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]
U of M-Dearborn ECE DepartmentMath Review with Matlab
16
Symbolic Toolbox: Fundamentals
c
b
a
00
00
00
Matrix Manipulation Example
Change the first element of the matrix from a to g:
g
» z(1,1)='g' z = [ g, 0, 0][ 0, b, 0][ 0, 0, c]
U of M-Dearborn ECE DepartmentMath Review with Matlab
17
Symbolic Toolbox: Fundamentals
Summary Matlab can be used to create and manipulate symbolic
variables and expressions
Symbolic variables representing numbers can be displayed with adjustable accuracy
Symbolic variables can be declared as real, complex, or converted to the default unreal state
Abstract functions can be created and manipulated symbolically
The double command converts symbolic variables into Matlab double precision floating point variables
top related