Download - Numerical Methods for Scientific and Eng
-
7/26/2019 Numerical Methods for Scientific and Eng
1/152
Scilab Textbook Companion for
Numerical Methods For Scientific And
Engineering Computation
by M. K. Jain, S. R. K. Iyengar And R. K.Jain1
Created byM Vamsi KrishanB.Tech (pursuing)
Electronics Engineering
Visvesvaraya National Institute of TechnologyCollege TeacherMr. Nagarajun
Cross-Checked bySantosh Kumar, IITB
July 4, 2014
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/26/2019 Numerical Methods for Scientific and Eng
2/152
-
7/26/2019 Numerical Methods for Scientific and Eng
3/152
Scilab numbering policy used in this document and the relation to theabove book.
Exa Example (Solved example)
Eqn Equation (Particular equation of the above book)
AP Appendix to Example(Scilab Code that is an Appednix to a particularExample of the above book)
For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 meansa scilab code whose theory is explained in Section 2.3 of the book.
2
-
7/26/2019 Numerical Methods for Scientific and Eng
4/152
Contents
List of Scilab Codes 4
2 TRANSCENDENTAL AND POLINOMIAL EQUATIONS 9
3 SYSTEM OF LINEAR ALGEBRIC EQUATIONS AND
EIGENVALUE PROBLEMS 46
4 INTERPOLATION AND APPROXIMATION 59
5 DIFFERENTIATION AND INTEGRATION 77
6 ORDINARY DIFFERENTIAL EQUATIONS INNITIAL VALUE
PROBLEMS 91
7 ORDINARY DIFFERENTIAL EQUATIONS BOUNDARY
VALUE PROBLEM 102
3
-
7/26/2019 Numerical Methods for Scientific and Eng
5/152
-
7/26/2019 Numerical Methods for Scientific and Eng
6/152
Exa 3.9 solution using the inverse of the matrix . . . . . . . . 50
Exa 3.10 decomposition method . . . . . . . . . . . . . . . . . . 51Exa 3.11 inverse using LU decoposition . . . . . . . . . . . . . . 51Exa 3.12 solution by decomposition method . . . . . . . . . . . 52Exa 3.13 LU decomposition . . . . . . . . . . . . . . . . . . . . 53Exa 3.14 cholesky method . . . . . . . . . . . . . . . . . . . . . 54Exa 3.15 cholesky method . . . . . . . . . . . . . . . . . . . . . 55Exa 3.21 jacobi iteration method . . . . . . . . . . . . . . . . . 56Exa 3.22 solution by gauss siedal method. . . . . . . . . . . . . 57Exa 3.27 eigen vale and eigen vector . . . . . . . . . . . . . . . 57Exa 4.3 linear interpolation polinomial . . . . . . . . . . . . . 59Exa 4.4 linear interpolation polinomial . . . . . . . . . . . . . 60
Exa 4.6 legrange linear interpolation polinomial . . . . . . . . 61Exa 4.7 polynomial of degree two . . . . . . . . . . . . . . . . 61Exa 4.8 solution by quadratic interpolation . . . . . . . . . . . 62Exa 4.9 polinomial of degree two. . . . . . . . . . . . . . . . . 63Exa 4.15 forward and backward difference polynomial . . . . . . 63Exa 4.20 hermite interpolation . . . . . . . . . . . . . . . . . . 64Exa 4.21 piecewise linear interpolating polinomial . . . . . . . . 65Exa 4.22 piecewise quadratic interpolating polinomial . . . . . . 66Exa 4.23 piecewise cubical interpolating polinomial . . . . . . . 67Exa 4.31 linear approximation . . . . . . . . . . . . . . . . . . . 68
Exa 4.32 linear polinomial approximation . . . . . . . . . . . . 69Exa 4.34 least square straight fit . . . . . . . . . . . . . . . . . 70Exa 4.35 least square approximation . . . . . . . . . . . . . . . 71Exa 4.36 least square fit . . . . . . . . . . . . . . . . . . . . . . 71Exa 4.37 least square fit . . . . . . . . . . . . . . . . . . . . . . 72Exa 4.38 gram schmidt orthogonalisation . . . . . . . . . . . . . 73Exa 4.39 gram schmidt orthogonalisation . . . . . . . . . . . . . 74Exa 4.41 chebishev polinomial . . . . . . . . . . . . . . . . . . . 76Exa 5.1 linear interpolation. . . . . . . . . . . . . . . . . . . . 77Exa 5.2 quadratic interpolation . . . . . . . . . . . . . . . . . 78Exa 5.10 jacobian matrix of the given system . . . . . . . . . . 78
Exa 5.11 solution by trapizoidal and simpsons . . . . . . . . . . 79Exa 5.12 integral approximation by mid point and two point . . 79Exa 5.13 integral approximation by simpson three eight rule . . 80Exa 5.15 quadrature formula. . . . . . . . . . . . . . . . . . . . 81Exa 5.16 gauss legendary three point method . . . . . . . . . . 82
5
-
7/26/2019 Numerical Methods for Scientific and Eng
7/152
Exa 5.17 gauss legendary method . . . . . . . . . . . . . . . . . 82
Exa 5.18 integral approximation by gauss chebishev . . . . . . . 83Exa 5.20 integral approximation by gauss legurre method. . . . 84Exa 5.21 integral approximation by gauss legurre method. . . . 85Exa 5.22 integral approximation by gauss legurre method. . . . 85Exa 5.26 composite trapizoidal and composite simpson . . . . . 86Exa 5.27 integral approximation by gauss legurre method. . . . 87Exa 5.29 double integral using simpson rule . . . . . . . . . . . 88Exa 5.30 double integral using simpson rule . . . . . . . . . . . 89Exa 6.3 solution to the system of equations . . . . . . . . . . . 91Exa 6.4 solution ti the IVP . . . . . . . . . . . . . . . . . . . . 92Exa 6.9 euler method to solve the IVP . . . . . . . . . . . . . 93
Exa 6.12 solution ti IVP by back euler method . . . . . . . . . 93Exa 6.13 solution ti IVP by euler mid point method. . . . . . . 94Exa 6.15 solution ti IVP by taylor expansion. . . . . . . . . . . 94Exa 6.17 solution ti IVP by modified euler cauchy and heun . . 95Exa 6.18 solution ti IVP by fourth order range kutta method. . 96Exa 6.20 solution to the IVP systems . . . . . . . . . . . . . . . 96Exa 6.21 solution ti IVP by second order range kutta method . 97Exa 6.25 solution ti IVP by third order adamsbashfort meth . . 98Exa 6.27 solution ti IVP by third order adams moult method . 99Exa 6.32 solution by numerov method . . . . . . . . . . . . . . 100
Exa 7.1 solution to the BVP by shooting method. . . . . . . . 102Exa 7.3 solution to the BVP by shooting method. . . . . . . . 103Exa 7.4 solution to the BVP by shooting method. . . . . . . . 104Exa 7.5 solution to the BVP . . . . . . . . . . . . . . . . . . . 105Exa 7.6 solution to the BVP by finite differences . . . . . . . . 107Exa 7.11 solution to the BVP by finite differences . . . . . . . . 108AP 1 shooting method for solving BVP. . . . . . . . . . . . 110AP 2 euler method . . . . . . . . . . . . . . . . . . . . . . . 111AP 3 eigen vectors and eigen values. . . . . . . . . . . . . . 111AP 4 adams bashforth third order method . . . . . . . . . . 113AP 5 newton raphson method . . . . . . . . . . . . . . . . . 113
AP 6 euler cauchy solution to the simultanioys equations . . 114AP 7 simultaneous fourth order range kutta . . . . . . . . . 115AP 8 fourth order range kutta method . . . . . . . . . . . . 116AP 9 modified euler method . . . . . . . . . . . . . . . . . . 117AP 10 euler cauchy or heun . . . . . . . . . . . . . . . . . . . 118
6
-
7/26/2019 Numerical Methods for Scientific and Eng
8/152
AP 11 taylor series. . . . . . . . . . . . . . . . . . . . . . . . 118
AP 12 factorial . . . . . . . . . . . . . . . . . . . . . . . . . . 119AP 13 mid point nmethod. . . . . . . . . . . . . . . . . . . . 119AP 14 back euler method . . . . . . . . . . . . . . . . . . . . 120AP 15 composite trapizoidal rule . . . . . . . . . . . . . . . . 121AP 16 simpson rule . . . . . . . . . . . . . . . . . . . . . . . 122AP 17 simpson rule . . . . . . . . . . . . . . . . . . . . . . . 123AP 18 approximation to the integral by simpson method. . . 124AP 19 integration by trapizoidal method. . . . . . . . . . . . 124AP 20 jacobian of a given matrix . . . . . . . . . . . . . . . . 124AP 21 linear interpolating polinomial . . . . . . . . . . . . . 125AP 22 iterated interpolation . . . . . . . . . . . . . . . . . . 125
AP 23 newton divided differences interpolation order two . . 125AP 24 legrange fundamental polynomial . . . . . . . . . . . . 125AP 25 legrange interpolation . . . . . . . . . . . . . . . . . . 126AP 26 quadratic approximation. . . . . . . . . . . . . . . . . 127AP 27 straight line approximation . . . . . . . . . . . . . . . 127AP 28 aitken interpolation . . . . . . . . . . . . . . . . . . . 128AP 29 newton divided differences interpolation . . . . . . . . 128AP 30 hermite interpolation . . . . . . . . . . . . . . . . . . 128AP 31 newton backward differences polinomial . . . . . . . . 129AP 32 gauss jorden . . . . . . . . . . . . . . . . . . . . . . . 131
AP 33 gauss elimination with pivoting . . . . . . . . . . . . . 131AP 34 gauss elimination . . . . . . . . . . . . . . . . . . . . . 132AP 35 eigen vector and eigen value. . . . . . . . . . . . . . . 134AP 36 gauss siedel method . . . . . . . . . . . . . . . . . . . 135AP 37 jacobi iteration method . . . . . . . . . . . . . . . . . 136AP 38 back substitution . . . . . . . . . . . . . . . . . . . . . 136AP 39 cholesky method . . . . . . . . . . . . . . . . . . . . . 137AP 40 forward substitution . . . . . . . . . . . . . . . . . . . 137AP 41 L and U matrices. . . . . . . . . . . . . . . . . . . . . 138AP 42 newton raphson method . . . . . . . . . . . . . . . . . 138AP 43 four itterations of newton raphson method. . . . . . . 139
AP 44 secant method . . . . . . . . . . . . . . . . . . . . . . 139AP 45 regula falsi method. . . . . . . . . . . . . . . . . . . . 140AP 46 four iterations of regula falsi method . . . . . . . . . . 140AP 47 four iterations of secant method . . . . . . . . . . . . 140AP 48 five itterations by bisection method. . . . . . . . . . . 141
7
-
7/26/2019 Numerical Methods for Scientific and Eng
9/152
AP 49 bisection method . . . . . . . . . . . . . . . . . . . . . 142
AP 50 solution by newton method given in equation 2.63 . . 143AP 51 solution by secant method given in equation 2.64 . . . 144AP 52 solution by secant method given in equation 2.65 . . . 144AP 53 solution to the equation having multiple roots . . . . . 145AP 54 solution by two iterations of general iteration . . . . . 145AP 55 solution by aitken method . . . . . . . . . . . . . . . . 146AP 56 solution by general iteration. . . . . . . . . . . . . . . 146AP 57 solution by multipoint iteration given in equation 33 . 147AP 58 solution by multipoint iteration given in equation 31 . 148AP 59 solution by chebeshev method. . . . . . . . . . . . . . 148AP 60 solution by five iterations of muller method . . . . . . 149
AP 61 solution by three iterations of muller method . . . . . 150
8
-
7/26/2019 Numerical Methods for Scientific and Eng
10/152
Chapter 2
TRANSCENDENTAL AND
POLINOMIAL EQUATIONS
Scilab code Exa 2.1 intervals containing the roots of the equation
1 / / The e q u a t i on8x 3 12x
22x+3==0ha s t h re e r e a l
r o o t s .2 // t he gr a ph o f
t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,0);4 x = - 1 : . 0 1 : 2 . 5 ;
//
d e f i n i n g t h e r an ge o f x .5 deff ( [ y]= f (x ) , y=8x 312x22x+3 ) ;// d e f i n i n g t he c u n ct i o n
6 y = feval ( x , f ) ;
7
9
-
7/26/2019 Numerical Methods for Scientific and Eng
11/152
8 a = gca () ;
910 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 = 8x 3 12x 22x+3 )16
17 // from t he abov e p l o t we c an i n f r e t ha t t he
f u n c t i o n h as r o o t s b et we en18 / / t h e i n t e r v a l s ( 1 , 0 ) , ( 0 , 1 ) , ( 1 , 2 ) .
Scilab code Exa 2.2 interval containing the roots
1 / / The e q u a t i on
c o s ( x )x%ex==0 h as r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,1);4 x = 0 : . 0 1 : 2 ;
//
d e f i n i n g t h e r an ge o f x .5 deff ( [ y]= f (x ) , y= c os ( x )x%ex ) ;// d e f i n i n g t he c u n ct i o n .
6 y = feval ( x , f ) ;
7
10
-
7/26/2019 Numerical Methods for Scientific and Eng
12/152
8 a = gca () ;
910 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 t i t l e ( y = c os ( x )x%ex )15
16 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t b etw een
17 / / t he i n t e r v a l ( 0 , 1 )
check AppendixAP 49for dependency:
Vbisection.sce
check AppendixAP 48for dependency:
Vbisection5.sce
Scilab code Exa 2.3 solution to the eq by bisection method
1 // The e q ua t i on x35x+1==0ha s r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b e
o b s e r v e d h e r e .3 xset ( window ,2);4 x = - 2 : . 0 1 : 4 ;
//d e f i n i n g t h e r an ge o f x .
11
-
7/26/2019 Numerical Methods for Scientific and Eng
13/152
5 deff ( [ y]= f (x ) ,y=x35x+1 ) ; //
d e f i n i n g t he c u nc t io n .6 y = feval ( x , f ) ;7
8 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 h
14 t i t l e ( y = x35x+1 )15
16 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t s b et we en
17 // t he i n t e r v a l s ( 0 , 1 ) , ( 2 , 3 ) .18 // s i n c e we h ave be en a sk ed f o r t he s m a l l e s t
p o s i t i v e r oo t o f t h e e q ua ti on ,19 / / we a re i n t r e s t e d on t h e i n t e r v a l ( 0 , 1 )20 / / a = 0; b =1 ,21
22 // we c a l l a u se 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 te23 / / 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 .24
25 b i s e c t i o n ( 0 , 1 , f )
26
27 / / s i n c e i n t he ex ampl e 2 . 3 we h ave bee n a sk ed t op er f o rm 5 i t t e r a t i o n s
28 // t h e a p p r o x i m a t e r oo t a f t e r 5 i t e r a t i o n s can b eo b s e r v e d .
2930
31
32 b i s e c t i o n 5 ( 0 , 1 , f )
33
12
-
7/26/2019 Numerical Methods for Scientific and Eng
14/152
34
35 // h e n ce t h e a p p ro xi m a te r o o t a f t e r 5 i t e r a t i o n s i s0 . 2 03 12 5 w i t i n t he p e r m i s s i b l e e r r o r o f 104 ,
check AppendixAP 49for dependency:
Vbisection.sce
check AppendixAP 48for dependency:
Vbisection5.sce
Scilab code Exa 2.4 solution to the eq by bisection method
1 / / The e q u a t i onc o s ( x )x%ex==0 h as r e a lr o o t s .
2 // t he gr a ph o f
t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,3);4 x = 0 : . 0 1 : 2 ;
//d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) , y= c os ( x )x%ex ) ;// d e f i n i n g t he c u n ct i o n .
6 y = feval ( x , f ) ;
7
8 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
-
7/26/2019 Numerical Methods for Scientific and Eng
15/152
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 t i t l e ( y = c os ( x )x%ex )15
16 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t b etw een
17 / / t he i n t e r v a l ( 0 , 1 )18
19
20 / / a = 0; b =1 ,21
22 // we c a l l a u se rd 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
23 / / 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 .
24
25 b i s e c t i o n ( 0 , 1 , f )
26
27 / / s i n c e i n t he ex ampl e 2 . 4 we h ave bee n a sk ed t op e r f o r m 5 i t t e r a t i o n s ,
28
29 b i s e c t i o n 5 ( 0 , 1 , f )
30
31
32 // h e n ce t h e a p p ro xi m a te r o o t a f t e r 5 i t e r a t i o n s i s0 . 5 15 62 5 w i t i n t he p e r m i s s i b l e e r r o r o f 104 ,
check AppendixAP 46for dependency:
regulafalsi4.sce
check AppendixAP 47for dependency:
secant4.sce
Scilab code Exa 2.5 solution to the given equation
14
-
7/26/2019 Numerical Methods for Scientific and Eng
16/152
1 // The e q ua t i on x
3
5
x+1==0ha s r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,4);4 x = - 2 : . 0 1 : 4 ;
//d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) ,y=x35x+1 ) ; //d e f i n i n g t he c u nc t io n .
6 y = feval ( x , f ) ;
7
8 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 t i t l e ( y = x35x+1 )15
16 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t s b et we en
17 // t he i n t e r v a l s ( 0 , 1 ) , ( 2 , 3 ) .18 // s i n c e we have be en g iv en t h e i n t e r v a l t o be
c o n si d e r e d a s ( 0 , 1 )19 / / a = 0; b =1 ,20
2122 // S o l u ti o n by
s e c a n t m et ho d23
24
15
-
7/26/2019 Numerical Methods for Scientific and Eng
17/152
-
7/26/2019 Numerical Methods for Scientific and Eng
18/152
check AppendixAP 44for dependency:
Vsecant.sce
check AppendixAP 45for dependency:
regulafalsi.sce
Scilab code Exa 2.6 solution by secant and regula falsi
1 / / The e q u a t i o nc o s ( x )x%ex==0 ha s r e a l
r o o t s .2 // t he gr a ph o f
t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,3);4 x = 0 : . 0 1 : 2 ;
//d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) , y= c os ( x )x%ex ) ;// d e f i n i n g t he c u n ct i o n .
6 y = feval ( x , f ) ;
7
8 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 t i t l e ( y = c os ( x )x%ex )
17
-
7/26/2019 Numerical Methods for Scientific and Eng
19/152
15
16 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t b etw een17 / / t he i n t e r v a l ( 0 , 1 )18
19
20 / / a = 0; b =1 ,21
22
23 // S o l u ti o n bys e c a n t m et ho d
24
2526
27
28
29 / / s i n c e i n t h e ex ampl e 2 . 6 we h ave no s p e c i f i c a t i o no f t h e no . o f i t t e r a t i o n s ,
30 // we d e f i n e a f u n c t i o n s ec an t and e x e c u t e i t .31
32
33
34 s e c a n t ( 0 , 1 , f ) // we c a l l a u se r
d e f i n e df u n c ti o n s ec an t s o a s t o f i n d t he a pp ro xi ma te35 / / 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 .36
37
38
39 // h en ce t he a pp ro xi ma te r o o t o c cu r ed i n s e c a ntmethod w i t i n t he p e r m i s s i b l e e r r o r o f 105 i s ,
40
41
4243 // s o l u t i o n by r e g u l a r
f a l s i met hod44
45
18
-
7/26/2019 Numerical Methods for Scientific and Eng
20/152
46
4748
49 / / s i n c e i n t h e ex ampl e 2 . 6 we h ave no s p e c i f i c a t i o no f t h e no . o f i t t e r a t i o n s ,
50
51
52 r e g u l a f a l s i ( 0 , 1 , f ) // we c a l l a u se rd e f i n e d f u n c t i o n r e g u l a r f a l s i s o a s t o f i n d t h e
a p p r o x i m a t e53 / / 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 .
check AppendixAP 43for dependency:
Vnewton4.sce
Scilab code Exa 2.7 solution to the equation by newton raphson method
1 // The e q ua t i on x35x+1==0ha s r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,6);4 x = - 2 : . 0 1 : 4 ;
//d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) ,y=x35x+1 ) ; //d e f i n i n g t he f u n c t i o n .
6 deff ( [ y ] = f p ( x ) , y=3x25 ) ;
19
-
7/26/2019 Numerical Methods for Scientific and Eng
21/152
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 he gr ap h15 t i t l e ( y = x35x+1 )16
17 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t s b et we en
18 // t he i n t e r v a l s ( 0 , 1 ) , ( 2 , 3 ) .19 // s i n c e we h ave be en a sk ed f o r t he s m a l l e s t
p o s i t i v e r oo t o f t h e e q ua ti on ,20 / / we a re i n t r e s t e d on t h e i n t e r v a l ( 0 , 1 )21 / / a = 0; b =1 ,22
23 / / s i n c e i n t he ex ampl e 2 . 7 we h ave bee n a sk ed t op e r f o r m 4 i t t e r a t i o n s ,
24 // t h e a p p r o x i m a t e r oo t a f t e r 4 i t e r a t i o n s can b eo b s e r v e d .
25
26
27 n e w t o n 4 ( 0 . 5 , f , f p )
28
29
30 // h e n ce t h e a p p ro xi m a te r o o t a f t e r 4 i t e r a t i o n s i s0 . 2 01 64 0 w i t i n t he p e r m i s s i b l e e r r o r o f 10 15 ,
check AppendixAP 43for dependency:
Vnewton4.sce
20
-
7/26/2019 Numerical Methods for Scientific and Eng
22/152
Scilab code Exa 2.8 solution to the equation by newton raphson method
1 // The e q ua t i on x317==0 ha st h r e e r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,7);4 x = - 5 : . 0 0 1 : 5 ;
//d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) ,y=x317 ) ; //d e f i n i n g t he c u nc t io n .
6 deff ( [ y ] = f p ( x ) , y=3x2 ) ;7 y = feval ( x , f ) ;8
9 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 he gr ap h
15 t i t l e ( y = x3
17 )1617 / / 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 b etw een18 // t he i n t e r v a l ( 2 , 3 ) .
21
-
7/26/2019 Numerical Methods for Scientific and Eng
23/152
19
2021
22 / / s o l u t i o n by n ew to n r a ph s o n s m et ho d23
24
25
26 // s i n c e i n exa mp le no . 2 . 8 we h ave b ee n a sk ed t op e r f o r m 4 i t e r a t i o n s , we d e f i n e a f u c t i o nn ew to n4 w h ic h d o e s n ew to n r a p hs o n s m et ho d o f f i n d i n g a pp ro xi ma te r o ot upto 4 i t e r a t i o n s ,
27
2829
30 n e w t o n 4 ( 2 , f , f p ) // c a l l i n g t he pred e f i n e d f u n c t i o n n ewton4 .
check AppendixAP 42for dependency:
Vnewton.sce
Scilab code Exa 2.9 solution to the equation by newton raphson method
1 / / The e q u a t i onc o s ( x )x%ex==0 h as r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b e
o b s e r v e d h e r e .3 xset ( window ,8);4 x = - 1 : . 0 0 1 : 2 ;
//d e f i n i n g t h e r an ge o f x .
22
-
7/26/2019 Numerical Methods for Scientific and Eng
24/152
5 deff ( [ y]= f (x ) , y= c os ( x )x%ex ) ;
// d e f i n i n g t he c u n ct i o n .6 deff ( [ y ] = f p ( x ) , y=s i n ( x )x%ex%ex ) ;7 y = feval ( x , f ) ;
8
9 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 he gr ap h15 t i t l e ( y = c os ( x )x%ex )16
17 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t b etw een
18 / / t he i n t e r v a l ( 0 , 1 )19
20
21 / / a = 0; b =1 ,22
23
24
25 / / s o l u t i o n by n ew to n r a ph so n s m ethodw i t h a p e r m i s s i b l e e r r o r o f 10 8.
26
27
28 // we c a l l a u se rd 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
29 / / r o o t o f t h e e qu at i o n w it hi n t h e d e f i n e dp e r m i s s i b l e e r r o r l i m i t .
30
31 n e w t o n ( 1 , f , f p )32
33
34
35
23
-
7/26/2019 Numerical Methods for Scientific and Eng
25/152
36
37 / / h en ce t he a pp ro xi ma te r o ot w i t i n t he p e r m i s s i b l ee r r o r o f 1 08 i s 0 . 5 1 7 7 5 7 4 .
check AppendixAP 61for dependency:
muller3.sce
Scilab code Exa 2.11 solution to the given equation by muller method
1 // The e q ua t i on x35x+1==0 h asr e a l r o o t s .
2 // t h e g r aph o f t h i sf u n c t i o n can beo b s e r v e d h e r e .
3 xset ( window ,10);4 x = - 2 : . 0 1 : 4 ; //
d e f i n i n g t he r a ng e o f x .5 deff ( [ y]= f (x ) ,y=x35x+1 ) ; //
d e f i n i n g t he c u nc t io n .6 y = feval ( x , f ) ;7
8 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 g r ap h14 t i t l e ( y = x35x+1 )
1516 // from t he abov e p l o t we c an i n f r e t ha t t he
f u n c t i o n h as r o o t s b et we en17 // t he i n t e r v a l s ( 0 , 1 ) , ( 2 , 3 ) .
24
-
7/26/2019 Numerical Methods for Scientific and Eng
26/152
18 // s i n c e we h ave be en a sk ed f o r t he s m a l l e s t
p o s i t i v e r oo t o f t h e e q ua ti on ,19 / / we a re i n t r e s t e d on t h e i n t e r v a l ( 0 , 1 )20
21
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.
23
24 m u l l e r 3 ( 0 , . 5 , 1 , f )
check AppendixAP 60for dependency:
muller5.sce
Scilab code Exa 2.12 solution by five itrations of muller method
1 / / The e q u a t i onc o s ( x )x%ex
==0 h as r e a lr o o t s .2 // t he gr a ph o f
t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,8);4 x = - 1 : . 0 0 1 : 2 ;
//d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) , y= c os ( x )x%ex ) ;
// d e f i n i n g t he c u n ct i o n .6 deff ( [ y ] = f p ( x ) , y=s i n ( x )x%ex%ex ) ;7 y = feval ( x , f ) ;
8
9 a = gca () ;
25
-
7/26/2019 Numerical Methods for Scientific and Eng
27/152
10
11 a . y _ l o c at i o n = o r i g i n ;1213 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 he gr ap h15 t i t l e ( y = c os ( x )x%ex )16
17 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t b etw een
18 / / t he i n t e r v a l ( 0 , 1 )
1920
21 // s o l l u t i o n by m u ll e r method t o 5 i t e r a t i o n s.
22
23
24 m u l l e r 5 ( - 1 , 0 , 1 , f )
check AppendixAP 59for dependency:
chebyshev.sce
Scilab code Exa 2.13 solution by chebeshev method
1 // The e q ua t i on x35x+1==0ha s r e a l
r o o t s .2 // t he gr a ph o f
t h i s f u n c t io nc an b eo b s e r v e d h e r e .
26
-
7/26/2019 Numerical Methods for Scientific and Eng
28/152
3 xset ( window ,12);
4 x = - 2 : . 0 1 : 4 ; //d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) ,y=x35x+1 ) ; //d e f i n i n g t he f u n c t i o n .
6 deff ( [ y ] = f p ( x ) , y=3x25 ) ;7 deff ( [ y]= fpp (x ) , y=6x ) ;8 y = feval ( x , f ) ;
9
10 a = gca () ;
11
12 a . y _ l o c at i o n = o r i g i n ;13
14 a . x _ l o c at i o n = o r i g i n ;15 plot ( x , y )
// i n s t r u c t i o n t o p l o t t he gr ap h16 t i t l e ( y = x35x+1 )17
18 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t s b et we en
19 // t he i n t e r v a l s ( 0 , 1 ) , ( 2 , 3 ) .20 // s i n c e we h ave be en a sk ed f o r t he s m a l l e s t
p o s i t i v e r oo t o f t h e e q ua ti on ,21 / / we a re i n t r e s t e d on t h e i n t e r v a l ( 0 , 1 )22 / / a = 0; b =1 ,23
24
25 // s o l u t i o n by c h e b y s h e v method26
27 // t h e a p p r o x i m a t e r oo t a f t e r 4 i t e r a t i o n s can b eo b s e r v e d .
2829
30 c h e b y s h e v ( 0 . 5 , f , f p )
31
32
27
-
7/26/2019 Numerical Methods for Scientific and Eng
29/152
33 // h en ce t he a pp ro xi ma te r o ot w i t i n t he p e r m i s s i b l e
e r r o r o f 10
15 i s . 2 01 6 40 2 ,check AppendixAP 59for dependency:
chebyshev.sce
Scilab code Exa 2.14 solution by chebeshev method
12
3 / / The e q u a t i on1/ x7==0 has a
r e a l r o ot .4 // t he gr a ph o f
t h i s f u n c t io nc an b eo b s e r v e d h e r e .
5 xset ( window ,13);6 x = 0 . 0 0 1 : . 0 0 1 : . 2 5 ;
//d e f i n i n g t h e r an ge o f x .
7 deff ( [ y]= f (x ) , y=1/x7 ) ; //d e f i n i n g t he f u n c t i o n .
8 deff ( [ y ] = f p ( x ) , y=1/x2 ) ;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 ;
1415 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 gr ap h
28
-
7/26/2019 Numerical Methods for Scientific and Eng
30/152
17 t i t l e ( y =1/ x7 )
1819 // from t he abov e p l o t we c an i n f r e t ha t t he
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 ( 0 , 2 / 7 )21
22
23 / / s o l u t i o n by c h eb y s he v metho d24
25
26 c h e b y s h e v ( 0 . 1 , f , f p ) // c a l l i n g t hepred e f i n e d f u n c t i o n c h eby s he v t o f i n d t he
a pp ro xi ma te r o o t i n t he r an ge o f ( 0 , 2 / 7 ) .
check AppendixAP 59for dependency:
chebyshev.sce
Scilab code Exa 2.15 solution by chebeshev method
1 / / The e q u a t i onc o s ( x )x%ex==0 h as r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,8);
4 x = - 1 : . 0 0 1 : 2 ;//
d e f i n i n g t h e r an ge o f x .5 deff ( [ y]= f (x ) , y= c os ( x )x%ex ) ;
// d e f i n i n g t he c u n ct i o n .
29
-
7/26/2019 Numerical Methods for Scientific and Eng
31/152
6 deff ( [ y ] = f p ( x ) , y=s i n ( x )x%ex%ex ) ;
7 deff ( [ y]= fpp (x ) , y=
c o s ( x )
x
%ex
2
%ex ) ;8 y = feval ( x , f ) ;9
10 a = gca () ;
11
12 a . y _ l o c at i o n = o r i g i n ;13
14 a . x _ l o c at i o n = o r i g i n ;15 plot ( x , y )
// i n s t r u c t i o n t o p l o t t he gr ap h
16 t i t l e ( y = c os ( x )x%ex )17
18 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t b etw een
19 / / t he i n t e r v a l ( 0 , 1 )20
21
22 / / a = 0; b =1 ,23
24
25
26 // s o l u t i o n by c he by sh ev w it h ap e r m i s s i b l e e r r o r o f 10 15.
27
28 // we c a l l a u se rd e f i n e d f u n c t i o n c he by sh ev s o a st o f i n d t he a pp ro xi ma te
29 / / r o o t o f t h e e qu at i o n w it hi n t h e d e f i n e dp e r m i s s i b l e e r r o r l i m i t .
30
31 c h e b y s h e v ( 1 , f , f p )
32
3334
35 / / h en ce t he a pp ro xi ma te r o ot w i t i n t he p e r m i s s i b l ee r r o r o f 1015 i s
30
-
7/26/2019 Numerical Methods for Scientific and Eng
32/152
check AppendixAP 58for dependency:
multipoint_iteration31.sce
check AppendixAP 57for dependency:
multipoint_iteration33.sce
Scilab code Exa 2.16 multipoint iteration
1 // The e q ua t i on x35x+1==0ha s r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,15);
4 x = - 2 : . 0 1 : 4 ;//
d e f i n i n g t h e r an ge o f x .5 deff ( [ y]= f (x ) ,y=x35x+1 ) ; //
d e f i n i n g t he f u n c t i o n .6 deff ( [ y ] = f p ( x ) , y=3x25 ) ;7 deff ( [ y]= fpp (x ) , y=6x ) ;8 y = feval ( x , f ) ;
9
10 a = gca () ;
11
12 a . y _ l o c at i o n = o r i g i n ;13
14 a . x _ l o c at i o n = o r i g i n ;
31
-
7/26/2019 Numerical Methods for Scientific and Eng
33/152
15 plot ( x , y )
// i n s t r u c t i o n t o p l o t t he gr ap h16 t i t l e ( y = x35x+1 )17
18 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t s b et we en
19 // t he i n t e r v a l s ( 0 , 1 ) , ( 2 , 3 ) .20 // s i n c e we h ave be en a sk ed f o r t he s m a l l e s t
p o s i t i v e r oo t o f t h e e q ua ti on ,21 / / we a re i n t r e s t e d on t h e i n t e r v a l ( 0 , 1 )22 / / a = 0; b =1 ,
2324
25 // s o l u t i o n by m u l t i p o i n t i t e r a t i o nmethod
26
27 // t h e a p p r o x i m a t e r oo t a f t e r 3 i t e r a t i o n s can b eo b s e r v e d .
28
29
30 m u l t i p o i n t _ i t e r a t i o n 3 1 ( 0 . 5 , f , f p )
31
32 // h en ce t he a pp ro xi ma te r o ot w i t i n t he p e r m i s s i b l ee r r o r o f 1015 i s . 2 01 6 40 ,
33
34
35
36 m u l t i p o i n t _ i t e r a t i o n 3 3 ( 0 . 5 , f , f p )
37
38 // h en ce t he a pp ro xi ma te r o ot w i t i n t he p e r m i s s i b l ee r r o r o f 1015 i s . 2 01 6 40 ,
check AppendixAP 57for dependency:multipoint_iteration33.sce
32
-
7/26/2019 Numerical Methods for Scientific and Eng
34/152
Scilab code Exa 2.17 multipoint iteration
1 / / The e q u a t i onc o s ( x )x%ex==0 h as r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,8);4 x = - 1 : . 0 0 1 : 2 ;
//d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) , y= c os ( x )x%ex ) ;// d e f i n i n g t he f u n c t i o n .
6 deff ( [ y ] = f p ( x ) , y=s i n ( x )x%ex%ex ) ;7 deff ( [ y]= fpp (x ) , y=c o s ( x )x%ex2%ex ) ;8 y = feval ( x , f ) ;
9
10 a = gca () ;
11
12 a . y _ l o c at i o n = o r i g i n
;
13
14 a . x _ l o c at i o n = o r i g i n ;15 plot ( x , y )
// i n s t r u c t i o n t o p l o t t he gr ap h16 t i t l e ( y = c os ( x )x%ex )17
18 / / from t he abov e p l o t we c an i n f r e t ha t t h ef u n c t i o n h as r o o t b etw een
19 / / t he i n t e r v a l ( 0 , 1 )
2021
22 / / a = 0; b =1 ,23
24
33
-
7/26/2019 Numerical Methods for Scientific and Eng
35/152
25
26 // s o l u t i o n by m u l t i p o i n t i t e r a t i o nmethod u s i ng t he f or m ul a g i ve n i neq ua ti on no . 2 . 3 3 .
27
28 // we c a l l a u se rd e fi n e d f u nc t i o n m u l t i p o i n t i t e r a t i o n 3 3 s o a s t o f i n d t h ea p p r o x i m a t e
29 / / r o o t o f t h e e qu at i o n w it hi n t h e d e f i n e dp e r m i s s i b l e e r r o r l i m i t .
30
31 m u l t i p o i n t _ i t e r a t i o n 3 3 ( 1 , f , f p )
3233
34 / / h en ce t he a pp ro xi ma te r o ot w i t i n t he p e r m i s s i b l ee r r o r o f 1 05 i s 0 . 5 1 7 7 5 7 4 .
check AppendixAP 56for dependency:
generaliteration.sce
Scilab code Exa 2.23 general iteration
1 / / The e q u a t i on3x3+4x2+4x+1==0 ha st h r e e r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b e
o b s e r v e d h e r e .3 xset ( window ,22);4 x = - 1 . 5 : . 0 0 1 : 1 . 5 ;
//d e f i n i n g t h e r an ge o f x .
34
-
7/26/2019 Numerical Methods for Scientific and Eng
36/152
5 deff ( [ y]= f (x ) , y=3x3+4x2+4x+1 ) ;
// d e f i n i n g t he c u n ct i o n6 y = feval ( x , f ) ;7
8 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 h
1415 t i t l e ( y =3x3+4x2+4x+1 )16
17 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t b etw een
18 / / t h e i n t e r v a l ( 1 ,0 ) ,19
20 x 0 = - . 5 ; / / i n i t i a l a p p r o x i m a t i o n21
22
23 // l e t t h e i t e r a t i v e f u nc t i o n g ( x ) b e x+A
( 3
x3+4
x2+4x + 1) =g ( x ) ;24
25 // gp ( x )=(1+A ( 9 x2+8x +4) )26 / / we need t o c ho os e a v al ue f o r A , whi ch makes ab s
( g p ( x 0 ) )
-
7/26/2019 Numerical Methods for Scientific and Eng
37/152
abs ( gp( x 0) ) ,
36 / / we c an i n f e r t h at f o r t h e v a l e s o f A i n t h er an ge ( . 8 , 0) g ( x ) w i l l be g i v i n g a c on ve rg i n gs o l u t i o n ,
37
38 / / h en ce d e l i b e r a t e l e we c ho os e a t o be 0 . 5 ,39
40 A = - 0 . 5 ;
41
42 deff ( [ y ] = g ( x ) , y= x 0 . 5( 3 x3+4x2+4x+1) ) ;43 deff ( [ y ] = gp( x ) , y= 1 0. 5( 9 x2+8x+4) ) ; //
h en ce d e f i n i n g g ( x ) and gp ( x ) ,
44 g e n e r a l i t e r a t i o n ( x 0 , g , g p )
check AppendixAP 55for dependency:
aitken.sce
Scilab code Exa 2.24.1 solution by general iteration and aitken method
1 // The e q ua t i on x35x+1==0 h asr e a l r o o t s .
2 // t h e g r aph o f t h i sf u n c t i o n can beo b s e r v e d h e r e .
3 xset ( window ,2);4 x = - 2 : . 0 1 : 4 ; //
d e f i n i n g t he r a ng e o f x .
5 deff ( [ y]= f (x ) ,y=x35x+1 ) ; //d e f i n i n g t he f u n c t i o n .
6 y = feval ( x , f ) ;
7
8 a = gca () ;
36
-
7/26/2019 Numerical Methods for Scientific and Eng
38/152
9
10 a . y _ l o c at i o n = o r i g i n ;1112 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 g r ap h14 t i t l e ( y = x35x+1 )15
16 // from t he abov e p l o t we c an i n f r e t ha t t hef u n c t i o n h as r o o t s b et we en
17 // t he i n t e r v a l s ( 0 , 1 ) , ( 2 , 3 ) .18 // s i n c e we h ave be en a sk ed f o r t he s m a l l e s t
p o s i t i v e r oo t o f t h e e q ua ti on ,19 / / we a re i n t r e s t e d on t h e i n t e r v a l ( 0 , 1 )20
21 x 0 = . 5 ;
22
23 // s o l u t i o n u s in g l i n e a r i t e r a t i o n methodf o r t he f i r s t two i t e r a t i o n s and a it ke n s p r oc e ss two t im es f o r t he t h i r di t e r a t i o n .
24
25 deff ( [ y ] = g ( x )
, y = 1 /5
(x 3+1) ) ;
26 deff ( [ y ] = gp( x ) , y = 1 /5(3x 2 ) ) ;27
28
29 g e n e r a l i t e r a t i o n 2 ( x 0 , g , g p )
30
31
32 / / f rom t he abov e i t e r a t i o n s p er fo rm ed we c an i n f e rt h a t
33 x 1 = 0 . 2 2 5 ;
34 x 2 = 0 . 2 0 2 2 7 8 ;
3536
37
38
39 a i t k e n ( x 0 , x 1 , x 2 , g ) // c a l l i n g t h e a i t k e n
37
-
7/26/2019 Numerical Methods for Scientific and Eng
39/152
method f o r one i t e r a t i o n
check AppendixAP 54for dependency:
generaliteration2.sce
check AppendixAP 55for dependency:
aitken.sce
Scilab code Exa 2.24.2 solution by general iteration and aitken method
1 / / The e q u a t i on x%ex==0 ha s r e a l r o o t s .
2 // t h e g r aph o f t h i sf u n c t i o n can beo b s e r v e d h e r e .
3 xset ( window ,24);4 x = - 3 : . 0 1 : 4 ;
//d e f i n i n g t h e r an ge o f x .5 deff ( [ y]= f (x ) , y=x%ex ) ; //
d e f i n i n g t he c u nc t io n .6 y = feval ( x , f ) ;
7
8 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 t i t l e ( y = x%ex )15
38
-
7/26/2019 Numerical Methods for Scientific and Eng
40/152
16 // from t he abov e p l o t we c an i n f r e t ha t t he
f u n c t i o n h as r o o t b etw een17 / / t he i n t e r v a l ( 0 , 1 )18
19 x 0 = 1 ;
20
21 // s o l u t i o n u s in g l i n e a r i t e r a t i o n methodf o r t he f i r s t two i t e r a t i o n s and a i t ke n s p r o c e s s two t im es f o r t he t h i r di t e r a t i o n .
22
23
2425 deff ( [ y ] = g ( x ) , y=%ex ) ;26 deff ( [ y ] = gp( x ) , y=%ex ) ;27
28
29 g e n e r a l i t e r a t i o n 2 ( x 0 , g , g p )
30
31
32 / / f rom t he abov e i t e r a t i o n s p er fo rm ed we c an i n f e rt h a t
33 x 1 = 0 . 3 6 7 8 7 9 ;
34 x 2 = 0 . 6 9 2 2 0 1 ;
35
36
37
38
39 a i t k e n ( x 0 , x 1 , x 2 , g ) // c a l l i n g t h e a i t k e nmethod f o r one i t e r a t i o n
check AppendixAP 54for dependency:
generaliteration2.sce
check AppendixAP 55for dependency:
aitken.sce
check AppendixAP 54for dependency:
39
-
7/26/2019 Numerical Methods for Scientific and Eng
41/152
generaliteration2.sce
Scilab code Exa 2.25 solution by general iteration and aitken method
1 / / The e q u a t i onc o s ( x )x%ex==0 h as r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,25);4 x = 0 : . 0 1 : 2 ;
//d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) , y= c os ( x )x%ex ) ;// d e f i n i n g t he c u n ct i o n .
6 y = feval ( x , f ) ;
7
8 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 t i t l e ( y = c os ( x )x%ex )
1516 / / 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 b etw een17 / / t he i n t e r v a l ( 0 , 1 )18
40
-
7/26/2019 Numerical Methods for Scientific and Eng
42/152
19 x 0 = 0 ;
2021 // s o l u t i o n u s in g l i n e a r i t e r a t i o n method
f o r t he f i r s t two i t e r a t i o n s and a it ke n s p r oc e ss two t im es f o r t he t h i r di t e r a t i o n .
22
23 deff ( [ y ] = g ( x ) , y=x +1/2( c o s ( x )x%ex ) ) ;24 deff ( [ y ] = gp( x ) , y=1+1/2( s i n ( x )x%ex
%ex ) ) ;25
26
27 g e n e r a l i t e r a t i o n 2 ( x 0 , g , g p )28
29
30 / / f rom t he abov e i t e r a t i o n s p er fo rm ed we c an i n f e rt h a t
31 x 1 = 0 . 5 0 0 0 0 0 0 0 ;
32 x 2 = 0 . 5 2 6 6 1 1 0 ;
33
34
35
36 a i t k e n ( x 0 , x 1 , x 2 , g ) // c a l l i n g t h e a i t k e nmethod f o r one i t e r a t i o n
check AppendixAP 42for dependency:
Vnewton.sce
check AppendixAP 53for dependency:
modified_newton.sce
Scilab code Exa 2.26 solution to the eq with multiple roots
41
-
7/26/2019 Numerical Methods for Scientific and Eng
43/152
1 // The e q ua t i on x
3
7
x2+16
x12==0 ha sr e a l r o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,25);4 x = 0 : . 0 0 1 : 4 ;
//d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) ,y=x37x2+16x12 ) ;// d e f i n i n g t he c u n ct i o n .
6 deff ( [ y ] = f p ( x ) , y=3x 2 14x+16 ) ;7 y = feval ( x , f ) ;
8
9 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 he gr ap h15 t i t l e ( y = x37x2+16x12 )16
17
18
19
20 / / g i v e n t ha t t he e q u a ti o n ha s d ou bl e r o o t s a t x=2he nc e m= 2;
21
22 m = 2 ;23
24 // s o l u t i o n by newton r ap hs onmethod
25
42
-
7/26/2019 Numerical Methods for Scientific and Eng
44/152
26
27 n e w t o n ( 1 , f , f p ) // c a l l i n g t h e u se rd e f i n e d f u n c t i o n28
29
30
31
32 // s o l u t i o n by m o d if i edn ew to n r a p h s o n s m at ho d
33
34
35
36 m o d i f i e d _ n e w t o n ( 1 , f , f p )
check AppendixAP 42for dependency:
Vnewton.sce
check AppendixAP 43for dependency:
Vnewton4.sce
check AppendixAP 50for dependency:
newton63.sce
check AppendixAP 51for dependency:
secant64.sce
check AppendixAP 52for dependency:
secant65.sce
Scilab code Exa 2.27 solution to the given transcendental equation
43
-
7/26/2019 Numerical Methods for Scientific and Eng
45/152
1 / / The e q u a t i on
27
x5+27
x4+36x3+28x2+9x+1==0ha s r e a lr o o t s .
2 // t he gr a ph o f t h i s f u n c t io nc an b eo b s e r v e d h e r e .
3 xset ( window ,26);4 x = - 2 : . 0 0 1 : 3 ;
//d e f i n i n g t h e r an ge o f x .
5 deff ( [ y]= f (x ) , y=27x5+27x4+36x3+28x2+9x+1 ) ; // d e f i n i n g t he c u n ct i o n .
6 deff ( [ y ] = f p ( x ) , y = 275x4+274x3+363x2+282x+9 ) ;
7 deff ( [ y]= fpp (x ) , y = 2754x 3+ 2743x2+3632x+2 82 ) ;
8 y = feval ( x , f ) ;
9
10 a = gca () ;
11
12 a . y _ l o c at i o n = o r i g i n ;13
14 a . x _ l o c at i o n = o r i g i n ;15 plot ( x , y )
// i n s t r u c t i o n t o p l o t t he gr ap h16 t i t l e ( y = 27x5+27x4+36x3+28x2+9x+1 )17
18
1920
21 // s o l u t i o n by newton r ap hs onmethod a s p e r t h e e q u a t i o n no .
2 . 1 4
44
-
7/26/2019 Numerical Methods for Scientific and Eng
46/152
22
2324 n e w t o n ( - 1 , f , f p ) // c a l l i n g t h e u se r
d e f i n e d f u n c t i o n25
26
27 n e w t o n 4 ( - 1 , f , f p )
28
29
30 // s o l u t i o n by newtonr a ph s on method a s p e r t h e
e qu at i o n no . 2 . 6 3
3132 n e w t o n 6 3 ( - 1 , f , f p , f p p ) // c a l l i n g
t h e u se r d e f i ne d f u nc t i o n33 // s o l u t i o n by t he s e c a n t
method d e f i n e d t os a t i s f y t he e qu at io nno . 2 . 6 4 .
34
35
36 s e c a n t 6 4 ( 0 , - 1 , f , f p )
37
38
39
40
41
42
43 // s o l u t i o n by t he s e c a n tmethod d e f i n e d t o
s a t i s f y t he e qu at io nno . 2 . 6 5 .
44
4546 s e c a n t 6 5 ( 0 , - . 5 , f )
45
-
7/26/2019 Numerical Methods for Scientific and Eng
47/152
Chapter 3
SYSTEM OF LINEAR
ALGEBRIC EQUATIONS
AND EIGENVALUE
PROBLEMS
Scilab code Exa 3.1 determinent
1 / / e xa mp le 3 . 12 // P o s i t i v e d e f i n i t e3
4 A =[12 4 -1;4 7 1; -1 1 6];
5 // c he ck i f t he d et er mi na nt o f t he l e a d i ng m in or s i sa p o s i t i v e v a lu e
6
7 A 1 = A ( 1 ) ;
8 det ( A 1 )
9 A 2 = A ( 1 : 2 , 1 : 2 ) ;
10 det ( A 2 )11 A 3 = A ( 1 : 3 , 1 : 3 ) ;
12 det ( A 3 )
13
14 / /we o b se rv e t ha t t he d et er mi na nt o f t he l e a d i ng
46
-
7/26/2019 Numerical Methods for Scientific and Eng
48/152
m i n o r s i s a p o s i t i v e , h e n c e t he g i v en m at r i x A i s
a p o s i t i v e d e f i n i t e .
Scilab code Exa 3.2 property A in the book
1 / / e xa mp le no . 3 . 22 // show i f a g i v e n m at ri x A p o s s e s s e s p r op e r t y A3
4 A = [2 -1 0 -1; -1 2 -1 0;0 -1 2 0;0 0 -1 2 ]
56 P = [ 0 0 0 1 ; 0 1 0 0 ; 0 0 1 0 ; 1 0 0 0 ] //l e t us t ak e t he p em ut at io n m at ri x a s P
7
8 / / t h en we f i n d t ha t9
10 P * A * P
11
12 / / i s o f t h e form ( 3 . 2 ) . h e n c e t h e m a t r i x A ha sp r o p e r t y A
Scilab code Exa 3.4 solution to the system of equations
1 / / e xa mp le no . 3 . 42 // s o l v e t he s ys te m o f e q ua t i on s3
4 / / ( a ) . by u s i n g cr am er s r u l e ,5
6 A =[1 2 -1;3 6 1;3 3 2]
78 B1 =[2 2 -1;1 6 1;3 3 2]
9
10 B2 =[1 2 -1;3 1 1;3 3 2]
11
47
-
7/26/2019 Numerical Methods for Scientific and Eng
49/152
12 B 3 = [ 1 2 2 ; 3 6 1 ; 3 3 3 ]
1314
15 //we know ;16
17 X1 = det ( B 1 ) / det ( A )
18 X2 = det ( B 2 ) / det ( A )
19 X3 = det ( B 3 ) / det ( A )
20
21
22 / / ( b ) . by d e t e m i n in g t h e i n v e r s e o f t h e c o e f f i c i e n tm a t r i x
2324 A =[1 2 -1;3 6 1;3 3 2]
25
26 b =[2 ;1 ; 3]
27
28 //we know ;29
30 X = inv ( A ) * b
check AppendixAP 34for dependency:
Vgausselim.sce
Scilab code Exa 3.5 solution by gauss elimination method
1 //e x ampl e 3.52 // c ap t i o n s o l u t i o n by g a us s e l i m i b n a t i o n method3
4 A = [1 0 -1 2;1 10 -1;2 3 2 0] / / m a t r i c e s
A and b f ro m t h e a bo ve5 //
s y s t e m
o f
48
-
7/26/2019 Numerical Methods for Scientific and Eng
50/152
e q u a t i o n s
6 b = [ 4 ; 3 ; 7 ]
7
8 g a u s s e l i m ( A , b ) // c a l l g au ss e l i m i n a t i o nf u n c t i o n t o s o l v e t he
9 // m a t r i c e s A and b
check AppendixAP 33for dependency:
pivotgausselim.sci
Scilab code Exa 3.6 solution by pivoted gauss elimination method
1 //e x ampl e 3.62 // c ap t i o n s o l u t i o n by g a us s e l i m i b n a t i o n method3
4 A = [ 1 1 1 ; 3 3 4 ; 2 1 3 ] / / m a t r i c e s A andb fro m t he a bo ve
5 //
s y s t e m
o f
e q u a t i o n s
6
7 b = [ 6 ; 2 0 ; 1 3 ]
8
9 p i v o t g a u s s e l i m ( A , b ) // c a l l g a us s
e l i m i n a t i o n f u nc t i o n t o s o l v e t he10 // m a t r i c e s A and b
check AppendixAP 33for dependency:
pivotgausselim.sci
49
-
7/26/2019 Numerical Methods for Scientific and Eng
51/152
Scilab code Exa 3.8 solution by pivoted gauss elimination method
1 / / e xa mp le no . 3 . 82 / / s o l v i n g t h e m at r i x e qu at i o n w i t h p a r t i a l p i v o t i n g
i n g au ss e l i m i n a t i o n3
4 A =[2 1 1 -2;4 0 2 1;3 2 2 0;1 3 2 -1]
5
6 b = [ - 1 0 ; 8 ; 7 ; - 5 ]7
8 p i v o t g a u s s e l i m ( A , b )
check AppendixAP 32for dependency:
jordan.sce
Scilab code Exa 3.9 solution using the inverse of the matrix
1 / / e xa mp le no . 3 . 92 / / s o l v i n g t h e s ys te m u s i n g i n v e r s e o f t h e c o f f i c i e n t
m a t r i x3
4 A = [1 1 1;4 3 -1;3 5 3]
5
6 I = [ 1 0 0 ; 0 1 0 ; 0 0 1 ]
7
8 b =[1 ;6 ; 4]
9
10 M = j o r d e n ( A , I )11
12 I A = M ( 1 : 3 , 4 : 6 )
13
14 X = I A * b
50
-
7/26/2019 Numerical Methods for Scientific and Eng
52/152
check AppendixAP 41for dependency:
LandU.sce
check AppendixAP 38for dependency:
back.sce
check AppendixAP 40for dependency:
fore.sce
Scilab code Exa 3.10 decomposition method
1 / / e xa mp le no . 3 . 1 02 / / s o l v e s y st em by d e c o m p o s i t i o n metho d3
4 A = [1 1 1;4 3 -1;3 5 3]
5 n = 3 ;
6
7 b = [ 1 ; 6 ; 4 ]8
9 [ U , L ] = L a n d U ( A , 3 )
10
11 Z = f o r e ( L , b )
12
13 X = b a c k ( U , Z )
check AppendixAP 41for dependency:
LandU.sce
Scilab code Exa 3.11 inverse using LU decoposition
51
-
7/26/2019 Numerical Methods for Scientific and Eng
53/152
1 / / e xa mp le no . 3 . 1 1
2 / / c a p t i o n : I n v e r s e u s i n g LU d e c o mp o s i t i on34 A = [ 3 2 1 ; 2 3 2 ; 1 2 2 ]
5
6 [ U , L ] = L a n d U ( A , 3 ) // c a l l LandU f u n c t i o n t oe v a l u a t e U , L o f A ,
7
8 / / s i n c e A=LU ,9 // in v (A)=in v (U) i nv ( L)
10 / / l e t i n v (A)=AI11
12 A I = U ^ - 1 * L ^ - 1
check AppendixAP 41for dependency:
LandU.sce
check AppendixAP 38for dependency:
back.sce
check AppendixAP 40for dependency:
fore.sce
Scilab code Exa 3.12 solution by decomposition method
1 / / e xa mp le no . 3 . 1 22 / / s o l v e s y st em by d e c o m p o s i t i o n metho d3
4 A = [1 1 -1;2 2 5;3 2 -3]
56 b = [ 2 ; - 3 ; 6 ]
7
8
9
52
-
7/26/2019 Numerical Methods for Scientific and Eng
54/152
10 // h en ce we c an o b s e r v e t h a t LU d e c o m po s i t i on
method f a i l s t o s o l v e t h i s s ys te m s i n c et h e p i v o t L ( 2 , 2 ) =0;11
12
13 / / we n o t e t h a t t h e c o e f f i c i e n t m a t r i x i s n o ta p o s i t i v e d e f i n i t e m a tr ix and h e n ce i t sLU d e c o m po s i t i on i s n ot g u ar a nt ee d ,
14
15
16 // i f we i n t e r ch a n g e t h e r ow s o f A a s shownb el o w t h e LU d e c o m p o s i t i o n w ou ld work ,
1718 A =[3 2 -3;2 2 5;1 1 -1]
19
20 b = [ 6 ; - 3 ; 2 ]
21
22 [ U , L ] = L a n d U ( A , 3 ) / / c a l l LandUf u nc t i o n t o e v a l ua t e U, L o f A ,
23
24 n = 3 ;
25 Z = f o r e ( L , b ) ;
26
27 X = b a c k ( U , Z )
check AppendixAP 41for dependency:
LandU.sce
check AppendixAP 38for dependency:
back.sce
check AppendixAP 40for dependency:
fore.sce
Scilab code Exa 3.13 LU decomposition
53
-
7/26/2019 Numerical Methods for Scientific and Eng
55/152
1 / / e xa mp le no . 3 . 1 3
2 / / s o l v e s y st em by LU d e c o m p o s i t i o n metho d34 A =[2 1 1 -2;4 0 2 1;3 2 2 0;1 3 2 -1]
5
6 b = [ - 1 0 ; 8 ; 7 ; - 5 ]
7
8 [ U , L ] = L a n d U ( A , 4 )
9 n = 4 ;
10 Z = f o r e ( L , b ) ;
11
12 X = b a c k ( U , Z )
1314 / / s i n c e A=LU ,15 // in v (A)=in v (U) i nv ( L)16 / / l e t i n v (A)=AI17
18 A I = U ^ - 1 * L ^ - 1
check AppendixAP 38for dependency:
back.sce
check AppendixAP 39for dependency:
cholesky.sce
check AppendixAP 12for dependency:
fact.sci
Scilab code Exa 3.14 cholesky method
1 / / e xa mp le no . 3 . 1 42 / / s o l v e s ys te m by c h o l e s k y method3
4 A =[1 2 3;2 8 22;3 22 82]
54
-
7/26/2019 Numerical Methods for Scientific and Eng
56/152
5
6 b = [ 5 ; 6 ; - 1 0 ]7
8 L = c h o l e s ky ( A , 3 ) // c a l l c ho l es k y f u nc t i o n t oe v al u a t e t he r o ot o f t he s yst em
9 n = 3 ;
10 Z = f o r e ( L , b ) ;
11
12 X = b a c k ( L , Z )
check AppendixAP 38for dependency:
back.sce
check AppendixAP 39for dependency:
cholesky.sce
check AppendixAP 40for dependency:
fore.sce
Scilab code Exa 3.15 cholesky method
1 / / e xa mp le no . 3 . 1 52 / / s o l v e s ys te m by c h o l e s k y method3
4 A =[4 -1 0 0; -1 4 -1 0;0 -1 4 -1;0 0 -1 4]
5
6 b = [ 1 ; 0 ; 0 ; 0 ]
7
8 L = c h o l e s ky ( A , 4 ) // c a l l c ho l es k y f u nc t i o n t o
e v al u a t e t he r o ot o f t he s yst em9
10 n = 4 ;
11 Z = f o r e ( L , b ) ;
12
55
-
7/26/2019 Numerical Methods for Scientific and Eng
57/152
13 X = b a c k ( L , Z )
1415 / / s i n c e A=LL ,16 / / i n v ( A)=i n v ( L ) i nv ( L)17 / / l e t i n v (A)=AI18
19 A I = L ^ - 1 * L ^ - 1
check AppendixAP 37for dependency:
jacobiiteration.sce
Scilab code Exa 3.21 jacobi iteration method
1 / / e xa mp le no . 3 . 2 12 // s o l v e t he s ys te m by j a c o b i i t e r a t i o n method3
4 A = [ 4 1 1 ; 1 5 2 ; 1 2 3 ]
5
6 b = [2 ; - 6; - 4]
7
8 N = 3 ; // no . o f i e r a t i o n s9 n = 3 ; // o rd er o f t h e m a t r i x i s nn
10
11 X = [ . 5 ; - . 5 ; - . 5 ] // i n i t i a l app r ox i ma t i o n12
13
14 j a c o b i i t e r a t i o n ( A , n , N , X , b ) // c a l l t hef u n c ti o n which p er fo rm s j a c o b i i t e r a t i o n methodt o s o l v e t he s ys te m
check AppendixAP 36for dependency:Vgaussseidel.sce
56
-
7/26/2019 Numerical Methods for Scientific and Eng
58/152
Scilab code Exa 3.22 solution by gauss siedal method
1 / / e xa mp le no . 3 . 2 22 // s o l v e t he s ys te m by g a us s s e i d e l method3
4 A = [2 -1 0; -1 2 -1;0 -1 2]
5
6 b = [ 7 ; 1 ; 1 ]
7
8 N = 3 ; // no . o f i e r a t i o n s9 n = 3 ; // o rd er o f t h e m a t r i x i s nn
10
11 X = [ 0 ; 0 ; 0 ] // i n i t i a l app r ox i ma t i o n12
13
14 g a u s s s e i d e l ( A , n , N , X , b ) // c a l l t hef u n c t i o n wh ich p e rf o rm s g a us s s e i d e l method t os o l v e t he s ys te m
check AppendixAP 35for dependency:
geigenvectors.sci
Scilab code Exa 3.27 eigen vale and eigen vector
1 / / e xa mp le 3 . 2 72 // a ) f i n d e i ge n v a l u e and e i g e n v e c t o r ;3 // b ) v e r i f y i nv ( S )AS i s a d i ag o na l m at r i x ;4
5 / / 1 )6 A =[1 2 -2 ;1 1 1;1 3 -1];
78 B = [ 1 0 0 ; 0 1 0 ; 0 0 1 ] ;
9
10 [ x , l a m ] = g ei g e n ve c t o rs ( A , B ) ;
11
57
-
7/26/2019 Numerical Methods for Scientific and Eng
59/152
12 inv ( x ) * A * x
1314 // 2)15 A =[3 2 2;2 5 2;2 2 3];
16
17 B = [ 1 0 0 ; 0 1 0 ; 0 0 1 ] ;
18
19
20 [ x , l a m ] = g ei g e n ve c t o rs ( A , B ) ;
21
22 inv ( x ) * A * x
58
-
7/26/2019 Numerical Methods for Scientific and Eng
60/152
Chapter 4
INTERPOLATION AND
APPROXIMATION
check AppendixAP 29for dependency:
NDDinterpol.sci
check AppendixAP 28for dependency:
aitkeninterpol.sci
check AppendixAP 25for dependency:
legrangeinterpol.sci
Scilab code Exa 4.3 linear interpolation polinomial
1 / / ex am pl e 4 . 32 // f i n d t h e l i n e a r i n t e r p o l a t i o n p o l i n o m i a l3 // u s in g
45 disp ( f (2 ) =4 ) ;6 disp ( f ( 2 . 5 ) = 5. 5 ) ;7 // 1 ) l a g ra n ge i n t e r p o l a t i o n ,8
59
-
7/26/2019 Numerical Methods for Scientific and Eng
61/152
9 P 1 = l e g r a ng e i n te r p o l ( 2 , 2 . 5 , 4 , 5 .5 )
10 // 2 ) a it ke n s i t e r a t e d i n t e r p o l a t i o n ,1112 P 1 = a i t k en i n t e rp o l ( 2 , 2 . 5 , 4 , 5 . 5)
13
14 // 3 ) n ewton d ev id ed d i f f e r a n c e i n t e r p o l a t i o n ,15
16 P 1 = N D D i nt e r po l ( 2 , 2 . 5 , 4 , 5 . 5)
17
18 // h en ce a pp ro xi ma te v al u e o f f ( 2 . 2 ) = 4 . 6 ;
check AppendixAP 25for dependency:
legrangeinterpol.sci
Scilab code Exa 4.4 linear interpolation polinomial
1 / / ex am pl e 4 . 42 // f i n d t h e l i n e a r i n t e r p o l a t i o n p o l i n o m i a l3 // u s in g l a g ra n g e i n t e r p o l a t i o n ,
45 disp ( s i n ( . 1 ) =. 0 9 9 8 3 ; s i n ( . 2 ) = .1 9 86 7 ) ;6
7
8 P 1 = l e g r a ng e i n te r p o l ( . 1 , . 2 , . 0 99 8 3 , . 1 98 6 7)
9
10 / / h e n c e ;11 disp ( P ( . 1 5 ) = .0 0 0 9 9 +. 9 8 8 4 . 1 5 )12 disp ( P ( 0 . 1 5 ) = 0. 1 4 9 25 ) ;
check AppendixAP 25for dependency:
legrangeinterpol.sci
60
-
7/26/2019 Numerical Methods for Scientific and Eng
62/152
Scilab code Exa 4.6 legrange linear interpolation polinomial
1 // ex ampl e : 4 . 62 // c ap ti on : o bt ai n t he l e g r a n g e l i n e a r
i n t e r p o l a t i n g p o l i n om i al3
4
5 // 1 ) o bt a in t h e l e g r a n g e l i n e a r i n t e r p o l a t i n gp o l i n om i a l i n t h e i n t e r v a l [ 1 , 3 ] and o bt ai na p pr o xi m at e v a l u e o f f ( 1 . 5 ) , f ( 2 . 5 ) ;
6 x 0 = 1 ; x 1 = 2 ; x 2 = 3 ; f 0 = . 8 4 1 5 ; f 1 = . 9 0 9 3 ; f 2 = . 1 4 1 1 ;
7
8 P 13 = l e g r an g e i nt e r p o l ( x 0 , x2 , f 0 , f 2 ) // int h e r a n g e [ 1 , 3 ]
9
10
11 P 12 = l e g r an g e i nt e r p o l ( x 0 , x1 , f 0 , f 1 ) //i n t h e r a ng e [ 1 , 2 ]
12
13 P 23 = l e g r an g e i nt e r p o l ( x 1 , x2 , f 1 , f 2 )
// i n th e r a ng e [ 2 , 3 ]14
15 / / from P23 we f i n d t ha t ; where a s e xa ct v al ue i ss i n ( 2 . 5 ) =0 . 5 9 8 5 ;
16 disp ( P ( 1 . 5 ) = 0 . 87 5 4 ) ;17 disp ( e x a c t v a l u e o f s i n ( 1 . 5 ) = .9 97 5 )18 disp ( P ( 2 . 5 ) = 0 . 52 5 2 ) ;
check AppendixAP 24for dependency:
lagrangefundamentalpoly.sci
Scilab code Exa 4.7 polynomial of degree two
1 / / ex am pl e 4 . 72 // p o l i no m i a l o f d e g r ee 2 ;
61
-
7/26/2019 Numerical Methods for Scientific and Eng
63/152
3
4 // f (0 ) =1; f (1 ) =3; f (3 ) =55;56 // u s i ng l e g r a n g e f un da me nt al p o l i n o m i a l r u le ,7
8 x =[0 1 3 ]; // a r r a i n g i n g t h ei n p ut s o f t h e f u n c t i o n a s e le me nt s o f a row ,
9 f =[1 3 5 5] ; // a r r a i n g i ng t h eo ut pu ts o f t he f u n c ti o n a s e l e me nt s o f a r ow ,
10 n = 2 ; // d eg re e o f t h ep o l i n o m i a l ;
11
1213 P 2 = l a g r a n g e f u n d a m e n t a l p o l y ( x , f , n )
check AppendixAP 24for dependency:
lagrangefundamentalpoly.sci
Scilab code Exa 4.8 solution by quadratic interpolation
1
2 / / ex am pl e 4 . 83 // c ap t i o n : s o l u t i o n by q u a d r a ti c i n t e r p o l a t i o n ;4
5 // xd e g re e s : [ 1 0 20 3 0 ]6 // h en c e x i n r ad i a n s i s7 x = [ 3 . 1 4/ 1 8 3 . 1 4 /9 3 . 1 4 / 6 ];
8 f = [ 1 . 1 58 5 1 . 2 8 17 1 . 3 66 0 ] ;
9 n = 2 ;
10
1112 P 2 = l a g r a n g e f u n d a m e n t a l p o l y ( x , f , n )
13
14 // h en ce from P2 , t he e xa c t v al u e o f f ( 3 . 1 4 /1 2 ) i s1 . 2 2 4 6 ;
62
-
7/26/2019 Numerical Methods for Scientific and Eng
64/152
15 // w here a s e xa ct v al ue i s 1 . 2 2 4 7 ;
check AppendixAP 23for dependency:
NDDinterpol2.sci
check AppendixAP 22for dependency:
iteratedinterpol.sci
Scilab code Exa 4.9polinomial of degree two
1 / / e xa mp le 4 . 92 // c a p t i o n : o bt ai n t h e p o l i n om i al o f d eg re e 23
4 x = [0 1 3];
5 f = [1 3 5 5] ;
6 n = 2 ;
7
8 // 1 ) i t e r a t e d i n t e r p o l a t i o n ;9
1011 [ L 01 2 , L 0 2 , L 0 1 ] = i t e r a te d i n t er p o l ( x , f , n )
12
13 // 2 ) newton d i v i d e d d i f f r e n c e s i n t e r p o l a t i o n ;14
15
16 P 2 = N D D i nt e r p ol 2 ( x , f )
check AppendixAP 31for dependency:
NBDP.sci
Scilab code Exa 4.15 forward and backward difference polynomial
63
-
7/26/2019 Numerical Methods for Scientific and Eng
65/152
1 / / exa mp le 4 . 1 5 :
2 / / o bt ai n t he i n t e r p o l a t e u si ng backward d i f f e r e n c e sp o l i n o m i a l3
4
5 xL =[.1 .2 .3 .4 .5 ]
6
7 f = [ 1. 4 1 .5 6 1 .7 6 2 2 .2 8]
8 n = 2 ;
9
10
11 / / h e n c e ;
12 disp (P=1.4+(x . 1) ( . 1 6 / . 1 ) +(x . 5 ) ( x . 4 ) ( . 0 4 / . 0 2 ) )
13 disp (P=2x2+x +1. 28 ) ;14
15 // 1 ) o bt ai n t h e i n t e r p o l a t e a t x = 0 .2 5;16 x = 0 . 2 5 ;
17 [ P ] = N B D P ( x , n , x L , f ) ;
18 P
19 disp ( f ( . 2 5 ) = 1 . 6 5 5 ) ;20
21
22 // 2 ) o bt ai n t h e i n t e r p o l a t e a t x = 0 .3 5;23 x = 0 . 3 5 ;
24 [ P ] = N B D P ( x , n , x L , f ) ;
25 P
26 disp ( f ( . 3 5 ) = 1 . 8 7 5 ) ;
check AppendixAP 30for dependency:
hermiteinterpol.sci
Scilab code Exa 4.20 hermite interpolation
1 / / exa mp le 4 . 2 0 ;
64
-
7/26/2019 Numerical Methods for Scientific and Eng
66/152
2 // h er mi te i n t e r p o l a t i o n :
34 x =[ -1 0 1 ];
5
6 f=[1 1 3];
7
8 f p = [ -5 1 7 ];
9
10 P = h e r m i te i n t er p o l ( x , f , fp ) ;
11
12 / / h e n c e ;13 disp ( f ( 0.5)=3/8 ) ;
14 disp ( f (0 . 5 ) =11/8 ) ;
check AppendixAP 25for dependency:
legrangeinterpol.sci
Scilab code Exa 4.21 piecewise linear interpolating polinomial
1 / / e xa mp le : 4 . 2 1 ;2 // p i e c e w i s e l i n e a r i n t e r p o l a t i n g p o l i n o m i a l s :
3
4 x 1 = 1 ; x 2 = 2 ; x 3 = 4; x 4 = 8 ;
5 f 1 = 3 ; f 2 =7 ; f 3 = 21 ; f 4 = 73 ;
6 // we ne ed t o a pp ly l e g r a n g e s i n t e r p o l a t i o n i n subra n g es [ 1 , 2 ] ; [ 2 , 4 ] , [ 4 , 8 ] ;
7
8 x = poly (0 , x ) ;9
10 P 1 = l e g r a ng e i n t er p o l ( x1 , x 2 , f 1 , f 2 ) ; // i n t h e
r an ge [ 1 , 2 ]11 P1
12
13 P 1 = l e g r a ng e i n t er p o l ( x2 , x 3 , f 2 , f 3 ) ; // i n t h er an ge [ 2 , 4 ]
65
-
7/26/2019 Numerical Methods for Scientific and Eng
67/152
14 P1
1516 P 1 = l e g r a ng e i n t er p o l ( x3 , x 4 , f 3 , f 4 ) ; // i n t h e
r an ge [ 4 , 8 ]17 P1
check AppendixAP 24for dependency:
lagrangefundamentalpoly.sci
Scilab code Exa 4.22 piecewise quadratic interpolating polinomial
1 / / e xa mp le : 4 . 2 2 ;2 // p i e c e w i s e q u a d r a ti c i n t e r p o l a t i n g p o l i n o m i a l s :3
4 X =[ -3 -2 -1 1 3 6 7];
5 F = [ 36 9 2 22 1 71 1 65 2 07 9 90 1 77 9] ;
6 // we ne ed t o a pp ly l e g r a n g e s i n t e r p o l a t i o n i n subr an g es [3 , 1 ] ; [ 1 , 3 ] , [ 3 , 7 ] ;
7
8 x = poly (0 , x
) ;
9
10 // 1 ) i n t he r an g e [ 3 , 1]11 x = [ -3 -2 -1 ];
12 f = [ 36 9 2 22 17 1] ;
13 n = 2 ;
14 P 2 = l a g r a n g e f u n d a m e n t a l p o l y ( x , f , n ) ;
15
16 // 2 ) i n t he r a n g e [ 1 ,3 ]17 x =[ -1 1 3];
18 f = [ 17 1 1 6 5 2 07 ];
19 n = 2 ;20 P 2 = l a g r a n g e f u n d a m e n t a l p o l y ( x , f , n )
21
22 // 3 ) i n t h e r a n g e [ 3 , 7 ]23 x =[3 6 7];
66
-
7/26/2019 Numerical Methods for Scientific and Eng
68/152
24 f = [ 20 7 9 9 0 1 7 79 ] ;
25 n = 2 ;26 P 2 = l a g r a n g e f u n d a m e n t a l p o l y ( x , f , n )
27
28
29
30 // hence , we o b ta i n t he v a l ue s o f f ( 2 .5 ) =48 ; f ( 6 . 5 ) = 1 3 5 1 . 5 ;
check AppendixAP 24for dependency:
lagrangefundamentalpoly.sci
Scilab code Exa 4.23 piecewise cubical interpolating polinomial
1 / / e xa mp le : 4 . 2 3 ;2 / / p i e c e w i s e c u b i c a l i n t e r p o l a t i n g p o l i n o m i a l s :3
4 X =[ -3 -2 -1 1 3 6 7];
5 F = [ 36 9 2 22 1 71 1 65 2 07 9 90 1 77 9] ;
6 // we ne ed t o a pp ly l e g r a n g e s i n t e r p o l a t i o n i n sub
r an g es [3 , 1 ] ; [ 1 , 7 ] ;7
8
9 x = poly (0 , x ) ;10
11 // 1 ) i n t he r an g e [ 3 ,1 ]12 x =[ -3 -2 -1 1];
13 f = [ 36 9 22 2 1 71 1 65 ];
14 n = 3 ;
15 P 2 = l a g r a n g e f u n d a m e n t a l p o l y ( x , f , n ) ;
1617 // 2 ) i n t h e r a n g e [ 1 , 7 ]18 x =[1 3 6 7];
19 f = [ 16 5 2 0 7 9 9 0 1 7 79 ];
20 n = 3 ;
67
-
7/26/2019 Numerical Methods for Scientific and Eng
69/152
21 P 2 = l a g r a n g e f u n d a m e n t a l p o l y ( x , f , n )
2223
24
25 / / h en c e ,26 disp ( f ( 6 . 5 ) = 1 3 3 9 . 2 5 ) ;
Scilab code Exa 4.31 linear approximation
1 / / e xa mp le 4 . 3 12 // o b ta i n t he l i n e a r p o l i no m i a l a pp ro xi ma ti on t o t he
f u n c t i o n f ( x ) =x 33
4 / / l e t P ( x )=a0x+a15
6 / / h en ce I ( a 0 , a1 )= i n t e g r a l ( x 3( a0x+a1 ) ) 2 i n t hei n t e r v a l [ 0 , 1 ]
7
8 printf ( I =1/72(a0 /5+a1 /4)+a0 2/3+a 0a1+a12 )9 printf ( d I / d a 0 = 2/5+2/3a0+a1=0 )
1011 printf ( d I / d a 1 = 1/2+a0+2a1 =0 )12
13 / / h e nc e14
15 printf ( [ 2/ 3 1 ; 1 2 ] [ a 0 ; a 1 ] = [ 2 / 5 ; 1 / 2 ] )16
17 // s o l v i n g f o r a0 and a1 ;18
19 a 0 = 9 / 1 0 ;
20 a 1 = - 1 / 5 ;21 // h en ce c o n s i d e r i n g t he p o l i n om i al w i t h i n t e r c e p tP 1 ( x ) = ( 9x2 ) / 1 0 ;
22
23 // c o n s i d e r i n g t he p o l i n o m i a l a p pr ox im a ti o n t hr ou gh
68
-
7/26/2019 Numerical Methods for Scientific and Eng
70/152
-
7/26/2019 Numerical Methods for Scientific and Eng
71/152
28
29 printf ( d I / d c 0 = ( 2/ 3
c0
c1/2
c2 /2 )=0 )3031 printf ( dI /dc 1 =(2/5 c0/2c1/3c2 /4 ) =0 )32
33 printf ( dI /dc 2 =(2/7 c0/3c1/4c2 /5 ) =0 )34
35
36 / / h e nc e37
38 printf ( [ 1 1/2 1 / 2; 1 /2 1/3 1 /4 ; 1/ 3 1/4 1 / 5 ] [ c 0 ; c 1 ;c2 ]=[ 2 / 3 ; 2/5; 2/7] )
3940 // h en ce s o l v i n g f o r c0 , c1 and c 2 ;41
42
43 / / t h e f i r s t d e g r e e s q u a r e a p p r o x im a t i o n P ( x ) = (6+48x20x 2 ) / 3 5 ;
check AppendixAP 27for dependency:
straightlineapprox.sce
Scilab code Exa 4.34 least square straight fit
1 / / e xa mp le 4 . 3 42
3 // o bt ai n l e a s t s qu ar e s t r a i g h t l i n e f i t4
5 x = [. 2 .4 .6 .8 1] ;
6 f = [. 44 7 . 63 2 . 77 5 . 89 4 1 ];
78
9 [ P ] = s t r a i g h t l i n e a p p r o x ( x , f ) // c a l l o f t h ef u n c t i o n t o g et t h e d e s i r e d s o l u t i o n
70
-
7/26/2019 Numerical Methods for Scientific and Eng
72/152
check AppendixAP 26for dependency:
quadraticapprox.sci
Scilab code Exa 4.35 least square approximation
1 / / e xa mp le 4 . 3 52
3 / / o b ta i n l e a s t s qu ar e a pp ro xi ma ti on o f s ec on dd e g r e e ;
4 x =[ -2 -1 0 1 2];5 f = [15 1 1 3 19];
6
7 [ P ] = q u a d r a t i c a p p r o x ( x , f ) // c a l l o f t h ef u n c t i o n t o g et t h e d e s i r e d s o l u t i o n
Scilab code Exa 4.36 least square fit
1 / / e xa mp le 4 . 3 62 // method o f l e a s t s q u a r e s t o f i t t h e d a t a t o t h e
c u r v e P ( x ) =c 0X+c1 / s q u r t (X)3
4 x =[.2 .3 .5 1 2];
5 f =[16 14 11 6 3];
6
7 / / I ( c 0 , c 1 )= s um m at io n o f ( f ( x )( c0 X+c1 / sq r t (X) ) ) 28
9 / / h en ce on p a r c i a l l y d e r i v a t i n g t he summation ,
1011 n = length ( x ) ; m = length ( f ) ;
12 if m < > n then
13 error ( l i n r e g Ve ct or s x and f a re no t o f t h es am e l e n g t h . ) ;
71
-
7/26/2019 Numerical Methods for Scientific and Eng
73/152
14 abort ;
15 end ;16
17 s 1 = 0 ; / / s 1= s um ma ti on o f x ( i) f ( i )
18 s 2 = 0 ; / / s 2= s um ma ti on o f f ( i) / sq r t ( x ( i ) )
19 s 3 = 0 ;
20 for i = 1 : n
21 s 1 = s 1 + x ( i ) * f ( i ) ;
22 s 2 = s 2 + f ( i ) / sqrt ( x ( i ) ) ;
23 s 3 = s 3 + 1 / x ( i ) ;
24 end25
26 c0 = det ([s1 sum ( sqrt ( x ) ); s 2 s 3 ]) / det ([ sum ( x ^ 2 ) sum (
sqrt ( x ) ) ; sum ( sqrt ( x ) ) s 3 ])
27
28 c1 = det ([ sum ( x ^ 2) s 1 ;sum ( sqrt ( x ) ) s 2 ]) / det ([ sum ( x ^ 2 )
sum ( sqrt ( x ) ) ; sum ( sqrt ( x ) ) s 3 ])
29 X = poly (0 , X ) ;30 P = c 0 * X + c 1 / X ^ 1 / 2
31 // h e n ce c o n s i d e r i n g t he p o l i no m i a l P( x ) =7 .5 96 1X
1/2
1. 1836
X
Scilab code Exa 4.37 least square fit
1 / / e xa mp le 4 . 3 72 // method o f l e a s t s q u a r e s t o f i t t h e d a t a t o t h e
c u r v e P ( x ) =a%e(3 t )+b%e(2 t ) ;3
4 t = [. 1 .2 .3 . 4] ;5 f = [ .7 6 . 58 . 44 . 35 ];
6
7 / / I ( c 0 , c 1 )= s um m at io n o f ( f ( x )a%e(3 t )+b%e(2 t ) )
72
-
7/26/2019 Numerical Methods for Scientific and Eng
74/152
8
9 / / h en ce on p a r c i a l l y d e r i v a t i n g t he summation ,1011 n = length ( t ) ; m = length ( f ) ;
12 if m < > n then
13 error ( l i n r e g V ec to rs t and f a re no t o f t h es am e l e n g t h . ) ;
14 abort ;
15 end ;
16
17 s 1 = 0 ; / / s 1= s um ma ti on o f f ( i) %e(3 t ( i ) ) ;
18 s 2 = 0 ; / / s 2= s um ma ti on o f f ( i) %e(2 t ( i ) ) ;
19
20 for i = 1 : n
21 s 1 = s 1 + f ( i ) * % e ^ ( - 3 * t ( i ) ) ;
22 s 2 = s 2 + f ( i ) * % e ^ ( - 2 * t ( i ) ) ;
23
24 end
25
26 a = det ([s1 sum ( % e ^ ( - 5* t ) ) ; s 2 sum ( % e ^ ( - 4 * t ) ) ] ) / det ([
sum ( % e ^ ( - 6 * t ) ) sum ( % e ^ ( - 5 * t ) ) ; sum ( % e ^ ( - 5 * t ) ) sum
( % e ^ ( - 4 * t ) ) ] )
27
28 b = det ([ sum ( % e ^( - 6* t ) ) s 1 ; sum ( % e ^ ( - 5* t ) ) s 2 ] ) /det ([
sum ( % e ^ ( - 6 * t ) ) sum ( % e ^ ( - 5 * t ) ) ; sum ( % e ^ ( - 5 * t ) ) sum
( % e ^ ( - 4 * t ) ) ] )
29
30 // h en ce c o n s i d e r i n g t he p o l i no m i a l P( t ) =. 06 85 3%e(3 t ) +0. 305 8%e(2 t )
Scilab code Exa 4.38 gram schmidt orthogonalisation
1 / / e xa mp le 4 . 3 8
73
-
7/26/2019 Numerical Methods for Scientific and Eng
75/152
2 / / g ram s ch mi dt o r t h o g o n a l i s a t i o n
34 W = 1 ;
5 x = poly (0 , x ) ;6 P 0 = 1 ;
7 p h i 0 = P 0 ;
8 a 1 0 = i n t e g r a t e (Wx p h i 0 , x ,0,1)/ i n t e g r a t e (W1p h i 0 , x ,0,1)
9 P 1 = x - a 1 0 * p h i 0
10 p h i 1 = P 1 ;
11
12 a 2 0 = i n t e g r a t e (Wx 2p h i 0 , x ,0,1)/ i n t e g r a t e (
W1p h i 0 , x ,0,1)13
14 a 2 1 = i n t e g r a t e ( ( x 2 ) ( x1/2) , x ,0,1)/ i n t e g r a t e( ( x1/2)2 , x ,0,1)
15
16 P 2 = x ^ 2 - a 2 0 * x - a 2 1 * p h i 1
17
18 // s i n c e , I= i n t g r a l [ x ( 1 /2 )c0 P0c1 P1c 2 P 2 ] 2i n t h e r an ge [ 0 , 1 ]
19
20 // h e n c e p a r t i a l l y d e r i v a t i n g I21
22 c0 = i n t e g r a t e ( x ( 1 / 2 ) , x ,0,1)/ i n t e g r a t e ( 1 , x ,0,1)
23 c1 = i n t e g r a t e ( ( x ( 1 / 2 ) ) ( x (1/2 ) ) , x ,0,1)/i n t e g r a t e ( ( x(1/ 2) ) 2 , x ,0,1)
24 c1 = i n t e g r a t e ( ( x ( 1 / 2 ) ) ( x 24x/3+1/2) , x ,0,1)/i n t e g r a t e ( (x24x /3+ 1/2) 2 , x ,0,1)
Scilab code Exa 4.39 gram schmidt orthogonalisation
1 / / e xa mp le 4 . 3 92 / / g ram s ch mi dt o r t h o g o n a l i s a t i o n
74
-
7/26/2019 Numerical Methods for Scientific and Eng
76/152
3
4 / / 1 )5 W = 1 ;6 x = poly (0 , x ) ;7 P0=1
8 p h i 0 = P 0 ;
9 a10=0;
10 P 1 = x - a 1 0 * p h i 0
11 p h i 1 = P 1 ;
12
13 a 2 0 = i n t e g r a t e ( x 2 , x ,-1,1)/ i n t e g r a t e (W1p h i 0 , x ,-1,1);
1415 a 2 1 = i n t e g r a t e ( ( x 3 ) , x ,-1,1)/ i n t e g r a t e ( (x ) 2
, x ,-1,1);16
17 P 2 = x ^ 2 - a 2 0 * x - a 2 1 * p h i 1
18
19
20 / / 2 )21 disp ( W=1/(1x 2 ) ( 1 / 2 ) ) ;22 x = poly (0 , x ) ;23 P0=1
24 p h i 0 = P 0 ;
25 a10=0;
26 P 1 = x - a 1 0 * p h i 0
27 p h i 1 = P 1 ;
28
29 a 2 0 = i n t e g r a t e ( x 2/( 1x 2 ) ( 1 / 2 ) , x ,-1,1)/i n t e g r a t e ( 1/(1x 2 ) ( 1 / 2 ) , x ,-1,1);
30
31 a21=0; // s i n c e x 3 i san odd f u n c t i o n ;
3233 P 2 = x ^ 2 - a 2 0 * x - a 2 1 * p h i 1
75
-
7/26/2019 Numerical Methods for Scientific and Eng
77/152
Scilab code Exa 4.41 chebishev polinomial
1 / / e xa mp le 4 . 4 12 // u s in g c he by sh ev p o l i n o m i al s o b ta i n l e a s t s q ua r es
a p pr o x im a t io n o f s e co n d d e g r e e ;3
4 // t he c he be sh ev p o l i n o m i a l s ;5 x = poly (0 , x ) ;6 T 0 = 1 ;
7 T 1 = x ;
8 T 2 = 2 * x ^ 2 - 1 ;
9
10
11 / / I= i n t e g r a t e ( 1/ (1 x 2 ) ( 1 / 2 ) (x4c0 T0c 1 T1c2T2) 2 , x , 1 ,1 )
12
13 // s i n c e ;14 c0 = i n t e g r a t e ( ( 1 / 3 . 1 4 ) ( x 4 ) / ( 1x 2 ) ( 1 / 2 ) , x
,-1,1)
1516 c1 = i n t e g r a t e ( ( 2 / 3 . 1 4 ) ( x 5 ) / ( 1x 2 ) ( 1 / 2 ) , x
,-1,1)
17
18 c2 = i n t e g r a t e ( ( 2 / 3 . 1 4 ) ( x 4 ) ( 2 x 21)/(1x 2 ) ( 1 / 2 ) , x ,-1,1)
19
20 f = ( 3 / 8 ) * T 0 + ( 1 / 2 ) * T 2 ;
76
-
7/26/2019 Numerical Methods for Scientific and Eng
78/152
Chapter 5
DIFFERENTIATION AND
INTEGRATION
check AppendixAP 21for dependency:
linearinterpol.sci
Scilab code Exa 5.1 linear interpolation
1 / / e xa mp le : 5 . 12 // l i n e a r and q u a d ra t i c i n t e r p o l a t i o n :3
4 / / f ( x ) =l n x ;5
6 x L = [2 2 .2 2 .6 ];
7 f = [ . 6 9 31 5 . 7 8 84 6 . 9 5 55 1 ] ;
8
9 // 1 ) f p ( 2 ) w i t h l i n e a r i n t e r p o l a t i o n ;10
11 f p = l i n e a r i n t e r p o l ( x L , f ) ;12 disp ( f p ) ;
77
-
7/26/2019 Numerical Methods for Scientific and Eng
79/152
-
7/26/2019 Numerical Methods for Scientific and Eng
80/152
15 disp ( J ) ;
check AppendixAP 18for dependency:
simpson.sci
check AppendixAP 19for dependency:
trapezoidal.sci
Scilab code Exa 5.11solution by trapizoidal and simpsons
1 // exa mp le : 5 . 1 12 // s o l v e t h e d e f i n i t e i n t e g r a l by 1 ) t r a p e z o i d a l
r u l e , 2 ) s i mp s on s r u l e3 / / e xa ct v al ue o f t h e i n t e g r a l i s l n 2= 0 . 6 93 1 4 7 ,4
5 deff ( [ y]=F(x ) , y=1/(1+x ) )6
7 / / 1 ) t r a p e z o i d a l r u l e ,8
9 a = 0 ;10 b = 1 ;
11 I = t r a p e z oi d a l ( 0 ,1 , F )
12 disp ( error = . 7 5 - . 6 9 3 1 4 7 )
13
14 / / s im ps on s r u l e15
16 I = s i m p s o n ( a , b , F )
17
18 disp ( error = . 6 9 4 4 4 4 - . 6 9 3 1 4 7 )
Scilab code Exa 5.12 integral approximation by mid point and two point
79
-
7/26/2019 Numerical Methods for Scientific and Eng
81/152
-
7/26/2019 Numerical Methods for Scientific and Eng
82/152
10 [ I ] = s i mp so n 38 ( x , f )
Scilab code Exa 5.15 quadrature formula
1 / / exa mp le : 5 . 1 52 / / f i n d t he q ua dr at ur e f or mu la o f 3 / / i n t e g r a l o f f ( x ) ( 1 / s q r t ( x (1+ x ) ) ) i n t h e r a n g e
[ 0 , 1 ] = a1f (0 )+a2f ( 1 /2 ) +a3f (1)=I4 // h en ce f i n d i n t e g r a l 1/ s q r t ( xx 3 ) i n t he r an ge
[ 0 , 1 ]5
6 // making t he method e x ac t f o r p o l i n o m i a l s o f d e g re eu p to 2 ,
7 // I=I1=a1+a2+a38 // I=I2 = ( 1/2) a2+a39 // I=I3 = ( 1/4) a2+a3
10
11 / / A=[ a 1 a 2 a 3 ] 12
13 I1 = i n t e g r a t e ( 1 / s q r t ( x(1x ) ) , x ,0,1)
14 I2 = i n t e g r a t e ( x / s q r t ( x(1x ) ) , x ,0,1)15 I3 = i n t e g r a t e ( x 2 / s q r t ( x(1x ) ) , x ,0,1)16
17 / / h e n c e18 // [ 1 1 1 ; 0 1/2 1 ; 0 1/4 1 ]A=[ I 1 I 2 I 3 ] 19
20 A = inv ([ 1 1 1;0 1/2 1 ;0 1/4 1 ]) *[ I1 I2 I3 ]
21 / / I = ( 3 . 1 4 / 4 ) ( f (0 ) +2 f ( 1/ 2) + f ( 1) ) ;22
23 / / h ence , f o r s o l v i n g t h e i n t e g r a l 1/ s q r t ( xx 3 )
i n t he r an ge [ 0 , 1 ] = I2425 deff ( [ y]= f (x ) , y =1/ sq r t (1+ x ) ) ;26 I = ( 3 . 1 4 / 4 ) * [ 1 + 2 * sqrt ( 2 / 3 ) + sqrt ( 2 ) / 2 ]
81
-
7/26/2019 Numerical Methods for Scientific and Eng
83/152
Scilab code Exa 5.16 gauss legendary three point method
1 / / e xa mp le 5 . 1 62 // c a p t io n : g au ssl e g e n d r e t h r e e p o i n t method3 // I= i n t e g r a l 1/(1+ x ) i n t he r an ge [ 0 , 1 ] ;4 // f i r s t we ne ed t i t ra ns fo rm t he i n t e r v a l [ 0 , 1 ] t o
[ 1 , 1 ] , s i n c e g au ssl e g e n d r e t h r e e p o i n t methodi s a p p l i c a b l e i n t h e r a ng e [ 1 ,1] ,
5
6 / / l e t t=a x+b ;7 // s o l v i n g f o r a , b fr om t he two r an ge s , we g e t a =2;
b=1; t=2x1;8
9 / / h en ce I= i n t e g r a l 1/(1+ x ) i n t he r an ge [ 0 , 1 ] =i n t e g r a l 1 /( t +3) i n t he r an ge [ 1 , 1 ] ;
10
11
12 deff ( [ y ]= f ( t ) , y = 1/( t +3) ) ;13 / / s i n c e , from g au ss l e