26
數數數數2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

Embed Size (px)

DESCRIPTION

數值方法 2008, Applied Mathematics NDHU 3 A procedure for CSR ► function Q=CSR(fs,a,b,n) ► fx=inline(fs); ► h=(b-a)/(2*n); ► ind=0:1:2*n; ► Q=fx(a)+fx(b); ► % Red ► ind_odd=1:2:(2*n-1); ► % white ► ind_even=2:2:2*(n-1); ► Q=Q+sum(4*fx(a+ind_odd*h)); ► Q=Q+sum(2*fx(a+ind_even*h)); ► Q=Q/3*h;

Citation preview

Page 1: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 1

Numerical Differentiation

Page 2: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 2

Composite Simpson ruleComposite Simpson ruleh=(b-a)/2nh=(b-a)/2n

Simpson's Rule for Numerical Integration

Page 3: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 3

A procedure for CSRA procedure for CSR► function Q=CSR(fs,a,b,n) function Q=CSR(fs,a,b,n) ► fx=inline(fs);fx=inline(fs);► h=(b-a)/(2*n); h=(b-a)/(2*n); ► ind=0:1:2*n;ind=0:1:2*n;► Q=fx(a)+fx(b);Q=fx(a)+fx(b);► % Red% Red► ind_odd=1:2:(2*n-1); ind_odd=1:2:(2*n-1); ► % white% white► ind_even=2:2:2*(n-1);ind_even=2:2:2*(n-1);► Q=Q+sum(4*fx(a+ind_odd*h));Q=Q+sum(4*fx(a+ind_odd*h));► Q=Q+sum(2*fx(a+ind_even*h));Q=Q+sum(2*fx(a+ind_even*h));► Q=Q/3*h;Q=Q/3*h;

Page 4: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 4

Symbolic DifferentiationSymbolic Differentiation

demo_diff.m

Page 5: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 5

ExampleExample

-5 -4 -3 -2 -1 0 1 2 3 4 5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

function of x:tanh(x)

fx1 =

Inline function: fx1(x) = 1-tanh(x).^2

Page 6: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 6

Numerical differentiationNumerical differentiation

Page 7: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 7

Truncation error Truncation error

The truncation error linearly depends on h

Page 8: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 8

Better approximationBetter approximation

Page 9: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 9

Truncation errorTruncation error

O(h2) : big order of h square The truncation error linearly depends on h2

Page 10: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 10

Richardson extrapolationRichardson extrapolationRichardson extrapolation is with O(h3)Start at the formula that is with O(h2)

Strategy: elimination of h2 term

Page 11: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 11

Halving step-sizeHalving step-size

Page 12: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 12

Richardson extrapolationRichardson extrapolation

Page 13: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 13

Richardson extrapolationRichardson extrapolation

Page 14: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 14

Demo_RichardsonDemo_Richardsondemo_Richardson.m

Page 15: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 15

ExampleExample>>demo_Richardsonfunction of x:sin(x)

fx1 =

Inline function: fx1(x) = cos(x)

h:0.01

Page 16: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 16

Example: differentiation of Example: differentiation of sinsin

[-5 5]h=0.01

-5 -4 -3 -2 -1 0 1 2 3 4 5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 17: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 17

ExerciseExerciseDue to 12/26Due to 12/26

► Implement the Richardson Implement the Richardson extrapolation method for numerical extrapolation method for numerical differentiationdifferentiation

►Give two examples to verify your Give two examples to verify your matlab functions for Richardson matlab functions for Richardson extrapolation implementationextrapolation implementation

Page 18: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 18

Line fittingLine fitting

Page 19: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 19

Problem statementProblem statement►S={(uS={(ui i vvii)})}ii

►Find a line to fit a set of 2D points,Find a line to fit a set of 2D points,

Page 20: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 20

Paired dataPaired data

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 11.8

2

2.2

2.4

2.6

2.8

3

3.2

3.4

3.6

m=100;u=rand(1,m);v=1.5*u+2+rand(1,m)*0.1-0.05;plot(u,v,'.')

Page 21: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 21

Linear modelLinear model

Page 22: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 22

Over-determined Linear Over-determined Linear systemsystem

Page 23: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 23

Form A and bForm A and b

A=[u' ones(100,1)];b=v';

ii

i

vbu

1] [ia

Page 24: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 24

Line fittingLine fitting

>> x=inv(A'*A)*A'*b

x =

1.4816 2.0113

Page 25: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 25

Demo_line_fittingDemo_line_fittingdemo_line_fitting.m

Page 26: 數值方法 2008, Applied Mathematics NDHU 1 Numerical Differentiation

數值方法2008, Applied Mathematics NDHU 26

Stand alone executable fileStand alone executable file

mcc -m demo_line_fitting.m