Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
EP208 Computational Methods in Physics – Notes on Lecture 2 Numerical Differentiation NUMERICAL DIFFERENTIATION For a function f(x), first and second derivatives are given as follows: Method FDA Truncation Error order First Derivative f ' ( x) f ( x h) h f ( x) h CDA f ' ( x) f ( x h) f ( x h) 2h REA f ' ( x) f ( x 2h) 8 f ( x h) 8 f ( x h) 12h Method h2 f ( x 2h) h4 Error order Second Derivative FDA f ' ' ( x) CDA f ' ' ( x) REA f ( x h) 2 f ( x ) h2 f ( x h) f ( x 2h) 16 f ( x h) 30 f ( x) 16 f ( x h) 12h 2 h2 f ( x 2h) Partial Derivatives for a function f(x, y, z) [CDA only] f x f ( x h, y , z ) f ( x h, y , z ) 2h 2 f x 2 2 f x y f ( x h, y, z ) 2 f ( x, y, z ) h2 f ( x h, y h, z ) f ( x h, y, z ) f ( x h, y h, z ) f ( x h, y h, z ) 4h 2 here h is small but not zero 1 f ( x h, y h, z ) h4 EP208 Computational Methods in Physics – Notes on Lecture 2 Numerical Differentiation EXAMPLE Comparison of the numerical (first) derivative methods for f(x) = ex – 2x at x = 3 with h = 0.01 Method True FDA (single precision) CDA (single precision) REA (single precision) f’(x) 18.085537 18.186188 18.085766 18.085377 Error 0.000000 0.100651 (0.5565%) 0.000229 (0.0012%) 0.000160 (0.0008%) C Pseudo code to compute the first derivative of a function f(x). C The FDA, CDA and REA methods are implemented for comparison. function definition f(x) = ... input "input x ", x input "input h ", h fda cda rea = (f(x+h)-f(x))/h = (f(x+h)-f(x-h))/(2*h) = (f(x-2*h)-8*f(x-h)+8*f(x+h)-f(x+2*h))/(12*h) output "FDA = ", fda output "CDA = ", cda output "REA = ", rea Fortran C++ PROGRAM FirstDerivative IMPLICIT NONE #include <iostream> #include <cmath> using namespace std; REAL :: x, h, fda, cda, rea PRINT READ PRINT READ fda cda rea float f(float x){ float y = exp(x)-2*x; return y; } *,"Input x" *,x *,"Input h" *,h int main(){ float x, h, fda, cda, rea; = (f(x+h)-f(x))/h = (f(x+h)-f(x-h))/(2*h) = (f(x-2*h)-8*f(x-h) + & 8*f(x+h)-f(x+2*h))/(12*h) PRINT *,"FDA = ", fda PRINT *,"CDA = ", cda PRINT *,"REA = ", rea CONTAINS REAL FUNCTION F(x) REAL, INTENT(IN) :: x F = EXP(x)-2*x END FUNCTION << >> << >> "Input x "; x; "Input h "; h; fda cda rea = (f(x+h)-f(x))/h; = (f(x+h)-f(x-h))/(2*h); = (f(x-2*h)-8*f(x-h) + 8*f(x+h)-f(x+2*h))/(12*h); cout << "FDA = " << fda << endl; cout << "CDA = " << cda << endl; cout << "REA = " << rea << endl; END PROGRAM FirstDeivative Input x Input h FDA = CDA = REA = cout cin cout cin return 0; } Input x 3 Input h 0.01 FDA = 18.1862 CDA = 18.0858 REA = 18.0854 3 0.01 18.186188 18.085766 18.085377 2 EP208 Computational Methods in Physics – Notes on Lecture 2 Numerical Differentiation EXAMPLE 2 Comparison of the numerical (second) derivative methods for f(x) = 3sin(x) + x at x = π/4 with h= 0.01 Method True CDA2 (double precision) REA2 (double precision) f’(x) -0.121320343 -0.121302666 -0.121320343 Error 0.000000000 0.000017677 (0.02%) 0.000000000 C Pseudo code to compute the second derivative of a function f(x). C The FDA, CDA and REA methods are implemented for comparison. function definition f(x) = ... input "input x ", x input "input h ", h cda2 rea2 = ( f(x-h) - 2f(x) + f(x+h) ) / h2 = (-f(x-2h)+16f(x-h)-30f(x)+16f(x+h)-f(x+2h) ) / (12h2) output "CDA2 = ", cda2 output "REA2 = ", rea2 Fortran C++ PROGRAM SecondDerivative IMPLICIT NONE INTEGER, PARAMETER :: K = 8 REAL(KIND=K) :: x, h, cda2, rea2 #include <iostream> #include <cmath> using namespace std; #define Real double x = 3.14159265358979323846_K/4.0_K h = 0.01_K Real f(Real x){ Real y = 3.0*sin(x) + x*x; return y; } cda2 =(f(x-h)-2*f(x)+f(x+h))/h**2 rea2 =(-f(x-2*h)+ 16*f(x-h) - & 30*f(x) + 16*f(x+h) - & f(x+2*h))/(12*h**2) int main(){ Real x, h, cda2, rea2; PRINT *,"CDA2 = ", cda2 PRINT *,"REA2 = ", rea2 x = M_PI/4.0; h = 0.01; CONTAINS cda2 = (f(x-h)-2*f(x)+f(x+h))/(h*h); rea2 = (-f(x-2*h) + 16*f(x-h) 30*f(x) + 16*f(x+h) f(x+2*h))/(12*h*h); REAL (KIND=K) FUNCTION F(x) REAL(KIND=K), INTENT(IN) :: x F = 3*sin(x) + x**2 END FUNCTION cout << "CDA2 = " << cda2 << endl; cout << "REA2 = " << rea2 << endl; END PROGRAM SecondDerivative return 0; } if KIND CDA2 REA2 if KIND CDA2 REA2 = 4 = -0.12397766 = -0.12477239 = 8 = -0.12130266594301276 = -0.12132034331665172 if Real CDA2 REA2 if Real CDA2 REA2 3 is float = -0.123978 = -0.124772 is double = -0.121303 = -0.12132 EP208 Computational Methods in Physics – Notes on Lecture 2 Numerical Differentiation Errors in Numerical Derivative The approximation methods FDA, CDA, and REA can be used to demonstrate the effect of truncation errors and round-off errors. The error, for example (h2/6).f```(x) inherent to the CDA method, is an example of a truncation error, i.e. by truncating higher order terms in the Taylor expansion the method becomes only approximate. Another source of error exists when the FDA, CDA or REA are computed; this is the round-off error due to limited precision in numerical arithmetic (numerical values are stored in the computer with a limited number of binary bits). Round-off errors are compounded in arithmetic operations. The total error is therefore a combination of the two error sources: Total Error = Truncation Error + Round-off Error due to limited precision in numerical arithmetic due to truncating higher order terms in Taylor expansion The important parameter here is the value of h; the truncation error increases with increasing h, the round-off error decreases with increasing h Given a particular method, for example the CDA, the most accurate computed derivative is obtained by minimizing the total error, this corresponds to finding the optimal value of h. This optimal value will differ depending on i. ii. iii. The numerical method (FDA, CDA, REA, etc). The function being differentiated, and the value of x. The precision of the arithmetic (single-, double-, quad-precision). To arrive at the optimal value some study of the output of your program is needed. The total error in the CDA is given by: Error = CDA(x) – f`(x) We can form a plot of |Error| against log(h) to indicate the effect of h on error See Fig 2.1. A minimum error exists at some intermediate value of h corresponding to a minimum in the plot. If f`(x) is unknown we can only plot CDA versus h, but, as f'(x) is a constant the plot will have the same shape (only shift up or down). In this case again a minimum (or stationary) value in the plot will be observed corresponding to a minimum error. 4 EP208 Computational Methods in Physics – Notes on Lecture 2 Numerical Differentiation Fig 2.1: Error = CDA(x)- f ’(x) vs h plots for the function f(x) = ex – 2x at x = 3 for single- and double-precision arithmetic. Optimum values of h corresponding to minimum error are about 10–2 and 10–5 respectively. The rise on the left (as h increases) is due to truncation error which has the form of h2 and the rise on the right (as h decreases) is due to round-off errors. 5 EP208 Computational Methods in Physics – Notes on Lecture 2 Numerical Differentiation Exercise 1. Consider the position of a particle in meters is given by function x(t) = −1/t. [ v = x’(t) = 1/t2 and a = x’’(t)= -2/t3 ] 1st derivative of the function at t=3 s is f’(3) = 0.111 111 111 111 111 … m/s 2nd derivative of the function at t=3 is f’(3) = -0.074 074 074 074 074 … m/s2 (a) For the first derivative, compare the accuracy of FDA, CDA and REA. For this, use h = 0.01 s, and double precision data type. (b) For the second derivative, compare the accuracy of FDA, CDA and REA. For this, use h = 0.01 s, and double precision data type. 2. Write a program to find the partial derivatives ∂f/∂x and ∂f/∂y at x = y = 1 for the function f(x,y) = xy + sin(x)/y. 6