Lec 08. Symbolic Math Toolbox I

Download Lec 08. Symbolic Math Toolbox I

Post on 15-Jun-2015

848 views

Category:

Education

27 download

TRANSCRIPT

  • 1. MATLAB ProgrammingSymbolic Math Toolbox I kmste2@kaist.ac.kr1

2. MATLAB ProgrammingSymbolic Math Toolbox , >> x = 1; >> y = x + x y= 2y = x + x y = 2*x ?Symbolic Math Toolbox ! Symbolic Expression 2 3. MATLAB Programming Symbolic Expression Symbolic Expression syms x, y, z symbolic >> syms x y z Symbolic Expression , symbol >> y = x + x + 2*z y= 2*x + 2*z3 4. MATLAB Programming Symbolic Expression Symbolic Symbolic Expression >> syms x >> y = x + k Undefined function or variable 'k'. Symbol , >> syms x >> class(x) ans = sym >> x = single(10.3125); >> class(x) ans = single 4 5. MATLAB ProgrammingSymbolic Toolbox Calculus , , , , Linear Algebra, , determinant, canonical forms, Simplification, , , Equation Solving Variable-Precision Arithmetics Transformation Special Functions, , Fourier Transform, Laplace Transform, Z-transform, 5 6. MATLAB Programmingsym S = sym(A) A symbolic number/variable/object A , symbolic representation >> x = magic(2); >> xsym = sym(x) xsym = [ 1, 3] [ 4, 2] >> class(xsym) ans = sym6 7. MATLAB Programmingsym x = sym(x) x symbolic variable x= sym(x, real)x symbolic variable >> syms x real real >> x = sym(x, unreal)k = sym(k, positive) symbolic variable k >> A = sym('A', [2 3]) A= [ A1_1, A1_2, A1_3] [ A2_1, A2_2, A2_3] >> A = sym('A', [1 3])x = sym(x, clear) symbolic variable x real, positive A = sym(A, [M N]) A M x N symbolic variable Ai_j . Ak [ A1, A2, A3]A = sym(A, N) A N x N symbolic variable Ai_j . Ak A=A=>> A = sym('A', 2)[ A1_1, A1_2] [ A2_1, A2_2] 7 8. MATLAB Programmingsym A = sym(A_%d_%d, [M N]) A_i_j , A M x N symbolic variable A_k A(i, j) S = sym(A, flag) A , flag r, f, e, d r : rational (default) f : floating point e : estimated error d : decimal >> r = sym(1/3) r = 1/3>> A = sym('A_%d_%d', [3 2]) A= [ A_1_1, A_1_2] [ A_2_1, A_2_2] [ A_3_1, A_3_2] >> B = sym(B_%d_%d', [3 1]) B= B_1_1 B_2_1 B_3_1>> f = sym(1/3, 'f') f = 6004799503160661/18014398509481984>> A(2,2)>> d = sym(1/3, 'd') d = 0.33333333333333331482961625624739ans =>> e = sym(1/3, 'e') e = 1/3 - eps/12A_2_2 8 9. MATLAB Programming , 2 x 3 A, B, c, d symbolic variable , >> A = sym('A_%d_%d', [2, 3]); >> B = sym('B_%d_%d', [2, 3]); >> syms c d % >> A = sym(A, real); A, >> B = sym(B, real);B >> A+B ans = [ A_1_1 + B_1_1, A_1_2 + B_1_2, A_1_3 + B_1_3] [ A_2_1 + B_2_1, A_2_2 + B_2_2, A_2_3 + B_2_3] >> A-B ans = [ A_1_1 - B_1_1, A_1_2 - B_1_2, A_1_3 - B_1_3] [ A_2_1 - B_2_1, A_2_2 - B_2_2, A_2_3 - B_2_3]9 10. MATLAB Programming ( /) >> c*A+d ans = [ d + A_1_1*c, d + A_1_2*c, d + A_1_3*c] [ d + A_2_1*c, d + A_2_2*c, d + A_2_3*c] >> c./A ans = [ c/A_1_1, c/A_1_2, c/A_1_3] [ c/A_2_1, c/A_2_2, c/A_2_3] >> A .* B ans = [ A_1_1*B_1_1, A_1_2*B_1_2, A_1_3*B_1_3] [ A_2_1*B_2_1, A_2_2*B_2_2, A_2_3*B_2_3] >> A * B' ans = [ A_1_1*B_1_1 + A_1_2*B_1_2 + A_1_3*B_1_3, A_1_1*B_2_1 + A_1_2*B_2_2 + A_1_3*B_2_3] [ A_2_1*B_1_1 + A_2_2*B_1_2 + A_2_3*B_1_3, A_2_1*B_2_1 + A_2_2*B_2_2 + A_2_3*B_2_3]10 11. MATLAB Programming >> A = sym('A_%d_%d', [2 2]); >> inv(A) ans = 1=1 [ A_2_2/(A_1_1*A_2_2 - A_1_2*A_2_1), -A_1_2/(A_1_1*A_2_2 - A_1_2*A_2_1)] [ -A_2_1/(A_1_1*A_2_2 - A_1_2*A_2_1), A_1_1/(A_1_1*A_2_2 - A_1_2*A_2_1)] >> pretty(ans)Determinant >> det(A) ans = A_1_1*A_2_2 - A_1_2*A_2_111 12. MATLAB ProgrammingComplex Value real , i >> x = sym('x', 'real'); >> class(z) >> y = sym('y', 'real'); >> z = x + i * yans = symz=>> conj(z) ans = x - y*ix + y*i real , >> x = sym('x'); >> y = sym('y'); >> z = x+y*i z= x + y*i >> conj(z) ans = conj(x) - conj(y)*i 12 13. MATLAB Programming >> syms f(t) >> syms g(x,y) >> f(t) = 1/2*9.8*t^2 f(t) = (49*t^2)/10 >> g(x,y) = cos(x)*cos(y)+sin(x)*sin(y) g(x, y) = cos(x)*cos(y) + sin(x)*sin(y) >> f(5) ans = 245/2 >> g(3,4) ans = cos(3)*cos(4) + sin(3)*sin(4)13 14. MATLAB Programming >> syms f(t) >> syms g(x,y) >> f(t) = 1/2*9.8*t^2 f(t) =>> ezplot( f ) >> t = linspace(-2*pi, 2*pi, 50); >> plot( t, f(t) )(49*t^2)/10 >> g(x,y) = cos(x)*cos(y)+sin(x)*sin(y) g(x, y) = cos(x)*cos(y) + sin(x)*sin(y) >> ezsurf(g) >> t = linspace(-2*pi,2*pi,50) >> [X,Y] = meshgrid( t ); >> Z = double( g(X,Y) ); >> surf(X,Y,Z)14 15. MATLAB Programmingsym Expression y = sym(5*x^2 3)>> y = sym('5*x^2+3') y= 5*x^2 + 3 >> ezplot(y)15 16. MATLAB Programmingsym Expression expression ) y = sym(5*x^2 3 = 2)>> y = sym('5*x^2+3=2') y= 5*x^2 + 3 == 2 >> solve(y) ans = (5^(1/2)*i)/5 -(5^(1/2)*i)/516 17. MATLAB Programming expression , , >> z1 = sym('3*(1-x)^2+5*x-(y+1)^2'); >> z2 = sym('y^2-5*x*y+2*(x+y)-5'); >> z1 z1 = 5*x + 3*(x - 1)^2 - (y + 1)^2 >> z2 z2 = 2*x + 2*y - 5*x*y + y^2 - 5 >> z1+z2 ans = 7*x + 2*y + 3*(x - 1)^2 - (y + 1)^2 - 5*x*y + y^2 - 5 >> z1-z2 ans = 3*x - 2*y + 3*(x - 1)^2 - (y + 1)^2 + 5*x*y - y^2 + 5 >> z1*z2 ans = (5*x + 3*(x - 1)^2 - (y + 1)^2)*(2*x + 2*y - 5*x*y + y^2 - 5)17 18. MATLAB ProgrammingSymbolic Math , diff symbolic math >> help diff symbolic math diff help toolbox ) Overloaded methods: sym/diff >> help sym/diff 18 19. MATLAB ProgrammingSymbolic Math f(x) 1. symbolic >> syms x n 2. >> f = x^n 3. sym/diff f(x) >> df = diff( f ) df = n*x^(n - 1)>> df = diff( f, x ) df = n*x^(n - 1) 19 20. MATLAB ProgrammingSymbolic Math f(x) 1. symbolic >> syms x nx ? 2. symbolic toolbox x , x x >> f = x^n 3. sym/diff f(x) >> df = diff( f ) df = n*x^(n - 1)>> df = diff( f, x ) df = n*x^(n - 1) 20 21. MATLAB Programming n diff( , , n) >> syms a b t >> g = cos(a*t+b); >> dg1 = diff(g, t)>> syms x n >> f = x^n; >> df = diff(f, x, 2)dg1 =df = n*x^(n - 2)*(n - 1) f 2 >> dg2 = diff(gd1, t)>> df = diff(f, x, 3) df = n*x^(n - 3)*(n - 1)*(n - 2)-a*sin(b + a*t)dg2 = f 3 -a^2*cos(b + a*t) g 1 g 1 1 >> gd = diff(g, 2) gd = g 2 -a^2*cos(b + a*t)21 22. MATLAB Programming A = diff( ) m x n , , A >> syms a b x >> M = [a*x^2+b x*exp(-a*x); cos(a*x) -sin(a*x+b)] M= [ a*x^2 + b, x*exp(-a*x)] [ cos(a*x), -sin(b + a*x)] >> gM = diff(M, x, 1) gM = [ 2*a*x, exp(-a*x) - a*x*exp(-a*x)] [ -a*sin(a*x), -a*cos(b + a*x)] 22 23. MATLAB Programming n , >> syms x y >> f = x^2*exp(-y^2)-y/x; >> fx = diff(f, x) fx = 2*x*exp(-y^2) + y/x^2 >> fxy1 = diff(fx, y) fxy1 = 1/x^2 - 4*x*y*exp(-y^2) >> fxy2 = diff(fy, x)>> fy = diff(f, y)fxy2 =fy =1/x^2 - 4*x*y*exp(-y^2)- 1/x - 2*x^2*y*exp(-y^2)23 24. MATLAB Programming P(t) = (x(t), y(t), z(t)) v(t) = [x(t0), y(t0), z(t0)] P(t) = (cos(t), sin(t), t/2pi) . syms t x = cos(t); y = sin(t); z = t/(2*pi); m = 0:0.1:4*pi; plot3( subs(x,m), subs(y,m), subs(z,m) ); hold on; dx = diff(x); dy = diff(y); dz = diff(z); for s = linspace(0, 4*pi, 20) p = [subs(x,s), subs(y,s), subs(z,s)]; v = [subs(dx,s), subs(dy,s), subs(dz,s)]; nv = v / norm(v); ar = [p' p'+nv']; plot3(ar(1,:), ar(2,:), ar(3,:), 'color', 'r'); end grid on; axis on;24 25. MATLAB Programming >> syms x a t h; >> limit(sin(x)/x) ans = 1limit( F, x, a ) F x a limit( F ) F a 0 limit( F, x, a, right ) F x a limit( F, x, a, left ) F x a lim () 0lim () lim ()lim ()+MATLAB limit( f ) limit( f, x, 0 ) limit( f, x, a ) limit( f, a) limit( f, x, a, left ) limit( f, x, a, right )>> limit((x-2)/(x^2-4),2) ans = 1/4sin 0 lim lim2 2 2 42 >> limit((1+2*t/x)^(3*x),x,inf) lim 1 + ans = exp(6*t)>> limit(1/x,x,0,'right') ans = Inf >> limit(1/x,x,0,'left') ans = -Inflim+0lim031 1 >> limit((sin(x+h)-sin(x))/h,h,0) sin + sin ans = lim 0 cos(x) >> v = [(1 + a/x)^x, exp(-x)]; >> limit(v,x,inf,'left') ans = [ exp(a), 0] lim 1 + lim 25 26. MATLAB Programming Symbolic Math Toolbox MATLAB = lim 1 2 1% MATLAB % Symbolic Math Toolbox>> n = realmax>> syms n >> f = n*(2^(1/n)-1); >> limit(f, inf)n= 1.7977e+308ans = >> x = n*(2^(1/n)-1) log(2) x= 0 26 27. MATLAB ProgrammingQuiz limit . = lim 1 + = lim 1 = =0 4 2 3+1 lim 3 2 +25 1 lim 0 sin 27 28. MATLAB ProgrammingQuiz Sol. limit . = lim 1 + = lim 1 0 2 1 = lim sin 0ans = exp(x) >> syms x >> limit( 1/x, x, 0, 'left' ) = lim 423+1 3 +25 >> syms n x >> limit( (1+x/n)^n, n, inf )ans = -Inf >> syms x >> f = (4*x^2 - 3*x + 1) / (3*x^2 + 2*x - 5); >> limit( f, x, inf ) ans = 4/3 >> syms x >> limit( (exp(x)-1) / sin(x), x, 0 ) ans = 128 29. MATLAB ProgrammingQuiz . = lim 0+ () = diff , .29 30. MATLAB ProgrammingQuiz Sol. . = lim 0+ () = diff , . >> syms x >> diff(cos(x)) ans = -sin(x)>> syms x h >> limit( (cos(x+h) - cos(x))/h, h, 0 ) ans = -sin(x) 30 31. MATLAB Programming>> syms x x1 alpha u t; >> int(1/(1+x^2)) int( int( int( int( F F, v )>> int(x1*log(1+x1),0,1) F v S, a, b ) F [a, b] S, v, a, b )ans = 1/4 >> int(4*x*t,x,2,sin(t)) F v [a, b] ans = -2*t*(cos(t)^2 + 3) () () () ans = atan(x)F)MATLAB int( f ) int( f, x )ans = [ sin(t*x)/x, -cos(t*x)/x] [ cos(t*x)/x, sin(t*x)/x] >> int([exp(t),exp(alpha*t)]) C !!1 1 + 21 1 log 1 + 1 1 0sin 24 cos >> A = [cos(x*t),sin(x*t);-sin(x*t),cos(x*t)]; >> int(A,t)int( f, t )int( f, x, a, b )ans = [ exp(t), exp(alpha*t)/alpha] sin sin cos 31 32. MATLAB ProgrammingQuiz int . = () , = sin 2 = 3 2 2 + 1, 0 2 = tan32 , 0 1 1 = 3 sin , 0 32 33. MATLAB ProgrammingQuiz Sol. int . = 2, = sin = 3 2 2 + 1, 0 2 =tan1 , 320 1 = 3 sin , 0 >> syms x >> int( exp(-x^2), x, -inf, inf ) ans = pi^(1/2) >> syms x >> int( x*sin(x), x) ans = sin(x) - x*cos(x) >> syms x >> int( x^3-2*x^2+x-1, x, 0, 2 ) ans = -4/3 >> syms x >> int(atan(x)/x^(3/2), x, 0, 1) ans = - pi/2 + 2^(1/2)*atan(2^(1/2)*(1/2 - i/2))*(1 + i) + 2^(1/2)*atan(2^(1/2)*(1/2 + i/2))*(1 - i) >> syms x >> int(x^3-exp(x)*sin(x), 0, pi) ans = pi^4/4 - exp(pi)/2 - 1/233 34. MATLAB Programming 34 35. MATLAB Programming (analytic) trapz trapezoidal method quad Simpson quadrature rule quadl Lobatto quadrature rule 35 36. MATLAB Programming >> syms x real >> int( x*log(x), x, 2, 4 ) 4 2 log 01 ans = 14*log(2) 3 2>> quad( @(x) x.*log(x), 2, 4 ) ans = 6.7041 >> syms x real >> int( exp(-x^2)*log(1+x^2), x, 0, 1 ) Warning: Explicit integral could not be found.log 1 + 2 2 2 0 1+ 2 ()ans = int(exp(-x^2)*log(x^2 + 1), x == 0..1) >> quadl( @(x) exp(-x.^2).*log(1+x.^2), 0, 1 ) ans = 0.1539 >> syms x real >> int( sin(x)^2/sqrt(1+cos(x)^2), x, 0, 2*pi ) Warning: Explicit integral could not be found. ans = int(sin(x)^2/(cos(x)^2 + 1)^(1/2), x == 0..2*pi) >> quadl( @(x) sin(x).^2./sqrt(1+cos(x).^2), 0, 2*pi ) ans = 2.847836 37. MATLAB Programming x(t), y(t) t = cos 02 = sin 2 0clear all; n = 1000; x = zeros(1, n); y = zeros(1, n); t = linspace(0, 4*pi, n+1); for i = 1:n x(i) = quadl(@(x) cos(x.^2), t(i), t(i+1), 1e-3); y(i) = quadl(@(x) sin(x.^2), t(i), t(i+1), 1e-3); end x = cumsum(x); y = cumsum(y); plot([-x(end:-1:1) 0 x], [-y(end:-1:1) 0 y]) axis equal;37 38. MATLAB Programming , = , = , 1 0 ( 2 + + ) 23 4 0 ( 2 + cos ) 61>> syms x y real >> f = x^3 + x + y; >> int(int(f,y,0,3),x,1,2)ans = 81/4 >> syms x y real >> f = y^2*exp(x)+x*cos(y); >> int(int(f,x,0,1), y,4,6) ans = (152*exp(1))/3 - sin(4)/2 + sin(6)/2 - 152/3 >> double(ans) ans = 87.298338 39. MATLAB Programming numerical method dblquad, triplequad , 4 0 ( 2 + cos ) 61 0 0 0 ( + ) sin 2 + 2 111>> syms x y z real >> f = (y+z)*sin(pi*(x^2+y^2)); >> int(int(int(f,x,0,1),y,0,1),z,0,1)>> fxy = @(x, y) (y^2*exp(x) + x*cos(y)); >> dblquad(fxy, 0, 1, 4, 6) ans =87.2983ans =>> f = @(x,y,z) (y+z).*sin(pi*(x.^2+y.^2)); >> triplequad(f,0,1,0,1,0,1)(fresnelC(2^(1/2))*(2^(1/2) + pi*fresnelS(2^(1/2))))/(2*pi)ans =>> double(ans) ans = 0.30780.307839 40. MATLAB ProgrammingSymbolic : ( 2 + + ) >> syms x y a b c d real >> f = x^3 + x + y; >> int(int(f,y,c,d),x,a,b) ans = ((a - b)*(c - d)*(a^3 + a^2*b + a*b^2 + 2*a + b^3 + 2*b + 2*c + 2*d))/4 eg) 0 0 0 2>> syms r theta z h a real >> V = int(int(int(r,r,0,a),theta,0,2*pi),z,0,h) V= pi*a^2*h 40 41. MATLAB Programming (factor ) >> syms x y >> f = x^3 - 6*x^2 + 11*x - 6; >> factor(f) ans = (x - 3)*(x - 1)*(x - 2) (expand ) >> expand(ans) ans = x^3 - 6*x^2 + 11*x 6 >> expand(cos(x+y)) ans = cos(x)*cos(y) - sin(x)*sin(y) 41 42. MATLAB Programming (simplify ) >> syms x c alpha beta >> simplify(sin(x)^2 + cos(x)^2) ans = 1 >> simplify(exp(c*log(sqrt(alpha+beta)))) ans = (alpha + beta)^(c/2)42 43. MATLAB Programming (collect ) >> syms x y >> collect(x^2*y + y*x - x^2 - 2*x) ans = (y - 1)*x^2 + (y - 2)*xcollect(S, v) : S v polynomial >> syms x >> f = -1/4*x*exp(-2*x)+3/16*exp(-2*x) f= (3*exp(-2*x))/16 - (x*exp(-2*x))/4 >> collect(f, exp(-2*x)) ans = (3/16 - x/4)*exp(-2*x) 43 44. MATLAB Programming (pretty ) >> syms x y >> f = 1/(x-1)*sin(x) f= sin(x)/(x - 1) >> pretty(f) sin(x) -----x-1>> syms x >> int(sqrt(tan(x))) ans = (2^(1/2)*(log(2^(1/2)*tan(x)^(1/2) - tan(x) - 1) - log(tan(x) + 2^(1/2)*tan(x)^(1/2) + 1)))/4 + (2^(1/2)*(atan(2^(1/2)*tan(x)^(1/2) - 1) + atan(2^(1/2)*tan(x)^(1/2) + 1)))/2 >> pretty(ans) 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 1/2 2 (log(2 tan(x) - tan(x) - 1) - log(tan(x) + 2 tan(x) + 1)) 2 (atan(2 tan(x) - 1) + atan(2 tan(x) -------------------------------------------------------------------------- + ---------------------------------------------------------4 2+ 1))44 45. MATLAB Programming subs symbolic substitution subs(S) S MATLAB workspace subs(S, NEW) S (free variable) NEW subs(S, OLD, NEW) S OLD NEW OLD NEW , OLD NEW 1:1 45 46. MATLAB Programming subs( S ) , >> syms a t y C1 >> y = exp(-a*t)*C1 y= C1*exp(-a*t) a, C1 >> a = 980; C1= 3; >> y y= C1*exp(-a*t) workspace a, C1 >> y = subs(y) y= 3*exp(-980*t) 46 47. MATLAB Programming subs( S, NEW ) >> syms a b c x t >> f = a*sin(b*x)*exp(-a*x^c+t) f= a*exp(t - a*x^c)*sin(b*x) >> syms h >> subs(f, h) ans = a*exp(t - a*h^c)*sin(b*h)subs( S, OLD, NEW ) >> syms a b omega >> f = sin(t) f= sin(t) >> f = subs(f, t, omega*t) f= sin(omega*t)47 48. MATLAB Programming substitution>> syms a b alpha >> subs(cos(a)+sin(b),{a,b},[alpha,2]) ans = sin(2) + cos(alpha) substitution>> syms a t >> subs(exp(a*t), a,-magic(2)) ans = [ exp(-t), exp(-3*t)] [ exp(-4*t), exp(-2*t)] substitution>> syms x y >> subs(x*y,{x,y},{[0 1;-1 0],[1 -1;-2 1]}) ans = [ 0, -1] [ 2, 0]48 49. MATLAB ProgrammingSymbolic Summation symsum( f ) (default variable) f(0) + f(1) + + f(n-1) symsum( f, x ) x f(0) + f(1) + + f(n-1) symsum( f, a, b ) (default variable) f(a) + f(a+1) + + f(b) symsum( f, x, a, b ) x f(a) + f(a+1) + + f(b) 49 50. MATLAB ProgrammingSymbolic Summation >> sy...

Recommended

View more >