numerical methods and programming using mathematica

Upload: akulbansal

Post on 06-Mar-2016

218 views

Category:

Documents


2 download

DESCRIPTION

Numerical methods and Programming techniques for BSc(H) third semester delhi university using mathematica.

TRANSCRIPT

  • Bisection Method

    f@x_D := Cos@xD;x0 = 0.0; x1 = 2.0; n = 14;

    f1 = Sign@f@x1DD;If@f@x0D * f1 > 0,Print@"IVP not satisfied"D,For@i = 1, i n, i++, p = Hx0 + x1L 2;Print@i, " th iteration value is: ", pD;pSign = Sign@f@pDD;If@pSign * f1 < 0, x0 = p, x1 = p; f1 = pSignD;Print@"The new interval is: ", "H", x0, ",", x1, "L"D;Print@".........................................."D;D;Print@"Final Approximation is ", pDD

    1 th iteration value is: 1.

    The new interval is: H1.,2.L

    ..........................................

    2 th iteration value is: 1.5

    The new interval is: H1.5,2.L

    ..........................................

    3 th iteration value is: 1.75

    The new interval is: H1.5,1.75L

    ..........................................

    4 th iteration value is: 1.625

    The new interval is: H1.5,1.625L

    ..........................................

    5 th iteration value is: 1.5625

    The new interval is: H1.5625,1.625L

    ..........................................

    6 th iteration value is: 1.59375

    The new interval is: H1.5625,1.59375L

    ..........................................

    7 th iteration value is: 1.57813

    The new interval is: H1.5625,1.57813L

    ..........................................

    8 th iteration value is: 1.57031

    The new interval is: H1.57031,1.57813L

    ..........................................

    9 th iteration value is: 1.57422

    The new interval is: H1.57031,1.57422L

  • ..........................................

    10 th iteration value is: 1.57227

    The new interval is: H1.57031,1.57227L

    ..........................................

    11 th iteration value is: 1.57129

    The new interval is: H1.57031,1.57129L

    ..........................................

    12 th iteration value is: 1.5708

    The new interval is: H1.57031,1.5708L

    ..........................................

    13 th iteration value is: 1.57056

    The new interval is: H1.57056,1.5708L

    ..........................................

    14 th iteration value is: 1.57068

    The new interval is: H1.57068,1.5708L

    ..........................................

    Final Approximation is 1.57068

    Both error tolerance and max condition

    In[39]:= Clear@fD;f@x_D := Cos@xD;x0 = 0.0; x1 = 2.0; n = 20;

    f1 = Sign@f@x1DD;If@f@x0D * f1 > 0,Print@"IVP not satisfied"D,For@i = 1, i n, i++,p = Hx0 + x1L 2.0;Print@i, " th iteration value is ", pD;pSign = Sign@f@pDD;Print@"The error is ", Abs@x1 - x0D 2.0D;If@Abs@x1 - x0D < 2 * 0.0001, Break@DD;If@pSign * f1 < 0, x0 = p, x1 = p; f1 = pSignD;Print@"The root enclosing interval is: ", "H", x0, ",", x1, "L"D;Print@"....................................................."D;D;Print@"....................................................."D;Print@"Final approximation is: ", pD;D

    1 th iteration value is 1.

    The error is 1.

    The root enclosing interval is: H1.,2.L

    .....................................................

    2 th iteration value is 1.5

    The error is 0.5

    2 Numerical Methods and Programming.nb

  • The root enclosing interval is: H1.5,2.L

    .....................................................

    3 th iteration value is 1.75

    The error is 0.25

    The root enclosing interval is: H1.5,1.75L

    .....................................................

    4 th iteration value is 1.625

    The error is 0.125

    The root enclosing interval is: H1.5,1.625L

    .....................................................

    5 th iteration value is 1.5625

    The error is 0.0625

    The root enclosing interval is: H1.5625,1.625L

    .....................................................

    6 th iteration value is 1.59375

    The error is 0.03125

    The root enclosing interval is: H1.5625,1.59375L

    .....................................................

    7 th iteration value is 1.57813

    The error is 0.015625

    The root enclosing interval is: H1.5625,1.57813L

    .....................................................

    8 th iteration value is 1.57031

    The error is 0.0078125

    The root enclosing interval is: H1.57031,1.57813L

    .....................................................

    9 th iteration value is 1.57422

    The error is 0.00390625

    The root enclosing interval is: H1.57031,1.57422L

    .....................................................

    10 th iteration value is 1.57227

    The error is 0.00195313

    The root enclosing interval is: H1.57031,1.57227L

    .....................................................

    11 th iteration value is 1.57129

    The error is 0.000976563

    The root enclosing interval is: H1.57031,1.57129L

    .....................................................

    12 th iteration value is 1.5708

    Numerical Methods and Programming.nb 3

  • The error is 0.000488281

    The root enclosing interval is: H1.57031,1.5708L

    .....................................................

    13 th iteration value is 1.57056

    The error is 0.000244141

    The root enclosing interval is: H1.57056,1.5708L

    .....................................................

    14 th iteration value is 1.57068

    The error is 0.00012207

    The root enclosing interval is: H1.57068,1.5708L

    .....................................................

    15 th iteration value is 1.57074

    The error is 0.0000610352

    .....................................................

    Final approximation is: 1.57074

    Only Error tolerance

    In[56]:= Clear@f, x0, x1, i, p, f1D;In[97]:= f@x_D := Cos@xD;

    x0 = 0.0; x1 = 2.0;

    f1 = Sign@f@x1DD;If@f@x0D * f1 > 0, Print@"IVP not satisfied"D,

    For@i = 1, Abs@x1 - x0D > 2 * 0.0001, i++,p = Hx1 + x0L 2.0;Print@i, " th iteration approximation is ",p, " in interval ", "H", x0, ",", x1, "L"D;

    Print@"Error bound is ", Abs@x1 - x0D 2.0D;Print@"..............................................................."D;pSign = Sign@f@pDD;If@pSign * f1 < 0, x0 = p, x1 = p, f1 = pSignD;D;Print@"Final approximation is: ", p, " with error ", Abs@x1 - x0D 2.0DD;

    1 th iteration approximation is 1. in interval H0.,2.L

    Error bound is 1.

    ...............................................................

    2 th iteration approximation is 1.5 in interval H1.,2.L

    Error bound is 0.5

    ...............................................................

    3 th iteration approximation is 1.75 in interval H1.5,2.L

    Error bound is 0.25

    ...............................................................

    4 Numerical Methods and Programming.nb

  • 4 th iteration approximation is 1.625 in interval H1.5,1.75L

    Error bound is 0.125

    ...............................................................

    5 th iteration approximation is 1.5625 in interval H1.5,1.625L

    Error bound is 0.0625

    ...............................................................

    6 th iteration approximation is 1.59375 in interval H1.5625,1.625L

    Error bound is 0.03125

    ...............................................................

    7 th iteration approximation is 1.57813 in interval H1.5625,1.59375L

    Error bound is 0.015625

    ...............................................................

    8 th iteration approximation is 1.57031 in interval H1.5625,1.57813L

    Error bound is 0.0078125

    ...............................................................

    9 th iteration approximation is 1.57422 in interval H1.57031,1.57813L

    Error bound is 0.00390625

    ...............................................................

    10 th iteration approximation is 1.57227 in interval H1.57031,1.57422L

    Error bound is 0.00195313

    ...............................................................

    11 th iteration approximation is 1.57129 in interval H1.57031,1.57227L

    Error bound is 0.000976563

    ...............................................................

    12 th iteration approximation is 1.5708 in interval H1.57031,1.57129L

    Error bound is 0.000488281

    ...............................................................

    13 th iteration approximation is 1.57056 in interval H1.57031,1.5708L

    Error bound is 0.000244141

    ...............................................................

    14 th iteration approximation is 1.57068 in interval H1.57056,1.5708L

    Error bound is 0.00012207

    ...............................................................

    Final approximation is: 1.57068 with error 0.0000610352

    Newtons Method

    Numerical Methods and Programming.nb 5

  • In[122]:= x0 = 1.0;

    Nmax = 5; eps = 0.0001;

    f@x_D := Cos@xD;For@i = 1, i Nmax, i++,

    x1 = N@x0 - Hf@x0DL Hf'@x0DLD;If @Abs@x1 - x0D < eps, Break@D,Print@i, " th iteration is: ", x1D;Print@"Estimated error is: ", Abs@x1 - x0DD;Print@"__________________________________"D;x0 = x1D;

    D;Print@"Final approximation is: ", x1D1 th iteration is: 1.64209

    Estimated error is: 0.642093

    __________________________________

    2 th iteration is: 1.57068

    Estimated error is: 0.0714173

    __________________________________

    3 th iteration is: 1.5708

    Estimated error is: 0.00012105

    __________________________________

    Final approximation is: 1.5708

    Newtons method with error tolerance only

    In[216]:= x0 = 1.0;

    eps = 0.0001;

    f@x_D := Cos@xD;x1 = N@x0 - f@x0D f'@x0DD;Print@1, " th iteration approximation is: ", x1D;For@i = 2, Abs@x1 - x0D > eps, i++,

    x0 = x1;

    x1 = N@x0 - f@x0D f'@x0DD;Print@i, " th iteration approximation is: ", x1DD;

    Print@"Final approximation is: ", x1D1 th iteration approximation is: 1.64209

    2 th iteration approximation is: 1.57068

    3 th iteration approximation is: 1.5708

    4 th iteration approximation is: 1.5708

    Final approximation is: 1.5708

    Gauss Jacobi Method

    4x1+x2+x3=2

    x1+5x2+2x3=-6

    x1+2x2+3x3=-4

    6 Numerical Methods and Programming.nb

  • In[152]:= A = 885, 1, 2
  • 1 th iteration approximation is:

    2.

    -1.55556

    4.71429

    2 th iteration approximation is:

    0.425397

    -2.98413

    4.55556

    3 th iteration approximation is:

    0.774603

    -3.43845

    3.92245

    4 th iteration approximation is:

    1.11871

    -3.04067

    3.84253

    5 th iteration approximation is:

    1.07112

    -2.89044

    4.00534

    6 th iteration approximation is:

    0.975953

    -2.97867

    4.04146

    7 th iteration approximation is:

    0.979148

    -3.02644

    4.00266

    8 th iteration approximation is:

    1.00422

    -3.00813

    3.98947

    9 th iteration approximation is:

    1.00584

    -2.99391

    3.99828

    10 th iteration approximation is:

    0.99947

    -2.99729

    4.00257

    11 th iteration approximation is:

    0.998428

    -3.00132

    4.0007

    12 th iteration approximation is:

    0.999985

    -3.00083

    3.9994

    13 th iteration approximation is:

    1.00041

    -2.99974

    3.99976

    14 th and final Approximation is:

    1.00004

    -2.99976

    4.00013

    810.0007, -13.9974, -33.0004 0, Print@"IVP not satisfied."D,For@i = 1, i nMax, i++,p = N@x1 - f@x1D * HHx1 - x0L Hf@x1D - f@x0DLL, 7D;Print@i, " th approximation is: ", pD;pSign = Sign@f@pDD;If@pSign * f1 < 0, x0 = p, x1 = p; f1 = pSignDD

    D

    1 th approximation is: 1.1

    2 th approximation is: 1.15174

    3 th approximation is: 1.17684

    4 th approximation is: 1.18863

    5 th approximation is: 1.19408

    6 th approximation is: 1.19658

    7 th approximation is: 1.19773

    8 th approximation is: 1.19825

    9 th approximation is: 1.19849

    10 th approximation is: 1.1986

    Regular Falsi Method with stopping condition

    In[358]:= f@x_D := x^3 + 2 x^2 - 3 x - 1;x0 = 1.0; x1 = 2.0; n = 20;

    f1 = Sign@f@x1DD;errList = 8 0, Print@"Ivp not satisfied"D,For@i = 1, i 2, i++,p = x1 - HHx1 - x0L Hf@x1D - f@x0DLL * f@x1D;Print@i, " th approximation is: ", pD;errList = Append@errList, pD;pSign = Sign@f@pDD;If@pSign * f1 < 0, x0 = p, x1 = p; f1 = pSignDD;For@i = 3, i n, i++,p = x1 - HHx1 - x0L Hf@x1D - f@x0DLL * f@x1D;Print@i, " th approximation is: ", pD;errList = Append@errList, pD; = HerrList@@-1DD - errList@@-2DDL HerrList@@-2DD - errList@@-3DDL;If@Abs@ H - 1LD * Abs@HerrList@@-1DD - errList@@-2DDLD < eps, Break@DD;pSign = Sign@f@pDD;If@pSign * f1 < 0, x0 = p, x1 = p; f1 = pSignDD;D

    10 Numerical Methods and Programming.nb

  • 1 th approximation is: 1.1

    2 th approximation is: 1.15174

    3 th approximation is: 1.17684

    4 th approximation is: 1.18863

    5 th approximation is: 1.19408

    6 th approximation is: 1.19658

    7 th approximation is: 1.19773

    8 th approximation is: 1.19825

    9 th approximation is: 1.19849

    10 th approximation is: 1.1986

    11 th approximation is: 1.19865

    Basic Computations

    In[369]:= f@n_D := If@n 1, Return@1D, N@1 n + f@n - 1DDDIn[379]:= a = Input@"Enter the the n"D;

    f@aDOut[380]= 1.5

    In[388]:=

    a = 3

    Out[388]= 3

    In[389]:= sum = 0.0;

    For@i = 1, i a, i++, sum = sum + 1 [email protected]

    In[396]:=

    y = Input@"Enter an array which you want to sort ?"D;Print@"The array is ", yD;For@i = 1, i < 10, i++,

    If@y@@iDD > y@@i + 1DD,For@j = i, j 1, j--,If@y@@i + 1DD > y@@jDD, Break@DDD;

    y = Insert@y, y@@i + 1DD, j + 1D;y = Delete@y, i + 2D

    D;D;

    Print@"The sorted array is ", yDThe array is 810, 7, 2, 9, 8, 6, 3, 4, 5, 1