Download - 5.CECE Matlab Symbolic
-
8/3/2019 5.CECE Matlab Symbolic
1/42
MATLABCECE ACADEMIC TEAM
Prepared by: Sherif Harhash19/7/2011
-
8/3/2019 5.CECE Matlab Symbolic
2/42
Symbolic
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
3/42
Symbolic Processing with MATLAB
Contents :
Symbolic algebra.
Symbolic methods for solving algebra
Symbolicmethods for solving ordinarydifferential
equations ODE.
Symbolic calculus, including integration,
differentiation, limits, and series.
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
4/42
Symbolic Processing with MATLAB
Laplacetransforms.
Selected topics in linear algebra,including symbolicmethods forobtaining determinants, matrix inverses,and eigenvalues.
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
5/42
How to declare a symbol in matlab and do any
operations on it ?
Sym (x) >> decare symbol object x .
Typing >>x=sym(x,real)tells MATLAB to
assume that xis real. Typing
>>x=sym(x,unreal)tells MATLAB to assume
that x is notreal
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
6/42
typing >>symsxis equivalent to typing
>>x=sym(x), and
typing >>syms x y u v creates the four symbolic
variables x, y, u, and v.
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
7/42
You can use the symfunction tocreatesymbolicconstantsby using a numerical value
for the argument. For example, typing >> pi = sym(pi)
>> fraction = sym(1/3)
and
>> sqroot2 = sym(sqrt(2))
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
8/42
Numerical calc on symbolic vars.
>> syms x y
>> s = x + y;
>> r = sqrt(x^2 + y^2);
createsthe symbolic variables s and r. The terms s
= x + y and
r = sqrt(x^2 +y^2)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
9/42
Functions related to symbols
Find sym(E,n)returnsthe n symbolic variables in
Eclosest to x
Collect (E)collects coefficients of like powers in
the expression E.
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
10/42
example
>> syms x y
>> E = (x-5)^2+(y-3)^2;
>> collect(E)
ans =
x^2-10*x+25+(y-3)^2
>> collect(E,y)
ans =
y^2-6*y+(x-5)^2+9
Find sym(E,2)
ans = x,y
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
11/42
Expand (E) ..returns the expansion of the
equation E
Simplify(E) ..makes E in the simplist form
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
12/42
>>expand(sin(x+y
ans =
sin(x)*cos(y)+cos(x)*sin(y)
>>simplify(6*((sin(x))^2+(cos(x))^2))
ans =
6
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
13/42
Factor(E) factorize the equation E
Example :
>> syms x y
>> factor(x^2-1)
ans =(x-1)*(x+1)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
14/42
subs(E,old,new).. Substitutes new for old in the
expression E
Example :
>> syms x y
>> E = x^2+6*x+7;
>> F = subs(E,x,y)
F =y^2+6*y+7
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
15/42
If you want to tell MATLAB that f is a functionof thevariable t, type
f = sym(f(t)).
For example:
(t), the session is
>> syms t
>> f = sym(f(t)); >> g = subs(f,t,t+2)-f
g =
f(t+2)-f(t)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
16/42
ezplot(E ,[range]) ..works as the plot function in
matlab but for symbolic objects .
Example :
>> syms x
>> E = x^2-6*x+7;
>> ezplot(E,[-10,10])
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
17/42
Equationname
Range 19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
18/42
The solve function.
There are three ways to use the solve function. For
example, to solve the equationx + 5 = 0, one way
is
>> eq1 = x+5=0; >> solve(eq1)
ans = -5
The second way is >> solve(x+5=0)
ans = -5
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
19/42
The third way is
>> syms x
>> solve(x+5)
ans = -5 You can store the result in a named variable as
follows:
>> syms x
>> x = solve(x+5)
x = -5
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
20/42
Differentiation with the diff function.
>> syms n x y
>> diff(x^n)
ans =
x^n*n/x
>> simplify (ans)
ans =
x^(n-1)*n
Note : n wa x are symbols :D
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
21/42
Differentiation with the diff function.
>> diff(log(x))
ans =
1/x
>> diff((sin(x))^2)
ans =
2*sin(x)*cos(x)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
22/42
Partial derivative
If we have equation as E=sin(x*y)
So to diff this equation we must fiff it partially
Diff(E,x)diff E w.r.t X
Diff (E,y) diff E w.r.t Y
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
23/42
diff(E,n)..
returnsthe nth derivativeof the expression E withrespect to the default independent variable.(x)
Example :
>> syms x
>> diff(x^3,2) ans =
6*x
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
24/42
Integration with the int function.
The int function returns the integration of E w.r.t the
independent variable.
Example :
>> syms n x y
>> int(x^n)
ans =
x^(n+1)/(n+1)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
25/42
int(E,v) ..returnsthe integral of the expression Ewith respect to the variable v.
Example :
>> syms n x
>> int(x^n,n)
ans =
1/log(x)*x^n
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
26/42
Int (E,a,b) ..returns the integral of the expression
E with respect to the default independent variable
evaluated over the interval [a,b], where a and
bare numeric expressions. >> syms x
>> int(x^2,2,5)
ans =39
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
27/42
a or b may be any independent variable (t)
>> syms t x
>> int(x,1,t)
ans = 1/2*t^2-1/2
>> int(sin(x),t,exp(t))
ans = -cos(exp(t)) + cos(t)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
28/42
Exercise
Integrate 1/(x-1) from 0 to 2 ?
What will you expect ??
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
29/42
Taylor Series.
taylor(f,n,a) ..function gives the first n-1 terms in
the Taylor series for the function defined in the
expression (f).
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
30/42
Taylor Series.
Example :
>> syms x
>> f = exp(x);
>> taylor(f,4) ans =
1+x+1/2*x^2+1/6*x^3
>> taylor(f,3,2)
ans =
exp(2)+exp(2)*(x-2)+1/2*exp(2)* ... (x-2)^2
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
31/42
Series summation.
The symsum (E,a,b) function returns the sum of the expression E asthe default symbolic variable varies from a to b.
>> syms k n
>> symsum(k,0,10)
ans = 55
>> symsum(k^2, 1, 4)
ans =
30
>> symsum(k,0,n-1)
ans =
1/2*n^2-1/2*n
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
32/42
Finding limits
Limit(E,x,b) ..returns the limit of E w.r.t x as x tends
to b
Limit (E) returns the limit of E w,r,t x as x tends to
0.
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
33/42
limit(E,v,a,right) and limit(E,v,a,left)
specify the direction of the limit.
>> syms x >> limit(1/x,x,0,left)
ans = -inf
>> limit(1/x,x,0,right) ans = inf
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
34/42
Solving differential equations with the dsolve function.
The dsolve functions syntax for solving a single
equation is dsolve (eqn). The function returns a
symbolic solutionof the ODE specified by thesymbolic expression eqn.
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
35/42
>> dsolve(Dy+2*y=12) ans =
6+C1*exp(-2*t)
There can be symbolicconstantsin the equation.
>> dsolve(Dy=sin(a*t))
ans =
(-cos(a*t)+C1*a)/a
Here is a second-order example: >> dsolve(D2y=c^2*y)
ans =C1*exp(-c*t) + C2*exp(c*t)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
36/42
Solving sets of equations
Setsof equations can be solved with dsolve. The
appropriate syntax is dsolve(eqn1,eqn2,...).
>>[x, y]=dsolve(Dx=3*x+4*y,Dy=-4*x+3*y)
x = C1*exp(3*t)*cos(4*t)+C2*exp(3*t)*sin(4*t)y = -
C1*exp(3*t)*sin(4*t)+C2*exp(3*t)*cos(4*t)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
37/42
Conditions on the solutions at specified values of
the independent variable can be handled as
follows. The form
dsolve(eqn, cond1, cond2,...)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
38/42
If y is the dependent variable, these conditions are specifiedas follows: y(a) = b, Dy(a) = c, D2y(a)=d, and so on.
For example:
>> dsolve(D2y=c^2*y,y(0)=1,Dy(0)=0)
ans =
1/2*exp(c*t)+1/2*exp(-c*t)
Arbitrary boundary conditions, such asy(0) = c, can be used.
>> dsolve (Dy+a*y=b,y(0)=c)
ans =
1/a*b+exp(-a*t)*(-1/a*b+c)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
39/42
LAPLACE TRANSFORM
>>syms b t >>laplace(t^3) ans = 6/s^4 >>laplace(exp(-b*t)) ans = 1/(s+b)
>>laplace(sin(b*t)) ans = b/(s^2+b^2)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
40/42
INVERSE LAPLACE TRANSFORM
>>syms b s >>ilaplace(1/s^4) ans =
1/6*t^3 >>ilaplace(1/(s+b)) ans = exp(-b*t)
>>ilaplace(b/(s^2+b^2) ans = sin(b*t)
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
41/42
Characteristic Polynomialand Roots
>> syms k
>> A = [0, 1; -k, -2];
>> poly(A)
ans =x^2+2*x+k
>> solve(ans)
ans =[ -1+(1-k)^(1/2) ]
[ -1-(1-k)^(1/2) ]
19/7/2011Matlab CECE Academic Team
-
8/3/2019 5.CECE Matlab Symbolic
42/42
inv(A) and det(A)
>> inv(A) ans = [ -2/k, -1/k ] [ 1, 0 ]
>> A*ans ans = [ 1, 0 ] [ 0, 1 ]
>> det(A) ans = k