An Introduction to Numerical Analysis_K. E. Atkinson

download An Introduction to Numerical Analysis_K. E. Atkinson

of 99

Transcript of An Introduction to Numerical Analysis_K. E. Atkinson

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    1/99

    Scilab Textbook Companion for

    An Introduction To Numerical Analysis

    by K. E. Atkinson1

    Created byWarsha B

    B.Tech (pursuing)Electronics Engineering

    VNITCollege Teacher

    G.NAGA RAJUCross-Checked by

    Prashant Dave, IITB

    August 10, 2013

    1Funded by a grant from the National Mission on Education through ICT,http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilabcodes written in it can be downloaded from the Textbook Companion Projectsection at the website http://scilab.in

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    2/99

    Book Description

    Title: An Introduction To Numerical Analysis

    Author: K. E. Atkinson

    Publisher: John Wiley And Sons

    Edition: 2

    Year: 2001

    ISBN: 8126518502

    1

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    3/99

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    4/99

    Contents

    List of Scilab Codes 4

    1 Error Its sources Propagation and Analysis 8

    2 Rootfinding for Nonlinear equations 15

    3 Interpolation Theory 25

    4 Approximation of functions 31

    5 Numerical Integration 41

    6 Numerical methods for ordinary differential equations 54

    7 Linear Algebra 62

    8 Numerical solution of systems of linear equations 66

    9 The Matrix Eigenvalue Problem 76

    3

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    5/99

    List of Scilab Codes

    Exa 1.1 Taylor series . . . . . . . . . . . . . . . . . . . . . . . 8Exa 1.3 Vector norms . . . . . . . . . . . . . . . . . . . . . . . 9Exa 1.4 Conversion to decimal . . . . . . . . . . . . . . . . . . 9Exa 1.5 Error and relative error . . . . . . . . . . . . . . . . . 10Exa 1.6 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . 10Exa 1.7 Taylor series . . . . . . . . . . . . . . . . . . . . . . . 11Exa 1.8 Graph of polynomial . . . . . . . . . . . . . . . . . . . 11Exa 1.9 Error and Relative error . . . . . . . . . . . . . . . . . 12Exa 1.10 Loss of significance errors . . . . . . . . . . . . . . . . 13Exa 1.11 Loss of significance errors . . . . . . . . . . . . . . . . 14Exa 2.1 Bisection method. . . . . . . . . . . . . . . . . . . . . 15Exa 2.2 Newton method . . . . . . . . . . . . . . . . . . . . . 16Exa 2.3 Secant method . . . . . . . . . . . . . . . . . . . . . . 17

    Exa 2.4 Muller method . . . . . . . . . . . . . . . . . . . . . . 18Exa 2.6 Muller method . . . . . . . . . . . . . . . . . . . . . . 19Exa 2.7 One point iteration method . . . . . . . . . . . . . . . 20Exa 2.8 One point Iteration method . . . . . . . . . . . . . . . 21Exa 2.10 Aitken. . . . . . . . . . . . . . . . . . . . . . . . . . . 23Exa 2.11 Multiple roots . . . . . . . . . . . . . . . . . . . . . . 23Exa 3.1 Lagrange formula. . . . . . . . . . . . . . . . . . . . . 25Exa 3.2 Lagrange Formula . . . . . . . . . . . . . . . . . . . . 25Exa 3.3 Lagrange formula. . . . . . . . . . . . . . . . . . . . . 26Exa 3.4 Divided differences . . . . . . . . . . . . . . . . . . . . 26Exa 3.6 Bessel Function. . . . . . . . . . . . . . . . . . . . . . 27

    Exa 3.7 Divided differences . . . . . . . . . . . . . . . . . . . . 28Exa 3.8 Newton forward difference . . . . . . . . . . . . . . . . 28Exa 4.1 Error of approximating exponent of x . . . . . . . . . 31Exa 4.2 Minimax Approximation problem. . . . . . . . . . . . 31

    4

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    6/99

    Exa 4.3 Least squares approximation problem . . . . . . . . . 33

    Exa 4.4 Weight functions . . . . . . . . . . . . . . . . . . . . . 34Exa 4.5 Formulae . . . . . . . . . . . . . . . . . . . . . . . . . 35Exa 4.6 Formulae for laguerre and legendre polynomials . . . . 35Exa 4.7 Average error in approximation . . . . . . . . . . . . . 36Exa 4.8 Chebyshev expansion coefficients . . . . . . . . . . . . 37Exa 4.9 Max errors in cubic chebyshev least squares approx . . 38Exa 4.10 Near minimax approximation . . . . . . . . . . . . . . 38Exa 4.11 Forced oscillation of error . . . . . . . . . . . . . . . . 39Exa 5.1 Integration . . . . . . . . . . . . . . . . . . . . . . . . 41Exa 5.2 Trapezoidal rule for integration . . . . . . . . . . . . . 41Exa 5.3 Corrected trapezoidal rule . . . . . . . . . . . . . . . . 43

    Exa 5.4 Simpson s rule for integration . . . . . . . . . . . . . . 44Exa 5.5 Trapezoidal and simpson integration . . . . . . . . . . 45Exa 5.6 Newton Cotes formulae . . . . . . . . . . . . . . . . . 47Exa 5.7 Gaussian Quadrature . . . . . . . . . . . . . . . . . . 47Exa 5.8 Gaussian Legendre Quadrature . . . . . . . . . . . . . 48Exa 5.9 Integration . . . . . . . . . . . . . . . . . . . . . . . . 49Exa 5.10 Simpson Integration error . . . . . . . . . . . . . . . . 50Exa 5.11 Romberg Integration . . . . . . . . . . . . . . . . . . . 51Exa 5.12 Adaptive simpson . . . . . . . . . . . . . . . . . . . . 51Exa 5.13 Integration . . . . . . . . . . . . . . . . . . . . . . . . 52

    Exa 5.14 Integration . . . . . . . . . . . . . . . . . . . . . . . . 52Exa 6.1 1st order linear differential equation . . . . . . . . . . 54Exa 6.4 Stability of solution . . . . . . . . . . . . . . . . . . . 54Exa 6.5 Euler method. . . . . . . . . . . . . . . . . . . . . . . 55Exa 6.6 Euler . . . . . . . . . . . . . . . . . . . . . . . . . . . 56Exa 6.7 Asymptotic error analysis . . . . . . . . . . . . . . . . 57Exa 6.9 Midpoint and trapezoidal method . . . . . . . . . . . 57Exa 6.10 Euler . . . . . . . . . . . . . . . . . . . . . . . . . . . 58Exa 6.11 Trapezoidal method . . . . . . . . . . . . . . . . . . . 59Exa 6.16 Adams Moulton method. . . . . . . . . . . . . . . . . 59Exa 6.21 Euler method. . . . . . . . . . . . . . . . . . . . . . . 60

    Exa 6.24 Trapezoidal method . . . . . . . . . . . . . . . . . . . 60Exa 6.31 Boundary value problem. . . . . . . . . . . . . . . . . 61Exa 7.1 Orthonomal basis . . . . . . . . . . . . . . . . . . . . 62Exa 7.2 Canonical forms . . . . . . . . . . . . . . . . . . . . . 62Exa 7.3 Orthonomal eigen vectors . . . . . . . . . . . . . . . . 63

    5

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    7/99

    Exa 7.4 Vector and matrix norms . . . . . . . . . . . . . . . . 63

    Exa 7.5 Frobenious norm . . . . . . . . . . . . . . . . . . . . . 64Exa 7.6 Norm . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Exa 7.7 Inverse exists . . . . . . . . . . . . . . . . . . . . . . . 65Exa 8.2 LU decomposition . . . . . . . . . . . . . . . . . . . . 66Exa 8.4 LU decomposition . . . . . . . . . . . . . . . . . . . . 67Exa 8.5 Choleski Decomposition . . . . . . . . . . . . . . . . . 67Exa 8.6 LU decomposition . . . . . . . . . . . . . . . . . . . . 68Exa 8.7 Error analysis. . . . . . . . . . . . . . . . . . . . . . . 69Exa 8.8 Residual correction method . . . . . . . . . . . . . . . 70Exa 8.9 Residual correction method . . . . . . . . . . . . . . . 72Exa 8.10 Gauss Jacobi method . . . . . . . . . . . . . . . . . . 72

    Exa 8.11 Gauss seidel mathod . . . . . . . . . . . . . . . . . . . 73Exa 8.13 Conjugate gradient method . . . . . . . . . . . . . . . 74Exa 9.1 Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . 76Exa 9.2 Eigen values and matrix norm . . . . . . . . . . . . . 76Exa 9.3 Bounds for perturbed eigen values . . . . . . . . . . . 77Exa 9.4 Eigenvalues of nonsymmetric matrix . . . . . . . . . . 79Exa 9.5 Stability of eigenvalues for nonsymmetric matrices . . 80Exa 9.7 Rate of convergence . . . . . . . . . . . . . . . . . . . 81Exa 9.8 Rate of convergence after extrapolation . . . . . . . . 82Exa 9.9 Householder matrix . . . . . . . . . . . . . . . . . . . 82

    Exa 9.11 QR factorisation . . . . . . . . . . . . . . . . . . . . . 83Exa 9.12 Tridiagonal Matrix . . . . . . . . . . . . . . . . . . . . 84Exa 9.13 Planner Rotation Orthogonal Matrix . . . . . . . . . . 84Exa 9.14 Eigen values of a symmetric tridiagonal Matrix . . . . 84Exa 9.15 Sturm Sequence property . . . . . . . . . . . . . . . . 85Exa 9.16 QR Method. . . . . . . . . . . . . . . . . . . . . . . . 85Exa 9.18 Calculation of Eigen vectors and Inverse iteration . . . 86Exa 9.19 Inverse Iteration . . . . . . . . . . . . . . . . . . . . . 87AP 1 Gauss seidel method . . . . . . . . . . . . . . . . . . . 88AP 2 Euler method. . . . . . . . . . . . . . . . . . . . . . . 88AP 3 Eigen vectors . . . . . . . . . . . . . . . . . . . . . . . 89

    AP 4 Boundary value problem. . . . . . . . . . . . . . . . . 90AP 5 Trapezoidal method . . . . . . . . . . . . . . . . . . . 91AP 6 Legendre Polynomial. . . . . . . . . . . . . . . . . . . 92AP 7 Romberg Integration . . . . . . . . . . . . . . . . . . . 93AP 8 Lagrange . . . . . . . . . . . . . . . . . . . . . . . . . 94

    6

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    8/99

    AP 9 Muller method . . . . . . . . . . . . . . . . . . . . . . 95

    AP 10 Secant method . . . . . . . . . . . . . . . . . . . . . . 96AP 11 Newton . . . . . . . . . . . . . . . . . . . . . . . . . . 96AP 12 Aitken1 . . . . . . . . . . . . . . . . . . . . . . . . . . 97AP 13 Bisection method. . . . . . . . . . . . . . . . . . . . . 97

    7

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    9/99

    Chapter 1

    Error Its sources Propagation

    and Analysis

    Scilab code Exa 1.1 Taylor series

    1 // PG ( 6 )2

    3 // T a y l o r s e r i e s f o r e (x 2 ) u pt o f i r s t f o u rt e r m s

    4

    5 deff ( [ y]= f (x ) , y=exp(x 2 ) )6 funcprot (0)

    7 deff ( [ y ] = f p ( x ) , y=2x exp(x 2 ) )8 funcprot (0)

    9 deff ( [ y]= fpp (x ) , y=(12x 2 ) ( 2 e x p( 2x 2) ) )10 funcprot (0)

    11 deff ( [ y ] = g ( x ) , y=4x exp(x 2 ) (3 2 x 2 ) )12 funcprot (0)

    13 deff ( [ y ] = gp( x ) , y = ( 32 x 4 exp(x 2) ) +(72x 2 exp

    (x 2) ) + 12 exp(x 2 ) )14 funcprot (0)15 x 0 = 0 ;

    16 x = poly (0 , x ) ;17 T = f ( x0 ) + ( x -x 0) * fp ( x0 ) / fa ct or ia l ( 1) + ( x- x0 ) ^2 *

    8

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    10/99

    f pp ( x 0 ) / fa c to r ia l ( 2) + ( x - x0 ) ^ 3 * g ( x0 ) / f a ct o ri a l

    ( 3) + ( x - x0 ) ^ 4 * g p ( x0 ) / f a ct o ri a l ( 4)18

    19

    20

    21 // S i m i l a r i l y T a y l o r s e r i e s f o r i n v ( t a n ( x ) )

    Scilab code Exa 1.3 Vector norms

    1 // PG ( 1 1 )2

    3 A = [1 -1;3 2]

    4 x = [1 ;2 ]

    5 y = A * x

    6 norm (A , i n f )7 norm (x , i n f )8 norm (y , i n f )9

    10 x = [1 ;1 ]

    11 y = A * x

    12 norm (y , i n f )13 norm (A , i n f ) * norm (x , i n f )14

    15 / / norm ( y , i n f ) = norm ( A, i n f ) nor m( x , i n f )

    Scilab code Exa 1.4 Conversion to decimal

    1 // PG ( 1 2 )2

    3 // 1 10 11 .0 1 i s a b i n ar y number . I t s d e c i ma le q u i v al e n t i s :

    4

    9

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    11/99

    5 1 *2 ^4 + 1 *2 ^3 + 0 *2 ^2 + 1 *2 ^1 + 1 *2 ^0 + 0* 2^( - 1) +

    1 * 2 ^ ( - 2 )6

    7 // 56C . F i s a h e xa de ci ma l number . I t s d ec im ale q u i v al e n t i s :

    8

    9 5 *1 6^ 2 + 6 *1 6^ 1 + 1 2* 16 ^0 + 1 5* 16 ^( - 1 )

    Scilab code Exa 1.5 Error and relative error

    1 // PG ( 1 7 )2

    3 xT = exp (1)

    4 x A = 19 /7

    5

    6 // E r r o r ( xA )7

    8 x T - xA

    9

    10 // R e l a t i v e e rr or , R el ( xA )

    1112 ( x T - x A ) / x T

    Scilab code Exa 1.6 Errors

    1 // PG ( 1 8 )2

    3 x T = 1/3

    4 x A = 0 .3 335 abs ( x T - x A ) // E r r o r6

    7 //8

    10

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    12/99

    9 x T = 2 3. 49 6

    10 x A = 2 3. 49 411 abs ( x T - x A ) // E r r o r12

    13 //14

    15 x T = 0 .0 21 38

    16 x A = 0 .0 21 44

    17 abs ( x T - x A ) // E r r o r

    Scilab code Exa 1.7 Taylor series

    1 // PG ( 2 0 )2

    3 / / T a y l o r s e r i e s f o r t h e f i r s t two t e r m s4

    5 deff ( [ y]= f (x ) ,y=sq r t (1+x ) )6 funcprot (0)

    7 deff ( [ y ] = f p ( x ) , y = 0 .5(1+x ) ( 1/2) )8 funcprot (0)

    9 x 0 = 0 ;10 x = poly (0 , x ) ;11 T = f ( x0 ) + ( x - x0 ) * fp ( x 0 )/ f a c to r ia l ( 1 )

    Scilab code Exa 1.8 Graph of polynomial

    1 // PG ( 2 1 )2

    3 deff ( [ y]= f (x ) , y = x 33x2+3x1 )4 xset ( window ,0);5 x = - 0 : . 0 1 : 2 ;

    //d e f i n i n g t h e r an ge o f x .

    11

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    13/99

    6 y = feval ( x , f ) ;

    78 a = gca () ;

    9

    10 a . y _ l o c at i o n = o r i g i n ;11

    12 a . x _ l o c at i o n = o r i g i n ;13 plot ( x , y )

    // i n s t r u c t i o n t o p l o t t he gr ap h14

    15 t i t l e ( y = x33x2+3x1 )

    Scilab code Exa 1.9 Error and Relative error

    1 // PG ( 2 4 )2

    3 x T = %pi

    4 x A = 3 .1 41 6

    5 y T = 22 /7

    6 y A = 3 .1 42 97 x T - xA // E r r o r8 ( x T - xA ) / xT // R e l a t i v e E r r o r9 y T - yA // E r r o r

    10 ( y T - yA ) / yT // R e l a t i v e E r r o r11

    12 ( xT - yT ) - ( xA - yA )

    13 (( xT - yT ) - ( xA - yA )) /( xT - yT )

    14

    15 // A l t hou gh th e e r r o r i n xA yA i s q u i t e s ma ll ,

    16 // t h e r e l a t i v e e r r o r i n xA yA i s much l a r g e rt ha n t h at i n xA o r yA a l o ne .

    12

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    14/99

    Scilab code Exa 1.10 Loss of significance errors

    1 // PG ( 2 5 )2

    3 // C o n s i d e r s o l v i n g ax 2 + b x + c =4

    5

    6 // C o ns id e r a p o ly no mi al y = x 2 26 x + 1 = 07

    8 x = poly (0 , x ) ;9 y = x ^2 - 26* x + 1

    10 p = roots ( y )

    11 r a 1 = p (2 , 1)12 r a 2 = p (1 , 1)

    13

    14 // U s i n g t h e s ta nd ar d q ua dr a t i c f or m u la f o rf i n d i n g r oo t s ,

    15

    16 r t1 = ( - ( -26 )+ sqrt ( ( - 2 6) ^ 2 - 4 * 1* 1 ) ) / ( 2 *1 )

    17 r t2 = ( - ( -26 ) -sqrt ( ( - 2 6) ^ 2 - 4 * 1* 1 ) ) / ( 2 *1 )

    18

    19 // R e l a t i v e e r r o r20

    21 r el 1 = ( ra 1 - r t1 ) / r a1

    22 r el 2 = ( ra 2 - r t2 ) / r a2

    23

    24 // The s i g n i f i c a n t e r r o r s have bee n l o s t i n t h es u b t r ac t i o n r a2 = x a ya .

    25 // The a c c u r a c y i n r a2 i s much l e s s .26 // To c a l c u l a t e r a 2 a c c ur at e l y , we u s e :27

    28 r t2 = ( (1 3 - sqrt ( 1 6 8 ) ) * ( 1 3 + sqrt ( 1 6 8 ) ) ) / ( 1 * ( 1 3 + sqrt

    ( 1 6 8 ) ) )

    29 // Now , r t 2 i s n e a r l y e q ua l t o r a 2 . So , by e x a ctc a l c u l a t i o n s , we w i l l now g et a much b e t t e r r e l 2 .

    13

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    15/99

    Scilab code Exa 1.11 Loss of significance errors

    1 // PG ( 2 6 )2

    3 x = poly (0 , x ) ;4 x = 0 ;

    5 deff ( [ y ]= f ( t ) , y=exp ( x t ) )6 i n t e g r a t e ( e xp ( x t ) , t ,0,1)7

    8 / / So , f o r x = 0 , f ( 0 ) = 19 // f ( x ) i s c o n t i n u o u s a t x = 0 .

    10

    11 // To s e e t h a t t he r e i s a l o s s o f s i g n i f i c a n c ep ro bl em when x i s s m al l ,

    12 // we ev a lu at e f ( x ) a t 1 . 4 1 0 ( 9 )13

    14 x = 1 .4 *1 0^ ( - 9 )

    15 i n t e g r a t e ( e xp ( x t ) , t ,0,1)16 // When we u s e a ten d i g i t hand c a l c u l a t o r , t he

    r e s u l t i s 1 .0 00 00 00 0117

    18 // To av o i d t he l o s s o f s i g n i f i c a n c e e r r o r , wemay u s e a q u a d r a t i c T a yl o r a p p ro x i ma t i on t o exp ( x) and t h en s i m p l i f y f ( x ) .

    14

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    16/99

    Chapter 2

    Rootfinding for Nonlinear

    equations

    check AppendixAP 13for dependency:

    bisection1.sce

    Scilab code Exa 2.1 Bisection method

    1 // EXAMPLE ( PG 57)2 // To f i n d l a r g e s t ro o t , al pha , o f x 6 x

    1 = 03 // u s in g b i s e c t i o n method4 // The g r ap h o f t h i s f u n c t i o n can a l s o be

    o b s e r v e d h e r e .5

    6 deff ( [ y]= f (x ) , y=x6x1 )7 // I t i s s t r a i g h t f o r w a r d t o

    show t h a t 1

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    17/99

    12 x = - 5 : . 0 1 : 5 ;

    //d e f i n i n g t h e r an ge o f x .13 y = feval ( x , f ) ;

    14

    15 a = gca () ;

    16

    17 a . y _ l o c at i o n = o r i g i n ;18

    19 a . x _ l o c at i o n = o r i g i n ;20 plot ( x , y )

    // i n s t r u c t i o n t o p l o t t he gr ap h21

    22 t i t l e ( y = x6x1 )23

    24 / / e xe cu t i o n o f t h e u se r d e f i n e d f u n c t i o n s o a s t ou se i t i n program t o f i n d t he a pp ro xi ma tes o l u t i o n .

    25

    26 / / we c a l l a u s e r d e fi n e d f u nc t i o n b i s e c t i o n s o a st o f i n d t he a pp ro xi ma te

    27 / / r o o t o f t h e e qu at i o n w i t h a d e f i ne d p e r m i s s i b l ee r r o r .

    28

    29 b i s e c t i o n ( 1 , 2 , f )

    check AppendixAP 11for dependency:

    newton.sce

    Scilab code Exa 2.2 Newton method

    1 // EXAMPLE ( PG 60)2 // To f i n d l a r g e s t r oo t , a l pha , o f f ( x ) = x 6

    x 1 = 0

    16

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    18/99

    3 // u s in g newton s method

    45

    6 deff ( [ y]= f (x ) , y=x6x1 )7 deff ( [ y ] = f p ( x ) , y=6x5 1 ) //

    D e r i v a t i ve o f f ( x )8 x = ( 1 + 2 ) / 2 // I n i t i a l

    a p p o x i m a t i o n9

    10 / /we c a l l a u s er d e f i n e d f u n c t i o n newton s o a s t of i n d t h e a p pr o xi m at e

    11 / / r o o t o f t h e e qu at i o n w i t h a d e f i ne d p e r m i s s i b l e

    e r r o r .12

    13

    14 n e w t o n ( x , f , f p )

    check AppendixAP 10for dependency:

    secant.sce

    Scilab code Exa 2.3 Secant method

    1 / / EXAMPLE ( PG 6 6 )2 // To f i n d l a r g e s t r oo t , a l pha , o f f ( x ) = x 6

    x 1 = 03 // u s in g s ec an t method4

    5 deff ( [ y]= f (x ) , y=x6x1 )6 a =1

    7 b =2 // I n i t i a l a p pr o xi m at i on s

    89

    10 / / we c a l l a u s e r d e fi n e d f u nc t i o n s ec an t s o a s t of i n d t h e a p pr o xi m at e

    17

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    19/99

    11 / / r o o t o f t h e e qu at i o n w i t h a d e f i ne d p e r m i s s i b l e

    e r r o r .1213 s e c a n t ( a , b , f )

    check AppendixAP 9for dependency:

    muller.sce

    Scilab code Exa 2.4 Muller method

    1 // EXAMPLE1 ( PG 76)2 // f ( x ) = x 20 13 // s o l v i n g u s i n g M u l l e r s method4

    5

    6 xset ( window ,1);7 x = - 2 : . 0 1 : 4 ; //

    d e f i n i n g t he r a ng e o f x .8 deff ( [ y]= f (x ) , y=x20 1 ) ; //

    d e f i n i n g t he c u nc t io n .9 y = feval ( x , f ) ;10

    11 a = gca () ;

    12

    13 a . y _ l o c at i o n = o r i g i n ;14

    15 a . x _ l o c at i o n = o r i g i n ;16 plot ( x , y ) //

    i n s t r u c t i o n t o p l o t t he g r ap h17 t i t l e ( y = x 201 )

    1819 / / from t he abov e p l o t we c an i n f r e t ha t t h e

    f u n c t i o n h as r o o t s b et we en20 // t he i n t e r v a l s ( 0 , 1 ) , ( 2 , 3 ) .21

    18

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    20/99

    22 // s o l l u t i o n by m u ll e r method t o 3 i t e r a t i o n s

    .2324 m u l l e r ( 0 , . 5 , 1 , f )

    check AppendixAP 9for dependency:

    muller.sce

    Scilab code Exa 2.6 Muller method

    1 // EXAMPLE3 ( PG 76)2 // f ( x ) = x6 12 x 5 + 6 3 x 4 216 x 3

    + 5 6 7 x 2 972 x + 7 2 93 // o r f ( x ) = ( x2+9) ( x 3)44 // s o l v i n g u s i n g M u l l e r s method5

    6 deff ( [ y]= f (x ) , y=(x2+9) ( x 3)4 )7

    8 xset ( window ,2);9 x = - 1 0 : . 1 : 1 0 ;

    //d e f i n i n g t h e r an ge o f x .

    10 y = feval ( x , f ) ;

    11

    12 a = gca () ;

    13

    14 a . y _ l o c at i o n = o r i g i n ;15

    16 a . x _ l o c at i o n = o r i g i n ;17 plot ( x , y )

    // i n s t r u c t i o n t o p l o t t he gr ap h18

    19 t i t l e ( y = ( x 2+9 ) ( x 3)4 )20

    19

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    21/99

    21

    22 m u l l e r ( 0 , . 5 , 1 , f )

    Scilab code Exa 2.7 One point iteration method

    1 // EXAMPLE ( PG 77)2 // x2a = 03

    4 // The g r ap h f o r x23 c an a l s o be o bs er ve d

    h e r e .5

    6 deff ( [ y]= f (x ) , y=x x3 )7 funcprot (0)

    8 xset ( window ,3);9 x = - 2 : . 0 1 : 1 0 ;

    //d e f i n i n g t h e r an ge o f x .

    10 y = feval ( x , f ) ;

    11

    12 a = gca () ;

    1314 a . y _ l o c at i o n = o r i g i n ;15

    16 a . x _ l o c at i o n = o r i g i n ;17 plot ( x , y )

    // i n s t r u c t i o n t o p l o t t he gr ap h18

    19 t i t l e ( y = x23 )20 // CASE 1

    2122 / /We h a v e f ( x ) = x 2a .23 / / So , we a s su me g ( x ) = x 2+xa and t h e v al ue o f a =

    324

    20

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    22/99

    25 deff ( [ y ] = g ( x ) , y=x2+x3 )

    26 funcprot (0)27 x =2

    28 for n = 0 : 1 : 3

    29 g ( x ) ;

    30 x = g ( x )

    31 end

    32

    33 // CASE 234

    35 / /We h a v e f ( x ) = x 2a .36 // So , we as sume g ( x ) = a /x and t he v a lu e o f a = 3

    3738 deff ( [ y ] = g ( x ) , y=3/x )39 funcprot (0)

    40 x =2

    41 for n = 0 : 1 : 3

    42 g ( x ) ;

    43 x = g ( x )

    44 end

    45

    46 // CASE 347

    48 / /We h a v e f ( x ) = x 2a .49 / /So , we a ss um e g ( x ) = 0 . 5 ( x +( a / x ) ) a nd t h e v a l u e

    o f a = 350

    51 deff ( [ y ] = g ( x ) , y = 0 .5 ( x+(3/x) ) )52 funcprot (0)

    53 x =2

    54 for n = 0 : 1 : 3

    55 g ( x ) ;

    56 x = g ( x )

    57 end

    21

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    23/99

    Scilab code Exa 2.8 One point Iteration method

    1 // EXAMPLE ( PG 81)2

    3 // Assume a l ph a i s a s o l u t i o n o f x = g ( x )4

    5 a l p h a = sqrt ( 3 ) ;

    6

    7 // c a s e 18

    9

    10 deff ( [ y ] = g ( x ) , y=x2+x3 )

    11 deff ( [ z ] = g p ( x ) , z =2x+1 ) // D e r i v a t i v eo f g ( x )

    12 g p ( a l p h a )

    13

    14 // c a s e 215

    16 deff ( [ y ] = g ( x ) , y=3/x )17 funcprot (0)

    18 deff ( [ z ] = g p ( x ) , z=3/x ) // D e r i v a t i v e o f g ( x )

    19 g p ( a l p h a )

    20

    21 // c a s e 322

    23 deff ( [ y ] = g ( x ) , y = 0 .5 ( x+(3/x) ) )24 funcprot (0)

    25 deff ( [ z ] = g p ( x ) , z = 0. 5( 1 ( 3/( x 2) ) ) ) //D e r i v at i ve o f g ( x )

    26 g p ( a l p h a )

    check AppendixAP 12for dependency:

    aitken1.sce

    22

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    24/99

    Scilab code Exa 2.10 Aitken

    1 // EXAMPLE ( PG 85)2

    3 // x ( n+1) = 6 . 2 8 + s i n ( x ( n ) )4 // True r oo t i s a l p h a = 6 .0 15 50 30 72 975

    6 deff ( [ y]= f (x ) , f ( x ) = 6. 28+ si n ( x ( n ) ) )7 // k =6 .0 1 55 0 3 07 2 978

    9 / / x = 6 . 0 1 5 5 0 3 0 7 2 9 710

    11 deff ( [ y ] = g ( x ) , y= c os ( x ) )12

    13

    14 / / we c a l l a u s e r d e fi n e d f u nc t i o n a it ke n s o a s t of i n d t h e a p pr o xi m at e

    15 / / r o o t o f t h e e qu at i o n w i t h a d e f i ne d p e r m i s s i b l ee r r o r .

    16

    17

    18 a i t k e n ( 0 . 2 , 0 . 5 , 1 , g )

    Scilab code Exa 2.11 Multiple roots

    1 // EXAMPLE ( PG 87)2

    3 // f ( x ) = ( x 1 . 1 ) 3 ( x 2 . 1 )4

    5 c = [ 2. 79 51 - 8. 95 4 1 0. 56 -5. 4 1]

    6 p4 = poly (c , x , c o e f f )7 roots ( p 4 )8 deff ( [ y]= f (x ) , y=(x 1.1 ) 3 ( x 2. 1) )9 xset ( window ,0);

    23

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    25/99

    10 x = 0 : . 0 1 : 3 ;

    //d e f i n i n g t h e r an ge o f x .11 y = feval ( x , f ) ;

    12

    13 a = gca () ;

    14

    15 a . y _ l o c at i o n = o r i g i n ;16

    17 a . x _ l o c at i o n = o r i g i n ;18 plot ( x , y )

    // i n s t r u c t i o n t o p l o t t he gr ap h19

    20 t i t l e ( y = ( x 1. 1) 3 ( x 2. 1) )

    24

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    26/99

    Chapter 3

    Interpolation Theory

    check AppendixAP 8for dependency:

    lagrange.sce

    Scilab code Exa 3.1 Lagrange formula

    1 // PG ( 1 3 4 )2

    3 X = [0 , -1 , 1]

    4 Y = [1 , 2 , 3 ]

    5 l a g r a n g e ( X , Y )

    check AppendixAP 8for dependency:

    lagrange.sce

    Scilab code Exa 3.2 Lagrange Formula

    1 // PG ( 1 3 6 )2

    3

    25

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    27/99

    4

    5 X = [ 0 ]6 Y = [ 1 ]

    7 deff ( [ y]= f (x ) , y= l o g 1 0 ( x ) )8 p = l a g r a n g e ( X , Y )

    check AppendixAP 8for dependency:

    lagrange.sce

    Scilab code Exa 3.3 Lagrange formula

    1 // PG ( 1 3 7 )2

    3 X = [ 0 , - 1 ]

    4 Y = [ 1 , 2 ]

    5 deff ( [ y]= f (x ) , y= l o g ( x ) )6 deff ( [ y ] = f p ( x ) , y=1/x )7 deff ( [ y]= fpp (x ) , y=1/(x ) 2 )8 p = l a gr a ng e ( X , Y)

    9 // E = f ( x )p10 e = 0 .0 00 05 // f o r a f o u r p l a ce l o g a r i t hm i ct a b l e

    Scilab code Exa 3.4 Divided differences

    1 // PG ( 1 4 0 )2

    3 X = [ 2. 0 , 2. 1 , 2. 2 , 2. 3 , 2. 4]4 X1 = X (1 ,1)

    5 X2 = X (1 ,2)

    6 X3 = X (1 ,3)

    7 X4 = X (1 ,4)

    26

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    28/99

    8 X5 = X (1 ,5)

    9 deff ( [ y]= f (x ) , y= s q r t ( x ) )10 Y = [ f( X1 ) f ( X2 ) f (X3 ) f ( X4 ) f (X5 ) ]11 Y1 = Y (1 ,1)

    12 Y2 = Y (1 ,2)

    13 Y3 = Y (1 ,3)

    14 Y4 = Y (1 ,4)

    15 Y5 = Y (1 ,5)

    16

    17 // D i f f e r e n c e18

    19 // f [ X1 , X2 ]

    20 ( f ( X 2 ) - f ( X1 ) ) * 1021 // f [ X2 , X3 ]22 ( f ( X 3 ) - f ( X2 ) ) * 10

    23 // f [ X3 , X4 ]24 ( f ( X 4 ) - f ( X3 ) ) * 10

    25 // f [ X4 , X5 ]26 ( f ( X 5 ) - f ( X4 ) ) * 10

    27

    28 // D2 f [ Xi ]29

    30 ( ( f ( X3 ) - f ( X2 ) ) - ( f ( X2 ) - f ( X1 ) ) ) * 5 0

    31 ( ( f ( X4 ) - f ( X3 ) ) - ( f ( X3 ) - f ( X2 ) ) ) * 5 0

    32 ( ( f ( X5 ) - f ( X4 ) ) - ( f ( X4 ) - f ( X3 ) ) ) * 5 0

    Scilab code Exa 3.6 Bessel Function

    1 // PG ( 1 4 2 )2

    3 // V a l u e s o f B e s se l Fu n c t i o n Jo ( x )45 // x Jo ( x )6

    7 // 2 . 0 0 . 2 2 3 8 9 0 7 7 9 1

    27

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    29/99

    8 // 2 . 1 0 . 1 6 6 6 0 6 9 8 0 3

    9 // 2 . 2 0 . 1 1 0 3 6 2 2 6 6 910 // 2 . 3 0 . 0 5 5 5 3 9 7 8 4 411 // 2 . 4 0 . 0 0 2 5 0 7 6 8 3 212 // 2 . 5 0. 048383776413 // 2 . 6 0. 096804954414 // 2 . 7 0. 142449370015 // 2 . 8 0. 185036033416 // 2 . 9 0. 224311545817

    18 // C al cu l a t e t h e v a lu e o f x f o r w hi ch Jo ( x ) = 0 . 1

    Scilab code Exa 3.7 Divided differences

    1 // PG ( 1 4 4 )2

    3 deff ( [ y]= f (x ) , y= s q r t ( x ) )4 funcprot (0)

    5 deff ( [ y ] = f p ( x ) , y = 0 . 5/ s q r t ( x ) )6 funcprot (0)

    7 deff ( [ y]= fpp (x ) , y = 0.25 x ( 3/2) )8 funcprot (0)

    9 deff ( [ y]= fppp ( x ) , y=3x ( 2 . 5 ) / 8 )10 deff ( [ y]=f pppp ( x ) , y=15x ( 7/2) /16 )11

    12 // f [ 2 . 0 , 2 . 1 , . . . . . 2 . 4 ] = 0. 00208413

    14 f p p p p ( 2 . 3 1 0 3 ) / f a c t o r i a l ( 4 )

    Scilab code Exa 3.8 Newton forward difference

    1 // PG ( 1 5 0 )2

    28

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    30/99

    3 X = [ 2. 0 , 2. 1 , 2. 2 , 2. 3 , 2. 4]

    4 X1 = X (1 ,1)5 X2 = X (1 ,2)

    6 X3 = X (1 ,3)

    7 X4 = X (1 ,4)

    8 X5 = X (1 ,5)

    9 deff ( [ y]= f (x ) , y= s q r t ( x ) )10 Y = [ f( X1 ) f ( X2 ) f (X3 ) f ( X4 ) f (X5 ) ]

    11 Y1 = Y (1 ,1)

    12 Y2 = Y (1 ,2)

    13 Y3 = Y (1 ,3)

    14 Y4 = Y (1 ,4)

    15 Y5 = Y (1 ,5)16

    17 // D i f f e r e n c e18

    19 // f [ X1 , X2 ]20 ( f (X2 ) - f ( X1 ) )

    21 // f [ X2 , X3 ]22 ( f (X3 ) - f ( X2 ) )

    23 // f [ X3 , X4 ]24 ( f (X4 ) - f ( X3 ) )

    25 // f [ X4 , X5 ]26 ( f (X5 ) - f ( X4 ) )

    27

    28 // D2 f [ Xi ]29

    30 ( ( f ( X 3 ) - f ( X2 ) ) - ( f ( X 2 ) - f ( X1 ) ) )

    31 ( ( f ( X 4 ) - f ( X3 ) ) - ( f ( X 3 ) - f ( X2 ) ) )

    32 ( ( f ( X 5 ) - f ( X4 ) ) - ( f ( X 4 ) - f ( X3 ) ) )

    33

    34 // D3 f [ Xi ]35

    36 ( ( f ( X4 ) - f ( X3 ) ) - ( f ( X3 ) - f ( X2 ) ) ) - ( ( f( X 3 ) -f ( X2 ) ) - (f ( X 2 ) - f ( X 1 ) ) )

    37 ( ( f ( X5 ) - f ( X4 ) ) - ( f ( X4 ) - f ( X3 ) ) ) - ( ( f( X 4 ) -f ( X3 ) ) - (

    f ( X 3 ) - f ( X 2 ) ) )

    38

    29

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    31/99

    39 // D4 f [ Xi ]

    4041 ( (( f ( X 5 ) -f ( X4 ) ) - ( f ( X4 ) - f ( X3 ) ) ) - ( ( f( X 4 ) -f ( X3 ) ) -

    ( f ( X3 ) - f ( X2 ) ) ) ) - ( (( f ( X4 ) - f ( X3 ) ) - ( f ( X3 ) - f ( X2 ) )

    ) - ( ( f( X 3 ) -f ( X2 ) ) - ( f ( X2 ) - f ( X1 ) ) ) )

    42

    43 m u = 1. 5;

    44 x = 2. 15 ;

    45

    46 p1 = f (X1 ) + mu * ( f( X2 ) - f (X1 ))

    47 p 2 = p 1 + m u *( mu - 1) * ( ( f( X 3 ) -f ( X2 ) ) - ( f ( X2 ) - f ( X1 ) ) )

    /2

    4849 // S i m i l ar l y , p3 = 1 .4 6 6 28 850 // p4 = 1 . 4 6 6 2 8 8

    30

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    32/99

    Chapter 4

    Approximation of functions

    Scilab code Exa 4.1 Error of approximating exponent of x

    1 // PG ( 1 9 9 )2

    3 x = poly (0 , x ) ;4 p 3 = 1 + x + ( 1/ 2) * x ^( 2) + ( 1/ 6) * x^3

    5 deff ( [ y]= f (x ) , y=exp ( x ) )6 funcprot (0)

    7 x = - 1: 0. 01 :1 ;8 f ( x) - p3

    Scilab code Exa 4.2 Minimax Approximation problem

    1 // PG ( 2 0 0 )2

    3 deff ( [ y]= f (x ) , y=exp ( x ) )4

    5 xset ( window ,0);6 x = - 1 : . 0 1 : 1 ; // d e f i ni n g t h e r a ng e o f

    x .

    31

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    33/99

    7 y = feval ( x , f ) ;

    89 a = gca () ;

    10

    11 a . y _ l o c at i o n = o r i g i n ;12

    13 a . x _ l o c at i o n = o r i g i n ;14 plot ( x , y ) // i n s t r u c t i o n t o p l o t t h e

    gr aph15

    16

    17

    18 // p o s s i b l e a p p r o x i m a t i o n19 // y = q1 ( x )20

    21 / / Le t e ( x ) = exp ( x ) [ a0+a1 x ]22 // q1 ( x ) & exp ( x ) must b e e qu al a t two p o i n t s i n

    [ 1 , 1 ] , s ay a t x1 & x 223 / / s i gm a 1 = max ( a b s ( e ( x ) ) )24 / / e ( x1 ) = e ( x2 ) = 0 .25 // By a no th er argument b a s e d on s h i f t i n g t h e

    g ra ph o f y = q1 ( x ) ,26

    // we c on c l ud e t h a t t he maximum e r r o r s i gm a1 i sa t ta i n e d a t e x ac t l y 3 p o i n t s .27 // e ( 1 ) = s i g m a 128 // e ( 1 ) = s i g m a129 // e ( x3 ) = s i g m a 130 // x1 < x3 < x231 // S i n c e e ( x ) ha s a r e l a t i v e minimum a t x3 , we

    h av e e ( x ) = 032 / / Combining t h e s e 4 e qu at i o ns , we h ave . .33 // exp ( 1) [ a0a 1 ] = s i g ma 1 (

    i )

    34 // exp ( 1 ) [ a0+a1 ] = p1 (i i )

    35 // exp ( x3 ) [ a0+a1 x 3 ] = s i g m a 1 (i i i )

    32

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    34/99

    36 // exp ( x3 ) a1 = 0 (

    i v )3738 // These have t h e s o l u t i o n39

    40 a 1 = ( exp (1) - exp ( - 1 ) ) / 2

    41 x3 = log ( a 1 )

    42 s i g ma 1 = 0 .5 * exp ( -1) + x3 *( exp (1 ) - exp ( - 1 ) ) / 4

    43 a 0 = s ig ma 1 + (1 - x3 ) * a1

    44

    45 x = poly (0 , x ) ;46 // Thus ,

    47 q1 = a0 + a1 *x48

    49 deff ( [ y1]= f ( x ) , y 1 = 1 . 2 64 3 + 1 .1 7 5 2 x )50

    51 xset ( window ,0);52 x = - 1 : . 0 1 : 1 ; // d e f i ni n g t h e r a ng e o f

    x .53 y = feval ( x , f ) ;

    54

    55 a = gca () ;

    56

    57 a . y _ l o c at i o n = o r i g i n ;58

    59 a . x _ l o c at i o n = o r i g i n ;60 plot ( x , y ) // i n s t r u c t i o n t o p l o t t h e

    gr aph

    Scilab code Exa 4.3 Least squares approximation problem

    1 // PG ( 2 0 5 )2

    3 deff ( [ y]= f (x ) , y=exp ( x ) )4

    33

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    35/99

    5 x = - 1 : . 0 1 : 1 ; // d e f i ni n g t h e r a ng e o f

    x67 // Le t r 1 ( x ) = b0 + b1 ( x )8 // Mi n i m i z e9 // | | fr 1| | 2 = i n t e g r a t e ( ( e xp ( x )b0b1 x )

    2 , x , 1 , 1) = F ( b0 , b1)10 // F = i n t e g r a t e ( exp ( 2 x ) + b 0 2 + ( b1 2 ) ( x 2 )

    2 b0 x e xp ( x ) + 2 b0 b1 x , x , b0 , b1 )11 // To f i n d a minimum , we s e t12

    13 // d f / db0 = 0

    14 // d f / db1 = 0n e c e s s ar y c o n d i t i o n sa t a m in im al p o i nt

    15 // On s ol vi ng , we g et t h e v al ue s o f b0 & b116

    17 b 0 = 0. 5* i n t e g r a t e ( e x p ( x ) , x ,-1,1)18 b 1 = 1. 5* i n t e g r a t e ( x exp ( x ) , x ,-1,1)19 r 1 = b0 + b1 * x;

    20 norm ( exp ( x ) - r 1 , i n f ) // l e a s t s q u a r e sa p p r o x i m a t i o n

    21

    22 r 3 = 0 .9 96 29 4 + 0 .9 97 95 5* x + 0 .5 36 72 2* x ^ 2 +

    0 . 1 7 6 1 3 9 * x ^ 3

    23 norm ( exp ( x ) - r 3 , i n f ) // c u b i c l e a s t s q u a r e sa p p r o x i m a t i o n

    Scilab code Exa 4.4 Weight functions

    1 // PG ( 2 0 6 )

    23 // The f o l l o w i n g a r e t he w e i g ht f u n c t i o n s o f mosti n t e r e s t i n t h e

    4 // d e v e l o p m e n t s o f t h i s t e x t :5

    34

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    36/99

    6 // w( x ) =1 a < = x < = b

    78 // w( x ) =1/ s q r t (1x 2 ) 1 < = x < = 19

    10 // w( x )=exp(x ) 0 < = x < i n f i n i t y11

    12 // w( x )=exp(x 2 ) i n f i n i t y < x =16

    7 // I t i s an e x p l i c i t twos t e p m et ho d .8

    9

    10 // The t r a p e z o i d a l method i s d e f i n e d by11

    12 // y ( n+1) = yn + h [ f ( x n , y n) + f ( x ( n+ 1) , y ( n+ 1) )]n>=0

    13

    14 // I t i s an i m p l i c i t ones t e p m et ho d .

    check AppendixAP 2for dependency:

    euler.sce

    Scilab code Exa 6.10 Euler

    1 // PG ( 3 6 5 )23 deff ( [ z ]=g (x , y ) , z=y )4 [ y , x ] = E u l er 1 ( 0 . 25 , 1 , 2 .2 5 , 0 .2 5 , g )

    5

    6 //7

    8 deff ( [ z]= f (x , y ) , z=xy2 )9 [ y , x ] = E u l er 1 ( 0 . 25 , 0 , 3 .2 5 , 0 .2 5 , f )

    check AppendixAP 5for dependency:

    trapezoidal.sce

    58

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    60/99

    Scilab code Exa 6.11 Trapezoidal method

    1 // PG ( 3 7 2 )2

    3 deff ( [ y]= f ( x , y ) , y=y2 )4 [ x , y ] = t r a p ez o i d al ( 1 , 1 , 5 , 1 , f )

    Scilab code Exa 6.16 Adams Moulton method

    1 // PG ( 3 8 9 )2

    3 // U s i n g AdamsM oul ton F or mul a4

    5 deff ( [ z]= f (x , y ) , z = ( 1/( 1+ x 2) ) 2y2 )6 y 0 = 0;

    7

    8 // S o l u t i o n i s Y( x ) = x /(1+ x 2 )9

    10 function [ y , x ] = a d a m sm o u l to n 4 ( y 0 , x0 , x n , h , f )

    11

    12 / / a d a ms mo u lt on 4 4 t h o r d e r m et ho d s o l v i n g ODE13 / / dy / dx = f ( y , x ) , w i t h i n i t i a l14 / / c o n d i t i o n s y=y0 a t x=x 0 . The15 / / s o l u t i o n i s o b t a i n ed f o r x = [ x0 : h : xn ]16 / /and r e tu r n ed i n y17

    18 u m ax A ll o we d = 1 e + 10 0;

    19

    20 x = [ x 0 : h : x n ] ; y = zeros ( x ) ; n = length ( y) ; y (1 ) =

    y0 ;

    21 for j = 1: n-122 if j

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    61/99

    26 end ;

    2728 if j > = 2 then

    29 y ( j +2 ) = y ( j +1 ) + ( h / 12 ) * ( 23 * f ( x( j + 1) , y ( j +1 )

    ) - 1 6 * f ( x ( j ) , y ( j ) ) + 5 * f ( x ( j - 1 ) , y ( j - 1 ) ) ) ;

    30 end ;

    31 end ;

    32 e n d f u n c t i o n

    33

    34 a d a m s m o u l t o n 4 ( 0 , 2 . 0 , 1 0 . 0 , 2 . 0 , f )

    check AppendixAP 2for dependency:

    euler.sce

    Scilab code Exa 6.21 Euler method

    1 // PG ( 4 0 5 )2

    3 deff ( [ y]= f ( x , y ) , y=lamda y+(1l amda) c o s ( x ) (1+

    l amda) s i n ( x ) )4 l a md a = -1;5 [ x , y ] = E u l e r 1 ( 1 , 1 , 5 , 0 . 5 , f )

    6 l am da = -10 ;

    7 [ x , y ] = E u l e r 1 ( 1 , 1 , 5 , 0 . 1 , f )

    8 l am da = -50 ;

    9 [ x , y ] = E u l e r 1 ( 1 , 1 , 5 , 0 . 0 1 , f )

    check AppendixAP 5for dependency:

    trapezoidal.sce

    Scilab code Exa 6.24 Trapezoidal method

    60

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    62/99

    1 // PG ( 4 0 9 )

    23 deff ( [ y]= f ( x , y ) , y=lamda y+(1l amda) c o s ( x ) (1+

    l amda) s i n ( x ) )4 l a md a = -1;

    5 [ x , y ] = t r a p e z o i d a l ( 1 , 1 , 5 , 0 . 5 , f )

    6 l am da = -10 ;

    7 [ x , y ] = t r a p e z o i d a l ( 1 , 1 , 5 , 0 . 5 , f )

    8 l am da = -50 ;

    9 [ x , y ] = t r a p e z o i d a l ( 1 , 1 , 5 , 0 . 5 , f )

    check AppendixAP 4for dependency:

    bvpeigen.sce

    check AppendixAP 3for dependency:

    eigenvectors.sce

    Scilab code Exa 6.31 Boundary value problem

    1 // PG ( 4 3 4 )2

    3 // 2 p o i n t l i n e a r Bo unda ry v a l u e p ro bl em4

    5

    6 // Boundary va l ue pr ob le ms wi th e i g e n v a l u es c a s e : d y / dx 2 + l am y = 0

    7 / / s u b j e c t t o y ( 0 ) = 0 , y ( 1 ) = 0 , where lam i sunknown .

    8 // The f i n i t e d i f f e r e n c e a pp ro xi ma ti on i s :9 // ( y ( i1) 2y ( i )+y ( i +1) )=lam Dx2 y ( i ) , i =

    2 , 3 , . . . , n11011

    12 [ x ,y , l am ] = B V Pe i ge n 1 ( 1 ,5 )

    61

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    63/99

    Chapter 7

    Linear Algebra

    Scilab code Exa 7.1 Orthonomal basis

    1 // PG ( 4 7 0 )2

    3 u 1 = [1/2 , sqrt (3)/2]

    4 u 2 = [ - sqrt ( 3 ) / 2 , 1 / 2 ]

    5

    6 / / For a g iv en v e c t o r x = ( x1 , x2 ) , i t can be

    w r i t t e n a s7 // x = a l p h a 1 u 1 + a l p h a 2 u28 // a l p h a 1 = ( x1+x2 s q r t ( 3 ) ) / 29 // a l p h a 2 = ( x2x1 s q r t ( 3 ) ) / 2

    10

    11 // ( 1 , 0 ) = ( 1 / 2 ) u1 ( s q r t ( 3 ) / 2 ) u2

    Scilab code Exa 7.2 Canonical forms

    1 // PG ( 4 7 6 )2

    3 A = [ 0. 2 0.6 0 ;1 .6 -0.2 0; - 1.6 1.2 3. 0]

    62

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    64/99

    4 U = [ 0. 6 0 - 0.8 ;0 .8 0 0 .6 ;0 1 .0 0]

    5 U s ta r = inv ( U )6 T = U st ar * A *U

    7 trace ( A )

    8 l am = spec ( A )

    9 l a m1 = l am ( 1 , 1)

    10 l a m2 = l am ( 1 , 2)

    11 l a m3 = l am ( 1 , 3)

    12 la m1 + la m2 + l am3

    13

    14 // t r a c e (A) = lam1 + lam2 + lam315

    16 det ( A )17 l a m 1 * l a m 2 * l a m 3

    18

    19 // d e t (A) = lam1 lam2 lam3

    Scilab code Exa 7.3 Orthonomal eigen vectors

    1 // PG ( 4 7 7 )

    23 A = [2 1 0;1 3 1;0 1 2]

    4 l am = spec ( A )

    5 l a m1 = l am ( 1 , 1)

    6 l a m2 = l am ( 1 , 2)

    7 l a m3 = l am ( 1 , 3)

    8 // Orthonomal E i g e n v ec t o r s9

    10 u 1 = (1/ sqrt ( 3 ) ) * [ 1 ; - 1 ; 1 ]

    11 u 2 = (1/ sqrt ( 2 ) ) * [ 1 ; 0 ; - 1 ]

    12 u 3 = (1/ sqrt ( 6 ) ) * [ 1 ; 2 ; 1 ]

    Scilab code Exa 7.4 Vector and matrix norms

    63

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    65/99

    1 // PG ( 4 8 1 )

    23 x = [1 , 0 , -1 , 2]

    4 // 1norm5 norm ( x , 1 )

    6 // 2norm7 norm ( x , 2 )

    8 // i n f i n i t y norm9 norm (x , i n f )

    Scilab code Exa 7.5 Frobenious norm

    1 // PG ( 4 8 4 )2

    3 // A be n n4 // norm (Ax , 2 )5 // norm (Ax , 2 )

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    66/99

    Scilab code Exa 7.7 Inverse exists

    1 // PG ( 4 9 4 )2

    3 A = [4 1 0 0;1 4 1 0;0 1 4 1;0 0 1 4]

    4 B = A /4 - eye ()

    5 norm (B , i n f )6 // Le t ( I+B = C)7 C = eye () + B

    8 inv ( C )

    9 // I n v e r s e o f ( I + B) e x i s t s10 norm (C , i n f )11 // I n v e r s e o f A e x i s t s .

    65

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    67/99

    Chapter 8

    Numerical solution of systems

    of linear equations

    Scilab code Exa 8.2 LU decomposition

    1 // EXAMPLE ( PG 512 )2

    3 A = [1 2 1;2 2 3; -1 -3 0] //C o e f f i c i e n t m at ri x

    4 b = [0 3 2 ] // R i g h th an d m a t r i x

    5 [ l , u] = lu ( A )

    6 // l i s l o w e r t r i a n g u l a r ma t r i x & u i s u ppe rt r i a n g u l a r m at ri x

    7 l * u

    8 if ( A = = l * u )

    9 disp (A = LU i s v e r i f i e d )10 end

    11 det ( A )

    12 det ( u )13 if ( det ( A ) = = det ( u ) )

    14 disp ( D e t e r m i n a n t o f A i s e qu al t o t ha t o f i t su pp er t r i a n g u l a r m at ri x )

    15

    66

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    68/99

    16 // P r o du c t r u l e o f d e t e r m i n a nt s i s v e r i f i e d

    Scilab code Exa 8.4 LU decomposition

    1 // EXAMPLE ( PG 518 )2

    3 // Row i n t e r ch a n g e s on A can be r e pr e se n t e dby p r e m u l t i p l i c a t i o n o f A

    4 // by an a p p r o p r i a t e ma t r i x E , t o g et EA .

    5 // Then , G au s si a n El i m i n a t i o n l e a ds t o LU =PA6

    7 A = [ 0. 72 9 0 .8 1 0 .9 ;1 1 1 ;1 .3 31 1 .2 1 1 .1 ] //C o e f f i c i e n t Ma tr ix

    8 b = [ 0. 68 67 0 .8 33 8 1 .0 00 ] //R i g h t Hand M a t r i x

    9 [ L , U ,E ] = lu ( A )

    10 // L i s lo we r t r i a n g u l a r m a t r i x ( mxn )11 // U i s u pp er t r i a n g u l a r m at ri x ( mxmin (m, n ) )12 // E i s p e r mu ta ti o n m at ri x ( min (m, n ) xn )

    13 Z = L * U14

    15 disp ( LU = EA )16 E

    17

    18 // The r e s u l t EA i s t he ma t r i x A w i t h f i r s t ,r ow s 1 & 3 i n t e rc h a n g ed ,

    19 // and t he n row s 2 & 3 in t e rc ha ng ed .20

    21 // NOTE: A cc or di ng t o t he book , P i s r e p l a c e dby E h e r e .

    Scilab code Exa 8.5 Choleski Decomposition

    67

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    69/99

    1 // EXAMPLE ( PG 526 )

    23 disp ( C o ns i d er H i l b e r t m at ri x o f o r de r t h r ee )4

    5 n = 3 ; // Order o f t h e m a t r i x6 A = zeros ( n , n ) ; // a s y m me t r i c p o s i t i v e d e f i n i t e

    r e a l o r c om pl ex m a tr i x .7 for i = 1 : n / / I n i t i a l i z i n g f o r l o o p8 for j = 1 : n

    9 A ( i , j ) = 1 / ( i + j - 1 ) ;

    10 end

    11 end / /End o f f o r l o op

    12 A13 chol ( A ) // C h o l e s k i

    D e c o m p o s i t i o n14 L =[ chol ( A ) ] // Lower T r i an gu la r

    M at r i x15

    16 // The s qu a re r o o t s o bt ai ne d h er e can bea vo id ed u si ng a s l i g h t m o d i f i c a t i o n .

    17 // We f i n d a di ag o n a l m a t r i x D & a l ow e rt r i a n g u l a r m at ri x ( L ) ,

    18 // w i t h 1 s on t h e d ia go na l su c h t h a t A = ( L ) D ( L )

    19

    20

    21 // c ho l (A) u s e s o nl y t h e d i a g on a l and up pe rt r i a n g l e o f A .

    22 // The l ow er t r i a n g u l a r i s assumed t o be t h e23 // ( compl ex c on j u ga t e ) t r an s po s e o f th e upp er

    .

    Scilab code Exa 8.6 LU decomposition

    1 // EXAMPLE ( PG 529 )

    68

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    70/99

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    71/99

    22 // C o n s i d e r t h e p a r t i c u l a r c a s e

    2324 b = [1 0.7] ; // R i g h t hand m a t r i x25 x = A \ b ; // S o l u t i o n m a t r i x26

    27 // S o l u t i o n m a t r i x28

    29 x1 = x (1 ,:)

    30 x2 = x (2 ,:)

    31

    32 // For t h e p er tu rb ed system , we s o l v e f o r :33

    34 b = [ 1. 01 0 .6 9] ; // R i g h t hand m a t r i x35 x = A \ b ; // S o l u t i o n m a t r i x36

    37 // S o l u t i o n m a t r i x38

    39 x1 = x (1 ,:)

    40 x2 = x (2 ,:)

    41

    42 // The r e l a t i v e c h a n g e s i n x a r e q ui te l a r g ewhen c o mp a re d w i t h

    43 // t h e s i z e o f t h e r e l a t i v e c h a n g e s i n t h er i g h t s i d e b .

    Scilab code Exa 8.8 Residual correction method

    1 // EXAMPLE ( PG 541 )2

    3 // C o n s i d e r a H i l b e r t m a t r i x o f o r de r 3

    45 n = 3 ; // Order o f t h e m a t r i x6 A = zeros ( n , n ) ; // a s y mm e t r i c p o s i t i v e d e f i n i t e

    r e a l o r c om pl ex m a tr i x .7 for i = 1 : n / / I n i t i a l i z i n g f o r l o o p

    70

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    72/99

    8 for j = 1 : n

    9 A ( i , j ) = 1 / ( i + j - 1 ) ;10 end

    11 end // End o f f o r l o o p12 A

    13

    14 // Rounding o f f t o 4 d e c i m a l p l a c e s15 A = A *1 0^ 4;

    16 A = int ( A ) ;

    17 A = A * 10 ^( - 4) ;

    18 disp ( A ) // F i n a l S o l u t i o n19

    20 H = A // Here H d e n o t e d H ba r a s d e n o t e di n t h e t e x t

    21

    22 b = [1 0 0]

    23 x = H \ b

    24

    25 // Rounding o f f t o 3 d e c i m a l p l a c e s26 x = x *1 0^ 3;

    27 x = int ( x ) ;

    28 x = x * 10 ^( - 3) ;

    29 disp ( x ) // F i n a l S o l u t i o n30

    31 / /Now , u s i ng e l i m i n a t i o n w it h P a r t i a l P i vo t in g , weg e t t he f o l l o w i n g a ns we rs

    32

    33 x 0 = [ 8. 96 8 - 35 .7 7 2 9. 77 ]

    34

    35 // r o i s R e s i d u a l c o r r e c t i o n36

    37 r0 = b - A * x0

    38

    39 // A e 0 = r 040

    41 e0 = inv ( A ) * r 0

    42

    43 x1 = x0 + e0

    71

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    73/99

    44

    45 // R e p e a t i n g t h e above o p e r a t i o n s , we cang e t t he v a l u e s o f r1 , x2 , e1 . . .46 // The v e c t o r x2 i s a c c u r a t e t o 4 d e c i m a l

    d i g i t s .47 // Note t h a t x1 x0 = e0 i s an a c c u r a t e

    p r e d i c t o r o f t h e e r r o r e 0 i n x0 .

    Scilab code Exa 8.9 Residual correction method

    1 //EXAMPLE (PG 5 4 4 )2

    3 //A( e ) = A0 + eB4

    5 A 0 = [ 2 1 0 ; 1 2 1 ; 0 1 2 ]

    6 B = [0 1 1; -1 0 1; -1 -1 0]

    7 // in v (A( e ) ) = C = in v (A0)8 C = inv ( A 0 )

    9 b = [0 1 2]

    10 x = A 0 \ b

    11 r = b - A 0 * x

    Scilab code Exa 8.10 Gauss Jacobi method

    1 // EXAMPLE ( PG 547 )2

    3 // Gauss J ac ob i Method4

    5 A = [10 3 1;2 -10 3;1 3 10] //C o e f f i c i e n t Ma tr ix

    6 b = [14 -5 14] // R i g h th an d m a t r i x

    7

    72

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    74/99

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    75/99

    3 / / Ga us s S e i d e l Method

    45 exec g a u s s s e i d e l . s c e

    6 A = [10 3 1;2 -10 3;1 3 10] // C o e f f i c i e n tm a t r i x

    7 b = [14 -5 14] // R i g h t handm a t r i x

    8 x 0 = [0 0 0] // I n i t i a l Gauss9 g a u s s s e i d e l ( A , b , x 0 ) // C a l l i n g

    f u n c t i o n10

    11 // End t h e pr o bl e m

    Scilab code Exa 8.13 Conjugate gradient method

    1 // EXAMPLE ( PG 568 )2

    3 A = [5 4 3 2 1;4 5 4 3 2;3 4 5 4 3;2 3 4 5 4;1 2 3 4

    5] // Ma t r i x o f o r d e r 54 // G e t t i n g t h e e i g e n v a l u e s

    56 l am = spec ( A ) // lamda = s p e c t r a l

    r a d i u s o f m at ri x A7

    8 max ( l a m ) // L a r g e s t e i g en va l u e9 min ( l a m ) // S m a l l e s t e i g e n

    v a l u e10

    11 // For t h e e r r o r bound g i v e n e a r l i e r onPg 5 6 7

    1213 c = min ( l a m ) /max ( l a m )

    14

    15 (1 - sqrt ( c ) ) / ( 1 + sqrt ( c ) )

    16

    74

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    76/99

    17 // For l i n e a r system , c h o os e t h e f o l l o w i n g

    v a l ue s o f b1819 b = [ 7. 9 38 0 1 2 .9 76 3 1 7 .3 0 57 1 9. 4 33 2 1 8. 41 96 ]

    20

    21 x = A \ b ; // S o l u t i o n m a t r i x22

    23 // Rounding o f f t o 4 d e c i m a l p l a c e s24 x = x *1 0^ 4;

    25 x = int ( x ) ;

    26 x = x * 10 ^( - 4)

    27 disp ( x ) // F i n a l S o l u t i o n

    75

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    77/99

    Chapter 9

    The Matrix Eigenvalue

    Problem

    Scilab code Exa 9.1 Eigenvalues

    1 // EXAMPLE 5902

    3 A = [4 1 0;1 0 -1;1 1 -4]

    4 [ n , m] = size ( A ) ;

    5

    6 if m < > n then

    7 error ( e i g e n v e c t o r s ma tr ix A i s n ot s q ua r e ) ;8 abort ;

    9 end ;

    10

    11 l am = spec ( A ) / / E i g e n v a l u e s o f m a t r i x A

    Scilab code Exa 9.2 Eigen values and matrix norm

    1 / / PG 5 9 1

    76

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    78/99

    2

    3 n = 44 A = [4 1 0 0;1 4 1 0;0 1 4 1;0 0 1 4]

    5 l am = spec ( A )

    6

    7 // S in ce A i s sy mme tr i c , a l l e i ge n v a l u es a r er e a l .

    8 // The r a d i i a r e a l l 1 o r 2 .9 // The c e n t e r s o f a l l t h e c i r c l e s a re 4 .

    10 // A l l e i g e n v a lu e s m ust a l l l i e i n t h e i n t e r v a l[ 2 , 6 ]

    11 // S in ce t h e e ig e n v a l u es o f i nv (A) a r e t h e

    r e c i p r o c a l s o f t ho se o f A,12 // 1/6

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    79/99

    6 A = zeros ( n , n ) ; // a s y m me t r i c p o s i t i v e d e f i n i t e

    r e a l o r c om pl ex m a tr i x .7 for i = 1 : n / / I n i t i a l i z i n g f o r l o o p8 for j = 1 : n

    9 A ( i , j ) = 1 / ( i + j - 1 ) ;

    10 end

    11 end / /End o f f o r l o op12 A

    13

    14 [ n , m] = size ( A )

    15

    16 if m < > n then

    17 error ( e i g e n v e c t o r s ma tr ix A i s n ot s q ua r e ) ;18 abort ;

    19 end ;

    20

    21 l am = spec ( A ) / / E i g e n v a l u e s o f m a t r i x A

    22

    23 l a m1 = l am ( 1 , 1)

    24 l a m2 = l am ( 1 , 2)

    25 l a m3 = l am ( 1 , 3)

    26

    27 // Rounding o f f t o 4 d e c i m a l p l a c e s28

    29 A = A *1 0^ 4;

    30 A = int ( A ) ;

    31 A = A * 10 ^( - 4) ;

    32 disp ( A ) // F i n a l S o l u t i o n33

    34 l a mr = spec ( A )

    35

    36 l am r1 = l am r ( 1 ,1 )

    37 l am r2 = l am r ( 1 ,2 )38 l am r3 = l am r ( 1 ,3 )

    39

    40 // E r r o r s41

    78

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    80/99

    42 lam - l am r

    4344 // R e l a t i v e E r r o r s45

    46 R 1 = ( l am 1 - l a mr 1 ) / la m1

    47 R 2 = ( l am 2 - l a mr 2 ) / la m2

    48 R 3 = ( l am 3 - l a mr 3 ) / la m3

    Scilab code Exa 9.4 Eigenvalues of nonsymmetric matrix

    1 // PG 5942

    3 A = [ 10 1 - 90 ;1 10 - 98]

    4 [ n , m] = size ( A )

    5

    6 if m < > n then

    7 error ( e i g e n v e c t o r s ma tr ix A i s n ot s q ua r e ) ;8 abort ;

    9 end ;

    10

    11 l am = spec ( A ) / / E i g e n v a l u e s o f m a t r i x A

    12

    13

    14 // A+E = [101 e 90e ; 1 1 0 98]15 // Le t e = 0 . 0 0 116 e = 0 .0 01 ;

    17 // Le t A+E = D18 D = [1 01 - e -90 - e ;1 10 -9 8]

    19

    20 [ n , m] = size ( D )21

    22 if m < > n then

    23 error ( e i g e n v e c t o r s ma tr ix D i s n ot s q ua r e ) ;24 abort ;

    79

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    81/99

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    82/99

    Scilab code Exa 9.7 Rate of convergence

    1 // (PG 6 0 7 )2

    3 A = [1 2 3;2 3 4;3 4 5]

    4 l am = spec ( A )

    5 l a m1 = l am ( 1 , 3)

    6 l a m2 = l am ( 1 , 1)

    7 l a m3 = l am ( 1 , 2)8

    9 // T h e o r e t i c a l r a t i o o f c o nv e r g e n c e10

    11 l a m 2 / l a m 1

    12

    13 b = 0 .5 *( l a m2 + l a m3 )

    14 B = A - b* eye ( 3 , 3 )

    15

    16 // E i g e n v a l u e s o f Ab I a r e :17

    18 l a mb = spec ( B )

    19 l am b1 = l am b ( 1 ,3 )

    20 l am b2 = l am b ( 1 ,2 )

    21 l am b3 = l am b ( 1 ,1 )

    22

    23 // R at io o f c on ve rg en ce f o r t he power methoda p pl i ed t o Ab I w i l l be :

    24

    25 l a m b 2 / l a m b 1

    26

    27 // T hi s i s l e s s than h a l f t h e magni t ude o f t h e o r i g i n a l r a t i o .

    81

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    83/99

    Scilab code Exa 9.8 Rate of convergence after extrapolation

    1 // PG ( 6 0 8 )2

    3 A = [1 2 3;2 3 4;3 4 5]

    4 l am = spec ( A ) // E i g e n v a l u e s o f A5 l a m1 = l am ( 1 , 3)

    6 l a m2 = l am ( 1 , 1)

    7 l a m3 = l am ( 1 , 2)

    8

    9 // T h e o r e t i c a l r a t i o of c o n v e r g e n c e10

    11 l a m 2 / l a m 112

    13 // A ft e r e x t r ap ol a t i n g , we g e t14 l am e1 = 9 . 62 34 8 14

    15

    16 // E r r o r :17 l a m 1 - l a m e 1

    Scilab code Exa 9.9 Householder matrix

    1 // PG ( 6 1 0 )2

    3 w = [ 1/ 3 2/3 2/3]

    4 w1 = w (1 ,1)

    5 w2 = w (2 ,1)

    6 w3 = w (3 ,1)

    7

    8 U = [1 -2* abs ( w 1 ) ^ 2 - 2* w 1 * w2 - 2* w 1 * w3 ; - 2* w 1 * w 2

    1-2* abs ( w 2 ) ^ 2 - 2* w 2 * w3 ; - 2* w 1 * w 3 - 2* w 2 * w 3 1 - 2 *abs ( w 3 ) ^ 2 ]

    9 U

    10 inv ( U )

    11 // U = i n v (U)Hen ce , U i s H e r mi t i a n

    82

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    84/99

    12 U * U

    13 // UU = IHence , U i s o r t h o g o n a l

    Scilab code Exa 9.11 QR factorisation

    1 // PG ( 6 1 3 )2

    3 A = [4 1 1;1 4 1;1 1 4]

    4 w 1 = [ 0 .9 8 55 9 9 0 .1 1 95 73 0 .1 19 57 3]

    5 P1 = eye () - 2* w 1* w1 6 A 2 = P1 *A

    7 w 2 = [ 0 0 . 99 6 39 3 0 .0 84 85 72 ]

    8 P2 = eye () - 2* w 2* w2

    9 R = P2 *A2

    10 Q = P1 *P2

    11 Q * R

    12

    13 // A = Q R14

    15 abs ( det ( A ) )

    16 abs ( det ( Q ) * det ( R ) )17

    18 // |de t (A)| = |d et (Q) de t (R)| = |de t (R)| = 54 (appro x )

    19

    20 l am = spec ( A )

    21 l a m1 = l am ( 1 , 1)

    22 l a m2 = l am ( 1 , 2)

    23 l a m3 = l am ( 1 , 3)

    24 la m1 * l am2 * l am3

    2526 // P r o d uc t o f e i ge n v a l ue s a l s o comes ou t t o be54

    83

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    85/99

    Scilab code Exa 9.12 Tridiagonal Matrix

    1 // PG ( 6 1 7 )2

    3 A = [1 3 4;3 1 2;4 2 1]

    4 w 2 = [0 2/ sqrt ( 5) 1/ sqrt ( 5 ) ]

    5 P1 = eye () - 2* w 2* w2

    6 T = P1 * A * P1 // T r i di ag on a l m a t r i x

    Scilab code Exa 9.13 Planner Rotation Orthogonal Matrix

    1 // PG ( 6 1 9 )2

    3 x = %pi /4

    4 R = [ cos ( x ) 0 sin ( x) ;0 1 0; - sin ( x ) 0 cos ( x ) ]

    5

    6 // P la nn er Ro ta ti on Or th og on al Mat r ix

    Scilab code Exa 9.14 Eigen values of a symmetric tridiagonal Matrix

    1 // PG ( 6 2 0 )2

    3 T = [2 1 0 0 0 0;1 2 1 0 0 0;0 1 2 1 0 0;0 0 1 2 1

    0;0 0 0 1 2 1;0 0 0 0 1 2]

    4 l am = spec ( T ) 5 l a m1 = l am ( 1 , 1)

    6 B = [2 - lam1 1 0 0 0 0;1 2 - lam1 1 0 0 0;0 1 2 -lam1 1

    0 0;0 0 1 2 - lam1 1 0;0 0 0 1 2 - lam1 1;0 0 0 0 1

    2]

    84

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    86/99

    7 f0 = abs ( det ( B ) )

    8 f1 = 2 - la m1

    Scilab code Exa 9.15 Sturm Sequence property

    1 // PG ( 6 2 1 )2

    3 // For t he p r e v i ou s e xample , c o n s i d e r t h es e q u e n c e f 0 , f 1 . . . . f 6

    45 // For lam = 3 ,6

    7 // ( f 0 , . . . . . f 6 ) = ( 1 , 1 ,0 ,1 , 1 ,0 ,1 )8

    9 // The co rr es po nd in g s e q u e n c e o f s i g n s i s10

    11 // (+ , ,+,+, ,+,+)12

    13 // and s ( 3 ) = 2

    Scilab code Exa 9.16 QR Method

    1 // PG ( 6 2 4 )2

    3 A1 = [2 1 0;1 3 1;0 1 4]

    4 l am = spec ( A 1 )

    5 [ Q1 , R 1 ] = qr ( A 1 ) ;

    6 A2 = R1 * Q1

    7 [ Q2 , R 2 ] = qr ( A 2 ) ;8 A3 = R2 * Q2

    9 [ Q3 , R 3 ] = qr ( A 3 ) ;

    10 A4 = R3 * Q3

    11 [ Q4 , R 4 ] = qr ( A 4 ) ;

    85

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    87/99

    12 A5 = R4 * Q4

    13 [ Q5 , R 5 ] = qr ( A 5 ) ;14 A6 = R5 * Q5

    15 [ Q6 , R 6 ] = qr ( A 6 ) ;

    16 A7 = R6 * Q6

    17 [ Q7 , R 7 ] = qr ( A 7 ) ;

    18 A8 = R7 * Q7

    19 [ Q8 , R 8 ] = qr ( A 8 ) ;

    20 A9 = R8 * Q8

    21 [ Q9 , R 9 ] = qr ( A 9 ) ;

    22 A10 = R9 * Q9

    23 [ Q 10 , R 1 0 ] = qr ( A 1 0 ) ;

    Scilab code Exa 9.18 Calculation of Eigen vectors and Inverse iteration

    1 // PG ( 6 3 1 )2

    3 A = [2 1 0;1 3 1;0 1 4]

    4 l am = spec ( A )

    5 [ L , U] = lu ( A )

    6 y 1 = [1 1 1] 7 w 1 = [ 3 38 5 .2 - 24 77 .3 9 08 .2 0]

    8 z 1 = [ w1 / norm ( w 1 , i n f ) ] 9 w 2 = [ 20 3 45 - 14 89 4 5 45 1. 9]

    10 z 2 = [ w2 / norm ( w 2 , i n f ) ] 11 z 3 = z2

    12

    13 // The t r u e a ns w e r i s14

    15 x 3 = [1 1 - sqrt ( 3) 2 - sqrt ( 3 ) ]

    1617 // z2 e q u al s x3 to w i t h i n t he l i m i t s o f ro u n d i n ge r r o r a c c u mu l a t i o ns .

    86

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    88/99

    Scilab code Exa 9.19 Inverse Iteration

    1 // PG ( 6 3 3 )2

    3 A = [2 1 0;1 3 1;0 1 4]

    4 l am = spec ( A )

    5 [ L , U] = lu ( A )

    6 y 1 = [1 1 1]

    7 w 1 = [ 3 38 5 .2 - 24 77 .3 9 08 .2 0]

    8 z 1 = [ w1 / norm ( w 1 , i n f ) ] 9 w 2 = [ 20 3 45 - 14 89 4 5 45 1. 9]

    10 z 2 = [ w2 / norm ( w 2 , i n f ) ] 11 z 3 = z2

    12

    13 // The t r u e a ns w e r i s14

    15 x 3 = [1 1 - sqrt ( 3) 2 - sqrt ( 3 ) ]

    16

    17 // z2 e q u al s x3 to w i t h i n t he l i m i t s o f ro u n d i n g

    e r r o r a c c u mu l a t i o ns .18

    19 // C o n s i d e r lam = 1 .2 67 920

    21 // 0 . 7 3 2 1 x1 + x2 = 022 // x1 + 1 . 7 3 2 1 x2 + x3 = 023 // Taki ng x 1= 1 . 0 , we have t he a pp ro xi ma te

    e i g e n v e c t o r24

    25 // x = [ 1 . 0 0 0 0 0 .7 32 10 0 . 2 6 8 0 7 ]26

    27

    28 // Compared w it h t h e t r u e a ns we r o b t a in e d a bove ,t h i s i s a s l i g h t l y p o o r e r

    29 // r e s u l t o b t a i n e d by i n v e r s e i t e r a t i o n .

    87

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    89/99

    Appendix

    Scilab code AP 1 Gauss seidel method

    1 function [ x ] = g a u s s s e i d e l ( A , b , x 0 )

    2 [ n A , m A ] = size ( A )

    3 n = n A

    4 [ L , U] = lu ( A )

    5 d = diag ( A )

    6 a 1 1 = d (1 , 1)

    7 a 2 2 = d (2 , 1)

    8 a 3 3 = d (3 , 1)

    9 D = [ a11 0 0;0 a22 0;0 0 a33 ]

    10 H = - inv ( L + D ) * U

    11 C = inv ( L + D ) * b

    12 for m = 0 : 313 x = - inv ( D ) *( L + U )* x + inv ( D ) * b

    14 m = m + 1

    15 disp ( x )

    16 end

    17

    18 e n d f u n c t i o n

    Scilab code AP 2 Euler method

    1 function [x , y ] = E u le r 1 ( x0 , y 0 , x n , h , g )

    2

    3 / / E u l e r 1 s t o r d e r meth od s o l v i n g ODE4 / / dy / dx = g ( x , y ) , w i t h i n i t i a l5 // c o n d i t i o n s y=y0 a t x = x0 . The

    88

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    90/99

    6 / / s o l u t i o n i s o b t a i n ed f o r x = [ x0 : h : xn ]

    7 / /and r e tu r n ed i n y89 y m ax A ll o we d = 1 e + 10 0

    10

    11 x = [ x0 : h: xn ] ;

    12 y = zeros ( x ) ;

    13 n = length ( y ) ;

    14 y ( 1) = y0 ;

    15

    16 for j = 1: n-1

    17 y (j + 1) = y ( j) + h * g( x (j ) ,y (j ) );

    18 if y ( j + 1) > y m ax A ll o we d then19 disp ( E u l e r 1 WARNING: un de rf lo w or

    o v e r f l o w ) ;20 disp ( S o l u t i o n s ou gh t i n t he f o l l o w i n g r an ge :

    ) ;21 disp ([ x 0 h x n ]) ;

    22 disp ( S o l u t i o n e v al u at e d i n t he f o l l o w i n gr an ge : ) ;

    23 disp ([ x 0 h x ( j) ]) ;

    24 n = j ;

    25 x = x (1 ,1: n ); y = y (1 ,1: n) ;

    26 break ;

    27 end ;

    28 end ;

    29

    30 e n d f u n c t i o n

    31

    32 / /End f u n c t i o n E u l er 1

    Scilab code AP 3 Eigen vectors

    1 function [ x , l am ] = e i ge n ve c to r s ( A)2

    3 / / C a l c u l a t e s u n i t e i g e n v e c t o r s o f m at ri x A4 / / r e t u r n i n g a m a tr i x x wh os e c ol um ns a r e5 / / t he e i g e n v e c t o r s . The f u n c t i o n a l s o

    89

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    91/99

    6 // r e t u rn s t he e i g e n v al u e s o f t he m at ri x .

    78 [ n , m] = size ( A ) ;

    9

    10 if m < > n then

    11 error ( e i g e n v e c t o r s ma tr ix A i s n ot s q ua r e ) ;12 abort ;

    13 end ;

    14

    15 l am = spec ( A ) ; // E i g e n v a l u e s o f m a t r i x A

    16

    17 x = [];18

    19 for k = 1: n

    20 B = A - lam (k )* eye ( n , n ) ; // C h a r a c t e r i s t i c m at ri x21 C = B ( 1: n - 1 , 1: n - 1) ; // C o e f f . m a tr i x f o r

    r e d u ce d s y st em22 b = - B (1 :n -1 , n ); / /RHS v e c t o r f o r

    r e d u ce d s y st e m23 y = C \b ; // S o l u t i o n f o r r ed uc ed s ys te m24 y = [ y ;1 ]; / / Co mp le te e i g e n v e c t o r25 y = y / norm ( y ) ;

    / /Make u n i t e i g e n v e c t o r26 x = [ x y ]; / /Add e i g e n v e c t o r t o m a tr i x27 end ;

    28

    29 e n d f u n c t i o n

    30 / /End o f f u n c t i o n

    Scilab code AP 4 Boundary value problem

    1 function [ x , y , la m ] = B V Pe i ge n 1 (L , n )

    2

    3 D x = L /( n -1 ) ;4 x = [ 0 : D x : L ] ;

    5 a = 1/ Dx ^ 2;

    6 k = n -2;

    7

    90

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    92/99

    8 A = zeros ( k , k ) ;

    9 for j = 1: k10 A (j , j) = 2* a ;

    11 end ;

    12 for j = 1: k-1

    13 A (j , j +1 ) = - a;

    14 A (j +1 , j ) = - a;

    15 end ;

    16

    17 exec e i g e n v e c t o r s . s c e

    18

    19 [ y y , l a m ] = e i g e n v e c t o r s ( A ) ;

    20 // di sp ( y y ) ; di sp ( y y ) ;21

    22 y = [ zeros ( 1 , k ) ; y y ; zeros ( 1 , k ) ] ;

    23 // di sp ( y ) ; di sp ( y ) ;24

    25

    26 x m i n =min ( x ) ; x m a x =max ( x ) ; y m i n =min ( y ) ; y m a x =max ( y ) ;

    27 r ec t = [ x mi n y mi n x ma x y ma x ];

    28

    29 if k > = 5 then

    30 m = 5;

    31 else

    32 m = k ;

    33 end

    34

    35

    36 e n d f u n c t i o n

    Scilab code AP 5 Trapezoidal method

    1 function [ x , y ] = t r a pe z o i da l ( x 0 , y 0 , xn , h , g )

    23 / / T r a p e z o i d a l m et ho d s o l v i n g ODE4 / / dy / dx = g ( x , y ) , w i t h i n i t i a l5 // c o n d i t i o n s y=y0 a t x = x0 . The6 / / s o l u t i o n i s o b t a i n ed f o r x = [ x0 : h : xn ]

    91

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    93/99

    7 / /and r e tu r n ed i n y

    89 y m ax A ll o we d = 1 e + 10 0

    10

    11 x = [ x0 : h: xn ] ;

    12 y = zeros ( x ) ;

    13 n = length ( y ) ;

    14 y ( 1) = y0 ;

    15

    16 for j = 1: n-1

    17 y ( j +1 ) = y ( j ) + h * ( g( x ( j) , y (j ) ) +g ( x (j + 1) , y ( j +1 ) )

    ) / 2 ;

    18 if y ( j + 1) > y m ax A ll o we d then19 disp ( E u l e r 1 WARNING: un de rf lo w or

    o v e r f l o w ) ;20 disp ( S o l u t i o n s ou gh t i n t he f o l l o w i n g r an ge :

    ) ;21 disp ([ x 0 h x n ]) ;

    22 disp ( S o l u t i o n e v al u at e d i n t he f o l l o w i n gr an ge : ) ;

    23 disp ([ x 0 h x ( j) ]) ;

    24 n = j ;

    25 x = x (1 ,1: n ); y = y (1 ,1: n) ;

    26 break ;

    27 end ;

    28 end ;

    29

    30 e n d f u n c t i o n

    31

    32 // End f u n c t i o n t r a p e z o i d a l

    Scilab code AP 6 Legendre Polynomial

    12 function [ p L ] = l e ge n dr e po l ( n , va r )

    3

    4 // G en er at es t h e L e g e n dr e p o ly no mi al5 // o f o r d e r n i n v a r i a b l e v a r

    92

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    94/99

    6

    7 if n == 0 then8 cc = [ 1] ;

    9 elseif n == 1 then

    10 cc = [0 1];

    11 else

    12 i f m od ul o ( n ,2) == 0 then

    13 M = n /2

    14 else

    15 M = ( n -1) /2

    16 end ;

    17

    18 cc = zeros ( 1 , M + 1 ) ;19 for m = 0: M

    20 k = n -2* m;

    21 c c ( k + 1 ) = . . .

    22 ( - 1 ) ^ m * gamma ( 2 * n - 2 * m + 1 ) / ( 2 ^ n * gamma ( m + 1 ) *

    gamma ( n - m + 1 ) * gamma ( n - 2 * m + 1 ) ) ;

    23 end ;

    24 end ;

    25

    26 pL = poly ( c c , v a r , c o e f f ) ;27

    28 // End f u n c t i o n l e g e n d r e p o l

    Scilab code AP 7 Romberg Integration

    1 function [ I ] = R o m b e r g ( a , b , f , h )

    2

    3 // T h i s f u n c t i o n c a l c u l a t e s t he n u m e r i ca li n t e g r a l o f f ( x ) b et w ee n

    4 / / x = a and x = b , w i t h i n t e r v a l s h .I n t e r me d i a t e r e s u l t s a r e o b ta i n ed

    5 // by u s i ng SCILAB s own i n t t r a p f u n c ti o n6

    7 x = ( a : h : b )

    8 x 1 = x ( 1 , 1 )

    9 x 2 = x ( 1 , 2 )

    93

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    95/99

    10 x 3 = x ( 1 , 3 )

    11 x 4 = x ( 1 , 4 )12 y 1 = f ( x 1 )

    13 y 2 = f ( x 2 )

    14 y 3 = f ( x 3 )

    15 y 4 = f ( x 4 )

    16 y = [ y1 y2 y3 y4 ]

    17 I1 = inttrap ( x , y )

    18 x = ( a : h / 2 : b )

    19 x 1 = x ( 1 , 1 )

    20 x 2 = x ( 1 , 2 )

    21 x 3 = x ( 1 , 3 )

    22 x 4 = x ( 1 , 4 )23 x 5 = x ( 1 , 5 )

    24 x 6 = x ( 1 , 6 )

    25 x 7 = x ( 1 , 7 )

    26 y 1 = f ( x 1 )

    27 y 2 = f ( x 2 )

    28 y 3 = f ( x 3 )

    29 y 4 = f ( x 4 )

    30 y 5 = f ( x 5 )

    31 y 6 = f ( x 6 )

    32 y 7 = f ( x 7 )

    33 y =[ y1 y2 y3 y4 y5 y6 y7 ]

    34 I2 = inttrap ( x , y )

    35 I = I2 + ( 1. 0/ 3. 0) * ( I2 - I 1)

    36

    37 e n d f u n c t i o n

    38 / / e nd f u n c t i o n Romberg

    Scilab code AP 8 Lagrange

    1 function [ P ] = l a g r a n g e ( X , Y )

    23 // X nodes , Y v al ue s4 // P i s t h e n um er ic al L a g r a n g e p o ly n om i al

    i n t e r p o l a t i o n5 n = length ( X )

    94

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    96/99

    6 // n i s t h e number o f n o d e s . ( n1) i s t h e

    d e g r e e7 x = poly (0 , x )8 P =0

    9 for i = 1: n , L = 1

    10 for j = [ 1: i - 1 , i + 1 : n ] L = L * ( x - X (j ) ) / ( X ( i ) -X ( j ) )

    11 end

    12 P = P + L * Y ( i )

    13 end

    14 e n d f u n c t i o n

    Scilab code AP 9 Muller method

    1 function x = m u l l e r ( x 0 , x 1 , x 2 , f )

    2 R = 3 ;

    3 P E = 1 0 ^ - 8 ;

    4 m a x v a l = 1 0 ^ 4 ;

    5 for n = 1 : 1 : R

    6

    7 L a = ( x 2 - x 1 ) / ( x 1 - x 0 ) ;

    8 D a = 1 + L a ;

    9 g a = L a ^ 2 * f ( x 0 ) - D a ^ 2 * f ( x 1 ) + ( L a + D a ) * f ( x 2 ) ;

    10 C a = L a * ( L a * f ( x 0 ) - D a * f ( x 1 ) + f ( x 2 ) ) ;

    11

    12 q = g a ^ 2 - 4 * D a * C a * f ( x 2 ) ;

    13 if q m a x v a l ) t he n e r ro r ( S o l u t i o n

    d i v e r g e s ) ;23 abort ;

    24 break

    95

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    97/99

    25 else

    26 x 0 = x 1 ;27 x 1 = x 2 ;

    28 x 2 = x ;

    29 end

    30 end

    31 disp (n , no . o f i t e r a t i o n s = )32 e n d f u n c t i o n

    Scilab code AP 10 Secant method

    1 function [ x ] = s e c a n t ( a , b , f )

    2 N = 1 0 0 ; // d e f i n e max . no . i t e r a t i o n st o be p e rf o rm ed

    3 P E = 1 0 ^ - 4 // d e f i n e t o l e r a n c e f o rc o n v e r g e n c e

    4 for n = 1 : 1 : N // i n i t i a t i n g f o r l o o p5 x = a - ( a - b ) * f ( a ) / ( f ( a ) - f ( b ) ) ;

    6 if a bs ( f ( x ) ) < = P E t he n b r ea k ; // c h e c ki n g f o rt he r e q ui r e d c o n d i t i o n

    7 else a = b ;

    8 b = x ;

    9 end

    10 end

    11 disp (n , no . o f i t e r a t i o n s = ) //12 e n d f u n c t i o n

    Scilab code AP 11 Newton

    1 function x = n e w t o n ( x , f , f p )

    2 R = 1 0 0 ;

    3 P E = 1 0 ^ - 8 ;

    4 m a x v a l = 1 0 ^ 4 ;

    56 for n = 1 : 1 : R

    7 x = x - f ( x ) / f p ( x ) ;

    8 if a bs ( f ( x ) ) < = P E t he n b r ea k

    9 end

    96

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    98/99

    10 if ( abs ( f ( x ) ) > m a x v a l ) t he n e r ro r ( S o l u t i o n

    d i v e r g e s ) ;11 abort12 break

    13 end

    14 end

    15 disp (n , no . o f i t e r a t i o n s = )16 e n d f u n c t i o n

    Scilab code AP 12 Aitken1

    1 // t h i s program i s e x c l u s i v e l y c o de d t o p er fo rm one

    i t e r a t i o n o f a i t ke n method ,2

    3 function x 0 a a = a i t k e n ( x 0 , x 1 , x 2 , g )

    4 x 0 a = x 0 - ( x 1 - x 0 ) ^ 2 / ( x 2 - 2 * x 1 + x 0 ) ;

    5 x 1 a = g ( x 0 a ) ;

    6 x 2 a = g ( x 1 a ) ;

    7 x 0 a a = x 0 a - ( x 1 a - x 0 a ) ^ 2 / ( x 2 a - 2 * x 1 a + x 0 a ) ;

    8

    9 e n d f u n c t i o n

    Scilab code AP 13 Bisection method

    1 function x = b i s e c t i o n ( a , b , f )

    2 N = 1 0 0 ; //d e f i n e max . number o f i t e r a t i o n s

    3 P E = 1 0 ^ - 4 //d e f i n e t o l e r a n c e

    4 if ( f (a ) *f ( b) > 0 ) then

    5 error ( no r oo t p o s s i b l e f ( a ) f ( b ) > 0 )// c he ck i n g i f t he d e c id e d r an ge i sc o n ta i n i ng a r o ot

    6 abort ;7 end ;

    8 if ( abs ( f ( a) ) < PE ) then

    9 error ( s o l u t i o n a t a ) //s e e i n g i f t h e r e i s an a pp ro xi ma te r o ot

    97

  • 7/25/2019 An Introduction to Numerical Analysis_K. E. Atkinson

    99/99

    a t a ,

    10 abort ;11 end ;

    12 if ( abs ( f( b) ) < PE ) then //s e e i n g i f t h er e i s an a pp ro xi m at e r o o t a t b ,

    13 error ( s o l u t i o n a t b )14 abort ;

    15 end ;

    16 x = ( a + b ) / 2

    17 for n = 1 : 1 : N //i n i t i a l i s i n g fo r lo op ,

    18 p = f ( a ) * f ( x )

    19 if p