lecture 4 numerical integration

Upload: aminul-hoque

Post on 07-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/4/2019 Lecture 4 Numerical Integration

    1/23

    CLASS: 04

    ME 262: Numerical Analysis Sessional

    Department of Mechanical Engineering, BUET

  • 8/4/2019 Lecture 4 Numerical Integration

    2/23

    Numerical Integration

    15 May 2011

    2

    ME262 Numerical Analysis Sessional

  • 8/4/2019 Lecture 4 Numerical Integration

    3/23

  • 8/4/2019 Lecture 4 Numerical Integration

    4/23

    15 May 2011ME262 Numerical Analysis Sessional

    4

    x0 x1 xnxn-1x

    f(x)

    Graphical Representation of Integral

  • 8/4/2019 Lecture 4 Numerical Integration

    5/23

    15 May 2011ME262 Numerical Analysis Sessional

    5

    fn (x): linear fn (x): quadratic fn (x): higher-order

    polynomial

    Numerical Approximation of Integral

    Newton-Cotes Closed Formulae -- Use both end points

    Trapezoidal Rule : Linear

    Simpsons 1/3-Rule : Quadratic

    Simpsons 3/8-Rule : Cubic

  • 8/4/2019 Lecture 4 Numerical Integration

    6/23

    15 May 2011ME262 Numerical Analysis Sessional

    6

    6

    Approximate the function by a parabola

    0 1 2( ) ( ) 4 ( ) ( )3

    b

    a

    h f x dx f x f x f x

    x0 x1x

    f(x)

    x2h h

    L(x)

    Simpsons 1/3-Rule

  • 8/4/2019 Lecture 4 Numerical Integration

    7/23

    15 May 2011ME262 Numerical Analysis Sessional

    7

    n

    abh

    Piecewise Quadratic approximations

    x0 x2x

    f(x)

    x4h h xn-2h xn

    ...

    hx3x1 xn-1

    Composite Simpsons Rule

  • 8/4/2019 Lecture 4 Numerical Integration

    8/23

    15 May 2011ME262 Numerical Analysis Sessional

    8

    8

    i = 0 1 2 3 4 5 6 . . n-1 n

    Applicable only if

    the number of segments is

    even

    0 1 2( ) 4 ( ) ( )3

    hI f x f x f x

    n-1 n- 2

    0 i i ni =1,3,5 i = 2,4,6

    h I = f(x ) + 4 f(x ) + 2 f(x ) + f(x )

    3

    Composite Simpsons 1/3 Rule

  • 8/4/2019 Lecture 4 Numerical Integration

    9/23

    15 May 2011ME262 Numerical Analysis Sessional

    9

    Composite Simpsons 1/3 RuleAlgorithm for Uniform Spacing (Equal Segments)

    n

    abh

    2

    nm

    13

    m

    k

    2k - 2 2k -1 2kh

    I = f x + 4f x f x

    x0 x1 x2 x3 x4 x5 x6 . . . xn-1 xn

  • 8/4/2019 Lecture 4 Numerical Integration

    10/23

    15 May 2011ME262 Numerical Analysis Sessional

    10

    Composite Simpsons 1/3 Rule

    Evaluate the integral

    n = 2

    n = 4

    dxxeI4

    0

    x2

    %96.57411.82404)2(4032

    )4()2(4)0(3

    84

    eefff

    hI

    %70.8975.56704)3(4)2(2)(40

    3

    1

    )4()3(4)2(2)1(4)0(3

    8642

    eeeefffff

    hI

  • 8/4/2019 Lecture 4 Numerical Integration

    11/23

    15 May 2011ME262 Numerical Analysis Sessional

    11

    MATLAB Code

    dxxeI4

    0

    x2function s = fvalue(x)s = x*exp(2*x);

    a = 0; b = 4; n = 4;h = (b - a)/n;S = 0; m = n/2;

    for k = 1 : m

    S = S + fvalue(a+h*(2*k-2))+4*fvalue(a+h*(2*k-1))+fvalue(a+h*2*k);end

    I = h*S/3;disp(I);

    fvalue.m

    simpsn3.m

  • 8/4/2019 Lecture 4 Numerical Integration

    12/23

    15 May 2011ME262 Numerical Analysis Sessional

    12

    12

    Approximate the function by a cubic polynomial

    Simpsons 3/8-Rule

    0 1 2 33

    ( ) ( ) 3 ( ) 3 ( ) ( )8

    b

    a

    h f x dx f x f x f x f x

    1

    3m

    k

    3k -3 3k -2 3k -1 3k3h

    I = f x + 3f x f x f x8

    x0 x1x

    f(x)

    x2h h

    L(x)

    x3h

    n must be a multiple of3

    3

    nm

  • 8/4/2019 Lecture 4 Numerical Integration

    13/23

    15 May 2011ME262 Numerical Analysis Sessional

    13

    Example: Simpsons Rules

    Evaluate the integral

    Simpsons 1/3-Rule

    Simpsons 3/8-Rule

    dxxe4

    0

    x2

    %.

    .

    ..

    .)(

    )()()(

    96579265216

    41182409265216

    4118240e4e2403

    2

    4f2f40f3

    hdxxeI

    84

    4

    0

    x2

    %71.30

    926.5216

    209.6819926.5216

    209.6819832.11923)33933.552(3)18922.19(308

    )4/3(3

    )4(f)3

    8

    (f3)3

    4

    (f3)0(f8

    h3

    dxxeI

    4

    0

    x2

  • 8/4/2019 Lecture 4 Numerical Integration

    14/23

    15 May 2011ME262 Numerical Analysis Sessional

    14

    MATLAB Code

    dxxeI4

    0

    x2function s = fvalue(x)s = x*exp(2*x);

    a = 0; b = 4; n = 6;h = (b - a)/n;S = 0; m = n/3;

    for k = 1 : m

    S = S + fvalue(a+h*(3*k-3))+(3*fvalue(a+h*(3*k-2)))+(3*fvalue(a+h*(3*k-1)))+fvalue(a+h*3*k);end

    I = (3*h*S)/8;disp(I);

    fvalue.m

    simpsn38.m

  • 8/4/2019 Lecture 4 Numerical Integration

    15/23

    15 May 2011ME262 Numerical Analysis Sessional

    15

    MATLAB Code (Combined Rules)

    x=[0.5 2 3 4 6 8 10 11 ]f=[320 268 233 226 226 212 142 107 ]n=8;h =x(2) - x(1);k = 1;

    sum = 0;for j =2:n

    if j~=nhf = x(j+1) - x(j);

    elsehf = abs(h+1); % this is for last value

    end

    if abs(h-hf)

  • 8/4/2019 Lecture 4 Numerical Integration

    16/23

    15 May 2011ME262 Numerical Analysis Sessional

    16

    MATLAB Code

    if k ==1

    sum = sum + (h/2)*(f(j-1)+f(j));

    else

    if k==2

    sum = sum + (h/3)*(f(j-2)+(4*f(j-1))+f(j));

    elsesum = sum + ((3*h)/8)*(f(j-3)+(3*f(j-2))+(3*f(j-1))+f(j));

    end

    k=1;

    end

    end

    h = hf;

    end

    disp(sum);

  • 8/4/2019 Lecture 4 Numerical Integration

    17/23

    15 May 2011ME262 Numerical Analysis Sessional

    17

    MATLAB Functions

    Command Description

    quad (function,a,b,tol) Uses an adaptive Simpsons rule to compute the integral ofthe functionfunctionwith a as the lower integration limitand b as the upper limit. The parameter tol is optional. tolindicates the specified error tolerance.

    trapz (x,y) Uses trapezoidal integration to compute the integral ofywith respect to x, where the array y contains the functionvalues at the points contained in the array x.

    (use of the trapz function)>> x = linspace(0, pi, 10);>> y = sin(x);>> trapz(x, y)

    ans =

    1.97965081121648

    (use of the quad function)>> quad('sin(x)', 0, pi, 0.001)

    ans =

    1.99999349653496

  • 8/4/2019 Lecture 4 Numerical Integration

    18/23

    Curve Fitting

    15 May 2011

    18

    ME262 Numerical Analysis Sessional

  • 8/4/2019 Lecture 4 Numerical Integration

    19/23

    15 May 2011ME262 Numerical Analysis Sessional

    19

    MATLAB Functions

    Command Description

    polyfit(x,y,n) Two vectors x and y fits a polynomial of order n through

    the data points (xi,yi) and returns (n+1) coefficients of thepowers of x in the vector a. The coefficients are arrangedin the decreasing order of the powers of x, i.e.,

    a = [an an-1 . a1 a0 ]polyval(a,x) A data vector x and the coefficients of a polynomial in a

    row vector a, the command y = polyval(a,x) evaluates thepolynomial at the data points xi .

  • 8/4/2019 Lecture 4 Numerical Integration

    20/23

    15 May 2011ME262 Numerical Analysis Sessional

    20

    Example

    x = 5:5:50;y = [16 25 32 33 38 36 39 40 42 42];p = polyfit(x,y,2); % fit a line (2nd order polynomial)F_fit = polyval(p,x); % evaluate the polynomial at new pointsplot(x,y,'o',x,F_fit); % plot the data and fitted curve

    xlabel('x');ylabel('y');title ('The variation of x with y');

    x 5 10 15 20 25 30 35 40 50

    y 16 25 32 33 38 36 39 49 42

  • 8/4/2019 Lecture 4 Numerical Integration

    21/23

    Example (Contd.)

    15 May 2011ME262 Numerical Analysis Sessional

    21

  • 8/4/2019 Lecture 4 Numerical Integration

    22/23

    15 May 2011ME262 Numerical Analysis Sessional

    22

    Example

    % Power curve fit example y = a x^b ---- log(x) = log(a)+ b log(y)

    x = 5:5:50;

    y = [16 25 32 33 38 36 39 40 42 42];xbar = log10(x);ybar = log10(y);p = polyfit(xbar,ybar,1); % fit a line (1st order polynomial) p=[p1 p0]

    a = 10^p(2); % since p(2) is p0ynew = a.*(x).^p(1);

    plot(x,y,'o',x,ynew); % plot the data and fitted curvexlabel('x');ylabel('y');title ('The variation of x with y');disp([p]);disp([p(1) a]);

    x 5 10 15 20 25 30 35 40 50

    y 16 25 32 33 38 36 39 49 42

  • 8/4/2019 Lecture 4 Numerical Integration

    23/23

    THANK YOU

    5/15/2011

    23

    Its all about today!!