0% found this document useful (0 votes)
23 views6 pages

Matlab 10 Slide

Uploaded by

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

Matlab 10 Slide

Uploaded by

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

Lecture 11 Computer Applications 0933201 Chapter 10

Cover in this chapter a subset of the capabilities


UNIVERSITY OF JORDAN of the Symbolic Math toolbox. Specifically the
Faculty of Eng. and Tech.
followings:
Electrical Engineering Dept.
Instructor: Ziad R. Al-Khatib x Symbolic algebra.
x Symbolic methods for solving algebraic and
Introduction to MATLAB 7 transcendental equations.
for Engineers x Symbolic methods for solving ordinary
William J. Palm III
differential equations ODE.
Chapter 10
x Symbolic calculus, including integration,
Symbolic Processing with MATLAB differentiation, limits, and series.
x Laplace transforms.
x Selected topics in linear algebra, including
symbolic methods for obtaining determinants,
Z.R.K
matrix inverses, and eigenvalues.
Z.R.K
Z.R.K 10-
10-2

The sym function can be used to create The syms function enables you to combine more
“symbolic objects” in MATLAB. than one such statement into a single statement.
If the input argument to sym is a string, the For example, typing >> syms x is equivalent to typing
result is a symbolic number or variable. If the >> x = sym(’x’), and typing >> syms x y u v
input argument is a numeric scalar or matrix, the creates the four symbolic variables x, y, u, and v.
result is a symbolic representation of the given You can use the sym function to create symbolic
constants by using a numerical value for the
numeric values.
argument. For example, typing
For example, typing >> x = sym(’x’) creates the >> pi = sym(’pi’)
symbolic variable with name x, and typing >> fraction = sym(’1/3’)
>> y = sym(’y’) creates a symbolic variable and
named y. Typing >> x = sym(’x’,’real’) tells >> sqroot2 = sym(’sqrt(2)’)
MATLAB to assume that x is real. Typing create symbolic constants that avoid the floating-
>> x = sym(’x’,’unreal’) tells MATLAB to point approximations inherent in the values of S, 1/3,
assume that x is not real. and —2.
Z.R.K Z.R.K
10-
10-3 10-
10-4

The vector and matrix notation used in


You can use symbolic variables in expressions
MATLAB also applies to symbolic variables. For
and as arguments of functions. You use the
example, you can create a symbolic matrix A as
operators + - * / ^ and the built-in functions
just as you use them with numerical follows:
calculations. For example, typing >> n = 3;
>> syms x y >> syms x;
>> s = x + y; >> A = x.^((0:n)’*(0:n))
A =
>> r = sqrt(x^2 + y^2);
[ 1, 1, 1, 1]
creates the symbolic variables s and r. The [ 1, x, x^2, x^3]
terms s = x + y and r = sqrt(x^2 + y^2) [ 1, x^2, x^4, x^6]
are examples of symbolic expressions.
[ 1, x^3, x^6, x^9]
Z.R.K Z.R.K
10-
10-5 10-
10-6

Z.R.K. 2008 Page 1 of 6


Lecture 11 Computer Applications 0933201 Chapter 10

By contrast, the function findsym(E,n) returns the n symbolic


variables in E closest to x, with the tie breaker going to the variable
The function collect(E) collects coefficients
closer to z. of like powers in the expression E. If there is more
>>syms b x1 y than one variable, you can use the optional form
>>findsym(6*b+y)
collect(E,v), which collects all the coefficients
ans =
b,y with the same power of v.
>>findsym(6*b+y+x) %Note: x has not been declared symbolic.
>> syms x y
??? Undefined function or variable ’x’.
>>findsym(6*b+y,1) %Find the one variable closest to x >> E = (x-5)^2+(y-3)^2;
ans = >> collect(E)
y ans =
>>findsym(6*b+y+x1,1) %Find the one variable closest to x
ans =
x^2-10*x+25+(y-3)^2
x1 >> collect(E,y)
>>findsym(6*b+y*i) %i is not symbolic ans =
ans = y^2-6*y+(x-5)^2+9
b, y
Z.R.K Z.R.K
10-
10-7 10-
10-8

The factor function.


The expand and simplify functions. >> syms x y
>> factor(x^2-1)
>>syms x y ans =
>>expand((x+y)^2) % applies algebra rules (x-1)*(x+1)
ans =
The function subs(E,old,new) substitutes
x^2+2*x*y+y^2
>>expand(sin(x+y)) % applies trig identities new for old in the expression E, where old can be a
ans = symbolic variable or expression and new can be a
sin(x)*cos(y)+cos(x)*sin(y) symbolic variable, expression, or matrix, or a numeric
>>simplify(6*((sin(x))^2+(cos(x))^2))... value. For example,
% applies another trig identity >> syms x y
ans = >> E = x^2+6*x+7;
6 >> F = subs(E,x,y)
F =
Z.R.K
y^2+6*y+7 Z.R.K
10-
10-9 10-
10-10

If you want to tell MATLAB that f is a function Use the subs and double functions to
of the variable t, type f = sym(’f(t)’).
Thereafter, f behaves like a function of t, and evaluate an expression numerically. Use
subs(E,old,new) to replace old with a
you can manipulate it with the toolbox
numeric value new in the expression E. The
commands. For example, to create a new
function g(t) f (t 2)  f (t), the session is result is of class double. For example,
>> syms t >> syms x
>> f = sym(’f(t)’); >> E = x^2+6*x+7;
>> G = subs(E,x,2)
>> g = subs(f,t,t+2)-f
G =
g = 23
f(t+2)-f(t) >> class(G)
Once a specific function is defined for f (t), the ans =
function g (t) will be available. double
10-
10-11 Z.R.K
10-
10-12 Z.R.K More? See pages 592-593.

Z.R.K. 2008 Page 2 of 6


Lecture 11 Computer Applications 0933201 Chapter 10

Plot of the function E = x 2 - 6x + 7 generated by the ezplot.


The MATLAB function ezplot(E) generates Figure 10.1–1
a plot of a symbolic expression E, which is a
function of one variable. The default range of the
independent variable is the interval [2S, 2S]
unless this interval contains a singularity.
The optional form ezplot(E,[xmin xmax])
generates a plot over the range from xmin to
xmax. For example, plot the E = x 2 - 6x + 7 .
>> syms x
>> E = x^2-6*x+7;
>> ezplot(E,[-2,6])
See the graph in the next slide.
Z.R.K Z.R.K
10-
10-13 10-
10-14

Order of Precedence. The solve function.


There are three ways to use the solve function. For
MATLAB does not always arrange expressions
example, to solve the equation x 5 0, one way is
in a form that we normally would use. >> eq1 = ’x+5=0’;
For example, MATLAB might provide an answer in >> solve(eq1)
the form – c + b, whereas we would normally ans = -5
The second way is
write b - c. >> solve(’x+5=0’)
The order of precedence used by MATLAB must ans = -5
be constantly kept in mind to avoid mis- The third way is
>> syms x
interpreting the MATLAB output (see pages 9 and
>> solve(x+5)
10 for the order of precedence). ans = -5
MATLAB frequently expresses results in the You can store the result in a named variable as follows:
form 1/a*b, whereas we would normally write >> syms x
>> x = solve(x+5)
b/a. x = -5
10-
10-15 Z.R.K More? See page 595. 10-
10-16 Z.R.K

To solve the equation e2x  3ex 54, the session is


>> solve(’exp(2*x)+3*exp(x)=54’)
Differentiation with the diff function.
ans = >> syms n x y
[ log(-9)] >> diff(x^n)
[ log(6)]
ans =
Other examples:
x^n*n/x
>> eq2 = ’y^2+3*y+2=0’;
>> simplify(ans)
>> solve(eq2)
ans = ans =
[-2] x^(n-1)*n
[-1] >> diff(log(x))
>> eq3 = ’x^2+9*y^4=0’; ans =
>> solve(eq3) % Note that x is presumed to 1/x
% be the unknown variable
ans =
>> diff((sin(x))^2)
[ 3*i*y^2] ans =
[-3*i*y^2] 2*sin(x)*cos(x)
Z.R.K Z.R.K
10-
10-17 10-
10-18

Z.R.K. 2008 Page 3 of 6


Lecture 11 Computer Applications 0933201 Chapter 10

If the expression contains more than one variable, the The function diff(E,n) returns the nth derivative
diff function operates on the variable x, or the variable of the expression E with respect to the default
closest to x, unless told to do otherwise. When there is independent variable.
more than one variable, the diff function computes the
partial derivative.
>> syms x
>> diff(x^3,2)
>> diff(sin(x*y))
ans =
ans =
6*x
cos(x*y)*y
The function diff(E,v,n) returns the nth
The function diff(E,v) returns the derivative of derivative of the expression E with respect to the
the expression E with respect to the variable v. variable v.
>> syms x y >> syms x y
>> diff(x*sin(x*y),y) >> diff(x*sin(x*y),y,2)
ans = ans =
x^2*cos(x*y) -x^3*sin(x*y)
10-
10-19 Z.R.K
10-
10-20 Z.R.K More? See pages 603-606.

Integration with the int function. The form int(E,v) returns the integral of the
>> syms x expression E with respect to the variable v.
>> int(2*x) >> syms n x
ans = >> int(x^n,n)
x^2 ans =
The function int(E) returns the integral of the expression
1/log(x)*x^n
E with respect to the default independent variable.
>> syms n x y The form int(E,a,b) returns the integral of
>> int(x^n) the expression E with respect to the default
ans = independent variable evaluated over the
x^(n+1)/(n+1) interval [a, b], where a and b are numeric
>> int(1/x) expressions.
ans =
>> syms x
log(x)
>> int(cos(x)) >> int(x^2,2,5)
ans = ans =
sin(x) 39
Z.R.K Z.R.K
10-
10-21 10-
10-22

The form int(E,v,a,b) returns the integral of the


expression E with respect to the variable v evaluated over The following session gives an example for
the interval [a, b], where a and b are numeric quantities. which no integral can be found. The indefinite
>> syms x y
>> int(xy^2,y,0,5) integral exists, but the definite integral does not
ans = exist if the limits of integration include the
125/3*x singularity at x 1.
The form int(E,m,n) returns the integral of the
expression E with respect to the default independent >> syms x
variable evaluated over the interval [m, n], where m and n >> int(1/(x-1))
are symbolic expressions. ans =
>> syms t x
>> int(x,1,t) log(x-1)
ans = >>int(1/(x-1),0,2)
1/2*t^2-1/2 ans =
>> int(sin(x),t,exp(t))
ans = NaN
-cos(exp(t)) + cos(t) Z.R.K Z.R.K More? See pages 609-611.
10-
10-23 10-
10-24

Z.R.K. 2008 Page 4 of 6


Lecture 11 Computer Applications 0933201 Chapter 10

Taylor Series. Series summation.


The taylor(f,n,a) function gives the first n-1
The symsum(E,a,b) function returns the
terms in the Taylor series for the function defined
sum of the expression E as the default
in the expression f, evaluated at the point x a. If
symbolic variable varies from a to b.
the parameter a is omitted the function returns the
>> syms k n
series evaluated at x 0.
>> symsum(k,0,10)
>> syms x
ans =
>> f = exp(x);
55
>> taylor(f,4)
>> symsum(k^2, 1, 4)
ans =
ans =
1+x+1/2*x^2+1/6*x^3
30
>> taylor(f,3,2)
>> symsum(k,0,n-1)% try symsum(k,0,n)
ans =
ans =
exp(2)+exp(2)*(x-2)+1/2*exp(2)* ...
1/2*n^2-1/2*n
(x-2)^2
10-
10-25 Z.R.K
More? See page 612. 10-
10-26 Z.R.K More? See page 613.

Finding limits limit(E). Solvingdifferentialequations withthe dsolve function.


The basic form limit(E) finds the limit as x o 0.
>> syms a x The dsolve function’s syntax for solving a single equation is
>> limit(sin(a*x)/x) dsolve (’eqn’). The function returns a symbolic solution
ans = a of the ODE specified by the symbolic expression eqn.
The form limit(E,v,a) finds the limit as X o a. >> dsolve(’Dy+2*y=12’)
>> syms h x ans =
>> limit((x-3)/(x^2-9),3) 6+C1*exp(-2*t)
ans = 1/6
>> limit((sin(x+h)-sin(x))/h,h,0) There can be symbolic constants in the equation.
ans = cos(x) >> dsolve(’Dy=sin(a*t)’)
The forms limit(E,v,a,’right’) and ans =
limit(E,v,a,’left’) specify the direction of the limit. (-cos(a*t)+C1*a)/a
>> syms x Here is a second-order example:
>> limit(1/x,x,0,’left’) >> dsolve(’D2y=c^2*y’)
ans = -inf ans =
>> limit(1/x,x,0,’right’) C1*exp(-c*t) + C2*exp(c*t)
ans = inf Z.R.K More? See page 614. Z.R.K
10-
10-27 10-
10-28

Sets of equations can be solved with dsolve. The For example,


appropriate syntax is dsolve(’eqn1’,’eqn2’,...). >> dsolve(’D2y=c^2*y’,’y(0)=1’,’Dy(0)=0’)
>>[x, y] = dsolve(’Dx=3*x+4*y’,’Dy=-4*x+3*y’) ans =
x = 1/2*exp(c*t)+1/2*exp(-c*t)
Arbitrary boundary conditions, such as y(0) c, can
C1*exp(3*t)*cos(4*t)+C2*exp(3*t)*sin(4*t)
be used.
y =
-C1*exp(3*t)*sin(4*t)+C2*exp(3*t)*cos(4*t) >> dsolve(’Dy+a*y=b’,’y(0)=c’)
ans =
Conditions on the solutions at specified values of the
1/a*b+exp(-a*t)*(-1/a*b+c)
independent variable can be handled as follows. The form
Sets of equations with specified boundary conditions can
dsolve(’eqn’, ’cond1’, ’cond2’,...) be solved as follows. The function
returns a symbolic solution of the ODE specified by the
symbolic expression eqn, subject to the conditions dsolve(’eqn1’,’eqn2’,...,’cond1’,’cond2’,...)
specified in the expressions cond1, cond2, and so on. returns a symbolic solution of a set of equations
If y is the dependent variable, these conditions are specified by the symbolic expressions eqn1, eqn2, and
specified as follows: y(a) = b, Dy(a) = c, D2y(a) = d, so on, subject to the initial conditions specified in the
and so on. expressions cond1, cond2, and so on.
Z.R.K Z.R.K
10-
10-29 10-
10-30

Z.R.K. 2008 Page 5 of 6


Lecture 11 Computer Applications 0933201 Chapter 10

For example,
LAPLACE INVERSE LAPLACE
>> dsolve(’Dx=3*x+4*y’, ...
’Dy=-4*x+3*y’, ’x(0)=0’,’y(0)=1’) TRANSFORM TRANSFORM

[x,y] = >>syms b t >>syms b s


x = exp(3*t)*sin(4*t),... >>laplace(t^3) >>ilaplace(1/s^4)
y = exp(3*t)*cos(4*t) ans = ans =
6/s^4 1/6*t^3
It is not necessary to specify only initial conditions. >>laplace(exp(-b*t)) >>ilaplace(1/(s+b))
The conditions can be specified at different values of t.
ans = ans =
>> dsolve(’D2y+9*y=0’,... 1/(s+b) exp(-b*t)
’y(0)=1’, ’Dy(pi)=2’) >>laplace(sin(b*t)) >>ilaplace(b/(s^2+b^2)
ans = ans = ans =
-2/3*sin(3*t)+cos(3*t) b/(s^2+b^2) sin(b*t)
10-
10-31 Z.R.K More? See pages 615-622. 10-
10-32 Z.R.K More? See pages 622-630.

You can use the inv(A) and det(A)


Characteristic Polynomial and Roots functions to invert and find the determinant of a
>> syms k matrix symbolically.
>> A = [0, 1; -k, -2]; >> inv(A)
>> poly(A) ans =
ans = [ -2/k, -1/k ]
[ 1, 0 ]
x^2 + 2*x + k >> A*ans % verify that the inverse is correct
>> solve(ans) ans =
ans = [ 1, 0 ]
[ -1 + (1-k)^(1/2) ] [ 0, 1 ]
[ -1 - (1-k)^(1/2) ] >> det(A)
ans =
k
10-
10-33 Z.R.K More? See pages 631-634. 10-
10-34 Z.R.K

You can use matrix methods in MATLAB to solve linear


algebraic equations symbolically. You can use the
matrix inverse method, if the inverse exists, or the left-
division method. End of Chapter 10
>> syms c
>> A = sym([2, -3; 5, c]);
>> b = sym([3; 19]);
>> x = inv(A)*b % the matrix inverse method
x =
[3*c/(2*c+15)+ 57/(2*c+15)]
[23/(2*c+15)] Problems Page 636 - 648 !
>> x = A\b % the left-division method
Solve: 3, 4, 12, 18, 22, 23, 28, 31, 32, 37, 41, 50, 51.
x =
[3*(19+c)/(2*c+15)]
[23/(2*c+15)] % note the deference
www.ju.edu.jo\zkhatib
10-
10-35 Z.R.K More? See page 634. Z.R.J.K
Z.R.K

Z.R.K. 2008 Page 6 of 6

You might also like