數值方法 NM 5 微分及積分(1) 5.1 Differentiation 5.1.1 Derivative and Integral Calculus is an important tool for most modern professional practices. It is a branch of mathematical sciences to study the “variation” in natural phenomena, of which includes two main components – differentiation and integration. Mathematically, derivative is the change or slope ∆y/∆x (in Cartesian coordinates) of an imaginary tangent at a point on a curve. y f (x i x) f (x i ) x x (5.1) As ∆x approaches zero, ∆y/∆x becomes the so-called derivative, and can then be defined and calculated numerically using the concept of “limit”: dy f (x x) f (x) f (x) lim x0 dx x (5.2) The above derivative is calculated by FORWARD DIFFERENCE scheme. *Note: The first-order derivative can be calculated in three ways: 1). Forward difference scheme: f (x) lim h0 f (x h) f (x) h f (x) f (x h) h (5.4) f (x h) f (x h) 2 2h (5.5) 2). Backward difference scheme: f (x) lim h0 3). Central difference scheme: f (x) lim h 0 (5.3) Integration is the “antiderivative” of a function in calculus, i.e. I b f (t)dt a (5.6) For example, given y(t) as a function of time t (or in term of position x), then its derivative v(t) = dy(t)/dt and the integration of this derivative yields the t y(t ) v(t )dt 0 NM 05 微分及積分(1) (5.7) 1 5.1.2 Single variable in polynomial y = f(x) 5.1.2.1 Rounding error associated with very small increment. Q1: Use Eq. (5.2) to evaluate the f x of f x x 9 , and assess the rounding error due to very small increment h. A1: S5121_Q1.m fclose('all'); all; clc x = h = fx = fx1= clear variables; close 1; logspace(-20,0,21)'; @(x) x.^9; @(x) 9*x.^8; for ii = 1:length(h) dfdx(ii,1) = ( fx(x fx(x) )/h(ii); end + h(ii)) - loglog(h, abs( dfdx - fx1(1) ), '*b') axis([1e-18, 1, 1e-8, 1e4]); xlabel('h value'); ylabel('Error in approximation'); Conclusion: As h goes smaller, error decreases before h 108 , but soars after h 1011 . 5.1.2.2 Differentiation using straight forward simple rule dx n / dx nx n1 For this problems, the function polyder built in MATLAB can be used to compute the coefficients of the derivative polynomial. Another built-in function diff will approximate the numerical derivative. Q2: Find the first-order derivative of y(x) = 2x4 – 7x3 + 5x2 −1 using both MATLAB commands – f polyder and diff with 99 points and 6 points, respectively. (Harman et al., p.574-576) A2: (1) The first derivation of y(x) is y’(x) = 8x3 – 21x2 + 10x (2) S5122_Q2.m NM 05 微分及積分(1) 2 fclose('all'); clear variables; close all; clc f = @(x) 2*x.^4-7*x.^3+5*x.^2-1; % Polynomial % ployder p = [2, -7, 5, 0, -1]; % Coefficient of the polynomial pd = polyder(p); % Take derivative xi = linspace(0,3,100)'; % Assign x coordinates yd = polyval(pd,xi); % Corresponding f'(xi) % diff >> 6-pt x1 = linspace(0,3,6)'; y1 = diff( f(x1) )./diff(x1); % diff >> 99-pt x2 = linspace(0,3,99)'; y2 = diff( f(x2) )./diff(x2); % Plot plot(x1(1:end-1), y1, '^r') hold on plot(x2(1:end-1), y2, 'ok') hold on plot(xi, yd, '-b', 'LineWidth', 1.5) xlabel('x'), ylabel('f^\prime(x)'); legend('diff@6pts','diff@99 pts', 'polyder','Location','NorthWest') 5.1.3 Multivariate function f(x,y) For two variables f f x x , y y f x , y , mathematically the derivative of f x , y is df f f dx dy x y (use chain rule) (5.8) Other useful formulae for differentiation: (1). y u( x)n , then dy u nu n 1 dx x (2). y u ( x)v( x) , then dy v u u v dx x x u v v u u ( x) dy (3). y , then x 2 x v( x) dx v NM 05 微分及積分(1) (5.9) (5.10) (5.11) 3 Q3: Find and plot first-order derivative of xcosnx, using forward difference scheme (Backstrom, 1997). Note: Use n = 1 and h = 0.001. A3: S513_Q3.m fclose('all'); clear variables; close all; clc n = 1; h = 1e-3; f0 = @(x) x.*cos(x).^n; f1 = @(x) cos(x).^n n*x.*sin(x).*cos(x).^(n-1); x = linspace(0,10,51)'; % Exact solution s1 = f1(x); % Numerical result >> Finite difference s2 = (f0(x+h) - f0(x))/h; % Plot - Comparison plot(x, s1, '-b') hold on plot(x, s2, 'or') xlabel('x'); ylabel('f^\prime(x)'); legend('Exact','Numerical','Location','SouthW est') % Plot - Abs error figure plot(x, s2-s1, '-r') xlabel('x'); ylabel('Absolute error'); Exercise: Try different n and h, and observe what would happen. Q4: Plot the second-order derivative of xcos(x), using a central difference scheme. (Backstrom, 1997). A4: S513_Q4.m fclose('all'); clear variables; close all; clc h f0 f1 x = = = = 1e-3; @(x) x.*cos(x); @(x) cos(x) - x.*sin(x); linspace(0,10,51)'; % Exact solution s1 = f1(x); % Numerical result >> Finite difference s2 = (f0(x+h) - f0(x-h))/2/h; % Plot - Comparison plot(x, s1, '-b') NM 05 微分及積分(1) 4 hold on plot(x, s2, 'or') xlabel('x'); ylabel('f^\prime(x)'); legend('Exact','Numerical','Location','SouthW est') % Plot - Abs error figure plot(x, s2-s1, '-r') xlabel('x'); ylabel('Absolute error'); Exercise: Compare the errors between central difference scheme and forward difference scheme. 5.1.4 Mixed Problems in multivariate function (1). Find first-order derivative of f (x) x 2 x 2 1 and compare with exact solution f (x) x 2x . 2 x 1 2 x2 x2 1 1 (2). Find first-order derivative of f (x) 4 2x x3 and compare with exact solution f (x) (x 6) / x 5/2 . (3). Find first-order derivative of f (x) tan(x 2 1) and compare with exact solution f (x) 2x sec2 (x2 1) . (4). Find first-order derivative of x 2 y 2 3 and compare with exact solution f (x) d 2y 3 dy x and f (x) 2 ... 3 dx y dx y (5). Find first-order derivative of f ( ) ln f ( ) 1 x NM 05 微分及積分(1) e x and compare with exact solution x! 【 f () lne lnx lnx! x ln lnx! 】 5 5.2 Extrema of real-values functions 5.2.1 Higher-order derivatives [Landfield and Penny 199, p.122-123] Second-order derivatives are useful for the determination of local maximum and minimum. In central difference scheme, the second-order derivative f (x) can be expressed as f (x) lim h0 f (x h) f (x h) f (x 2h) 2 f (x) f (x 2h) ... lim h0 2h 4h2 (5.12) f (x h) 2 f (x) f (x h) h2 (5.13) or alternatively, f (x) lim h0 To evaluate the approximation error, derivation by Taylor series is suggested. Let us begin with a Taylor series expanding f (x) from f (xi ) to f ( x i h) , f (xi h) f (xi ) hf (xi ) (h2 / 2!) f (xi ) (h3 / 3!) f (xi ) (h4 / 4!) f (4) (xi ) ... (5.14) The above is equivalent to fi 1 fi hf (xi ) (h2 / 2!) f (xi ) (h3 / 3!) f (xi ) (h4 / 4!) f (4) (xi ) ... (5.15) Similarly, we can use the same form to immediately obtain: fi 1 fi hf (xi ) (h2 / 2!) f (xi ) (h3 / 3!) f (xi ) (h4 / 4!) f (4) (xi ) ... (5.16) Subtracting (5.16) from (5.15) gives fi 1 fi 1 2hf (xi ) 2(h3 / 3!) f (xi ) 2(h5 / 5!) f (5) (xi ) ... (5.17) By neglecting terms with an order of h3 and higher, result will render a forward difference approximation, expressed as: f (xi ) fi 1 fi 1 O(h2 ) 2h (5.18) By adding (5.15) and (5.16), we obtain fi1 fi1 2 fi 2(h2 / 2!) f (xi ) 2(h4 / 4!) f (iv) (xi ) ... (5.19) And neglecting terms in h4 and higher, we have f (xi ) fi 1 2 fi fi 1 O(h2 ) h2 (5.20) By taking more terms in the Taylor series, together with those for f(x + 2h) and f(x − 2h) etc., and performing similar manipulations, we can obtain higher derivatives, and perhaps more accurate derivative approximations (see table below). NM 05 微分及積分(1) 6 Multipliers for fi-3 … fi+3 fi-3 fi-2 fi-1 fi fi+1 fi+2 fi+3 Order of error 2hf (xi ) 0 0 -1 0 1 0 0 h2 h2 f (xi ) 0 0 1 -2 1 0 0 h2 2h3 f (xi ) 0 -1 2 0 -2 1 0 h2 h4 f (4) (xi ) 0 1 -4 6 -4 1 0 h2 12hf (xi ) 0 1 -8 0 8 -1 0 h4 12h2 f (xi ) 0 -1 16 -30 16 -1 0 h4 8h3 f (xi ) 1 -8 13 0 -13 8 -1 h4 6h4 f (4) (xi ) -1 12 -39 56 -39 12 -1 h4 Exercise: S521.m Write a MATLAB program to determine the 2nd derivative of y = x7 when x = 1, using: f (x) f (x 2h) 16 f (x h) 30 f (x) 16 f (x h) f (x 2h) f (x h) 2 f (x) f (x h) and f (x) 2 12h2 h 5.2.2 Local extrema (minimum or maximum) for single variable function For a differentiable function y f (x) in an open interval, a local or relative minimum or maximum (including inflection point) exists at a point where f (x) 0 . This is called the necessary condition (充 分條件). Relative minimum at x0: If f (x0 ) 0 and f ( x0 ) 0 (5.21) Relative maximum at x0: If f (x0 ) 0 and f ( x0 ) 0 (5.22) Eqs. (5.21) and (5.22) are called sufficient conditions (必要條件) for local extrema. NM 05 微分及積分(1) 7 5.2.3 Local maximum for multivariate function (Harman et al., 2000, p.581) Necessary condition for the extrema of a given function f(x0) where its critical point is x0 = [x1, x2]: f f f f , f’(x0) 0 find x0 = [x1, x2] x1 x2 x1 x2 (5.23) Sufficient conditions: Let f (x , y) have continuous first-order and second-order partial derivatives in an open region R containing the point ( x 0 , y 0 ) , If f (x0 , y0 ) x 0 , and f (x0 , y0 ) y 0 , (5.24) and the product D (1). D fxx (x0 , y0 ) fyy (x0 , y0 ) fxy (x0 , y0 ) 0 2 (5.25) (a) f (x , y) has local minimum at 0 (x0 , y0 ) , if fxx (x0 , y0 ) 0 (5.26) (b) f (x , y) has local maximum at 0 (x0 , y0 ) , if fxx (x0 , y0 ) 0 (5.27) (2). If D < 0, f (x , y) is neither a minimum or maximum at 0 (x0 , y0 ) , and x 0 is called a saddle point of f (x , y) (3). If D = 0 No information about the critical points of f (x , y) . Higher order (n ≥ 3) is required for judgement. 5.2.4 Useful MATLAB commands fmin (or fminbnd) to find minimum of a single variable function; find find specific value; fzero find zero crossing; fmins find minimum of a multivariate function. NM 05 微分及積分(1) 8 5.2.5 Examples for extrema of a function Q5: Determine the minimum of the following given function: (Harman et al., 2000; p.583-) z f (x , y) x 2 xy y 2 3x 3y 1 (5.28) A5: (1) Anayltical solution: f f 2 x y 3 0; x 2y 3 0 x y x0 , y0 At (x0 , y0 ) 3,3 fxx 2, fyy 2, fxy 1; 3, 3 D fxx fyy fxy2 3 0 >> Local minimum f(−3, 3)= −8 at the critical point. (2) Plot the function S525_Q5a.m fclose('all'); clear variables; close all; clc fn = 'x.^2+x.*y+y.^2+3*x-3*y+1'; % Input >> Function % fn = @(x,y) x.^2+x.*y+y.^2+3*x3*y+1; % Input >> Function x0 = [-5, 5, -5, 5]; % Input >> Bounds for x and y coordinate X = x0(1):0.1:x0(2); X=X'; Y = x0(3):0.1:x0(4); Y=Y'; [x,y] = meshgrid(X,Y); fgrid = eval(fn); % Get value of f(x,y) by "eval" % fgrid = fn(x,y); % Get value of f(x,y) by @(x,y) surfc(x, y, fgrid) xlabel('x'); ylabel('y'); zlabel('f(x,y)') (3) Find the minimum using command fmins S525_Q5b.m fclose('all'); clear variables; close all; clc fn = 'x(1)^2+x(1)*x(2)+x(2)^2+3*x(1)-3*x(2)+1'; xguess = [-1, 1]; xmin = fminsearch(fn, xguess); x(1) = xmin(1); % In f(x,y) corresponds to x x(2 )= xmin(2); % In f(x,y) corresponds to y zmin = eval(fn); >> xguess = -1 1 >> xmin = -3.0000 3.0000 >> zmin = -8.0000 NM 05 微分及積分(1) 9 5.3 Integral & Sums Generally speaking, integral is a sum of a function over a specified range. In addition to do the summing manually, this can be simply done by using the command sum in MATLAB, such as: S(n) n k i 1 k k 2 k 3 ... k n (5.29) i 0 In some cases, a sum tends to approach a value as i increases. Plot the integral for examination, if required. 5.3.1 Convergent series Q6: Sum of a geometric series S(n) n k i 1 k k 2 k 3 ... k n , using k = 0.5, n =20. i 0 A6: S531_Q6.m k = 0.5; n = 20; s = 0; % Method#1 for i = 1:n s = s + k^(i-1); s1(i,1) = s; end % Method#2 s2 = cumsum( k.^(0:1:n-1)' ); plot((0:1:n-1)', s1, '-ob') xlabel('i'); ylabel('Cumulative sum'); grid on; box off axis([0, 20, 0.5, 2.5]) NM 05 微分及積分(1) 10 How can we stop the calculations when a limit is reached? Q7: Sum of a geometric series S(n) n k i 1 k k 2 k 3 ... k n , using k = 0.5. i 0 A7: S531_Q7.m fclose('all'); clear variables; close all; clc k = 0.5; s = 0; for i = 1:1e4 s = s + k^(i-1); s1(i,1) = s; if i > 1 && s1(i)-s1(i-1) < 1e-8; break; end end plot((0:1:i-1)', s1, '-ob') xlabel('i'); ylabel('Cumulative sum'); grid on; box off axis([0, i, 0.5, 2.5]) 5.3.2 Trigonometric series The sine and cosine functions expressed as an infinite series can be calculated numerically using the summing technique: sin( x) x 2i 1 x x 2i x2 x3 x5 (1) (2i 1)! 1! 3! 5! ... i (5.30) i 0 cos( x) x4 (1) (2i)! 1 2! 4! ... i (5.31) i 0 However, rather than summing up terms by terms directly, it may be useful to compare the ratio of two consecutive terms. For example, for sin(x), the ratio gives ti (1)i x 2i 1 (2i 1)! x2 t i 1 (2i 1)! (1)i 1 x 2i 1 (2i 1)2i (5.32) Q8 & A8. Summing up the terms in series for sin(x) S532_Q8.m fclose('all'); clear variables; close all; clc k = 0.5; s = 0; for i = 1:1e4 s = s - k^2/(2*i+1)/2/i; s1(i,1) = s; if i > 1 rel_val = ( s1(i) - s1(i-1) )/s1(i1); if abs(rel_val) < 1e-6; break; end end end plot((0:1:i-1)', s1, '-ob') xlabel('i'); ylabel('Cumulative sum'); grid on; box off axis([0, i, -inf, inf]) NM 05 微分及積分(1) 11