Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
201 views32 pages

004 MATLAB Symbolic Math DAY 3 1

1) Symbolic processing is the process of obtaining answers in mathematical expressions rather than numerical values. It allows computers to perform algebraic operations similarly to how humans work with algebra. 2) Symbolic objects in MATLAB represent symbolic variables, constants, expressions, and matrices that can be manipulated algebraically. Functions like sym(), syms(), and subs() are used to create, classify, and manipulate symbolic objects. 3) Functions like collect(), expand(), factor(), simple(), and solve() allow symbolic expressions to be simplified, expanded, factored, and have equations solved symbolically. Symbolic objects allow algebraic operations to be performed and solutions expressed without numerical approximations.

Uploaded by

Josh Hukshiam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
201 views32 pages

004 MATLAB Symbolic Math DAY 3 1

1) Symbolic processing is the process of obtaining answers in mathematical expressions rather than numerical values. It allows computers to perform algebraic operations similarly to how humans work with algebra. 2) Symbolic objects in MATLAB represent symbolic variables, constants, expressions, and matrices that can be manipulated algebraically. Functions like sym(), syms(), and subs() are used to create, classify, and manipulate symbolic objects. 3) Functions like collect(), expand(), factor(), simple(), and solve() allow symbolic expressions to be simplified, expanded, factored, and have equations solved symbolically. Symbolic objects allow algebraic operations to be performed and solutions expressed without numerical approximations.

Uploaded by

Josh Hukshiam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

SYMBOLIC PROCESSING

Symbolic Processing is the process of


obtaining answers in the form of expressions.
It is the term used to describe how a
computer performs operations or
mathematical expressions in the way, for
example that humans do algebra with pencil
and paper.
Symbolic Object
Symbolic object is a data structure that
stores a string representation of the symbol.
The two ways to create symbolic object are:
1.   Using sym function
Typing x = sym(‘x’) creates the symbolic
variable with name x.  
2.   Using syms command
syms x is equivalent to typing x = sym(‘x’).
Typing syms x y z creates three symbolic
variables x, y and z.
Symbolic objects can be classified as:
1.   symbolic variable (ex. x, y, z)
2.   symbolic constant (ex. pi, sqrt(3))
3.   symbolic expression (ex. x^2 + y^2)
4.   symbolic matrix (ex. [a, b, c; b, c, a; c, a, b]
To create symbolic constant, use the sym
function.
Example:
>> pi = sym(‘pi’)
>> fraction = sym(‘1/4’)
>> sqroot2 = sym(‘sqrt(2)’)
Using symbolic constant, it avoids the
floating-point approximation inherent in the
irrational constant. If you create these
symbolic constants, say , it temporarily
replaces the built-in numeric constant, and
you no longer obtain a numerical value when
you type its name. Its advantage is that they
need not be evaluated until a numeric answer
is required. For example,
>> pi = sym(‘pi’) % it outputs pi, not the
numerical value of  which is 3.1415
>> sqroot2 = sym(‘sqrt(2)’) % it outputs
sqrt(2) not the value of ____.
Type the following commands and
compare the result:
>> sq2 = sym(‘sqrt(2)’)
>> a = 3*sqrt(2)
>> b = 3*sq2
A symbolic variable containing a symbolic
expression is automatically created from a
symbolic expression whose variables were
previously created.
Example:
>> syms x y
>> a = x + y
>> b = sqrt(x^2 – y^2)
Symbolic variables a and b were created even
they were not created by sym or syms
commands.
Symbolic matrices can be created by defining
the elements as symbolic objects or defining
symbolic matrix variables with elements not
symbolic objects.
Example:
>> syms a b c
>> A = [a b c;b a c; c a b] or
>> syms A
>> A = [1, 2 3; 4, 5, 6]
In both cases, symbolic matrix A was created.
Functions for Manipulating Symbolic
Expression
collect(E) Collects coefficients of
like powers in the
expression E.
expand(E) Expands the expression
E by carrying out powers
factor(E) Factors the expression E.
horner(E) Transforms a symbolic
polynomial E into its
nested form.
poly2sym(E) Converts a polynomial
coefficient vector p to a
symbolic polynomial. The
form poly2sym(p, ‘v’)
generates the polynomial
in terms of the variable v.
pretty(E) Displays the expression E
on the screen in a form
that resembles typeset
mathematics.
simple(E) Searches for the shortest form
of the expression E in terms of
number of characters. When
called, the function displays the
results of each step of its
search. When called without
the argument, simple acts on
the previous expression. The
form [r,how] = simple(E) does
not display intermediate steps,
but saves those steps in the
string how. The shortest form
found is stored in r.
simplify(E) Simplifies the expression
E using Maple’s
simplification rules.
subs(E, old, new) Substitutes new for old in
the expression E, where
old can be a symbolic
variable or expression,
new can be a symbolic
variable, expression, or
matrix, or a numeric value
or matrix.
sym2poly(E) Converts the expression
E to a polynomial
coefficient vector.
Predefined Constants
MATLAB Description
pi the transcendental constant 
i or j the imaginary unit i which is equal to 1
 
 
Transcendental Functions
•Trigonometric Functions
sin(x), cos(x), tan(x), cot(x), sec(x), csc(x)

•Inverse Trigonometric Functions


asin(x), acos(x), atan(x), acot(x), asec(x), acsc(x)

•Exponential Function
MATLAB Mathematical Form
exp(x) ex

•Logarithmic Functions
MATLAB Mathematical Form
log(x) ln(x)
log10(x) log(x)
Note: For other log with bases other than e and 10, use the change of base formula.
Example:
log( x ) log10( x )
log3 ( x )  
log(3) log 10(3)
pretty(E)
>> f = x^3-6*x^2+11*x-6
>> g = (x-1)*(x-2)*(x-3)
>> pretty(f)
>> pretty(g)
collect(E)
Use collect function to simplify the
following expressions:
1. (x-1)(x-2)(x-3)
2. x[x(x-6)+11]-6
3. (1+x)t + xt
4. (x-5)2 + (y-3)2
In number 4, using the command collect(E)
where E is the given expression, variables x
are collected because the default independent
variable. However, y can be collected using
the optional form collect(E, ‘y’).
expand(E)
Use expand function to simplify the following
expressions:
1. a(x+y)
2. (x-2)(x-3)(x-5)
3. ea+b
4. cos(x+y)
5. cos(3 cos x)
6. (x2-15)(y3-2)
factor(E)
Factor the following expressions:
1. x3-6x2+11x-6
2. x3-6x2+11x-5
3. x6+1 
simple(E)
Use simple function to simplify the
following expressions:
1. cos2x + sin2x
2. 2 cos2x – sin2x
3. cos2x – sin2x
4. cos x + i sin x
5. cos(3 Cos-1x)
6. 1 6 12
3
3
 2
 8
a a a
The simple function has several forms,
each returning different output. The form
simple(E)
displays each trial simplification and the
simplification function that produced it in the
MATLAB command window. The simple
function then returns the shortest result. If
you are not interested in how simple achieve
its result, use the form
f = simple(E)
This form simply returns the shortest
expression form. If you want to know which
simplification returned the shortest result, use
the multiple output form
[f, how] = simple(E) 
This form returns the shortest result in the
first variable and the simplification method
used to achieve the result in the second
variable.
Evaluating Expressions

The function
double(E)
converts the expression E to numeric form.
The expression E must not contain any
symbolic variables. For example,
>> sqr2 = sym(‘sqrt(2)’);
>> z = 6*sqr2 % it outputs z = 6*2^(1/2)
>> double(z) % it outputs 8.4853
To replace a variable by a new one, use the
function
subs(E, old, new).
For example,
>> syms x y
>> exp1 = x^3+6*x-7;
>> exp2 = subs(exp1, x, y)% it substituted x
by y
To create a new function form a given
function, the function subs can also be used:
>> syms t
>> f = sym(‘f(t)’);
>> g = subs(f, t, t-2)-f % it outputs g = f(t-2)-
f(t)
Use the subs to evaluate an expression
numerically.
For example,
>> syms x
>> F = x^4-2*x^2+x-3
>> subs(F, x, 2)
Multiple substitutions
 You can use the subs() command to do
multiple substitutions in one command.
This is done by grouping the variables and
their substitutes (other variables or
numerical values) in braces.
subs(symbolic_function, {substitutant}, {substitute})

subs(quadratic, {a,b,c,x}, {1,2,3,4})

ans =
27
Sometimes, double is necessary to obtain
the numerical value of an expression. For
example,
>> syms x
>> y = sym(‘log(3)’)
>> z = exp(y) % it outputs z = eln 3
>> w = double(z) % it outputs w = 3
Converting Polynomial Expression to
Symbolic Expression and Vice-versa

The function poly2sym(p) converts a


coefficient vector p to a symbolic polynomial.
The form poly2sym(p, ‘v’) generates the
polynomial in terms of y. For example,
>> poly2sym([1 2 3]) % it outputs x2
+ 2x +3
>> poly2sym([1 2 3], ‘z’) % it outputs z2
+ 2z + 3
The function sym2poly(E) converts the
expression E to a polynomial coefficient
vector.
>> syms x
>> sym2poly(2*x^2-x+6) % it outputs [2
–1 6]
Solving Algebraic and Transcendental
Equations
solve(E)
Solves a symbolic expression or equation
represented by the expression E. If E
represents an equation, the equation’s
expression must be enclosed in single
quotes.
If E represents an expression, then the
solution obtained will be the roots of the
expression E; that is, the solution of the
equation E = 0. You need not declare the
symbolic variable with the sym or syms
function before using solve.
solve(E1, … En)
Solves multiple expressions or equations.
S = solve(E)
Saves the solution in the structure S.
To solve algebraic and transcendental
equation, we can use the function solve.
There are three ways an equation. For
example, to solve the equation 3x – 5 = 0,
Method 1: >> eq1 = ‘3*x – 5 = 0’;
>> solve(eq1)
Method 2: >> solve(‘3*x – 5 = 0’)
Method 3: >> syms x
>> solve(3*x+5)
Plotting symbolical functions
 Plotting symbolic functions in MATLAB is
done with the ezplot() set of commands.
 The syntax of ezplot() when y is a function
of x is:
ezplot(y) x2-2 x+3

y = sym('x^2 – 2*x + 3'); 50

ezplot(y) 40

30

20

10

0
-6 -4 -2 0 2 4 6
x
Differentiation
 MATLAB allows you to differentiate
symbolic functions with the diff() command.
 The syntax of diff(), when f is a symbolic
function of the variable x and n is the order
of the derivative to be determined, is:

diff(f,'x' ,n)

diff(y,'x',1)

ans = Try a second derivative. Your result


2*x-2 should be 2.
Integration
 MATLAB allows you to integrate symbolic
functions with the int() command. Either
definite integrals, with assigned numeric or
symbolic bounds, or indefinite integrals (note that
when you compute indefinite integrals, the constant of integration does not appear explicitly).

int(y,'x',a,b)

int(y,'x')

ans = Try a definite integral for 1<x<2.


1/3*x^3-x^2+3*x The result should be 7/3.
Integration constant
• If you want to display the integration
constant when applying the int() function,
you can do the following:
syms x y a
y = 2*x;
int(y,’a’,’x’)
ans =
x^2–a^2
Try to solve the following equations:
1.     e2x + 3ex = 54
2.     y2 + 3y + 2 = 0
3.     x2 + 9y4 = 0 % x is presumed to be
the unknown variable
In example 3, to solve for y use the format
solve(E, ‘y’) where E is the given equation. 
4.     b2 + 8c + 2b = 0, solve for c, then
solve for b.
5.     1  x 2  x
You can also solve systems of equation using
the format
[a, b, c, …] = solve(eq1, eq2, eq3, …)
6.  6x + 2y = 14
3x + 7y = 31
7.  x + 6y = a
2x – 3y = 9 [ express the answer in
terms of a]
8. 4 cos 1  3 cos( 1   2 )  6
4 sin 1  3 sin( 1   2 )  2

You might also like