Professional Documents
Culture Documents
SymbolicProgramming
So far we only dealt with mostly numerical programming. Numerical
processes are carried out by numerical expressions that contain numbers and
variables with preassigned numerical values. When numerical expressions are
executed the results are also numerical. In many engineering application often
thesolutionneedstobeexpressedasexpression.Inthesecasesitisnecessaryto
perform the analysis symbolically. A simple example could be to find the
derivativeof2t3+ 5t8with respect to twhichis6t2+5. This can be done in
MATLAB,forexample:
>>symst
>>f=2*t^3+5*t8
f=2*t^3+5*t8
>>diff(f)
ans=
6*t^2+5
7.1CREATINGSYMBOLICVARIABLESANDSYMBOLICEXPRESSIONS
The first thing we need to do in symbolic programming is to create the
symbolic object using syms command. Like the example above we created
symbolic variable t and then use in symbolic expression f. Several symbolic
variablescanbecreatedinonecommand.
>>symsxt
>>f=2*cos(t)+x^2
f=x^2+2*cos(t)
2x 1
Anotherexampleofcreationofsymbolicexpression, f ( x )
x2
>>symsx
>>f=(2*x+1)/(x2)
x 2 ( x 2) 2
Kosasih2012Chapter7SymbolicProgramming1
>>diff(f)
ans=
2/(x2)(2*x+1)/(x2)^2
Orfindthelimit lim f ( x) 7
x 3
>>limit(f,3)
>>ans=
7
>>symst
>>a=1.5;b=3.75;
>>f=a*t^2+t^b
f=
(3*t^2)/2+t^(15/4)
Note in the workspace that a and b are numeric class and t and f are of
symbolicclass.
>>subs(f,x,5.5)
ans=
3.4286
Symbolic expression can also be created in terms of variables that have not
been created using sym command. Say with the workspace clean to create
2x 1
f ( x)
x2
>>f=sym((2*x+1)/(x2))
f=
(2*x+1)/(x2)
Kosasih2012Chapter7SymbolicProgramming2
Note however that variablex does not appearin theworkspace becauseit was
notcreatedasanindependentvariable.
7.2SOMEBASICSYMBOLICEXPRESSIONOPERATORS
Symbolic tool in MATLAB is very useful in manipulating mathematical
expressions.Belowaresomeoftheusefulsymbolicexpressionoperators.
Commandname
Operation
findsym(S)
find the symbolic variables in a symbolic
expression,S
collect(S)
collectcommonterminexpression,S
expand(S)
expandpolynomialsandelementaryfunctions
factor(S)
change a polynomial expression to be a
productofpolynomialsofalowerdegree
numden(S)
Find the numerator and denominator of
polynomialfraction
simple(S)
Find the expression with the least number of
characters
simplify(S)
Generatesimplerformoftheexpression
pretty(S)
Writetheexpressioninstandardmathematical
format
symadd(A,B)
PerformssymbolicadditionA+B
symdiv(A,B)
PerformssymbolicdivisionA/B
symmul(A,B)
PerformssymbolicmultiplicationA*B
sympow(S,p)
PerformssymbolicpowerS^p
symsub(A,B)
PerformssymbolicsubstractionAB
2x 1
Letususethefollowingexpression f ( x )
x2
findsym
>>findsym(f)
ans=
x
numden
>>[n,d]=numden(f)
n=
2*x+1
d=
x2
Kosasih2012Chapter7SymbolicProgramming3
simplify
>>simplify(f)
ans=
5/(x2)+2
Letusconsidernow f ( x)
x 3 4 x 2 16 x
x 3 64
simple
>>symsx
>>f=(x^34*x^2+16*x)/(x^3+64)
f=
(x^34*x^2+16*x)/(x^3+64)
>>f=simple(f)
f=
x/(x+4)
factor
>>factor(f)
ans=
x/(x+4)
pretty
>>pretty(f)
32
x4x+16x
3
x+64
Letusconsidernow f ( x) ( x 2 x e x )( x 3)
collect
>>symsxy
>>f=(x^2+xexp(x))*(x+3)
f=
(x+3)*(xexp(x)+x^2)
>>collect(f)
Kosasih2012Chapter7SymbolicProgramming4
ans=
x^3+4*x^2+(3exp(x))*x3*exp(x)
expand
>>expand(f)
ans=
3*x3*exp(x)x*exp(x)+4*x^2+x^3
7.3SOLVINGSINGLEALGEBRAICEQUATION
Itisoftenweneedtofindsolutionofalgebraicequation,forthisapplication
MATLABhasafunction,solvethatcandothat.
Mathematicallysolvinganequationisfindingthesolutionoff(x)=0.Thisis
example is for one variable function for which the solution is a numeric. For
morethanonevariablethesolutionisasymbolicexpression.
Theformatofsolvecommandcanbe:
h=solve(f)
or
h=solve(f,var)
Someexample:
Solve:f(x)=e2x5
>>symsx
>>f=e^(2*x)5
>>f=exp(2*x)5
f=
exp(2*x)5
>>h=solve(f)
h=
log(5)/2
Alternatively
>>solve(exp(2*x)5)
ans=
log(5)/2
Kosasih2012Chapter7SymbolicProgramming5
Solve:f(x)=x2x6
>>symsx
>>f=x^2x6
f=
x^2x6
>>solve(f)
ans=
2
3
Toconverttonumerictype
>>double(ans)
ans=
2
3
Solve:f(x)=g(x)wheref(x)=cos(2x)+3sin(x)andg(x)=x^3
>>solve(cos(2*x)+3*sin(x)=2)
ans=
pi/2
pi/6
(5*pi)/6
0.4
0.2
f(x)
-0.2
-0.4
-0.6
-0.8
-1
0
0.5
1.5
2.5
Kosasih2012Chapter7SymbolicProgramming6
Solve:f(x,y,z)=4xy2+20x5z
>>symsxyz
>>f=4*x*y^2+20*x5*z
f=
4*x*y^2+20*x5*z
>>solve(f)
ans=
(5*z)/(4*y^2+20)
Comparethiswith:
>>solve(f,z)
ans=
(4*x*y^2)/5+4*x
7.4SOLVINGSYSTEMALGEBRAICEQUATIONS
Solvecommandcanbeusedforsolvingasystemofequations.Ifthenumber
ofequationsandvariablesisthesame,thesolutionisnumerical.Ifthenumberof
variablesisgreaterthanthenumberofequations,thesolutionwillsymbolicfor
thedesiredvariablesintermsoftheothervariables.
Solvethefollowingsystemofequations:
3x+2y=5
x+7.5y=4.2
>>symsxy
>>f=3*x+2*y5
f=
3*x+2*y5
>>g=x+7.5*y4.2
g=
x+(15*y)/221/5
>>[ab]=solve(f,g)
a=
291/205
b=
76/205
Kosasih2012Chapter7SymbolicProgramming7
Solvethefollowingsystemofequations:
3x+2y+z=5
x+7.5y+5z=4.2
>>symsxyz
>>f=3*x+2*y+z5
f=
3*x+2*y+z5
>>g=x+7.5*y+5*z4.2
g=
x+(15*y)/2+5*z21/5
>>[ab]=solve(f,g)
a=
(5*z)/41+291/205
b=
76/205(28*z)/41
Comparetheabovewith
>>[ab]=solve(f,g,z,y)
a=
208/25(28*x)/5
b=
(41*x)/5291/25
7.5DIFFERENTIATION
Symbolic differentiation in MATLAB is done using the diff command. The
formofthediffcommandare
diff(f)
returns the derivative of the expression f wrt the default
independentvariable
diff(f,t)
Returnsthederivativeoftheexpressionfwrtt
diff(f,n)
returns the nth derivative of the expression f wrt the default
independentvariable
diff(f,t,n)
Returnsthenthderivativeoftheexpressionfwrtt
Kosasih2012Chapter7SymbolicProgramming8
In the diff(f) command, if there is only one variable in the expression, the
differentiation is carried out with respect to that variable. If var is used the
expressionisdifferentiatedwithrespecttovariablevar.Secondorhigher(nth)
derivativecanbedeterminedusingthenargument.
Findthe1stand2ndderivativeof:f(x)=5x4x26x+2.5
>>symsx
>>f=5*x^4x^26*x+2.5
f=
5*x^4x^26*x+5/2
>>diff(f)
ans=
20*x^32*x6
>>diff(f,2)
ans=
60*x^22
7.6INTEGRATION
Symbolic differentiation in MATLAB is done using the int command. The
formoftheintcommandis
int(f)
returns the integral of the expression f wrt the default
independentvariable
int(f,t)
returnstheintegraloftheexpressionfwrtt
int(f,a,b)
returns the integral of the expression f wrt the default
independentvariableevaluatedovertheinterval[a,b]
int(f,t,a,b)
returns the integral of the expression f wrt tevaluated over the
interval[a,b]
Integrate:f(x)=2cos(x)6*x
>>symsx
>>f=2*cos(x)6*x
f=
2*cos(x)6*x
>>int(f)
ans=
2*sin(x)3*x^2
Kosasih2012Chapter7SymbolicProgramming9
Integrate:f(y,y)=5y2cos(4*t)
>>symsyt
>>f=5*y^2*cos(4*t)
f=
5*y^2*cos(4*t)
>>int(f)
ans=
(5*y^3*cos(4*t))/3
>>int(f,t)
ans=
(5*y^2*sin(4*t))/4
7.7SOLVINGORDINARYDIFFERENTIALEQUATION
MATLAB also provides function dsolve for solving ordinary differential
equation (ODE). It can be used to solve first order or higher order ODE. The
formofdsolvecommandis
h=dsolve(equation,condition)
dy
SolveODE:
4t 2 y ,theansweris y 2t 1 C e 2t
dt
>>dsolve(Dy=4*t+2*y)
ans=
C3*exp(2*t)2*t1Notetheconstant
Iftheinitialconditiony(0)=0
>>dsolve(Dy=4*t+2*y,y(0)=0)
ans=
exp(2*t)2*t1
d2y
dy
Kosasih2012Chapter7SymbolicProgramming10
7.8PLOTTINGSYMBOLICEXPRESSIONS
Symbolicexpressioncanbeplottedusingtheezplotcommand.Someformsof
theezplotcommandaregivenbelow:
ezplot(f)
Plots the function within MATLAB
specifiedrange
ezplot(f,[xminxmax])
Plotsthefunctionbetweenxminandxmax
ezplot(f,[xminxmax,ymin,ymax])
Plots the function between independent
variable range (xmin and xmax) and
dependentvariable(yminandymax)
Plotf(x)=5x4x26x+2.5over[5,5]range
>>symsx
>>f=5*x^4x^26*x+2.5
f=
5*x^4x^26*x+5/2
>>ezplot(f,[55])
5 x4 - x2 - 6 x + 5/2
data 1
3000
2500
f(x)
2000
1500
1000
500
0
-5
0
x
Someothergraphicalcommandsforsymbolicexpressionsaregivenbelow.
Kosasih2012Chapter7SymbolicProgramming11
ezmesh(f)
ezmeshc(f)
ezsurf(f)
ezsurfc(f)
ezplot3(f)
Plotf(x,y)=x2+y2
>>symsxy
>>f=x^2+y^2
f=
x^2+y^2
>>ezmesh(f)
x +y
80
60
40
20
0
5
5
0
-5
-5
>>ezmeshc(f)
f(x,y) =x2+y2
80
f(x,y)
60
40
20
0
5
5
0
y
-5
-5
0
x
Kosasih2012Chapter7SymbolicProgramming12
>>ezsurf(f)
>>colorbar
x2 + y2
70
80
60
70
60
50
f(x,y)
50
40
40
30
20
30
10
20
0
6
4
6
2
4
0
10
-2
-2
-4
-4
-6
-6
Kosasih2012Chapter7SymbolicProgramming13
40
30
20
10
0
4
2
0
y
-2
-4
-2
-3
-1
0
x
7.9TUTORIALQUESTIONS
1.Defineyasasymbolicvariableandcreatethetwosymbolicexpressions:
S1 ( y 2) 2 2 (2 y 1) and S 2 y 2 2 y 4
Use the symbolic expressions to determine the simplest form of the following
expressions:
a) S1.S2
S
b) 1
S2
c) S1+S2
d) Usethesubscommandtoevaluatethenumericalvalueoftheresultsfory
=5.
2.Definexasasymbolicvariablethen
a) Showthattherootsofthepolynomial:
f ( x) x 5 6 x 4 6 x 3 64 x 2 27 x 90
are1,2,3,3and5byusingthefactorcommand.
Kosasih2012Chapter7SymbolicProgramming14
b) Derivetheequationofthepolynomialthathastheroots:x=6,x=4,x=1,
andx=2
3.Evaluatetheindefiniteintegral I e 2 x 2 e 2 x dx
4.Theequationofanellipseis
x2 y2
1
a2 b2
ShowthattheareaenclosedbytheellipseisgivenbyA=ab.
5.ThermsvalueofanACvoltageisdefinedby:
Vrms
1 2
v (t ) dt
T 0
WhereTistheperiodofthewaveform.
a) A voltage is given by v(t) = V cos(t). Show that Vrms
and is
2
independentof(TherelationshipbetweentheperiodTandtheradian
2
frequencyis: T
b) Avoltageisgivenbyv(t)=2.5cos(350t)+3V.Determine Vrms .
6.Determinethegeneralsolutionofthedifferentialequation:
d2y
2y 0
dt 2
Showthatthesolutioniscorrect.(Derivethefirstandsecondderivativesofthe
solution,andthensubstitutebackintheequation.)
7.Determinethesolutionofthefollowingdifferentialequationthatsatisfiesthe
giveninitialconditions.
dy
d2y
1
4 y 5 y(0)=0,
2
dx x 0
dx
Kosasih2012Chapter7SymbolicProgramming15