9 symbolic processing wuth matlab
DESCRIPTION
MatlabTRANSCRIPT
Symbolic Processingwith
MATLAB
Cheng-Liang Chen
PSELABORATORY
Department of Chemical EngineeringNational 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 Expressionssyms x ys = 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 yfindsym(6*b+y)
ans =b, y
findsym(6*b+y,1)
ans =y
findsym(6*b+y+x1,1)
ans = % give one varx1 % closest to x
CL Chen PSE LAB NTU 3
Manipulating and Evaluating Expressions
syms x yE = (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 yexpand((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 yfactor(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 yE1 = 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 xsym2poly(9*x^2+4*x+6)
ans =9 4 6
syms x yE = x^2+6*x+7;F = subs(E,x,y)
F =y^2+6*y+7
CL Chen PSE LAB NTU 6
syms tf = sym(’f(t)’);g = subs(f,t,t+2)-f
g =f(t+2)-f(t)
syms k nkfac = sym(’k!’);E = subs(kfac,k,n-1),...F = expand(E)
E =(n-1)!
F =n!/n
syms a b xE = a*sin(b);F = subs(E,{a,b},{x,2})
F =x*sin(2)
syms xE = 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 xE = 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
x2−6 x+7
x2 −6x
+7
CL Chen PSE LAB NTU 8
Symbolic Expressions and AlgebraTest Your Understanding
T9.1-1Given the expressions: E1 = x3 − 15x2 + 75x− 125 and
E2 = (x + 5)2 − 20x, use MATLAB to
1. Find the product E1E2 and express it in its simplest form.
2. Find the quotient E1/E2 and express it in its simplest form.
3. Evaluate the sum E1 + E2 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 xy = 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 EquationsTest Your Understanding
T9.2-1Use MATLAB to solve the equation
√1− x2 = x.
(ANS: x =√
2/2)
T9.2-2Use 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 EquationsEx: Intersection of Two Circles
We want to find the intersection points oftwo circles. The first circle has a radiusof 2 and is centered at x = 3, y = 5.The second circle has a radius b and iscentered at x = 5, y = 3.1. Find the (x, y) coordinates of the
intersection points in terms of theparameter b.
2. Evaluate the solution for the casewhere b =
√3.
Solution:
(x− 3)2 + (y − 5)2 = 4
(x− 5)2 + (y − 3)2 = b2
⇒ x = 92 −
b2±√−16+24b2−b4
8
CL Chen PSE LAB NTU 13
syms x y bS = 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.98203.2680
yi =4.73203.0180
CL Chen PSE LAB NTU 14
Algebraic and Transcendental EquationsEx: Positioning A Robot Arm
The figure shows a robot arm havingtwo joints and two links. The anglesof rotation of the motors at the joints areθ1 and θ2. From trigonometry we canderive the following expressions for the(x, y) coordinates of the hand.
x = L1 cos θ1 + L2 cos(θ1 + θ2)y = L1 sin θ1 + L2 sin(θ1 + θ2)
Suppose that the link lengths are L1 = 4 feet and L2 = 3 feet.
1. Compute the motor angles required to position the hand at x = 6 feet, y = 2feet.
2. It is desired to move the hand along the straight line where x is constant at 6feet and y varies from y = 0.1 to y = 3.6 feet. Obtain a plot of the requiredmotor 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.298140.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 (de
g)
0 0.5 1 1.5 2 2.5 3 3.5 4−100
−50
0
50
100
y (ft)
θ 2 (de
g)
CL Chen PSE LAB NTU 18
Calculus: Differentiation
syms n x yA = 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 ydiff(sin(x*y))
ans =cos(x*y)*y
syms x ydiff(x*sin(x*y),y)
ans =x^2*cos(x*y)
syms xdiff(x^3,2)
ans =6*x
syms x ydiff(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 field at Fenway Park in Boston.The wall is 310 feet from home plate down the left-field line. Assuming that thebatter 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 theGreen Monster. In addition, find 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) = (v0 cos θ)t y(t) = − gt2
2 + (v0 sin θ)t
⇒ y(t) = −g2
x2(t)
v20 cos2 θ
+ x(t) tan θ
x = d (distance to the wall)
y = h (relative heigh of the wall, = 37− 4 = 33 feet)
⇒ h = −g2
d2
v20 cos2 θ
+ d tan θ
⇒ v20 = g
2d2
cos2 θ(d tan θ−h)(g, d : constants)
f = 1cos2 θ(d tan θ−h)
syms d g h thf=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 or0.8384 % 48 deg.s-0.7324
CL Chen PSE LAB NTU 22
second = diff(f,2,th) % 2nd derivativesecond = subs(second, {th, d, h}, {thmin(1),310,33})v2 = (g*d^2/2)*fv2min = 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: DifferentiationTest Your Understanding
T9.3-1Given that y = sin(3x) cosh(5x), use MATLAB to find
dy/dx at x = 0.2. (ANS: 9.2288)
T9.3-2Given that z = 5 cos(2x) ln(4y), use MATLAB to find
∂z/∂y at x = 0.2. (ANS: 5 cos(2x)/y)
CL Chen PSE LAB NTU 24
Calculus: Integration
syms n x yint(x^n)
ans =x^(n+1)/(n+1)
int(1/x)
ans = % naturallog(x) % log
int(cos(x))
ans =sin(x)
int(sin(y))
ans =-cos(y)
∫ 5
2x2dx =
x3
3
∣∣∣∣32= 39
syms xint(x^2,2,5)
ans =39
syms x a bint(x^2,a,b)
ans =1/3*b^3-1/3*a^3
CL Chen PSE LAB NTU 25
syms x yint(x*y^2,y,0,5)
ans =125/3*x
syms t xint(x,1,t)
ans =1/2*t^2-1/2
syms t xint(sin(x),t,exp(t))
ans =-cos(exp(t))+cos(t)
syms xint(1/(x-1))
ans =log(x-1)
CL Chen PSE LAB NTU 26
Calculus: IntegrationTest Your Understanding
T9.3-3Given that y = x sin(3x), use MATLAB to find
∫ydx.
(ANS: [sin(3x)− 3x cos(3x)]/9)
T9.3-4Given that z = 6y2 tan(8x), use MATLAB to find∫
zdy. (ANS: 2y3 tan(8x))
T9.3-5Use MATLAB to evaluate∫ 5
−2x sin(3x)dx (ANS: 0.6672)
CL Chen PSE LAB NTU 27
Calculus: Taylor Series
f(x) = f(a) +(
dfdx
)∣∣∣x=a
(x− a) + 12
(d2fdx2
)∣∣∣x=a
(x− a)2 + · · ·+
+ 1k!
(dkfdxk
)∣∣∣x=a
(x− a)k + · · ·+ Rn︸︷︷︸1n!
(dnfdxn
)∣∣∣x=b
(x− a)n
sin(x) = x− x3
3! + x5
5! −x7
7! + · · · −∞ < x <∞cos(x) = 1− x2
2! + x4
4! −x6
6! + · · · −∞ < x <∞ex = 1 + x + x2
2! + x3
3! + x4
4! + · · · −∞ < x <∞
syms xf = 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
x−1∑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
n−1∑k=0
k = 0 + 1 + 2 + 3 + · · ·+ n− 1 = 12n
2 − 12n
4∑k=1
k2 = 1 + 4 + 9 + 16 = 30
CL Chen PSE LAB NTU 29
syms k nA = 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
limx→a
E(x)
limx→0
sin(ax)x
= a
limx→3
x− 3x2 − 9
= 16
limx→0
sin(x + h)− sin(x)h
limx→0−
1x
= −∞
limx→0+
1x
= ∞
CL Chen PSE LAB NTU 31
syms a x hA = limit(sin(a*x)/x); % x-->0B = limit((x-3)/(x^2-9),3); % x-->3C = 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 LimitsTest Your Understanding
T9.3-6Use MATLAB to find the first three nonzero terms in
the Taylor series for cos(x). ANS: 1− x2
2 + x4
24
T9.3-7Use MATLAB to find a formula for the sum
m−1∑m=0
m3
ANS: m4/4−m3/2 + m2/4
CL Chen PSE LAB NTU 33
T9.3-8Use MATLAB to evaluate
7∑n=0
cos(nπ)
ANS: 0
T9.3-9Use MATLAB to evaluate
limx→5
2x− 10x3 − 125
ANS: 2/75
CL Chen PSE LAB NTU 34
Calculus: Differential Equations
dy
dt= f(t, y)
d2y
dt2= f(t, y, dy
dt)
dy
dt+ 2y = 12
⇒ y(t) = 6 + C1e−2t
dy
dt= sin(at)
⇒ y(t) = −cos(at)a + C1
d2y
dt2= c2y
⇒ y(t) = C1ect + C2e
−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 Differential Equations
dxdt = 3x + 4ydydt = −4x + 3y
⇒ x(t) = C1e3t cos(4t) + C2e
3t sin(4t)
y(t) = −C1e3t sin(4t) + C2e
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
dydt = sin(bt), y(0) = 0
⇒ y(t) = 1−cos(bt)b
d2ydt2
= c2y, y(0) = 1, y(0) = 0
⇒ y(t) = ect+e−ct
2
dydt + ay = b, y(0) = c
⇒ y(t) = bc +
(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
dydt + 10y = 10 + 4 sin(4t), y(0) = 0
⇒ y(t) = 1− 429 cos(4t) + 10
29 sin(4t)− 2529e−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 50
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
t
1−4/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 ty = 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 50
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
dxdt = 3x + 4y x(0) = 0dydt = −4x + 3y y(0) = 1
⇒ x(t) = e3t sin(4t)
y(t) = e3t 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− y2, 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 ==> dsolveError, (in dsolve/IC) The ’implicit’ option is not
available when giving Initial Conditions.