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

numerical differentiation code
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Numerical differentiation: Code

numerical_diff.m

1

function [approx deriv,error] = ... numerical diff(test case,x,hvals)

2

%function[approx derive,error] = ... numerical diff(test case,x,hvals)

3

%demonstrates errors and convergence rates for

4

%approximating derivatives of a function f

5

%(specified below) at x for a sequence of

6

%values of hvals

7

%PC MA428

8

.

9

.

10

.

September 3, 2019 1 / 5

slide-2
SLIDE 2

Numerical differentiation: Code

run_numerical_diff.m

1

%This script runs the numerical diff to print out

2

%the error for each h. Test cases:

3

% A - first order forward difference

4

% B - Centred finite difference

5

% C - Richard Extrapolation formula

6

h0 =1; % initial value of h

7

x0 = (1/3)*pi; %point to estimate derivative

8

n = 17; %number of refinements of h

9

hvals=zeros(n,1);

10

for i=1:n

11

hvals(i) = h0*(1/10ˆ(i-1));

12

end

13

disp(' h Approx Deriv Error');

14

[approx deriv,error]=numerical diff('C',x0,hvals);

15

disp vec = [hvals,approx deriv,error];

16

format short e

17

disp(disp vec);

September 3, 2019 2 / 5

slide-3
SLIDE 3

Numerical differentiation

First order forward difference

f ′(x0) = f (x0+h)−f (x0)

h

− h

2f ′′(cx),

x0 = π

3 ,

f (x) = sin(x)

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 5.0000e-01

September 3, 2019 3 / 5

slide-4
SLIDE 4

Numerical differentiation

Centered finite difference formula

f ′(x0) = f (x0+h)−f (x0−h)

2h

− h2

2 (f ′′(c1) + f ′′(c2)),

x0 = π

3 ,

f (x) = sin(x)

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 5.0000e-01

September 3, 2019 4 / 5

slide-5
SLIDE 5

Numerical differentiation

Richardson Extrapolation formula

f ′(x0) = 4

3F( h 2) − 1 3F(h) + O(h4),

x0 = π

3 ,

f (x) = sin(x)

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 5.0000e-01

September 3, 2019 5 / 5