alidn i lappliednumerical analysis - دانشگاه صنعتی شریف -...

69
A li dN i l A li dN i l AppliedNumerical AppliedNumerical Analysis Analysis Analysis Analysis Differentiation and Integration Differentiation and Integration Lecturer: Emad Fatemizadeh Lecturer: Emad Fatemizadeh Lecturer: Emad Fatemizadeh Lecturer: Emad Fatemizadeh Applied Numerical Methods Applied Numerical Methods E. Fatemizadeh E. Fatemizadeh

Upload: dinhdien

Post on 26-May-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

A li d N i lA li d N i lApplied NumericalApplied NumericalAnalysisAnalysisAnalysisAnalysis

Differentiation and IntegrationDifferentiation and IntegrationLecturer: Emad FatemizadehLecturer: Emad FatemizadehLecturer: Emad FatemizadehLecturer: Emad Fatemizadeh

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Need for numerical differentiation:Need for numerical differentiation:�� Need for numerical differentiation:Need for numerical differentiation:•• No explicit function (x(t) No explicit function (x(t) �� v(t)=?)v(t)=?)•• Too complex functionToo complex function•• Too complex functionToo complex function

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Initial Ideas:Initial Ideas:�� Initial Ideas:Initial Ideas:� � � � � � � � � �2 31 1

2! 3!f x h f x hf x h f x h f x� �� ���� � � � � ��

� � � � � � � � � �21 12! 3!

f x h f xf x hf x h f x

h� �

� �� ���� � � � ��

� � � � � � � � � �

� � � �

2 31 12 3!

1 1

f x h f x hf x h f x h f x

f x f x h

� �� ���� � � � � �

� �

� � � � � � � � � �

� � � � � � � �

2

2

1 12 3!1

f x f x hf x hf x h f x

hf x h f x h

f h f

� �� ���� � � � �

� � �� ���

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� � � � � � � �2

2 3!f f

f x h f xh

� ���� � ��

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Forward and Backward EstimationForward and Backward Estimation�� Forward and Backward EstimationForward and Backward Estimation

� � � �Forward Estimation:

f x h f x�� � � � � �

Backward Estimation:

f x h f xf x

h� �

� � � � � �f x f x hf x

h� �

� � � � � �Central:

f x h f x hf x

� � ��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� �2

f xh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Richardson Method:Richardson Method:�� Richardson Method:Richardson Method:

� � � � � � � �2 8 8 2f h f h f h f h� � � �� � � � � � � � � �

� � � �54

2 8 8 212

f x h f x h f x h f x hf x

hh f c

� � � � � � � ��

� �30

h f cE �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Example:Example:�� Example:Example:� � � �cos , 0.8, 0.01f h � � �

MethodMethod ForwardForward BackwardBackward AverageAverage RichardsonRichardson

ErrorError --0.00350.0035 0.00350.0035 1.1956e1.1956e--005005 2.3911e2.3911e--010010

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� 22ndnd DerivativeDerivative�� 22 DerivativeDerivative

� � � � � � � �22f x h f x h f x h f x��� � � � � ��� � � � � � � �

� � � � � � � � � � � �422

2

2 112

f x h f x h f x h f x

f x h f x f x hf x h f c

h

� � � � �

� � � ���� � �

� � � � � � � �2

122

hf x h f x f x h

f xh

� � � ���

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Gauss Method:Gauss Method:�� Gauss Method:Gauss Method:•• We have a set of We have a set of xxii and and ffii for for ii==00,,11,…,,…,NN

N� � � �

� �0

1

Nk

i i ii

n

f x A f

f x x x�

� �

•• Now solve for (n+1) unknown parameters.Now solve for (n+1) unknown parameters.� � 1, , ,f x x x� �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Gauss MethodGauss Method--Example (1Example (1stst):):�� Gauss MethodGauss Method Example (1Example (1 ):):•• We have (xWe have (xii,f,fii), (x), (xi+1i+1,f,fi+1i+1))

� � � � � �� � � � � �

1

11 0i i i

i i

f x Af x Bf x

f x Af x Bf x A B�� � �

� � � � � �� � � � � �� � � � � �

� � � �

1

1 1

1 0

1

1

i i

i i i i

f x Af x Bf x A B

f x x Af x Bf x Ax Bx

f x f x

� �

� � �

� � � � � �

�� � � � � �1

1 1

1 i ii

i i i i

f x f xA B f x

x x x x�

� �

��� � � � �

� �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Gauss MethodGauss Method--Example(1Example(1stst):):�� Gauss MethodGauss Method Example(1Example(1 ):):•• We have (xWe have (xii--11,f,fii--11), (x), (xii,f,fii), (x), (xi+1i+1,f,fi+1i+1))•• xx == h xh x =0 x=0 x =h=h•• xxii--11==--h, xh, xii=0, x=0, xi+1i+1=h=h

� � � � � � � �1 1i i i if x Af x Bf x Cf x� �� � � �

� �� � � � � � � �

1 0

1 0

f x A B C

f x x A h B C h

� � � � �

� � � � � � �

� � � � � � � �

� � � � � �

2 2 2

1 1

2 0 0i

i i

f x x x A h B C h

f x f xf � �

� � � � � �

��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� � � � � �1 1

2i i

if xh

�� �

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Gauss MethodGauss Method--Example(2Example(2ndnd):):�� Gauss MethodGauss Method Example(2Example(2 ):):•• We have (xWe have (xii--11,f,fii--11), (x), (xii,f,fii), (x), (xi+1i+1,f,fi+1i+1))•• xx == h xh x =0 x=0 x =h=h•• xxii--11==--h, xh, xii=0, x=0, xi+1i+1=h=h

� � � � � � � �1 1i i i if x Af x Bf x Cf x� ��� � � �

� �� � � � � � � �

1 0

0 0

f x A B C

f x x A h B C h

� � � � �

� � � � � � �

� � � � � � � �

� � � � � � � �

2 2 2

1 1

2 0

2i i i

f x x A h B C h

f x f x f x�

� � � � �

� ���

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� � � � � � � �1 12

2i i ii

f x f x f xf x

h� ��

�� �

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Introduction theIntroduction the zz operator:operator:�� Introduction the Introduction the zz operator:operator:

� �� � � �1i iz f x f x ��

� �� � � �� �� � � �

22

11

i i

i i

z f x f x

z f x f x�

��

�� �� � � �� �� � � �2

2i iz f x f x���

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Summary of Method for 1Summary of Method for 1stst derivative:derivative:�� Summary of Method for 1Summary of Method for 1 derivative:derivative:1zh�

1

1

1 zh

��

1

1

24 3

z zhz z �

��

� � �

2 1 2

4 328 8

z zh

z z z z� �

��

� � � ��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

12h�

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Summary of Method for 2Summary of Method for 2stst derivative:derivative:�� Summary of Method for 2Summary of Method for 2 derivative:derivative:1

2

2z zh

�� ��

1 2 2 1

2 2

4 5 2 4 5 2h

z z z z z zh h

� � �� � � � � � � ��

2 1 2

2

3 1 3 22

z z z zh

� �� � � � ��

2 1 2

2

16 30 1612

z z z zh

� �� � � � ��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Summary of Method for 3rd derivative:Summary of Method for 3rd derivative:�� Summary of Method for 3rd derivative:Summary of Method for 3rd derivative:1 2 2 1

3 3

3 3 3 3z z z z z zh h

� � �� � � � � ��

2 1 2

3

2 22

h hz z z z

h

� �� � ��

�� Summary of Method for 3rd derivative:Summary of Method for 3rd derivative:

2 1 2

4

4 6 4z z z zh

� �� � � ��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

h

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Differentiation Using Interpolation:Differentiation Using Interpolation:�� Differentiation Using Interpolation:Differentiation Using Interpolation:•• Find an interpolator or do curve fitting:Find an interpolator or do curve fitting:•• Take DerivativeTake Derivative•• Take Derivative.Take Derivative.

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Example (Lagrange Polynomial):Example (Lagrange Polynomial):�� Example (Lagrange Polynomial):Example (Lagrange Polynomial):

� � � � � �1 1 1 1 1 1, , , , , ,k k k k k k k k k kx f x f x f x x x x h� � � � � �� � � �

� � � �� �� �� �

� �� �� �� �

1 1 11

1 1 1 1 1

k k k kk k

k k k k k k k k

x x x x x x x xf x f f

x x x x x x x x� � �

�� � � � �

� � � �� �

� � � �

� �� �� �� �

� �� � � �� � � �� �

11

1 1 1

k kk

k k k k

x x x xf

x x x x�

�� � �

� ��

� ��

� � � �� � � �� � � �� �1 1 1 11 12 2 22 2

k k k k k kk k k

x x x x x x x x x x x xf x f f f

h h h� � � �

� �

� � � � � �� � �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Example (Lagrange Polynomial):Example (Lagrange Polynomial):�� Example (Lagrange Polynomial):Example (Lagrange Polynomial):

� � � � � � � � � �1 1 1 11 12 2 2 20 0

2 2k k k k k k k k

k k k k k

x x x x x x x xf x f f f f

h h h h� � � �

� �

� � � �� � � � � � �

� � � � � � � � � �1 12 2 22

2 2

2 2k k k k k

h h h hh h h h

f x f f f fh hh h� �

� �� � � � �

� � 1 1

2k k

kf ff x

h� ��� �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Two Dimensional Case:Two Dimensional Case:�� Two Dimensional Case:Two Dimensional Case:•• We deal with Gradient:We deal with Gradient:

� � � � � � � � � � � �, , , , , , or or

2f x h y f x y f x y f x h y f x h y f x h yf

x h h h� � � � � � ��

� � � � � � � � � � � �2

, , , , , , or or

2

x h h hf x y h f x y f x y f x y h f x y h f x y hf

y h h h

�� � � � � � ��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical Differentiation

MatlabMatlab Simple Command: Simple Command: diffdiff((x,nx,n))•• dydy = = diff(x,ndiff(x,n););

dy(kdy(k)= x(k+1))= x(k+1)--x(k)x(k)h = 0.01;

t = (0:h:1) ;

a = sin(2*pi*1*t); % 1Hz sin, from 0 to 1 sec.

da = (2*pi*1)*cos(2*pi*1*t);

df = diff(a,1)/h;

subplot(211), plot(t,da,’b’,t(2:end),df,’r’);

subplot(212), plot(t(2:end),abs(da(2:end)-df));

err = norm(df-da(2:end),’fro’)/norm(da(2:end),’fro’); %0.0314

( )2

1( , ' ')

N

inorm a fro a n

=

∼ ∑

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Example (MatlabExample (Matlab DiffDiff))�� Example (Matlab Example (Matlab DiffDiff))•• 22ndnd order derivativeorder derivative

h = 0.01;

t = (0:h:1) ;

a = sin(2*pi*1*t); % 1Hz sin from 0 to 1 seca = sin(2*pi*1*t); % 1Hz sin, from 0 to 1 sec.

d2a = -((2*pi*1)^2)*sin(2*pi*1*t);

d2f = diff(a,2)/(h*h);

subplot(211), plot(t,d2a,’b’,t(3:end),d2f,’r’);

subplot(212), plot(t(3:end),abs(d2a(3:end)-d2f));

f f f

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

err = norm(d2f-d2a(3:end),’fro’)/norm(d2a(3:end),’fro’); %0.0622

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Matlab Commands: GradientMatlab Commands: Gradient�� Matlab Commands: Gradient.Matlab Commands: Gradient.•• 1D case: dy = gradient(f,h);1D case: dy = gradient(f,h);h 0 01h = 0.01;

t = (0:h:1) ;

a = sin(2*pi*1*t); % 1Hz sin, from 0 to 1 sec.a s ( p t); % s , o 0 to sec

da = (2*pi*1)*cos(2*pi*1*t);

df = gradient(a,h);

subplot(211), plot(t,da,’b’,t,df,’r’);

subplot(212), plot(t,abs(da-df));

err = norm(df da ’fro’)/norm(da ’fro’); %err=6 5784e 004

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

err = norm(df-da, fro )/norm(da, fro ); %err=6.5784e-004

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Matlab Commands: GradientMatlab Commands: Gradient�� Matlab Commands: Gradient.Matlab Commands: Gradient.•• 2D case: [fx,fy] = gradient(f,hx,hy);2D case: [fx,fy] = gradient(f,hx,hy);

[x,y] = meshgrid(-2:.2:2, -2:.2:2);z = x .* exp(-x.^2 - y.^2);[px,py] = gradient(z,.2,.2);contour(z) hold on quiver(px py) hold off

•• 3D case: [fx,fy,fy] = gradient(f,hx,hy,hz);3D case: [fx,fy,fy] = gradient(f,hx,hy,hz);

contour(z),hold on, quiver(px,py), hold off

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Matlab Programming:Matlab Programming:�� Matlab Programming:Matlab Programming:

� � � � � �1n nn

f x f xf x

h� �

df = [(x(2:end) x(1:end 1))/h 0];

11 22 33 …… NN--11 NN

� � � � � �1n nn

f x f xf x

h��

df = [(x(2:end)-x(1:end-1))/h,0];

h11 22 33 …… NN--11 NN

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

df =[0,(x(2:end)-x(1:end-1))/h];

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation� � � � � �1 1

2n n

n

f x f xf x

h� ��

� � �2nfh

11 22 33 …… NN--11 NN

df =[0, (x(3:end)-x(1:end-2))/(2*h),0];

� � � � � � � �8 8f x f x f x f x� �� � � � � � � �2 1 1 28 812

n n n nf x f x f x f xh

� � � �� � � �

11 22 33 NN 22 NN 11 NN

df =[0,0, (-x(5:end)+8*x(4:end-1)-8*x(2:end-3)+x(1:end-4))/(12*h) 0 0];

11 22 33 …… NN--22 NN--11 NN

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

4))/(12*h),0,0];

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Comparison:Comparison:�� Comparison:Comparison:

� � � � � �1 0.0324n nn err

f x f xf x

h��

� ��

� � � � � �1 0.0324n nn err

hf x f x

f xh

� �� ��

� � � � � �1 1

26.5784e-004n n

n er

hf x f x

f xh

r� ��� ��

� � � � � � � � � �2 1 1 28 85.1927e-007

12n n n n

n

f x f x f xer

ff x

hr

x� � � �� � � �� ��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Matlab Command:Matlab Command: del2del2�� Matlab Command: Matlab Command: del2del2•• Discrete Laplacian!Discrete Laplacian!

2 2

L del2(f hx hy);L del2(f hx hy);

2 22

2 2

f ffx y

� �� � �

� �

•• L=del2(f,hx,hy);L=del2(f,hx,hy);•• L=del2(f,hx,hy,hz);L=del2(f,hx,hy,hz);

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

�� Noise Corrupted case:Noise Corrupted case:�� Noise Corrupted case:Noise Corrupted case:h = 0.01;

t (0:h:1) ;t = (0:h:1) ;

a = sin(2*pi*1*t); % 1Hz sin, from 0 to 1 sec.

Noisya = a + randn(size(a))*0.1;

da = (2*pi*1)*cos(2*pi*1*t);

df = diff(Noisya,1)/h;

subplot(211), plot(t,da,’b’,t(2:end),df,’r’);

subplot(212), plot(t(2:end),abs(da(2:end)-df));

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical DifferentiationNumerical DifferentiationNumerical DifferentiationNumerical Differentiation

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Problem Statement:Problem Statement:�� Problem Statement:Problem Statement:•• Analytical Function Analytical Function –– Analytical SolutionAnalytical Solution

Analytical FunctionAnalytical Function No SolutionNo Solution

� �0

exp( ) 1f x x dx� � ��

•• Analytical Function Analytical Function –– No SolutionNo Solution

� �12

2exp( )f x x dx� ��

•• Discrete Data: ECG dataDiscrete Data: ECG data1�

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� NewtonNewton--Cotes Method:Cotes Method: � � � �b b

f x dx P x dx� ��� NewtonNewton Cotes Method:Cotes Method:

T id l th d f t i tT id l th d f t i t

� � � �na a

f x dx P x dx� �

�� Trapezoidal method for two points:Trapezoidal method for two points:

� � � � � �b b x a x bf x dx f b f a dx� �� � �� �� �� �� � � � � �

� � � � � � � � � � � �a ab

f f fb a a b

f a f b f a f bf x dx b a h

� �� �� �

� � � �

� �

� � � � �

� � � � � �3 3

2 2a h

f

b a hE f c f c�

�� ��� �

� ���

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� � � �12 12

E f c f c� � � �

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Trapezoidal method for N+1 points:Trapezoidal method for N+1 points:�� Trapezoidal method for N+1 points:Trapezoidal method for N+1 points:� �0 1, , , Nx x x

x x�

0

0 1 11 2

N

N N

x xhNf f f ff fI h h h

��

� ��0 1 11 2

1

2 2 2

2

N N

N

f f f ff fI h h h

hI f f f

� � � �

� �� � �� ��

� � � � � � � � � � � �

01

3 2 20 0

22 N i

i

N N

I f f f

x x x x h b a hE f c f c f �

� � �� �� �

� � ��� �� �� � � � �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� � � � � �212 12 12E f c f c f

N� � � � �

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Simpson (1/3) method for three points:Simpson (1/3) method for three points:�� Simpson (1/3) method for three points:Simpson (1/3) method for three points:

� � 2 00 1 2, , ,

2x xx x x h �

� �� � � �� �

� �� �� �� �� �� �

� �� �� �� �

2

0

1 2 0 2 0 10 1 2

0 1 0 2 1 0 1 2 2 0 2 1

x b

x a

b

x x x x x x x x x x x xf x dx f f f dx

x x x x x x x x x x x x� �� � � � � �

� �� �� � � � � �� �� �

� � � �

� �

0 1 2

54

43

b

a

hf x dx f f f

h

� ��� � � �4

90hE f c� �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration�� Simpson (1/3) method for (N+1) points (N, even):Simpson (1/3) method for (N+1) points (N, even):p ( / ) ( ) p ( , )p ( / ) ( ) p ( , )

� �0 1

0

, , , N

N

x x xx xh �

� � � � � �

0

0 1 2 2 3 4 2 14 4 4

N

N N N

hN

h h hI f f f f f f f f f

� � � � � � � � � ��� � � � � �0 1 2 2 3 4 2 1

1 2

0

4 4 43 3 3

4 23

N N N

N N

N i i

I f f f f f f f f f

hI f f f f

� �

� �

� � � � � � � � �

� �� � � �� �

� �� �

� � � � � �

01,3, 2,4,

44

80

3

1

N i ii i

b a hE f �

� �� �� �

� �

� �� �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� �801

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration�� Simpson (3/8) method for 4 points:Simpson (3/8) method for 4 points:p ( / ) pp ( / ) p

� �

� �0 1 2 3

3, , ,

3 3

x x x xhI f f f f� �

� � � �

0 1 2 3

54

3 3

38

I f f f f

hE f c

� � � �

�� Another methods:Another methods:

� �80

E f c

� � � � � �674 87 32 12 32 7hI f f f f f E h f� � � � � �

� � � � � �

670 1 2 3 4

670 1 2 3 4 5

7 32 12 32 7 ,90 9455 27519 75 50 50 75 19 ,

I f f f f f E h f c

hI f f f f f f E h f c

� � � � � � �

� � � � � � � �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� � � �0 1 2 3 4 519 75 50 50 75 19 ,288 12096

I f f f f f f E h f c� � � � �

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Romberg Method:Romberg Method:�� Romberg Method:Romberg Method:� � � � 2

0 0 0I . . : Trapezoidal/Simpson Method Errorb

a

h h f x dx I h c h� � � ��

� � � � � �20 0 0II. 2 2 2

b

ab

h h f x dx I h c h� � � ��

� � � � � �

� � � �

20 0 0III. 4 4 4

2

b

a

h h f x dx I h c h

I h I h

� � � �

�� � � �

� �� � � �

0 02 20

22

2I. &II.

1 1 2

2b

I h I hc

h

I h I hh

�� �

��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� � � � � � � �� � � �20 00

0 02 2 20

2II. 2 2 Less Error

2 1 1 2a

I h I hhf x dx I h c hh

�� � � ���

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Romberg Method:Romberg Method:�� Romberg Method:Romberg Method:

� � � � � � � � � �20 00 02

22 2 Less Error

b I h I hf x dx I h c h

��� � � �� � � � � � �

� � � �

0 02

2

2 1II. and III.:

2 / 2

a

b

f

I h I h

��

� � � � � � � �� �

220 02 2

0 02

2 / 22 2

2 11

b

a

I h I hf x dx I h c h

���� � �

��

n

1Better Estimate=More Accurate+ (More Accurate- Less Accurate)2 -1

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Romberg Method Example:Romberg Method Example:�� Romberg Method, Example:Romberg Method, Example:2

1.5

0 2

0.652

bx b aI e dx h

�� �

� � � ��

� � � � � � � �� �0.2

First Estimate: 2 0.662112

0 65

a

hI h f a f a h f b

� � � � �

� � � � � � � � � � � �� �

0.65Second Estimate: h=2

2 2 2 2 3 0 65948hI h f a f a h f a h f a h f b

� � � � � � �� � � � � � � � � � � �� �2 2 2 2 3 0.659482

0.65947 0.66211Better Estimate: 0.65947 0.658594 1

I h f a f a h f a h f a h f b� � � � � � � � �

�� �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

4 1�

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Cubic Spline:Cubic Spline:�� Cubic Spline:Cubic Spline:

1 :i ix x x �� �

� � � � � � � �

� �

3 2

4 3 2N

i i i i i i i

x N N N N

f x a x x b x x c x x d

h h hf x dx a b c h d

� � � � � � �

� � �� � � �� � �1

1 1 1 14 3 2i i i ii i i ix

f x dx a b c h d� � � �

� � � �� � � ��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Gauss Method (1):Gauss Method (1):�� Gauss Method (1):Gauss Method (1):� �� �

� �

0,

ni i i

b n

x f

f d A f b

�� � � 00

1

i i nia

b n

f x dx A f x a b x

f fdx b a A

� � � �

� !

��

��

� �

0

2 2

1 iia

b n

ni i

f fdx b a A

b af x fdx A x

� � � � � !!!� !� � � � !"

��

�� � �0 0

1 1

2 ni ii ia i

b n n n

f fA

b

� �

� �

� ! �"!!!

���

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

1 1

01

n n nn n

i iia

b af x fdx A xn

� �

!�� � � � !

� !#��

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Gauss Method Example:Gauss Method Example:�� Gauss Method, Example:Gauss Method, Example:� � � � � �4

2 3

1, 0.3679 , 2, 0.0183 , 3,1.2341 10�$

22.3

1.25

1 05 0 1930

xI e

A A A A

��

% %

0 1 2 0

0 1 2 1

1.05 0.19302 3 1.8637 0.90024 9 3 4046 0 0433

A A A AA A A AA A A A

� � � �% %! !� � � � �& &! !� �' '0 1 2 2

0 0 1 1 2 2

4 9 3.4046 -0.04330.0875

A A A AI A f A f A f

! !� � � �' ' � � �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Gauss For Unknown PointsGauss For Unknown Points�� Gauss For Unknown PointsGauss For Unknown Points� � � � � �

1

1 21

f t dt af t bf t�

� ��

� �� �

1

1 2

2110

a bf ta bat btf t t

� �%�% ! � �� � %! !! ! !� �� �� �

1 2

2 2 22 11 2

3

123 3

f t tt tf t t at bt

f t t

! !�! ! !� �& & & � � �� � �! ! !'! !�' � �

� �

3 31 2

1

0

1 1

f t t at bt

f t dt f f�

! !�' � �!'

( ) ( )� � �* + * +�Applied Numerical MethodsApplied Numerical Methods

E. FatemizadehE. Fatemizadeh

� �1 3 3f t dt f f

�* + * +, - , -�

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Gauss For Unknown PointsGauss For Unknown Points�� Gauss For Unknown PointsGauss For Unknown Points

� �b

f x dx�� � � �

2 2

a

b a t b a b ax dx dt

� � � �� � �

� � � � � �1

12 2

b

a

b a b a t b af x dx f dt

� � � �( )� * +

, -( )( ) ( )

� �

� � � � � � � �1 3 1 32 2 2

b

a

b a b a b a b a b af x dx f f dt

( )( ) ( )� � � � � � � �* + �* + * +* + * +* +, - , -, -

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Example:Example:�� Example:Example:

� �2

sinI x dx.

� �

� � � �

0

Actual Value: 1sin 0 sin 2. . .� ( )� � � �sin 0 sin 2

Trapezoidal: 0 0.7853982 2 4

G i i 0 998473

. . .

. . . . .

� ( )� � * +, -

( )( ) ( )* +Gauss: sin sin 0.998473

4 4 44 3 4 3. . . . .( )( ) ( )� � � � * +* + * +

, - , -, -

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Gauss in General form:Gauss in General form:�� Gauss in General form:Gauss in General form:•• Step 1:Step 1:

� � � �1b b a b a t b a� � � � �( )� �

Step 2:Step 2:

� � � � � � � � � �1

,2 2a

b a b a t b aI f x dx I g t dx g t f

� �( )� � � � * +

, -� �

•• Step 2:Step 2:

� � � �2 1 : Has n roots in [-1,+1]n n

n n

dP t t� �� � � �� � � � � � � �2

0 1 2

[ , ]

1, 2 , 4 3 1

n ndtP t P t t P t t� � � �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

•• Step 3Step 3Step 3Step 3

� � � � � � � �1

0 0 1 11

n ng t dt A g t A g t A g t�

� � � �� �

� �� �

0 1

0 0 1 1

21 0

2

n

n n

A A Ag t A t A t A tg t t

� � � �%!�% � � � �!! � !!

��

� �� �

� �

2 2 22 0 0 1 1

23n n

g t tA t A t A tg t t

!! � � � �!! � !! !�& &! !

��

� �

� �

� �0 0 1 1

2 1

1 11

nnn n n

n n

n

g t tA t A t A t

ng t t �

! !� � �! ! � � � �! ! �! !�' !

��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� �2 1 2 1 2 1

0 0 1 1 0n n nn n

g t tA t A t A t� � �

' !� � � �!' �

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� tt are roots ofare roots of PP ((xx))�� ttii are roots of are roots of PPnn((xx))�� ReplaceReplace ttii in first (n+1) equation and getin first (n+1) equation and getAAAAii

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Example n=2Example n=2�� Example n=2Example n=2

� � � �1b

I f x dx I g t dx�

� � �� �� � � �

� � � �1

22 0 1

1 14 3 1 0 ,3 3

a

f g

P t t t t

� � � � � � � �

� �

� � � �

0 1 0

3 32 1

10A A A

AA t A t� � �% %

�& & �� � '' 10 0 1 1 10 AA t A t �� � ''

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Example:Example:�� Example:Example:

1

%! � �

� �

1

01

Real Value:arctan 4 0.7854

1Trapezoidal: 1+0.5 0.7500

x

dx

.!

� !!!� &� � �2

0

2 2

Trapezoidal: 1 0.5 0.75001 2

1 1 1Gauss:= + 0.7869

x� &� !

! ( )�! * +* +!

� � � �2 22 1+ 0.211 1+ 0.789! * +* +! , -'

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Subdivide to n section:Subdivide to n section: h=xh=x 11--xx�� Subdivide to n section: Subdivide to n section: h xh xi+1i+1--xxii

� � � � � �2

b n

i ihf x dx f C f D �� �� ��� � � � � � �

1

1

2

3 3, ,2 6 6

ia

i ii i i i ix xA C h A D h A

� �

�� � � � � � �

��

�� ExampleExample::

2 6 6

�� ExampleExample::1

20

0.25, 0.785401dxhx

� ��

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

0

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Example n=3Example n=3�� Example n=3Example n=3

� � � �1b

I f x dx I g t dx�

� � �� �� � � �1

0 1 23 30

a

I f x dx I g t dx

t t t

� � � � �

� �

0 1 2

0 2 1

, , 0,5 5

5 8,9 9

t t t

A A A

� � �9 9

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Example:Example:�� Example:Example:

Actual Value: 0 30117%1

0

Actual Value: 0.30117sin Simpson=0.30005

Gauss 0.30117:=x xdx

%!� &!'

�G'

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Numerical IntegrationNumerical IntegrationNumerical IntegrationNumerical Integration

�� Subdivide to n section:Subdivide to n section: h=xh=x 11--xx�� Subdivide to n section: Subdivide to n section: h xh xi+1i+1--xxii

� � � � � � � �5 8 518

b n

i i ihf x dx f C f A f D � �� �� ���

1

1

18

3 3, ,2 5 2 5 2

ia

i ii i i i ix x h hA C A D A

� �

�� � � � � � �

2 5 2 5 2

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Multiple IntegralsMultiple IntegralsMultiple IntegralsMultiple Integrals

�� Formulation:Formulation:�� Formulation:Formulation:

� � � �b d b d

f x y dydx f x y dy dx%

� & "� � � �� � � �, ,a c a c

f x y dydx f x y dy dx� & "' #

� � � �

d

a b

c

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

a b

Multiple IntegralsMultiple IntegralsMultiple IntegralsMultiple Integrals

�� Trapezoidal:Trapezoidal:�� Trapezoidal:Trapezoidal:

� � � � � � � �, , , ,2

b d b d b d cf x y dydx f x y dy dx f x d f x c dx% �

� �� �& " � �' #

� � � � �� � � � � � � �

� �� � � � � � � � � �

2

, , , ,4

a c a c a

b a d cf a d f b d f a c f b c

� �' #

� � � � �� �� �

� � � � �

� � � � � � � �4 � �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Multiple IntegralsMultiple IntegralsMultiple IntegralsMultiple Integrals

�� Simpson:Simpson:�� Simpson:Simpson:� � � �, ,

b d b d

a c a c

f x y dydx f x y dy dx%

� & "' #

� � � �

� � � �� � � �, 4 , 2 ,6

b

a

d c f x d f x d c f x c dx� � � � � �� ��� � � � � � � �, 4 , ,

36 2d c b a a bf a d f d f b d

d b d

� � �� ( ) � � �* +� , -�� � �( ) ( ) � �� �

� � � �

4 , 16 , 4 , 22 2 2

4

d c a b d cf a f f b d c

a bf f f b

� � �( ) ( )� � � �* + * +, - , -

� �( )* +

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

� � � �, 4 , ,2

f a c f c f b c �( )� �* + �, - �

Multiple IntegralsMultiple IntegralsMultiple IntegralsMultiple Integrals

�� Matrix formMatrix form�� Matrix formMatrix formd

1 4 1� �1 4 11 4 16 4

361 4 1

� �� �� �� �� �

c

1 4 1� �� �

a b

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Multiple IntegralsMultiple IntegralsMultiple IntegralsMultiple Integrals

�� Gauss Method:Gauss Method:�� Gauss Method:Gauss Method:

Th b ti i t f llTh b ti i t f ll� � � �

1 1 1

1 1 11 1 1

, , , ,n n n

i j k i j ki j k

f x y z dxdydz a a a f x y z� � �

� � �� � �

� ���� � ��� The above equation is correct for all The above equation is correct for all

polynomial polynomial xx��yy��zz�� of degree s(� of degree s(� ��++��++��))� �

1 1 1 1 1 1

, ,f x y z x y z

I d d d d d d

� / 0

� / 0 � / 0� � � � � �

( ) ( ) ( )* + * + * +� � � � � �

1 1 1 1 1 1

n n n n n n

I x y z dxdydz x dx y dy z dz

I a x a y a z a a a x y z

� / 0 � / 0

� / 0 � / 0

� � � � � �

� � * + * + * +, - , - , -

( )( ) ( )� �* +* + * +

� � � � � �

� � � ���Applied Numerical MethodsApplied Numerical Methods

E. FatemizadehE. Fatemizadeh

1 1 1 1 1 1i i j j k k i j k i j k

i j k i j kI a x a y a z a a a x y z

� � � � � �

� �* +* + * +, - , -, -� � � ���

Multiple IntegralsMultiple IntegralsMultiple IntegralsMultiple Integrals

�� Example:Example:�� Example:Example:

� �� �1 1 11 1 1

16xI u v e dxdudv

� � �

� � �� � � � �� �1 1 1

2 2 3

16Use two term for u and v and three terms for x

1

� � �� � �

� �� �1 2 1

1 2

1I= 1 116

1

kxi j k i j

i j ka a b u v e

a a� � �

� �

� �

���

1 2

1 3 1

15 8,9 9

a a

b b b� � �

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Matlab CommandMatlab CommandMatlab CommandMatlab Command

�� Simpson method:Simpson method:�� Simpson method:Simpson method:•• I = quad(I = quad(funfun,a,b);,a,b);

�� I=quad(@I=quad(@myfunmyfun 0 1);0 1);

b

a

fdx��� I=quad(@I=quad(@myfunmyfun,0,1);,0,1);�� I=quad(‘exp(I=quad(‘exp(--x.^2’,1,2);x.^2’,1,2);

•• I = quad(I = quad(funfun,a,b,Tol); Tol = 1e,a,b,Tol); Tol = 1e--6 by6 byI quad(I quad(funfun,a,b,Tol); Tol 1e,a,b,Tol); Tol 1e 6 by 6 by default.default.

y = myfun(x)

y = 4./(1+x.^2);I = quad(@myfun,0,1);

err = (pi-I)/pi;

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

err = 1.8 e-8

Matlab CommandMatlab CommandMatlab CommandMatlab Command

�� Double IntegralDouble Integral�� Double IntegralDouble Integral•• dblquad(dblquad(funfun,,XminXmin,,XmaxXmax,,YminYmin,,YmaxYmax))•• I=dblquad('exp(I=dblquad('exp(--x ^2x ^2--y ^2)'y ^2)' --1 +11 +1 --2 +2);2 +2);•• I=dblquad( exp(I=dblquad( exp(--x. 2x. 2--y. 2) ,y. 2) ,--1,+1,1,+1,--2,+2);2,+2);•• dblquad(@myfun,dblquad(@myfun,--1,+1,1,+1,--2,+2)2,+2)

�� Trilpele IntegralTrilpele Integral�� Trilpele IntegralTrilpele Integral•• triplequad(fun,triplequad(fun,XminXmin,,XmaxXmax,,YminYmin,,YmaxYmax,,ZminZmin

,,ZmaxZmax););,,ZmaxZmax););•• triplequad('exp(triplequad('exp(--x.^2x.^2--y.^2y.^2--z.^2)',z.^2)',--1,+1,1,+1,--2,+2,2,+2,--1,1);1,1);•• triplequad(@myfun,triplequad(@myfun,--1,+1,1,+1,--2,+2,2,+2,--1,1);1,1);

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

Matlab CommandMatlab CommandMatlab CommandMatlab Command

�� Another method:Another method: b�� Another method:Another method:•• I = quadl(I = quadl(funfun,a,b);,a,b);

�� I=quadl(@I=quadl(@myfunmyfun 0 1);0 1);a

fdx��� I=quadl(@I=quadl(@myfunmyfun,0,1);,0,1);�� I=quadl(‘exp(I=quadl(‘exp(--x.^2’,1,2);x.^2’,1,2);

•• I = quadl(I = quadl(funfun,a,b,Tol); Tol = 1e,a,b,Tol); Tol = 1e--6 by6 byI quadl(I quadl(funfun,a,b,Tol); Tol 1e,a,b,Tol); Tol 1e 6 by 6 by default.default.

y = myfun(x)

y = 4./(1+x.^2);I = quadl(@myfun,0,1);

err = (pi-I)/pi;

Applied Numerical MethodsApplied Numerical MethodsE. FatemizadehE. Fatemizadeh

err = 1.7 e-8

Matlab CommandMatlab CommandMatlab CommandMatlab Command

�� A nice example:A nice example:�� A nice example:A nice example:1

2 2 2

1 1 1 1,I dx dx dx t� �

� � � �� � �2 2 20 0 1

1 1

2 2

,1 1 1

1 1

x x x x

dx dt

� � �

� �

� � �

� �2 20 0

1

2

1 1

121

x t

dx

� �

� �

� 20 1 x��

Applied Numerical MethodsApplied Numerical MethodsE. E. FatemizadehFatemizadeh

Matlab CommandMatlab CommandMatlab CommandMatlab Command

�� A nice example:A nice example:�� A nice example:A nice example:

2 2 21 1x x xI e dx e dx e dx t

� �� � ��� � �

2

0 0 11

1 1

,

e t

I e dx e dx e dx tx

� � � �� � �

2

20 0

e

0 7468 0 1394 0 8862

txe dx dt

t�� �

� � �

� �0.7468 0.1394 0.8862� � �

Applied Numerical AnalysisApplied Numerical AnalysisE. E. FatemizadehFatemizadeh