KeytoMatlabExercise5SchoolofMathematicalSciencesXiamenUniversityhttp:/gdjpkc.xmu.edr Key to MatLAB Exercise 5-Symbol Computation >>al=1/4+1/6 >>a2=sym(1/4+1/6 >>a3=1/4+1/6 >>a4=1/4+l1/6;eval(a4) ration >>clear: cl=a+2*a: cl ??? Undefined function or variable 'a' a+2*a Conclusion. It is illegal in the statement of c1=a+2*a" of a)where " a'is not defined before we assign data to variable cl. But"c2=a+2*a' " of b)is right, due to here a' being a character > clear; c3=a+2*a'; eval(c3) 29 Error using al Undefined function or variable 'a > clear; c4=a+2*a, a=3; eval(c4) ans = s>clear; syms a; c5=a+2*a'; eval(c5) 3*a >>clear; syms a; c6=a+2 a; eval(c6) ns 3*a > clear; syms a; c7=sym(a+2*a): subs(c7, a, 2) ans s > clear; syms a; c8=sym(a+2*a'); subs(c8, 2) ans clear; c9=sym( a+2*a+b); c9 Ex5-1
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Key to Ex51 Key to MATLAB Exercise 5 – Symbol Computation 1. >> a1=1/4+1/6 % double >> a2=sym(1/4+1/6) % sym >> a3='1/4+1/6' % char >> a4='1/4+1/6'; eval(a4) % rational 2. 1) a) >> clear; c1=a+2*a; c1 ??? Undefined function or variable 'a'. b) >> clear; c2='a+2*a'; c2 c2 = a+2*a Conclusion. It is illegal in the statement of “c1=a+2*a” of a) where ‘a’ is not defined before we assign data to variable c1. But “c2=a+2*a” of b) is right, due to here ‘a’ being a character. 2) a) >> clear; c3='a+2*a'; eval(c3) ??? Error using ==> eval Undefined function or variable 'a'. b) >> clear; c4='a+2*a'; a=3; eval(c4) ans = 9 3) a) >> clear; syms a; c5='a+2*a'; eval(c5) ans = 3*a b) >> clear; syms a; c6= a+2*a; eval(c6) ans = 3*a c) >> clear; syms a; c7=sym('a+2*a'); subs(c7, a, 2) ans = 6 d) >> clear; syms a; c8=sym('a+2*a'); subs(c8, 2) ans = 6 4) a) >> clear; c9=sym(' a+2*a+b'); c9
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen Univer http:/edjpkc.xmu.ed a+2=*a+b b) n(at2*a+b); subs(c10, 1) =sym( a+2*a+b); subs(cll, a, 1) 22? Undefined function or variable 'a cI (a+2 a+b); subs(c12, a, 1) ans = 3+b e > clear; syms a b: c13=sym(a+2*a+b): subs(c13, a, 1, b, 2) ?? Error using==> sym.subs Too many input arguments f) > clear; syms a b; c14=sym( a+2*a+b); subs(c14, [a, b], [1, sym('pi)D ans >> syms x;fx3-6*x^2+11*x-6;,g=(x-1)(X-2)°(x-3),h=x*x*(x-6)+11 d(h) X^3-6*x^2+11*x-6 > f actori (X-1)+(x-2)°(x-3) orner(f) (x*(x-6)+11)6 x,f[x^(-3) (1/x^3+6Xx^2+12/x+8)^(1/3)
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Key to Ex52 c9 = a+2*a+b b) >> clear; c10=sym('a+2*a+b'); subs(c10, 1) ans = 3*a+1 c) >> clear; c11=sym(' a+2*a+b'); subs(c11, a, 1) ??? Undefined function or variable 'a'. d) >> clear; syms a; c12=sym('a+2*a+b'); subs(c12,a,1) ans = 3+b e) >> clear; syms a b; c13=sym('a+2*a+b'); subs(c13,a,1, b,2) ??? Error using ==> sym.subs Too many input arguments. f) >> clear; syms a b; c14=sym('a+2*a+b'); subs(c14, [a,b], [1, sym('pi')]) ans = 3+pi 3. 1) >> syms x; f=x^36*x^2+11*x6; g=(x1)*(x2)*(x3); h=x*[x*(x6)+11]6; >> expand(g) ans = x^36*x^2+11*x6 >> expand(h) ans = x^36*x^2+11*x6 2) >> f actor(f) ans = (x1)*(x2)*(x3) 3) >> horner(f) ans = x*(x*(x6)+11)6 4. 1) >> syms x; f=[x^(3)+6*x^(2)+12*x^(1)+8]^(1/3) f = (1/x^3+6/x^2+12/x+8)^(1/3) >> simple(f)
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen Univer http:/edjpkc.xmu.ed (2*x+1)3/x3)^(1/3) (1+6*x+12*x22+8*x3)x^3)^(1/3) factor (2*x+1)3/x^3)(1/3) (1/x^3+6/Xx^2+12/x+8)(1/3) combine (1/x^3+6/Xx^2+12/x+8)(1/3) (1/x^3+6/Xx^2+12/x+8)^(1/3) convert(sincos) (1/x^3+6/Xx^2+12/x+8)^(1/3) (1/x^3+6x^2+12/x+8)^(1/3) (1/x^3+6Xx^2+12/x+8)^(1/3) (1/x^3+6Xx^2+12/x+8)^(1/3) (2 y+1)X (2*x+1)^3/x^3)(1/3) etty(f) /1612\1/3 32x XX 12 +8 Omitted 2)to 6) >>clear; syms a x y; s=a*sin(x )+exp(y) ms t; subs(s, y, log(t))
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Key to Ex53 simplify: ((2*x+1)^3/x^3)^(1/3) radsimp: (2*x+1)/x combine(trig): ((1+6*x+12*x^2+8*x^3)/x^3)^(1/3) factor: ((2*x+1)^3/x^3)^(1/3) expand: (1/x^3+6/x^2+12/x+8)^(1/3) combine: (1/x^3+6/x^2+12/x+8)^(1/3) convert(exp): (1/x^3+6/x^2+12/x+8)^(1/3) convert(sincos): (1/x^3+6/x^2+12/x+8)^(1/3) convert(tan): (1/x^3+6/x^2+12/x+8)^(1/3) collect(x): (1/x^3+6/x^2+12/x+8)^(1/3) mwcos2sin: (1/x^3+6/x^2+12/x+8)^(1/3) ans = (2*x+1)/x >> simplify(f) ans = ((2*x+1)^3/x^3)^(1/3) >> pretty(f) / 1 6 12 \1/3 | + + + 8 | | 3 2 x | \ x x / It implies that the result is 1 3 3 2 1 6 12 8 x x x Ê ˆ Á + + + ˜ Ë ¯ . Omitted 2) to 6). 5. >>clear; syms a x y; s=a*sin(x)+exp(y) s = a*sin(x)+exp(y) 1) >> syms t; subs(s, y, log(t)) ans =
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen Univer http:/edjpkc.xmu.ed a°sin(x)+t subs(s,x, yi, (pi/3, 0)) 1/2*a*3~(1/2)+1 > clear; syms a x y; p=a+x y+2 y2+3x2y a+x*y+2*x*y^2+3*x^2y ans 3*x^2*y+(y+2*y^2)*x+a >collect(p, y) 2*x*y^2+(x+3*x^2)y+a >>A=randn(2): syms x:s=3* x+5 > polyvalm(sym2 poly(s), A) 8.743113.0285 Notice: subs(s, A)cannt be used here, because it equals to 3. *A *A+5. [1, 1; 1, 1-2 * > clear; syms x; f-exp(-x) f >subs(f, 0 ans ans 1.0000 >a=[1: 1: 20]; format short; subs(f, a) Columns 1 through 9 36790.1353 0.0498 0.0183 0.0067 0.0025 0.0009 0.00030.0001 Columns 10 through 18 0.0000 0.0000 0.0000 0.0000 0.0000
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Key to Ex54 a*sin(x)+ t 2) >> subs(s,{x,y},{pi/3,0}) ans = 1/2*a*3^(1/2)+1 6. 1) >> clear; syms a x y; p=a+x*y+2*x*y^2+3*x^2*y p = a+x*y+2*x*y^2+3*x^2*y >> collect(p, x) ans = 3*x^2*y+(y+2*y^2)*x+a 2) >> collect(p, y) ans = 2*x*y^2+(x+3*x^2)*y+a 7. >> A=randn(2); syms x; s=3*x^22*x+5; >> polyvalm(sym2poly(s), A) ans = 5.2187 4.6712 8.7431 13.0285 Notice: subs(s, A) cann’t be used here, because it equals to 3.*A.*A+5.*[1, 1; 1, 1]2.*A. 8. >> clear; syms x; f=exp(x) f = exp(x) 1) >> subs(f, 0) ans = 1 2) >> subs(f, eps) ans = 1.0000 3) >> a=[1:1:20]; format short; subs(f, a) ans = Columns 1 through 9 0.3679 0.1353 0.0498 0.0183 0.0067 0.0025 0.0009 0.0003 0.0001 Columns 10 through 18 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen Univer http:/edjpkc.xmu.ed 0.00000.0000 amns 19 through 20 0.00000.0000 syms a bcduvst; s a=la b; cd; s b=luv s t s ats b atu, b+v] [ c+s, d+t] 2) [2*a32*b [2*c,2°d [a*u+b°s,a*v+b Is b [(-b*s+u*d)(a*d-cb),-(b°t-v*d/(a*d-c*b) 1/a*d-c*b)*(c*u-a*s),(a*t-c*v)(a*d-c*b)] [(-s*b+a*t)/(utv*s),(-V°a+ub)(u°t-v*s) [-(s*d-c*t)(u*t-v*s),(u*d-v*c(u*t-V*s) >>s c=s a+i*s b [ c+i*s, d+i*t] I conj(a+i*u), conj(c+i*s)I
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Key to Ex55 0.0000 0.0000 Columns 19 through 20 0.0000 0.0000 9. >> syms a b c d u v s t; s_a=[a b;c d]; s_b=[u v;s t]; 1) >> s_a+s_b ans = [ a+u, b+v] [ c+s, d+t] 2) >> 2 * s_a ans = [ 2*a, 2*b] [ 2*c, 2*d] 3) >> s_a * s_b ans = [ a*u+b*s, a*v+b*t] [ c*u+d*s, c*v+d*t] 4) >> s_a .* s_b ans = [ a*u, b*v] [ c*s, d*t] 5) >> s_a\s_b ans = [ (b*s+u*d)/(a*dc*b), (b*tv*d)/(a*dc*b)] [ 1/(a*dc*b)*(c*ua*s), (a*tc*v)/(a*dc*b)] 6) >> s_a/s_b ans = [ (s*b+a*t)/(u*tv*s), (v*a+u*b)/(u*tv*s)] [ (s*dc*t)/(u*tv*s), (u*dv*c)/(u*tv*s)] 7) >> s_c=s_a+i*s_b s_c = [ a+i*u, b+i*v] [ c+i*s, d+i*t] 8) >> s_c' ans = [ conj(a+i*u), conj(c+i*s)]
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen Univer http:/edjpkc.xmu.ed [conj(b+i°v),conj(d+i°t) ans [b+i*v, d+i*t >> det(s a) a*d-c*b 11 ans c/(a*d-c*b), a/(ad-c"b) 10 1) syms x; b=ll; x > a=[1 2: 34; syms y, y=inv(a) *b 1/2*x Omitted 2)to 6) >>a=[1234];[vdl=eig(a) -0.8246-0.4160 0.5658-0.9094 -0.3723 0 05.3723 > a=sym(1) sym(2); sym(3) sym(4) [3,4 >v, d=eig(a) ans [3/4+14*33^(1/2),314-14*33^(1/2) [5/2+1/2833^(1/2 Ex5-6
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Key to Ex56 [ conj(b+i*v), conj(d+i*t) 9) >> s_c.' ans = [ a+i*u, c+i*s] [ b+i*v, d+i*t] 10) >> det(s_a) ans = a*dc*b 11) >> inv(s_a) ans = [ d/(a*dc*b), b/(a*dc*b)] [ c/(a*dc*b), a/(a*dc*b)] 10. 1) >> syms x ; b=[1;x]; >> a=[1 2; 3 4]; syms y; y=inv(a)*b y = 2+x 3/21/2*x Omitted 2) to 6). 11. 1) >> a=[1 2;3 4]; [v,d]=eig(a) v = 0.8246 0.4160 0.5658 0.9094 d = 0.3723 0 0 5.3723 2) >> a=[sym(1) sym(2); sym(3) sym(4)] a = [ 1, 2] [ 3, 4] >> [v, d]=eig(a) ans = v = [ 1, 1] [ 3/4+1/4*33^(1/2), 3/41/4*33^(1/2)] d = [ 5/2+1/2*33^(1/2), 0]
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen Univer http:/edjpkc.xmu.ed 0,52-1/2*33(1/2) 3) syms x; a=sym(1) sym(x), sym(3)sym(4)) [3,4] >v, d=eig(a) -1/2+1/6(9+12*x)^(1/2,2-12-16*(9+12*x)(1/2 d [5/2+1/2*(9+12*x)(1/2) 0,52-12*(9+12*x)^(1/2 syms stuv, a=[s t; u v]; [ v, d]=eig(a) -(-1/2*s+1/2*v-1/2°(s^2-2*s*v+v2+4*t市u)(1/2) -(-1/2s (1/2)/u d l/2*s+1/2*v+1/2*(S^2-2*s*v+v2+4*t°u)(1/2) 0] 1/2*s+1/2*v-1/2*(s^2-2*s*v+V2+4*t*u)^(1/2) 2. For example 2) > ut=schur(a) ?? Function'schur' is not defined for values of class'sym Error in ==> schur at 30 Ivarargout( 1: nargout)]=builtin(,, varargin( 1); [,u=lu(a) ?? Function lu is not defined for values of class'sym Error in ==> lu at 54 Ivarargout( 1: nargout)=builtin('lu, varargin( ) >u, v, t=svd(a) -40455358483375693164244872262782,-91451429567730445267917697381021] -.91451429567730445267917697381021,40455358483375693164244872262780 [54649857042190426504511884932842 0,36596619062625782042296438426142 -57604843676632079133109858194273,81741556047036327308865238846391] -.81741556047036327308865238846391,-57604843676632079133109858194273] Key to Ex5-7
Key to MATLAB Exercise 5 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Key to Ex57 [ 0, 5/21/2*33^(1/2)] 3) >> syms x ; a=[sym(1) sym(x);sym(3) sym(4)] a = [ 1, x] [ 3, 4] >> [v, d]=eig(a) v = [ 1/2+1/6*(9+12*x)^(1/2), 1/21/6*(9+12*x)^(1/2)] [ 1, 1] d = [ 5/2+1/2*(9+12*x)^(1/2), 0] [ 0, 5/21/2*(9+12*x)^(1/2)] 4) >> syms s t u v; a=[s t;u v]; [v,d]=eig(a) v = [ (1/2*s+1/2*v1/2*(s^22*s*v+v^2+4*t*u)^(1/2))/u, (1/2*s+1/2*v+1/2*(s^22*s*v+v^2+4*t*u)^(1/2))/u] [ 1, 1] d = [ 1/2*s+1/2*v+1/2*(s^22*s*v+v^2+4*t*u)^(1/2), 0] [ 0, 1/2*s+1/2*v1/2*(s^22*s*v+v^2+4*t*u)^(1/2)] 12. For example 2) >> [u,t]=schur(a) ??? Function 'schur' is not defined for values of class 'sym'. Error in ==> schur at 30 [varargout{1:nargout}] = builtin('schur', varargin{:}); >> [l,u]=lu(a) ??? Function 'lu' is not defined for values of class 'sym'. Error in ==> lu at 54 [varargout{1:nargout}] = builtin('lu', varargin{:}); >> [u,v,t]=svd(a) u = [ .40455358483375693164244872262782, .91451429567730445267917697381021] [ .91451429567730445267917697381021, .40455358483375693164244872262780] v = [ 5.4649857042190426504511884932842, 0] [ 0, .36596619062625782042296438426142] t = [ .57604843676632079133109858194273, .81741556047036327308865238846391] [ .81741556047036327308865238846391, .57604843676632079133109858194273]