0 ratings 0% found this document useful (0 votes) 8 views 20 pages Notes - 4928 MATLAB Unit 4
The document discusses the use of symbolic math in MATLAB, highlighting the difference between numerical and symbolic operations. It explains the Symbolic Math Toolbox, how to create symbolic objects and expressions, and the commands available for manipulating these expressions. Additionally, it covers converting symbolic expressions to numerical form and various commands for simplifying and changing the form of symbolic expressions.
AI-enhanced title and description
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here .
Available Formats
Download as PDF or read online on Scribd
Go to previous items Go to next items
Save Notes_4928 MATLAB Unit 4 For Later
SYMBOLIC MATH:
All of the mathematical operations done with MATLAB so far were numerical. The
operations were carried out by writing numerical expressions that could contain numbers
and variables with preassigned numerical values. When a numerical expression is executed
by MATLAB, the outcome is also numerical (a single number or an array with numbers). The
number, or numbers, are either exact or a floating point-approximated value.
For example, typing 1/4 gives 0.2500—an exact value, and typing 1/3 gives 0.3333—an
approximated value.
Many applications in math, science, and engineering require symbolic operations ,that is,
mathematical operations with expressions that contain symbolic variables (variables that
don’t have spe:
ic numerical values when the operation is executed).
The result of such operations is also a mathematical expression in terms of the symbolic
variables
For Example:
1. solving an algebraic equation that contains several variables and solving for one
variable in terms of the others.
Ifa, b, and x are symbolic variables and ax-b=0, x can be solved in terms of a and b to give
xeb/a
2. the derivative of 2t*3+5t-8 with respect tot is 6t%2+5.
Also, MATLAB has the capability of carrying out many types of symbolic operations. The
numerical part of the symbolic operation is carried out by MATLAB exactly, with no
approximation of numerical values.
For example, the result of adding x/4 and x/3 is 7x/12 and not 0.5833x
NOTE: symbolic operations can be performed by MATLAB once the Symbolic Math
Toolbox is installed.
SYMBOLIC MATH TOOLBOX:
The Symbolic Math Toolbox is a collection of MATLAB functions that are used for execution
of symbolic operations. The commands and functions for the symbolic operations have the
same style and syntax as those for the numerical operations.
The symbolic operations themselves are executed primarily by MuPad®, which is
mathematical software designed for this purpose.
(The Symbolic Math Toolbox is included in the student version of MATLAB. In the standard
version, the toolbox is purchased separately)To check if the Symbolic Math Toolbox is installed on a computer, the user can type the
command ver in the Command Window. In response, MATLAB displays information about
the version that is used as well as a list of the toolboxes that are installed.
SYMBOLIC OBJECTS:
Symbolic objects are made of variables and numbers that, when used in mathematical
expressions, tell MATLAB to execute the expression symbolically.
‘A symbolic object can be a variable (without a preassigned numerical value), a number, or
an expression made of symbolic variables and numbers.
Typically, the user first defines (creates) the symbolic variables (objects) that are needed,
and then uses them to create symbolic expressions that are subsequently used in symbolic
operations. If needed, symbolic expressions can be used in numerical operations .
Creating Symbolic Objects
Symbolic objects can be variables or numbers.
They can be created with the sym and/or syms commands,
A single symbolic object can be created with the sym command:
object_name = sym('string’)
where the string, which is the symbolic object, is assigned to a name.
The string can be:
* Asingle letter or a combination of several letters (no spaces).
Examples: ‘a’,’x’,’xy’
* Acombination of letters and digits starting with a letter and with no spaces
Examples: ‘xh12’,‘r2d2’
* Anumber.
Examples: ‘15’, ‘4
In the first two examples(where the string is a single letter, a combination of several
letters, or a combination of letters and digits), the symbolic object is a symbolic
variable.
In this case, it is convenient (but not necessary) to give the object the same name
as the string.
For example, a, bb, and x, can be defined as symbolic variables
>> a=sym('a')
a=
a
>> b=sym('b')
b
bThe name of the symbolic object can be different from the name of the variable.
For example:
>> cesym('d!)
c=
d
>> desym(‘e')
d=
e
Symbolic objects can also be numbers.
The numbers don’t have to be typed as strings.
For example, the sym command is used next to create symbolic objects from the
numbers 5 and 7 and assign them to the variables c and d respectively
>> cesym(5)
c=
5
>> d=sym(7)
d
7
Note that the display of symbolic objects starts at the beginning of the line and is
not indented as is the display of numerical variables,
For example: numercal variable c=5 is displayed as
>>e=5
Creating several symbolic variables
Several symbolic variables can be created in one command by using the syms
‘command, which has the form
syms variable_name variable_name variable_name
The command creates symbolic objects that have the same names as the symbolic
variables,
For example, the variables y, z, and d can all be created as symbolic variables in one
‘command by typing:
>> symsyzd
>y
ye
yNOTE:
The variables created by the syms command are not displayed automatically(even if
a semicolon is not typed at the end of the command). Typing the name of the
variable shows that the variable was created.
SYMBOLIC EXPRESSIONS:
A symbolic expression is a mathematical expression containing one or more
symbolic objects. When typed, a symbolic expression may look like a standard
numerical expression
However, because the expression contains symbolic objects, i
MATLAB symbolically.
s executed by
Creating Symbolic Expression:
‘Symbolic expressions are mathematical expressions written in terms of symbolic
variables. Once symbolic variables are created, they can be used for creating
symbolic expressions. The symbolic expression is a symbolic object (the display is
not indented).
The form for creating a symbolic expression is:
Expression_name = Mathematical expression
EXAMPLE:
>> syms xy zt
>> faxtytztt*2
fe
xeyez4tn2 Sédisplay is not indented
Here x,y,z,t are defined as symbolic variables.
‘An expression x+y+z#t*2 is created with these variables and assigned tof.
When a symbolic expression, which includes mathematical operations that can be executed
(addition, subtraction, multiplication, and division), is entered, MATLAB executes the
operations as the expression is created
EXAMPLE:
2442424 2a laos
+ +Et+Zt+Z+7+05 is entered
>> f= x/2+3*x/2+y/542/3+2*y/34t/740.5 %I+F
f=
24413/15*y41/34241/7*t41/2 ox + 424542 is displayedNotice that all the calculations are carried out exactly, with no numerical approximations.
(for example: 0.5 is displayed as 1/2).
Difference between exact and approximate calculations is shown by
following example:
Here,the same mathematical operations are carried out—once with symbolic variables and
once with numerical variables.
Use of symbolic variables:
>> azsym(4);
>> b=sym(7);
>> cza/b+sqrt(4)
c
18/7
>> cea/b+sqrt(2)
c
4/7+2%(1/2) _ % An exact value of e is displayed as a symbolic object (the display is not
Indented.
Use of numerical variables:
>>d=4;
>> e=7;
>> c=d/etsqrt(2)
c
1.9856 % An approximated value of cis displayed as a number
EXPRESSION CONTAINING BOTH SYMBOLIC AS WELL AS NUMERICAL VARIABLE:
‘An expression that is created can include both symbolic objects and numerical variables.
If an expression includes a symbolic object (or several), all the mathematical operations will
be carried out exactly.EXAMPLE:
>> assym(4);
>> b=3;
>> c=a/7+sqrt{b)
c
4/7+39(1/2)
Additional facts about symbolic expressions and symbolic objects:
1
‘Symbolic expressions can include numerical variables that have been obtained from
the execution of numerical expressions. When these variables are inserted in
symbolic expressions their exact value is used, even if the variable was displayed
before with an approximated value.
For Example:
>> h=10/7
>> ceatbth
c=
59/7 Sbexact value of h is used
CONVERTING SYMBOLIC EXPRESSIONS TO NUMERICAL FORM:
Itis possible to convert symbolic expressions/variables to numerical form.
The double(S) command can be used to convert a symbolic expression (object) $ that
is written in an exact form to numerical form.
(The name “double” comes from the fact that the command returns a double-
precision floating point number representing the value of S).
Example:
>> h=10/7;
ym(3);
>> c=atbth
c=
59/7
>> double(c) %symbolic variable c is converted to its floating point form
ans =
8.42863. Asymbolic object that is created can also be a symbolic expression written in terms
of variables that were not first created as symbolic objects.
For example, the quadratic expression ax? + bx + c can be created as a symbolic
object named f by using the sym command:
>> fesym('atx"2+bFx+c’)
a*xA2tb*xtc
Note:
The variables a, b, c, and x included in the object do not exist individually as independent
symbolic objects(the whole expression is one object).
This means that it is impossible to perform symbolic math operations associated with the
individual variables in the object(for example: it will not be possible to differentiate f with
respect to x),
4, Existing symbolic expressions can be used to create new symbolic expressions. This is
done by simply using the name of the existing expression in the new expression.
For example:
>> syms xy;
>> sl=xty
si=
xty
>> sd=cy
s2=
xy
>> s3=s1+2*52/s1
s3=
xy+(2*x-2*y)/(x+y)
The findsym Command and the Default Symbolic Variabl
The findsym command can be used to find which symbolic variables are present in an
existing symbolic expression
The format of the command
findsym(s)
or
findsym(S,n)* The findsym(s) command displays the names of all the symbolic variables (separated
by commas) that are in the expression S in alphabetical order.
Example:
>> symsxuacn
>> textutatcen
t=
xtutatcrn
>> findsym(t)
ans =
a,c,n,u,x
The findsym(S,n) command displays n symbolic variables that are in expression S in
the default order.
Example:
>> symsxuacn
>> textutatcén;
>> findsym(t,4)
ans =
xune
>> findsym(t,5)
ans =
xun,ca
>> findsym(t,6)
ans =
a, 6,1, u,x
>> findsym(t,1)
ans =
x
For one-letter symbolic variables,
the default order starts with x, and followed by letters, according to their closeness
tox. If there are two letters equally close to x, the letter that is after x in alphabetical
order is first (y before w, and z before v). The default symbolic variable in an
expression S can be identified by typing findsym(S,1).
CHANGING THE FORM OF AN EXISTING SYMBOLIC EXPRESSION:
‘Symbolic expressions are either created by the user or by MATLAB as the result of symbolic
operations.
The expressions created by MATLAB might not be in the simplest form or in a form that the
user prefers. The form of an existing symbolic expression can be changed by collecting
terms with the same power, by expanding products, by factoring out common multipliers,
by using mathematical and trigonometric identities, and by many other operations.Some commands that can be used for this purpose are:
1. The collect comman
The collect command collects the terms in the expression that have the variable with
the same power.
In the new expression, the terms will be ordered in decreasing order of power.
‘The command has the form
collect(s)
or
collect(S, variable_name)
where S is the expression
The collect(S) form works best when an expression has only one symbolic variable.
Example:
>> syms xy
N24+x-exp(x))* (X43)
s
(x924x-exp(x))* (x43)
>> S1=collect(S)
Sis
X93+4*xA24(-exp(x)+3)*x-3* exp(x)
If an expression has more than one variable, MATLAB will collect the terms of one
variable first, then those of a second variable, and so on. The order of the variables is
determined by MATLAB.
The user can specify the first variable by using the collect(S, variable_name) form of
the command,
EXAMPLE:
>> Ta(2*xA2492)* (xt 243)
t
(2*xA2+yA2)*(x4yA243)
>> Si=collect(T)
S1=
DHaABH(2*YA2+6)*XAZYA2*HYAD*(yA23)
>> S1=collect(T,y)
S1=
Ms (24xA24x43) 4YA24D*KAZ* 43)
NOTE:
when collect(T) is used, the reformatted expression is written in order of decreasing
powers of x, but when collect(T,y)is used, the reformatted expression is written in
order of decreasing powers of y.The expand command:
‘The expand command expands expre:
ns in two ways.
It carries out products of terms that include summation (used with at least one of
the terms), and it uses trigonometric identities and exponential and logarithmic laws
to expand corresponding terms that include summation.
The form of the commani
expand{(s)
where S is symbolic expression
Example:
>> symsaxy
S=(x+5)* (a) (xd)
s
(x45) *(-a)*(x44)
>> T=expand{(s)
T
XABEO*XAZ-aMKAZ-9MK4AH20"-20%2
>> expand(tan(x+y))
ans =
(tan(x)+tan(y))/(1-tan(x)*tan(y))
The factor command:
The factor command changes an expression that is a polynomial to a product of
polynomials of a lower degree.
The form of the command i
factor(S)
where S is the symbolic expression
Example:
> syms x
>> S=xhB+4%XA2-11°%-30
s=
XA3444X2-11%-30
>> factor(S)
ans =
(-3)*(+5)*(+2)
The simplify command:
The simplify command uses mathematical operations (addition, multiplication, rules
of fractions, powers, logarithms, etc.) and functional and trigonometric identities to
generate a simpler form of the expression
The format of the simplify command isExai
simplify(S)
where
er S is the name of the existing expression to be simp
or
an expression to be simplified can be typed in for S.
mple:
>> syms xy
>> S=(xh245#x46)/(x+2)
Ss
(«9245 %x+6)/(x42)
Des:
x43
implify(S)
>> simplify((x+y)/(1/x+1/y))
ans
xy
5. The simple command:
The simple command finds the form of the expression with the fewest number of
characters.
When the command is executed, MATLAB creates several forms of the expression by
applying the collect, expand, factor, and simplify commands, and other simplification
functions that are not covered here. Then MATLAB returns the expression with the
shortest form.
The simple command has the following three forms:
A. F=simple(S)
(The shortest form of S is assigned to F)
Example:
>> syms x
S=(x93-4*X2416"X)/(x43+64)
S=
(43-4°x92416*x)/(xA3464)
B. simple(s)
(All the simplification trails are displayed. The shortest is assigned to ans.)Example:
>> simple(S)
simplify:
wx)
radsimp:
x/(x+4)
combine(trig):
x/(xra)
factor:
xix)
expand:
AU(xh3464)*x43-4/ (993464) N2416/(x93+64)*x
combine:
(x93-4*x"2416*x)/(x3+64)
convert(exp):
(093-4°x"2416%X)/(x"3+64)
convert(sincos):
(093-4°x42416%X)/(x"3+64)
convert(tan):
(093-4*x42416*X)/(x3 +64)
collect(x):
(093-444 2416*X)/(0x3 +64)
ans =
x/(x+a)C._[F how] = simple(s)
(The shortest form of S is assigned to F. The name (string) of the simplification
method is assigned to how)
Example:
>> [G how] = simple(S)
Ge
x/(x+4)
how =
factor
6. The pretty Command:
The pretty command displays a symbolic expression in a format resembling the
mathematical format in which expressions are generally typed
The command has form:
pretty(S)
where either S is the name of the existing expression to be simplified or an
expression to be simplified can be typed in for S.
EXAMPLE:
>> syms ab ex
S=sqrt(a*x*2 + b*x +c)
s=
(atx 2+b*xtc)A(1/2)
>> pretty(S)
2 12
(ax +bx+c)
Symbolic and Numeric Conversions:
Consider the ordinary MATLAB quantity
The sym function has four options for returning a symbolic representation of the numeric
value stored in
1) The '£' option
sym(t,'f£')
returns a symbolic floating-point representation,
'1.999999999999a'*2*(-4)
2) The 'r! optionsym(t,'r')
returns the rational form
1/10
This is the default setting for sym. That is, calling sym without a second argument is the
same as using sym with the 'r' option.
sym(t)
ans =
1/10
3) The third option ' e' returns the rational form of t plus the difference between the
theoretical rational expression for t and its actual (machine) floating-point value in
terms of eps (the floating-point relative accuracy).
sym(t,'e')
ans
1/10+eps/40
4) The fourth option 'cl' returns the decimal expansion of t up to the number of
significant digits specified by digits
sym(t,'d')
ans =
-1000900000000000055511151231
The default value of digits is 32 (hence, sym(t, 'cl") retums a number with 32
significant digits), but if you prefer a shorter representation, use the digit s command as
follows.
digits (7)
sym(t,'d")
ans =
1000000
Constructing Real and Complex Variable:
In Symbolic Math Toolbox™, symbolic variables are complex variables by default. For
example, if user declare z as a symbolic variable using
syms z
‘Then MATLAB" assumes that z is a complex variable.
User can always check if a symbolic variable is assumed to be complex or real by
using assumptions. If 2 is complex, assumptions(z) returns an empty symbolic object:
assumptions(z)
ans =
Empty sym: 1-by-0The sym command allows to specify the mathematical properties of symbolic variables by
using the ‘real’ option. That is, the statements
x= sym('x;,‘real');
y=sym('y’,'real’);
or more efficiently
syms x y real
z=xtity
create symbolic variables x and y that have the added mathematical property of being real
variables.
Specifically this means that the expression
F=xA2 + yA2
is strictly nonnegative.
Hence, z is a (formal) complex variable and can be manipulated as such. Thus, the
commands
conj(x), conj(z), expand(z*conj(2))
return the complex conjugates of the variables
X HIY, WAZHYAZ
(The conj command is the complex conjugate operator for the toolbox.)
Also, If conj(x) == x returns 1,
then xis a real variable.
Clearing real property of a variable:
To clear x of its "real" property, type
syms x unreal
or
x= sym('x;unreal')
Note: The command
clear x
does not make x a nonreal variable.
Example:
>> symsz %By default z is complex variable
>> conj(z)
ans =
conj(z)
>>symszreal %defining z as real
>> conj(z)
ans =z
>> z=sym('z'/'unreal') _% converting z from real to non real variable
2
z
>> conj(2)
ans =
conj(z)
Creating Abstract Function:
If user want to create an abstract (i.e.
indeterminant) function fix), type
f= sym("f(x)')
Then f acts like f(x) and can be manipulated by the toolbox commands.
for example:
To construct the first difference ratio, type
df = (subs(f,'x’,'x+h!) -f/'ht
or
syms xh
df = (subs(f,x,x+h)-f)/h
which returns
di
(fleth)f(x))/h
This application of sym is useful when computing Fourier, Laplace, and z-transforms.
PLOTTING SYMBOLIC EXPRESSION.
In many cases, there is a need to plot a symbolic expression. This can easily be done with
the ezplot command, For a symbolic expression S that contains one variable var, MATLAB
considers the expression to be a function s(var), and the command creates a plot of s(var)
versus var.
For a symbolic expression that contains two symbolic variables vari and var2, MATLAB
considers the expression to be a function in the form s(var1,var2)=0 , and the command
creates a plot of one variable versus the other.
To plot a symbolic expression S that contains one or two variables, the ezplot
command is:ezplot(s)
or
ezplot(S,[min,max])
or
ezplot(s,[xmin,xmax,ymin,ymax])
where xmin/min xmax/max represents domain of independent variable whereas
ymin,ymax is domain of dependent variable.
Some points about ezplot command:
Sis the symbolic expression to be plotted. It can be the name of a previously created
symbolic expression, or an expression can be typed in for S.
Itis also possible to type the expression to be plotted as a string without having the
variables in the expression first created as symbolic objects.
IFS has one symbolic variable, a plot of S(var) versus var is created, with the values
of var (the independent variable) on the abscissa (horizontal axis), and the values of
on the ordinate (vertical axis).
If the symbolic expression S has two symbolic variables, var1 and var2, the
expression is assumed to be a function with the form S(var1,var2)=0 . MATLAB
creates a plot of one variable versus the other variable. The variable that is first in
alphabetic order is taken to be the independent variable.
For example, if the variables in S are x and y, then x is the independent variable and
is plotted on the abscissa and y is the dependent variable plotted on the ordinate. If
the variables in S are u and v, then u is the independent variable and vis the
dependent variable.
In the ezplot{S) command, if $ has one variable (S(var) ), the plot is over the domain -
2ncvar<2n(default domain) and the range is selected by MATLAB.
IFS has two variables (S(var1,var2)), the plot is over 2n> syms x
S=(3*x+2)/(4*x-1)
Se
(3*x#2)/(4"*%-1)
>> ezplot(s)
DeeeNAny 22. >> syms xy
SEA KN2-1B*xtA*yA2412¢y-11
s=
APKA2-ABt xed tyAD+ 124 Y-LL
>> ezplot(s)
Birqueno:s
Fin _Eait_View Inewt Toolt_ Window Halp
(Deas RAZ 2 EO
3. >> symst
(=cos(2*t)
x=
cos(2*t)
>> yesin(4*t);
>> explot(xy)
Tete_Window Hae
lowuelsary 25CALCULUS:
Calculus is an essential subject in mathematics required for science and engineering
students. It contains two main topics which are the differentiation and integration of
functions. The former one(calculus) is based on understanding the determination of limits.
With the capability of the symbolic manipulation software today, solutions to calculus
problems can be obtained rapidly.
MATLAB provides various ways for solving problems of differential and integral calculus,
solving differential equations of any degree and calculation of limits. Best of all, user can
easily plot the graphs of complex functions and check maxima, minima and other stationery
points on a graph by solving the original function, as well as its derivative.
SOLVING EQUATIONS:
1. Using solve command:
If eqns an equation, solve(eqn) / solve(eqn, x) solves eqn for the symbolic
variable x.
(Use the == operator to specify the equation and solve it using solve.)
For solving equations,use the command solve by following these steps.
‘* Represent the variable you are solving using syms command.
‘* Move every term to the left side of the equation so that the equations of the form
‘atx)=h(x) become g(x}-h(x)=0
© Ifthe term on the left side is f(x) and the equation is f(x)=0
Example:
>> syms ab ex
>> eqn =a*x2+b*x +c
eqn=
at x2+b xtc
>> solx=solve(eqn, x) %solved with respect to x
solx
[1/2/a*(-b+(b*2-4*a*c)*(1/2))]
[ 1/2/a*(-b-(b*2-4*a*c)4(1/2))]
n2HyAD
solve(eqn, y) _ %solved with respect to y
solve does not automatically return all solutions of an equation. Solve the
equation cos(x) == -sin(x). The solve function returns one of many solutions.
Example:
>osyms x
>> solx = solve(cos(x}+sin(x), x)
solx =
-1/4*pi