Based on Maple kernel, symbolic Math Toolbox performs calculation symbolically in Matlab environment. The following examples introduce some basic operations available in Basic Symbolic Math toolbox version 2.1.3.
Example 1: Simplifying an expression.
- In Matlab command window, we will first need to define alpha as a symbolic expression.
>> alpha = sym('alpha')
alternately, you may also enter:
>> syms alpha
The commands "sym" and "syms" are Matlab's reserved words. When "syms" is used by itself at the command prompt, all defined symbolic values will be listed.
- Next, we will enter the expression of z.
>> z = sin(alpha)^2 + cos(alpha)^2;
Note that ";" is used to suppress echo.
Matlab will yield "1", as expected:
You may also specify the format of the output in symbolic calculation by adding the option as shown in the example below.
>> syms rho
'r' stands for rational form. Similarly, you may use 'e', 'd' format. Please refer to Matlab's help files or click here for more info on format.
Example 2: Derivative.
We wish to take the derivative of function f(x):
Matlab command entries:
>> syms x
Note that the command "diff" was used to obtain the derivative of function f.
Since function f has only one independent variable, the diff command performed the calculation based on x. If there are more than one independent variable in a function, you should include the "intended" variable in the following format:
where x is the "intended" variable. For example,
We wish to obtain the derivative of the following function
Matlab command entries:
>> syms x y
Note that in this case, the command diff(f,y) is equivalent to
Example 3: Integral
To integrate function f(x,y) as shown in Example 2, we will use the command "int" as shown below.
The syntax of the integral command can be viewed by typing >> help int in Matlab command window.
If we wish to perform the following definite integral:
Matlab command entry:
Example 4: Finding roots.
Consider the following polynomial:
Suppose we wish to find the roots of this polynomial. In Matlab Command window:
>> syms x
>> f=2*x^2 + 4*x -8;
Alternately, you may use the following lines in Matlab to perform the same calculation:
>> f=[2 4 -8];
Note that the results from both approaches are the same.
Example 5: Matrix Symbolic Calculation
This example demonstrates how Matlab handles matrix calculation symbolically.
First we need to define the symbolic variables:
>> syms a b c d e f g h
Matrix A is then defined as:
>> A=[a b; c d]
[ a, b]
[ c, d]
Next, matrix B is defined as:
>> B=[e f;g h]
[ e, f]
[ g, h]
The addition of these two matrices yields:
[ a+e, b+f]
[ c+g, d+h]
and the product of A and B is:
[ a*e+b*g, a*f+b*h]
[ c*e+d*g, c*f+d*h]
If we wish to evaluate a specific matrix numerically, we simply assign the numeric values to the appropriate variable then use the command eval as demonstrate below.
The inverse of A can be expressed symbolically:
[ d/(a*d-b*c), -b/(a*d-b*c)]
[ -c/(a*d-b*c), a/(a*d-b*c)]
Numerically, D is expressed by
As a verification, we may evaluate D directly:
You may also try
to verify if you get the same result.
|© T. Nguyen MMIII, MMIV, MMV. All rights reserved. Users may not download and print extracts of content from this website without written permission from the author. Republication or redistribution of the content in this site, including by framing or similar means, is expressly prohibited without the prior written consent of the author.|