MATLAB Lecture 7 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr MATLAB Lecture 7-Calculus 微积分 Ref: Symbolic Math Toolbox-Using the Symbolic Math Toolbox Calculus ● Vocabulary ulus微积分 function函数 composite/compound function复合函数 inverse function反函数 limit极限 derivative导数 erentiation微分 differential quotient微商,导数 definite integral不定积分 definite integral定积分 Taylor series泰勒级数 Taylor expansion泰勒展开式 Taylor formula泰勒公式 item/term项 summation求和 accumulate累加 singleton单元素 dimension维数 ndex/subscript/suffix下标 theN- th order difference n阶微分 ● Some functions compose inverse limit diff int symsum taylor *gradient *sum *prod ● Calculus This section explains how to use the Symbolic Math Toolbox to perform many common mathematical operations Y Function composition compose(fg)returns f(g(y)) where f=f(x)and g=gly). Here x is the symbolic variable of f as defined by findsym and y is the symbolic variable of g as defined by findsym. compose(f,g, z) returns f(g(z) where f= f(x),g=g(y), and x and y are the symbolic ariables of f and g as defined by findsym compose(f,g, x, z)returns f(g(z)and makes x the independent variable for f. That is, if f cos(x /t), then compose(f, g, x, z) returns cos(g(z)t) whereas compose (fg, t, z)returns cos(x/g(z) compose(fg, x,y, z)returns f(g(z))and makes x the independent variable for f and y the independent variable for g. For f=cos(x/t) and g= sin(y/u), compose (fg,,y, z)returns s(sin(z/u)/t) whereas compose(f, g, x, u, z)returns cos(sin(y/z)/t) > syms yatu, >>f=1/(1+x2): g=sin(y); h=xt: p=exp(-y/u); > compose(f, g) 1+si > compose(f g, t):%
MATLAB Lecture 7 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec71 MATLAB Lecture 7 – Calculus 微积分 Ref: Symbolic Math Toolbox→Using the Symbolic Math Toolbox →Calculus l Vocabulary: calculus 微积分 function 函数 composite/compound function 复合函数 inverse function 反函数 limit 极限 derivative 导数 differentiation 微分 differential quotient 微商, 导数 indefinite integral 不定积分 definite integral 定积分 Taylor series 泰勒级数 Taylor expansion 泰勒展开式 Taylor formula 泰勒公式 item/term 项 summation 求和 accumulate 累加 singleton 单元素 dimension 维数 index/subscript/suffix 下标 the Nth order difference N 阶微分 l Some functions compose finverse limit diff int symsum taylor *gradient *sum *prod l Calculus This section explains how to use the Symbolic Math Toolbox to perform many common mathematical operations. ² Function composition compose (f,g) returns f(g(y)) where f = f(x) and g = g(y). Here x is the symbolic variable of f as defined by findsym and y is the symbolic variable of g as defined by findsym. compose (f,g,z) returns f(g(z)) where f = f(x), g = g(y), and x and y are the symbolic variables of f and g as defined by findsym. compose (f,g,x,z) returns f(g(z)) and makes x the independent variable for f. That is, if f = cos(x/t), then compose (f,g,x,z) returns cos(g(z)/t) whereas compose (f,g,t,z) returns cos(x/g(z)). compose (f,g,x,y,z) returns f(g(z)) and makes x the independent variable for f and y the independent variable for g. For f = cos(x/t) and g = sin(y/u), compose (f,g,x,y,z) returns cos(sin(z/u)/t) whereas compose (f,g,x,u,z) returns cos(sin(y/z)/t). Examples: >> syms x y z t u; >> f = 1/(1 + x^2); g = sin(y); h = x^t; p = exp(y/u); >> compose(f,g); % 2 1 1+ sin y >> compose(f,g,t); % 2 1 1+ sin t
ATLAB Lecture 7 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr > compose(h, g, x, z) > compose(h g, t, z): %x > compose(h,p, x, y, z) %oe > compose(h, p, t, u, z); %x NOTICE: the order of the codes is important. >>clear; syms x y z: z-exp(y): y=sin(x); compose(z, y),z ans exp(sin(x)) exple) > clear; syms x y z, y=sin(x); z=exp(y ) compose(z, y),z %NOT RECOMMEND exp(sin(sin(x))) exp(sin(x)) > clear; syms uv y z; y=sin(u): z=exp(v); compose(z, y) %RECOMMEND to use different variables as independent variable for different functions ☆ Functional inverse g= inverse(f)returns the functional inverse of f. f is a scalar sym representing a function of exactly one symbolic variable, say 'x'. Then g is a scalar sym that satisfies g(f(x))=x g= inverse(f, v) uses the symbolic variable v, where v is a sym, as the independent variable Then g is a scalar sym that satisfies g(fv))=v. Use this form when f contains more than one symbolic variable > inverse(1/tan(x)): %returns atan( >> inverse(fy): %oreturns-x2+y > inverse(f) Warning: inverse(x 2+y) is not unique >In sym. inverse at 43 (-y+x)^(1/2) Limit of an expression limit(F, x, a)takes the limit of the symbolic expression F asx -a limit(F, a)uses findsym(F)as the independent variable limit(F)uses a=0 as the limit point limit( E,x, a,'right)or limit(F x, a,'left) specify the direction of a one-sided limit
MATLAB Lecture 7 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec72 >> compose(h,g,x,z); %sint z >> compose(h,g,t,z); % sin z x >> compose(h,p,x,y,z); % ( / ) t z u e - >> compose(h,p,t,u,z); % y / z e x - NOTICE: the order of the codes is important. >> clear; syms x y z; z=exp(y); y=sin(x); compose(z, y), z ans = exp(sin(x)) z = exp(y) >> clear; syms x y z; y=sin(x); z=exp(y); compose(z, y), z %NOT RECOMMEND ans = exp(sin(sin(x))) z = exp(sin(x)) >> clear; syms u v y z; y=sin(u); z=exp(v); compose(z, y) %RECOMMEND to use… different variables as independent variable for different functions ² Functional inverse g = finverse(f) returns the functional inverse of f. f is a scalar sym representing a function of exactly one symbolic variable, say 'x'. Then g is a scalar sym that satisfies g(f(x)) = x. g = finverse(f,v) uses the symbolic variable v, where v is a sym, as the independent variable. Then g is a scalar sym that satisfies g(f(v)) = v. Use this form when f contains more than one symbolic variable. Examples: >> finverse(1/tan(x)); %returns atan(1/x). >> f = x^2+y; >> finverse(f,y); %returns x^2+y. >> finverse(f) Warning: finverse(x^2+y) is not unique. > In sym.finverse at 43 ans = (y+x)^(1/2) ² Limit of an expression limit(F,x,a) takes the limit of the symbolic expression F as x → a. limit(F,a) uses findsym(F) as the independent variable. limit(F) uses a = 0 as the limit point. limit(F,x,a,'right') or limit(F,x,a,'left') specify the direction of a onesided limit
ATLAB Lecture 7 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr > syms x a th >>limit(sin(x)/x) %evaluate lim sIn x limit(x-2)/(x/2-4).2) %evaluate lim--f-2 >> limit(1+2*t/x)(3*x),x,inf) %evaluate lim[ 1+ exp(6 t) >> limit(1/x, x, 0,'right) %evaluate lim x→0 >>limit( 1/x, x, 0, left) %evaluate lim >> limit(sin( x+h)-sin(x))h,h, 0) % evaluate lim sin(x+h)sin x h cos(x) >>V=[(1+ax)^x,exp(-x) >>limit(v, x, inf, "left) ans I exp(a) 0] Y Difference and approximate derivative diff (X), for a vector X, is [X(2)-X(1)X(3)-X(2) X(n)-X(n-D)I diff (X), for a matrix X, is the matrix of row differences, X(2: n,: )-X(I: n-1, )1 diff (X, N)is the N-th order difference along the first non-singleton dimension(denote it by dim). If N > size(X, dim), diff takes successive differences along the next non-singleton dimension diff(x, N, DIM) is the nth difference function along dimension dim. Ifn > size(X, dIM) diff returns an empty array
MATLAB Lecture 7 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec73 Examples: >> syms x a t h; >> limit(sin(x)/x) %evaluate 0 sin lim x x Æ x ans = 1 >> limit((x2)/(x^24),2) %evaluate 2 2 2 lim x 4 x Æ x - - ans = 1/4 >> limit((1+2*t/x)^(3*x),x,inf) %evaluate 3 2 lim 1 x x t Æ• x Ê ˆ + Á ˜ Ë ¯ ans = exp(6*t) >> limit(1/x,x,0,'right') %evaluate 0 1 lim x x Æ + ans = Inf >> limit(1/x,x,0,'left') %evaluate 0 1 lim x x Æ - ans = Inf >> limit((sin(x+h)sin(x))/h,h,0) %evaluate 0 sin( ) sin lim h x h x Æ h + - ans = cos(x) >> v = [(1 + a/x)^x, exp(x)]; >> limit(v,x,inf,'left') %evaluate lim 1 , lim x x x x a e x - Æ• Æ• Ê ˆ + Á ˜ Ë ¯ ans = [ exp(a), 0] ² Difference and approximate derivative diff (X), for a vector X, is [X(2)X(1) X(3)X(2) ... X(n)X(n1)]. diff (X), for a matrix X, is the matrix of row differences, [X(2:n,:) X(1:n1,:)]. diff (X,N) is the Nth order difference along the first nonsingleton dimension (denote it by dim). If N >= size(X,dim), diff takes successive differences along the next nonsingleton dimension. diff (X,N,DIM) is the Nth difference function along dimension DIM. If N >= size(X,DIM), diff returns an empty array
ATLAB Lecture 7 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr h=.001:x=0h sin(d)-sin(i-h) > diff(sin(x. 2))/h; i=hh: pi, there are approximation pl >>diff(1: 10). 2) %evaluate(i+D)'-i4, i 3 51719 >>X=375: 257; diff(X) %the same as diff(X, 1, 1) ans diff(X, 1, 2 >> diff(X, 2, 2) %the 2nd order difference along the dimension 2 diff(X,3, 2) Empty matrix: 2-by-0 > syms x y, y=atan((x+l(x-1)) (1/x-1)(x+1)(x-1)^2)/(1+(x+1)^2/(x-1)^2) (-2/x-1)^2+2°(x+1)(x-1)^3)(1+(x+1)^2/x-1)^2)-(1(x-1)-(x+1)(x-1)^2 (1+(x+1)y^2/(x-1)^2)2*(2*(X+1)x-1)^2-2*(x+1)2/(x-1)3) >> syms x y >>z=x^4+y4-cos(2*x+3y) cos(2x+3 >>diff(z, x) 4*x^3+2*sin(2*x+3*y)
MATLAB Lecture 7 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec74 Examples: >> h = .001; x = 0:h:pi; >> diff(sin(x.^2))/h; % 2 2 sin( ) sin( ) , : : i i h i h h pi h - - = , there are approximation … to 2*cos(x.^2).*x, x=0:h:pi >> diff((1:10).^2) % evaluate 2 2 (i +1) -i ,i =1, 2,...,9 ans = 3 5 7 9 11 13 15 17 19 >>X = [3 7 5; 2 5 7]; diff(X) %the same as diff(X,1,1) ans = 1 2 2 >> diff(X,1,2) ans = 4 2 3 2 >> diff(X,2,2) %the 2nd order difference along the dimension 2 ans = 6 1 >> diff(X,3,2) ans = Empty matrix: 2by0 >> syms x y; y=atan((x+1)/(x1)); >> yx=diff(y,x) yx = (1/(x1)(x+1)/(x1)^2)/(1+(x+1)^2/(x1)^2) >> yxx=diff(y,x,2) yxx = (2/(x1)^2+2*(x+1)/(x1)^3)/(1+(x+1)^2/(x1)^2)(1/(x1)(x+1)/(x1)^2)/ (1+(x+1)^2/(x1)^2)^2*(2*(x+1)/(x1)^22*(x+1)^2/(x1)^3) >> syms x y >> z=x^4+y^4cos(2*x+3*y); >> zx=diff(z,x) %compute 4 4 (x y cos(2x 3y )) x ¶ + - + ¶ zx = 4*x^3+2*sin(2*x+3*y) >> zy=diff(z,y) %compute z y ¶ ¶ zy = 4*y^3+3*sin(2*x+3*y)
MATLAB Lecture 7 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr >>zxxdiff(zx, x)% equivalent to zxx=diff(z, x, 2) ZXX- 12*x^2+4*cos(2*x+3y) ◆ Integrate t(s)is the indefinite integral of s with respect to its symbolic variable as defined by findsym. S is a sym(matrix or scalar). If s is a constant, the integral is with respect to'x int(S, v) is the indefinite integral of S with respect to v. v is a scalar sym. int(S, a, b)is the definite integral of S with respect to its symbolic variable from a to b a and b are each double or symbolic scalars int(s, v, a, b)is the definite integral of s with respect to v from a to b > syms x xI alpha t >>A=[cos(x*t), sin(x*t): -sin(x*t), cos(x*t); /(1+x2))%compute dx without constant item C atan(x) > int(sin(alpha*u), alpha)%compute sin(au )da without constant item C 1/u cos(alpha u) > int(x1*log(1+x1),0, 1) %compute L x In(1+x,dx 1/4 t(4*x°tx,2,sin(t) 2°t°(Sint)2-4) > int(exp(O),exp(alpha D) %compute [e'dt, e"dt exp(t), 1/alpha*exp(alpha t) > int(A, t compute[ cos xtd, sin xtd;[-sin xtdt, cos xdr [I/x*sin(x* t),-cos(x*t)x y Taylor series expansion taylor(f) is the fifth order Maclaurin polynomial approximation to f
MATLAB Lecture 7 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec75 >> zxx=diff(zx,x) %compute 2 2 z x ¶ ¶ , equivalent to zxx=diff(z,x,2) zxx = 12*x^2+4*cos(2*x+3*y) ² Integrate int(S) is the indefinite integral of S with respect to its symbolic variable as defined by findsym. S is a sym (matrix or scalar). If S is a constant, the integral is with respect to 'x'. int(S,v) is the indefinite integral of S with respect to v. v is a scalar sym. int(S,a,b) is the definite integral of S with respect to its symbolic variable from a to b. a and b are each double or symbolic scalars. int(S,v,a,b) is the definite integral of S with respect to v from a to b. Examples: >> syms x x1 alpha u t; >> A = [cos(x*t), sin(x*t);sin(x*t), cos(x*t)]; >> int(1/(1+x^2)) %compute 2 1 1 dx + x Ú without constant item C ans = atan(x) >> int(sin(alpha*u),alpha) %compute sin(au)da Ú without constant item C ans = 1/u*cos(alpha*u) >> int(x1*log(1+x1),0,1) %compute 1 1 1 1 0 x ln(1+ x )dx Ú ans = 1/4 >> int(4*x*t,x,2,sin(t)) ans = 2*t*(sin(t)^24) >> int([exp(t),exp(alpha*t)]) %compute [ , ] t t e dt e dt a Ú Ú ans = [ exp(t), 1/alpha*exp(alpha*t)] >> int(A,t) %compute [ cos xtdt, sin xtdt; -sin xtdt, cos xtdt] Ú Ú Ú Ú ans = [ 1/x*sin(x*t), cos(x*t)/x] [ cos(x*t)/x, 1/x*sin(x*t)] ² Taylor series expansion taylor(f) is the fifth order Maclaurin polynomial approximation to f
ATLAB Lecture 7 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr Three additional parameters can be specified, in almost any order taylor(f, n)is the(n-1)-st order Maclaurin polynomial taylor(f, a) is the Taylor polynomial approximation about point a taylor(f x)uses the independent variable x instead of findsym(f) > taylor(exp(-x)) %evaluate the first 6 items of Taylor series expansion at O 1-X+1/2*x^2-1/6*x^3+1/24*x^4-1/120*x^5 > taylor(log(x), 6, 1) %evaluate the first 6 items of Taylor series expansion at 1 l-x+1/2*x^2-1/6*x^3+1/24*x4-1/120*x > taylor(sin(x), 6, pi/2) %evaluate the first 6 items of Taylor series expansion at pi/ 1-1/2*(x-1/2*pi)^2+1/24*(X-1/2pi)4 >>taylor(sin(x)t, t) %evaluate the first 6 items of Taylor series expansion responding ans > taylor(sin(x)t, x)%evaluate the first 6 items of Taylor series expansion responding to x x*t-1/6*x^3+1/120°t*x^5 令 Sum of elements S= sum(X) is the sum of the elements of the vector X If X is a matrix. s is a row vector with the sum over each column If X is floating point, that is double or single, S is accumulated natively, that is in the same class as x and s has the same class as x If X is not floating point, S is accumulated in double and S has class double S= sum(X, DIM) sums along the dimension dim >>X=[012,345] >> sum(X) %evaluate a row vector with the sum over each column ans 357 > sum(X, 1) %evaluate a row vector with the sum over each column > sum(, 2) %evaluate a column vector with the sum over each row ans- Lec7-6
MATLAB Lecture 7 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec76 Three additional parameters can be specified, in almost any order. taylor(f,n) is the (n1)st order Maclaurin polynomial. taylor (f,a) is the Taylor polynomial approximation about point a. taylor (f,x) uses the independent variable x instead of findsym(f). Examples: >> taylor(exp(x)) %evaluate the first 6 items of Taylor series expansion at 0 ans = 1x+1/2*x^21/6*x^3+1/24*x^41/120*x^5 >> taylor(log(x),6,1) %evaluate the first 6 items of Taylor series expansion at 1 ans = 1x+1/2*x^21/6*x^3+1/24*x^41/120*x^5 >> taylor(sin(x),6,pi/2) %evaluate the first 6 items of Taylor series expansion at pi/2 ans = 11/2*(x1/2*pi)^2+1/24*(x1/2*pi)^4 >> taylor(sin(x)*t,t) %evaluate the first 6 items of Taylor series expansion responding … to t ans = sin(x)*t >> taylor(sin(x)*t,x) %evaluate the first 6 items of Taylor series expansion responding … to x ans = x*t1/6*t*x^3+1/120*t*x^5 ² Sum of elements S = sum(X) is the sum of the elements of the vector X. If X is a matrix, S is a row vector with the sum over each column. If X is floating point, that is double or single, S is accumulated natively, that is in the same class as X, and S has the same class as X. If X is not floating point, S is accumulated in double and S has class double. S = sum(X,DIM) sums along the dimension DIM. Examples: >> X = [0 1 2; 3 4 5]; >> sum(X) %evaluate a row vector with the sum over each column ans = 3 5 7 >> sum(X,1) %evaluate a row vector with the sum over each column ans = 3 5 7 >> sum(X,2) %evaluate a column vector with the sum over each row ans = 3
MATLAB Lecture 7 School of Mathematical Sciences Xiamen University http∥gdjpkc.xmu.edu.cr ◆ Symbolic summation symsum(S)is the indefinite summation of s with respect to the symbolic variable determined symsum(S, v)is the indefinite summation with respect to v symsum(S, a, b)and symsum(s, v, a, b) are the definite summation from a to b > syms k n > alsimple( symsum(k)) %evaluate 12+k*(k-1) >>a2-=simple(symsum(k,0, n-1)%evaluate >ik l/2*n°(n-1) >>a3=simple(symsum(k,0, n) %evaluate >ik n°(n+1) le(symsum(k 2, 0, n)) %evaluate 1/6°n°(n+1)°(2*n+1) >>symsum(k,0, 10) %evaluate 510 385 > symsum(k/2, 11, 10) %evaluate >mk2)2%aae∑1 Psi( > symsum(1/k/2, 1, Inf) %evaluate 2 1/6°p^2 Lec7-7
MATLAB Lecture 7 School of Mathematical Sciences Xiamen University http://gdjpkc.xmu.edu.cn Lec77 12 ² Symbolic summation symsum(S) is the indefinite summation of S with respect to the symbolic variable determined by findsym. symsum(S,v) is the indefinite summation with respect to v. symsum(S,a,b) and symsum(S,v,a,b) are the definite summation from a to b. Examples: >> syms k n >> a1=simple(symsum(k)) %evaluate 1 0 k i i -  = a1 = 1/2*k*(k1) >> a2=simple(symsum(k,0,n1)) %evaluate 1 0 n k k -  = a2 = 1/2*n*(n1) >> a3=simple(symsum(k,0,n)) %evaluate 0 n k k  = a3 = 1/2*n*(n+1) >> a4=simple(symsum(k^2,0,n)) %evaluate 2 0 n k k  = a4 = 1/6*n*(n+1)*(2*n+1) >> symsum(k^2,0,10) %evaluate 10 2 k 0 k  = ans = 385 >> symsum(k^2,11,10) %evaluate 10 2 k 11 k  = ans = 0 >> symsum(1/k^2) %evaluate 1 2 k 1 i i  = ans = Psi(1,k) >> symsum(1/k^2,1,Inf) %evaluate 1 2 1 k k •  = ans = 1/6*pi^2