numerical differentiation code
play

Numerical differentiation: Code numerical_diff.m function [approx - PowerPoint PPT Presentation

Numerical differentiation: Code numerical_diff.m function [approx deriv,error] = ... 1 numerical diff(test case,x,hvals) %function[approx derive,error] = ... 2 numerical diff(test case,x,hvals) %demonstrates errors and convergence rates for


  1. Numerical differentiation: Code numerical_diff.m function [approx deriv,error] = ... 1 numerical diff(test case,x,hvals) %function[approx derive,error] = ... 2 numerical diff(test case,x,hvals) %demonstrates errors and convergence rates for 3 %approximating derivatives of a function f 4 %(specified below) at x for a sequence of 5 %values of hvals 6 %PC MA428 7 . 8 . 9 . 10 September 3, 2019 1 / 5

  2. Numerical differentiation: Code run_numerical_diff.m %This script runs the numerical diff to print out 1 %the error for each h. Test cases: 2 % A - first order forward difference 3 % B - Centred finite difference 4 % C - Richard Extrapolation formula 5 h0 =1; % initial value of h 6 x0 = (1/3)*pi; %point to estimate derivative 7 n = 17; %number of refinements of h 8 hvals=zeros(n,1); 9 for i=1:n 10 hvals(i) = h0*(1/10ˆ(i-1)); 11 end 12 disp(' h Approx Deriv Error'); 13 [approx deriv,error]=numerical diff('C',x0,hvals); 14 disp vec = [hvals,approx deriv,error]; 15 format short e 16 disp(disp vec); 17 September 3, 2019 2 / 5

  3. Numerical differentiation First order forward difference f ′ ( x 0 ) = f ( x 0 + h ) − f ( x 0 ) − h 2 f ′′ ( c x ) , x 0 = π f ( x ) = sin( x ) h 3 , Errors: >> run_numerical_diff h Approx Deriv Error 1.0000e+00 2.2626e-02 4.7737e-01 1.0000e-01 4.5590e-01 4.4098e-02 1.0000e-02 4.9566e-01 4.3384e-03 1.0000e-03 4.9957e-01 4.3310e-04 1.0000e-04 4.9996e-01 4.3302e-05 1.0000e-05 5.0000e-01 4.3301e-06 1.0000e-06 5.0000e-01 4.3303e-07 1.0000e-07 5.0000e-01 4.3007e-08 1.0000e-08 5.0000e-01 3.0387e-09 1.0000e-09 5.0000e-01 4.1370e-08 1.0000e-10 5.0000e-01 4.1370e-08 . . . 1.0000e-16 0 5.0000e-01 September 3, 2019 3 / 5

  4. Numerical differentiation Centered finite difference formula f ′ ( x 0 ) = f ( x 0 + h ) − f ( x 0 − h ) − h 2 2 ( f ′′ ( c 1 ) + f ′′ ( c 2 )) , x 0 = π f ( x ) = sin( x ) 2 h 3 , Errors: >> run_numerical_diff h Approx Deriv Error 1.0000e+00 4.2074e-01 7.9265e-02 1.0000e-01 4.9917e-01 8.3292e-04 1.0000e-02 4.9999e-01 8.3333e-06 1.0000e-03 5.0000e-01 8.3333e-08 1.0000e-04 5.0000e-01 8.3383e-10 1.0000e-05 5.0000e-01 7.8268e-12 1.0000e-06 5.0000e-01 4.1133e-11 1.0000e-07 5.0000e-01 2.9193e-10 . . . 1.0000e-13 4.9960e-01 3.9964e-04 1.0000e-14 4.9960e-01 3.9964e-04 1.0000e-15 5.5511e-01 5.5112e-02 1.0000e-16 0 5.0000e-01 September 3, 2019 4 / 5

  5. Numerical differentiation Richardson Extrapolation formula f ′ ( x 0 ) = 4 3 F ( h 2 ) − 1 3 F ( h ) + O ( h 4 ) , x 0 = π f ( x ) = sin( x ) 3 , Errors: >>run_numerical_diff h Approx Deriv Error 1.0000e+00 4.9899e-01 1.0111e-03 1.0000e-01 5.0000e-01 1.0414e-07 1.0000e-02 5.0000e-01 1.0419e-11 1.0000e-03 5.0000e-01 5.5178e-14 1.0000e-04 5.0000e-01 1.0550e-12 1.0000e-05 5.0000e-01 6.9763e-12 1.0000e-06 5.0000e-01 1.0690e-10 1.0000e-07 5.0000e-01 1.1884e-09 1.0000e-08 5.0000e-01 3.0387e-09 . . . 1.0000e-14 5.1440e-01 1.4403e-02 1.0000e-15 4.0708e-01 9.2918e-02 1.0000e-16 0 5.0000e-01 September 3, 2019 5 / 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend