You are on page 1of 42

Symbolic Processing

with
MATLAB
Cheng-Liang Chen
PSE
LABORATORY
Department of Chemical Engineering
National Taiwan University
CL Chen PSE LAB NTU 1
Symbolic Expression and Algebra
pi = sym(pi)
pi =
pi
sqroot2 = sym(sqrt(2))
sqroot2 =
sqrt(2)
a = 2*sqrt(2),...
b = 3*sqroot2
a =
2.8284
b =
3*2^(1/2)
CL Chen PSE LAB NTU 2
Symbolic Expressions
syms x y
s = x + y;
r = sqrt(x^2+y^2);
s, r
s =
x+y
r =
(x^2+y^2)^(1/2)
syms x; n = 3;
A = x.^((0:n)*(0:n))
A =
[ 1, 1, 1, 1]
[ 1, x, x^2, x^3]
[ 1, x^2, x^4, x^6]
[ 1, x^3, x^6, x^9]
syms b x1 y
findsym(6*b+y)
ans =
b, y
findsym(6*b+y,1)
ans =
y
findsym(6*b+y+x1,1)
ans = % give one var
x1 % closest to x
CL Chen PSE LAB NTU 3
Manipulating and Evaluating Expressions
syms x y
E = (x-5)^2+(y-3)^2;
collect(E),collect(E,y)
ans =
x^2-10*x+25+(y-3)^2
ans =
y^2-6*y+(x-5)^2+9
syms x y
expand((x+y)^2),...
expand(sin(x+y))
ans =
x^2+2*x*y+y^2
ans =
sin(x)*cos(y)+cos(x)*sin(y)
syms x y
factor(x^2-1),...
simplify(x*sqrt(x^8*y^2)),...
[r,s]=simple(x*sqrt(x^8*y^2))
ans =
(x-1)*(x+1)
ans =
x*(x^8*y^2)^(1/2)
r =
x^5*y
s =
radsimp
CL Chen PSE LAB NTU 4
syms x y
E1 = x^2+5; E2 = y^3-2;
E3 = x^3+2*x^2+5*x+10;
E4 = 1/(x+6);
S1 = E1+E2; S2 = E1*E2;
S3 = E3/E1;
[S4,S5] = numden(E1+E4);
S1, S2, S3, S4, S5,...
expand(S2), simplify(S3)
S1 =
x^2+3+y^3
S2 =
(x^2+5)*(y^3-2)
S3 =
(x^3+2*x^2+5*x+10)/(x^2+5)
S4 =
x^3+6*x^2+5*x+31
S5 =
x+6
ans =
x^2*y^3-2*x^2+5*y^3-10
ans =
x+2
CL Chen PSE LAB NTU 5
sqroot2 = sym(sqrt(2));
y = 6*sqroot2,...
z = double(y)
y =
6*2^(1/2)
z =
8.4853
poly2sym([2,6,4]),...
poly2sym([2,6,4],y)
ans =
2*x^2+6*x+4
ans =
2*y^2+6*y+4
syms x
sym2poly(9*x^2+4*x+6)
ans =
9 4 6
syms x y
E = x^2+6*x+7;
F = subs(E,x,y)
F =
y^2+6*y+7
CL Chen PSE LAB NTU 6
syms t
f = sym(f(t));
g = subs(f,t,t+2)-f
g =
f(t+2)-f(t)
syms k n
kfac = sym(k!);
E = subs(kfac,k,n-1),...
F = expand(E)
E =
(n-1)!
F =
n!/n
syms a b x
E = a*sin(b);
F = subs(E,{a,b},{x,2})
F =
x*sin(2)
syms x
E = x^2+6*x+7;
G = subs(E,x,2),...
F = class(G)
G =
23
F =
double
CL Chen PSE LAB NTU 7
Plotting Expressions
syms x
E = x^2-6*x+7;
ezplot(E, [-2,6]),...
axis([-2 6 -5 25]),...
ylabel(x^2-6x+7)
2 1 0 1 2 3 4 5 6
5
0
5
10
15
20
25
x
x
2
6 x+7
x
2

6
x
+
7
CL Chen PSE LAB NTU 8
Symbolic Expressions and Algebra
Test Your Understanding
T9.1-1
Given the expressions: E
1
= x
3
15x
2
+ 75x 125 and
E
2
= (x + 5)
2
20x, use MATLAB to
1. Find the product E
1
E
2
and express it in its simplest form.
2. Find the quotient E
1
/E
2
and express it in its simplest form.
3. Evaluate the sum E
1
+ E
2
and x = 7.1 in symbolic form and in
numeric form.
ANS: (x 5)
5
; (x 5); 13671/1000 and 13.6170
CL Chen PSE LAB NTU 9
Algebraic and Transcendental Equations
eq1 = x+5=0;
solve(eq1)
ans =
-5
solve(x+5=0)
ans =
-5
syms x
y = solve(x+5)
y =
-5
solve(exp(2*x)+3*exp(x)=54)
ans =
[ log(6)]
[ log(9)+i*pi]
eq2 = y^2+3*y+2=0;
eq3 = x^2+9*y^4=0;
A2 = solve(eq2),...
A3 = solve(eq3)
A2 =
[ -1]
[ -2]
A3 =
[ 3*i*y^2]
[ -3*i*y^2]
CL Chen PSE LAB NTU 10
A = solve(b^2+8*c+2*b=0),...
B = solve(b^2+8*c+2*b=0,b)
A =
-1/8*b^2-1/4*b
B =
[ -1+(1-8*c)^(1/2)]
[ -1-(1-8*c)^(1/2)]
eq4 = 6*x+2*y=14;
eq5 = 3*x+7*y=31;
S = solve(eq4,eq5),...
Ax = S.x, Ay = S.y
S =
x: [1x1 sym]
y: [1x1 sym]
Ax =
1
Ay =
4
CL Chen PSE LAB NTU 11
Algebraic and Transcendental Equations
Test Your Understanding
T9.2-1
Use MATLAB to solve the equation

1 x
2
= x.
(ANS: x =

2/2)
T9.2-2
Use MATLAB to solve the equation set
x + 6y = a, 2x 3y = 9 for x and y in terms of the
parameter a. (ANS: x = (a +18)/5, y = (2a 9)/15)
CL Chen PSE LAB NTU 12
Algebraic and Transcendental Equations
Ex: Intersection of Two Circles
We want to nd the intersection points of
two circles. The rst circle has a radius
of 2 and is centered at x = 3, y = 5.
The second circle has a radius b and is
centered at x = 5, y = 3.
1. Find the (x, y) coordinates of the
intersection points in terms of the
parameter b.
2. Evaluate the solution for the case
where b =

3.
Solution:
(x 3)
2
+ (y 5)
2
= 4
(x 5)
2
+ (y 3)
2
= b
2
x =
9
2

b
2

16+24b
2
b
4
8
CL Chen PSE LAB NTU 13
syms x y b
S = solve(...
(x-3)^2+(y-5)^2-4,...
(x-5)^2+(y-3)^2-b^2);
S, xn=S.x, yn=S.y,...
xi=subs(xn,b,sqrt(3)),...
yi=subs(yn,b,sqrt(3))
S =
x: [2x1 sym]
y: [2x1 sym]
xn =
[ 9/2-1/8*b^2+1/8*(-16+24*b^2-b^4)^(1/2)]
[ 9/2-1/8*b^2-1/8*(-16+24*b^2-b^4)^(1/2)]
yn =
[ 7/2+1/8*b^2+1/8*(-16+24*b^2-b^4)^(1/2)]
[ 7/2+1/8*b^2-1/8*(-16+24*b^2-b^4)^(1/2)]
xi =
4.9820
3.2680
yi =
4.7320
3.0180
CL Chen PSE LAB NTU 14
Algebraic and Transcendental Equations
Ex: Positioning A Robot Arm
The gure shows a robot arm having
two joints and two links. The angles
of rotation of the motors at the joints are

1
and
2
. From trigonometry we can
derive the following expressions for the
(x, y) coordinates of the hand.
x = L
1
cos
1
+ L
2
cos(
1
+
2
)
y = L
1
sin
1
+ L
2
sin(
1
+
2
)
Suppose that the link lengths are L
1
= 4 feet and L
2
= 3 feet.
1. Compute the motor angles required to position the hand at x = 6 feet, y = 2
feet.
2. It is desired to move the hand along the straight line where x is constant at 6
feet and y varies from y = 0.1 to y = 3.6 feet. Obtain a plot of the required
motor angles as a function of y.
CL Chen PSE LAB NTU 15
Solution:
6 = 4 cos
1
+ 3 cos(
1
+
2
)
2 = 4 sin
1
+ 3 sin(
1
+
2
)
S=solve(4*cos(th1)+3*cos(th1+th2)=6,...
4*sin(th1)+3*sin(th1+th2)=2);
th1d = double(S.th1)*(180/pi),...
th2d = double(S.th2)*(180/pi)
th1d =
-3.2981
40.1680
th2d =
51.3178
-51.3178
CL Chen PSE LAB NTU 16
S=solve(4*cos(th1)+3*cos(th1+th2)=6,...
4*sin(th1)+3*sin(th1+th2)=y,...
th1,th2);
yr = [0.1 : 0.1 : 3.6];
th1r = [subs(S.th1(1),y,yr)
subs(S.th1(2),y,yr)];
th2r = [subs(S.th2(1),y,yr)
subs(S.th2(2),y,yr)];
th1d = th1r*(180/pi); th2d=th2r*(180/pi);
subplot(2,1,1)
plot(yr,th1d, 2,-3.2981,x, 2,40.168,o)
xlabel(y (ft)), ylabel(\theta_1 (deg))
subplot(2,1,2)
plot(yr,th2d,2,51.3178,x,2,-51.3178,o)
xlabel(y (ft)), ylabel(\theta_2 (deg))
CL Chen PSE LAB NTU 17
0 0.5 1 1.5 2 2.5 3 3.5 4
40
20
0
20
40
60
y (ft)

1

(
d
e
g
)
0 0.5 1 1.5 2 2.5 3 3.5 4
100
50
0
50
100
y (ft)

2

(
d
e
g
)
CL Chen PSE LAB NTU 18
Calculus: Dierentiation
syms n x y
A = diff(x^n),...
B = simplify(A)
A =
x^n*n/x
B =
x^(n-1)*n
C = diff(log(x)),...
D = diff((sin(x))^2),...
E = diff(sin(y))
C =
1/x
D =
2*sin(x)*cos(x)
E =
cos(y)
CL Chen PSE LAB NTU 19
syms x y
diff(sin(x*y))
ans =
cos(x*y)*y
syms x y
diff(x*sin(x*y),y)
ans =
x^2*cos(x*y)
syms x
diff(x^3,2)
ans =
6*x
syms x y
diff(x*sin(x*y),y,2)
ans =
-x^3*sin(x*y)
CL Chen PSE LAB NTU 20
Calculus: Ex. Topping the Green Monster
The Green Monster is a wall 37 feet high in left eld at Fenway Park in Boston.
The wall is 310 feet from home plate down the left-eld line. Assuming that the
batter hits the wall 4 feet above the ground, and neglecting air resistance,
determine the minimum speed the batter must give to the ball to hit it over the
Green Monster. In addition, nd the angle at which the ball must be hit.
A baseball trajectory to clear the Green Monster
CL Chen PSE LAB NTU 21
Solution:
x(t) = (v
0
cos )t y(t) =
gt
2
2
+ (v
0
sin)t
y(t) =
g
2
x
2
(t)
v
2
0
cos
2

+ x(t) tan
x = d (distance to the wall)
y = h (relative heigh of the wall, = 37 4 = 33 feet)
h =
g
2
d
2
v
2
0
cos
2

+ d tan
v
2
0
=
g
2
d
2
cos
2
(d tan h)
(g, d : constants)
f =
1
cos
2
(d tan h)
syms d g h th
f=1/(((cos(th))^2)*(d*tan(th)-h));
dfdth = diff(f, th);
thmin = solve(dfdth, th);
thmin = subs(thmin,{d,h},{310,33})
thmin = %radians or
0.8384 % 48 deg.s
-0.7324
CL Chen PSE LAB NTU 22
second = diff(f,2,th) % 2nd derivative
second = subs(second, {th, d, h}, {thmin(1),310,33})
v2 = (g*d^2/2)*f
v2min = subs(v2, {d,h,g}, {310,33,32.2})
vmin = sqrt(v2min)
vmin = double(subs(vmin(1),{th,d,h,g},{thmin(1),310,33,32.2}))
second =
6/cos(th)^4/(d*tan(th)-h)*sin(th)^2
- 4/cos(th)^3/(d*tan(th)-h)^2*sin(th)*d*(1+tan(th)^2)
+ 2/cos(th)^2/(d*tan(th)-h)
+ 2/cos(th)^2/(d*tan(th)-h)^3*d^2*(1+tan(th)^2)^2
- 2/cos(th)^2/(d*tan(th)-h)^2*d*tan(th)*(1+tan(th)^2)
second =
0.0321
v2 =
1/2*g*d^2/cos(th)^2/(d*tan(th)-h)
v2min =
1547210/cos(th)^2/(310*tan(th)-33)
vmin =
31*1610^(1/2)*(1/cos(th)^2/(310*tan(th)-33))^(1/2)
vmin =
105.3613 % (feet/sec; or 72 miles/hour)
CL Chen PSE LAB NTU 23
Calculus: Dierentiation
Test Your Understanding
T9.3-1
Given that y = sin(3x) cosh(5x), use MATLAB to nd
dy/dx at x = 0.2. (ANS: 9.2288)
T9.3-2
Given that z = 5 cos(2x) ln(4y), use MATLAB to nd
z/y at x = 0.2. (ANS: 5 cos(2x)/y)
CL Chen PSE LAB NTU 24
Calculus: Integration
syms n x y
int(x^n)
ans =
x^(n+1)/(n+1)
int(1/x)
ans = % natural
log(x) % log
int(cos(x))
ans =
sin(x)
int(sin(y))
ans =
-cos(y)
_
5
2
x
2
dx =
x
3
3

3
2
= 39
syms x
int(x^2,2,5)
ans =
39
syms x a b
int(x^2,a,b)
ans =
1/3*b^3-1/3*a^3
CL Chen PSE LAB NTU 25
syms x y
int(x*y^2,y,0,5)
ans =
125/3*x
syms t x
int(x,1,t)
ans =
1/2*t^2-1/2
syms t x
int(sin(x),t,exp(t))
ans =
-cos(exp(t))+cos(t)
syms x
int(1/(x-1))
ans =
log(x-1)
CL Chen PSE LAB NTU 26
Calculus: Integration
Test Your Understanding
T9.3-3
Given that y = xsin(3x), use MATLAB to nd
_
ydx.
(ANS: [sin(3x) 3xcos(3x)]/9)
T9.3-4
Given that z = 6y
2
tan(8x), use MATLAB to nd
_
zdy. (ANS: 2y
3
tan(8x))
T9.3-5
Use MATLAB to evaluate
_
5
2
xsin(3x)dx (ANS: 0.6672)
CL Chen PSE LAB NTU 27
Calculus: Taylor Series
f(x) = f(a) +
_
df
dx
_

x=a
(x a) +
1
2
_
d
2
f
dx
2
_

x=a
(x a)
2
+ +
+
1
k!
_
d
k
f
dx
k
_

x=a
(x a)
k
+ + R
n
..
1
n!

d
n
f
dx
n

x=b
(x a)
n
sin(x) = x
x
3
3!
+
x
5
5!

x
7
7!
+ < x <
cos(x) = 1
x
2
2!
+
x
4
4!

x
6
6!
+ < x <
e
x
= 1 + x +
x
2
2!
+
x
3
3!
+
x
4
4!
+ < x <
syms x
f = exp(x);
A = taylor(f,4);
B = taylor(f,3,5);
A,B %taylor(f,n,a)
A =
1+x+1/2*x^2+1/6*x^3
B =
exp(5)+exp(5)*(x-5)+1/2*exp(5)*(x-5)^2
CL Chen PSE LAB NTU 28
Calculus: Sums
x1

x=0
E(x) = E(0) + E(1) + E(2) + + E(x 1)
b

x=a
E(x) = E(a) + E(a + 1) + E(a + 2) + + E(b)
10

k=0
k = 0 + 1 + 2 + 3 + + 9 + 10 = 55
n1

k=0
k = 0 + 1 + 2 + 3 + + n 1 =
1
2
n
2

1
2
n
4

k=1
k
2
= 1 + 4 + 9 + 16 = 30
CL Chen PSE LAB NTU 29
syms k n
A = symsum(k, 0, 10);
B = symsum(k^2, 1, 4);
C = symsum(k, 0, n-1);
D = factor(C);
A, B, C, D
A =
55
B =
30
C =
1/2*n^2-1/2*n
D =
1/2*n*(n-1)
CL Chen PSE LAB NTU 30
Calculus: Limits
lim
xa
E(x)
lim
x0
sin(ax)
x
= a
lim
x3
x 3
x
2
9
=
1
6
lim
x0
sin(x + h) sin(x)
h
lim
x0

1
x
=
lim
x0
+
1
x
=
CL Chen PSE LAB NTU 31
syms a x h
A = limit(sin(a*x)/x); % x-->0
B = limit((x-3)/(x^2-9),3); % x-->3
C = limit((sin(x+h)-sin(x))/h,h,0);
D = limit(1/x,x,0,left);
E = limit(1/x,x,0,right);
A, B, C, D, E
A =
a
B =
1/6
C =
cos(x)
D =
-Inf
E =
Inf
CL Chen PSE LAB NTU 32
Calculus: Series and Limits
Test Your Understanding
T9.3-6
Use MATLAB to nd the rst three nonzero terms in
the Taylor series for cos(x). ANS: 1
x
2
2
+
x
4
24
T9.3-7
Use MATLAB to nd a formula for the sum
m1

m=0
m
3
ANS: m
4
/4 m
3
/2 + m
2
/4
CL Chen PSE LAB NTU 33
T9.3-8
Use MATLAB to evaluate
7

n=0
cos(n)
ANS: 0
T9.3-9
Use MATLAB to evaluate
lim
x5
2x 10
x
3
125
ANS: 2/75
CL Chen PSE LAB NTU 34
Calculus: Dierential Equations
dy
dt
= f(t, y)
d
2
y
dt
2
= f(t, y,
dy
dt
)
dy
dt
+ 2y = 12
y(t) = 6 + C
1
e
2t
dy
dt
= sin(at)
y(t) =
cos(at)
a
+ C
1
d
2
y
dt
2
= c
2
y
y(t) = C
1
e
ct
+ C
2
e
ct
CL Chen PSE LAB NTU 35
A = dsolve(Dy+2*y=12);
B = dsolve(Dy=sin(a*t));
C = dsolve(D2y=c^2*y);
A, B, C
A =
6+exp(-2*t)*C1
B =
-1/a*cos(a*t)+C1
C =
C1*exp(c*t)+C2*exp(-c*t)
CL Chen PSE LAB NTU 36
Calculus: Sets of Dierential Equations
dx
dt
= 3x + 4y
dy
dt
= 4x + 3y
x(t) = C
1
e
3t
cos(4t) + C
2
e
3t
sin(4t)
y(t) = C
1
e
3t
sin(4t) + C
2
e
3t
cos(4t)
[x, y] = dsolve(Dx=3*x+4*y, Dy=-4*x+3*y)
x =
-exp(3*t)*(C1*cos(4*t)-C2*sin(4*t))
y =
exp(3*t)*(C1*sin(4*t)+C2*cos(4*t))
CL Chen PSE LAB NTU 37
Calculus: Specifying Initial and Boundary Conditions
dy
dt
= sin(bt), y(0) = 0
y(t) =
1cos(bt)
b
d
2
y
dt
2
= c
2
y, y(0) = 1, y(0) = 0
y(t) =
e
ct
+e
ct
2
dy
dt
+ ay = b, y(0) = c
y(t) =
b
c
+
_
c
b
a
_
e
at
A=dsolve(Dy=sin(b*t),y(0)=0);
B=dsolve(D2y=c^2*y,y(0)=1,Dy(0)=0);
C=dsolve(Dy+a*y=b,y(0)=c);
A, B, C
A =
-1/b*cos(b*t)+1/b
B =
1/2*exp(c*t)+1/2*exp(-c*t)
C =
b/a+exp(-a*t)*(-b+c*a)/a
CL Chen PSE LAB NTU 38
Calculus: Specifying Initial and Boundary Conditions
dy
dt
+ 10y = 10 + 4 sin(4t), y(0) = 0
y(t) = 1
4
29
cos(4t) +
10
29
sin(4t)
25
29
e
10t
y = dsolve(...
Dy+10*y=10+4*sin(4*t),...
y(0)=0)
ezplot(y),...
axis([0 5 0 2])
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
14/29 cos(4 t)+10/29 sin(4 t)25/29 exp(10 t)
y =
1-4/29*cos(4*t)+10/29*sin(4*t)-25/29*exp(-10*t)
CL Chen PSE LAB NTU 39
syms t
y = dsolve(...
Dy+10*y=10+4*sin(4*t),...
y(0)=0)
x = [0:0.05:5];
P = subs(y,t,x);
plot(x,P),...
xlabel(t), ylabel(y),...
axis([0 5 0 2])
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
y
y =
1-4/29*cos(4*t)+10/29*sin(4*t)-25/29*exp(-10*t)
CL Chen PSE LAB NTU 40
Calculus: Eq. Sets with Boundary Cond.s
dx
dt
= 3x + 4y x(0) = 0
dy
dt
= 4x + 3y y(0) = 1
x(t) = e
3t
sin(4t)
y(t) = e
3t
cos(4t)
[x,y]=dsolve(Dx=3*x+4*y,Dy=-4*x+3*y,x(0)=0,y(0)=1)
x =
exp(3*t)*sin(4*t)
y =
exp(3*t)*cos(4*t)
y = dsolve(D2y+9*y=0, y(0)=1, Dy(pi)=2)
y =
-2/3*sin(3*t)+cos(3*t)
CL Chen PSE LAB NTU 41
Calculus: Solving Nonlinear Equations
dy
dt
= 4 y
2
, y(0) = 1
A = dsolve(Dy=4-y^2, y(0)=1);
A, B = simple(A)
A =
(-6*exp(4*t)+2)/(-1-3*exp(4*t))
B =
(6*exp(4*t)-2)/(1+3*exp(4*t))
dsolve(D2y+9*sin(y)=0, y(0)=1, Dy(0)=0);
??? Error using ==> dsolve
Error, (in dsolve/IC) The implicit option is not
available when giving Initial Conditions.

You might also like