100% found this document useful (1 vote)
750 views51 pages

Elementary Numerical Analysis - An Algorithmic Approach - S. D. Conte and C. de Boor

This document provides Scilab code examples that correspond to problems from the textbook "Elementary Numerical Analysis: An Algorithmic Approach" by S. D. Conte and C. de Boor. It contains code for topics like number systems, interpolation, root finding, matrices, approximation, differentiation, and differential equations. The codes are accompanied by descriptions linking them to the relevant textbook examples and sections.

Uploaded by

Andhy Diaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
750 views51 pages

Elementary Numerical Analysis - An Algorithmic Approach - S. D. Conte and C. de Boor

This document provides Scilab code examples that correspond to problems from the textbook "Elementary Numerical Analysis: An Algorithmic Approach" by S. D. Conte and C. de Boor. It contains code for topics like number systems, interpolation, root finding, matrices, approximation, differentiation, and differential equations. The codes are accompanied by descriptions linking them to the relevant textbook examples and sections.

Uploaded by

Andhy Diaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Contents: Lists the titles and starting pages of major chapters covering numerical analysis topics.
  • Chapter 1: Number systems and Errors: Discusses the numerical representation of number systems and associated errors, with Scilab code examples.
  • Chapter 2: Interpolation by polynomials: Covers polynomial interpolation methods using algorithms, illustrated with Scilab scripts.
  • Chapter 3: The solution of nonlinear equations: Features methods and solutions for nonlinear equations, with dependencies on appendix examples.
  • Chapter 4: Matrices and Systems of linear Equations: Provides methodologies for solving linear equations using matrix operations, supported by Scilab codes.
  • Chapter 5: Systems of equations and unconstrained optimization: Explores system equation solutions and optimization problems without constraints, with step-by-step examples.
  • Chapter 6: Approximation: Focuses on approximation techniques and errors involving numerical solutions.
  • Chapter 7: Differentiation and integration: Covers numerical differentiation and integration methods, applying Scilab for demonstrations.
  • Chapter 8: The solution of differential equations: Details differential equation solutions with examples and Scilab implementations.
  • Appendix: Provides additional Scilab code and supplementary materials for in-depth study of previous chapters.

Scilab Textbook Companion for

Elementary Numerical Analysis: An


Algorithmic Approach
by S. D. Conte And C. de Boor 1

Created by
Pravalika
B.Tech (pursuing)
Electronics Engineering
Visvesvaraya National Institute Of Technology
College Teacher
M. Devakar, VNIT Nagpur
Cross-Checked by
K. Suryanarayan and Prashant Dave, IITB

July 31, 2019

1 Funded by a grant from the National Mission on Education through ICT,


http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab
codes written in it can be downloaded from the ”Textbook Companion Project”
section at the website http://scilab.in
Book Description

Title: Elementary Numerical Analysis: An Algorithmic Approach

Author: S. D. Conte And C. de Boor

Publisher: McGraw - Hill Companies

Edition: 3

Year: 1980

ISBN: 70124477

1
Scilab numbering policy used in this document and the relation to the
above 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 particular


Example of the above book)

For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means
a scilab code whose theory is explained in Section 2.3 of the book.

2
Contents

List of Scilab Codes 4

1 Number systems and Errors 5

2 Interpolation by polynomials 6

3 The solution of nonlinear equations 11

4 Matrices and Systems of linear Equations 21

5 systems of equations and unconstraned optimization 28

6 Approximation 30

7 differentiation and integration 38

8 THE SOLUTION OF DIFFERENTIAL EQUATIONS 42

3
List of Scilab Codes

Exa 1.1 number system . . . . . . . . . . . . . . . . 5


Exa 2.1 shifted power form . . . . . . . . . . . . . . 6
Exa 2.2 second degree interpolating polynomial . . . 7
Exa 2.3 determine polynomial by newton formula . . 7
Exa 2.5 Newtons formula . . . . . . . . . . . . . . . 8
Exa 2.7 find N . . . . . . . . . . . . . . . . . . . . . 9
Exa 3.1 Root finding . . . . . . . . . . . . . . . . . . 11
Exa 3.2.a finding roots . . . . . . . . . . . . . . . . . . 12
Exa 3.3 Fixed point iteration . . . . . . . . . . . . . 13
Exa 3.4 Fixed point iteration . . . . . . . . . . . . . 14
Exa 3.5 Secant method . . . . . . . . . . . . . . . . 14
Exa 3.8.a Roots of a polynomial equation . . . . . . . 15
Exa 3.8 Polynomial equations real roots . . . . . . . 16
Exa 3.10 Roots of polynomial equation . . . . . . . . 17
Exa 3.11 Roots of polynomial equation . . . . . . . . 17
Exa 3.12 Roots of polynomial equation . . . . . . . . 18
Exa 3.14 Roots of a polynomial equation . . . . . . . 18
Exa 3.15 Roots of a polynomial equation . . . . . . . 18
Exa 3.16 Roots of polynomial equation . . . . . . . . 19
Exa 3.17.a Roots of a polynomial equation . . . . . . . 19
Exa 4.1 matrix multiplication . . . . . . . . . . . . . 21
Exa 4.2 matrix multiplication . . . . . . . . . . . . . 21
Exa 4.3 properties of matrices . . . . . . . . . . . . 22
Exa 4.4 matrix . . . . . . . . . . . . . . . . . . . . . 23
Exa 4.6 determinant . . . . . . . . . . . . . . . . . . 23
Exa 4.7 matrix . . . . . . . . . . . . . . . . . . . . . 23
Exa 4.8 Backward substitution . . . . . . . . . . . . 24
Exa 4.14 norm . . . . . . . . . . . . . . . . . . . . . . 25

4
Exa 4.16 determinant . . . . . . . . . . . . . . . . . . 25
Exa 4.17 determinant . . . . . . . . . . . . . . . . . . 25
Exa 4.18 determinant . . . . . . . . . . . . . . . . . . 26
Exa 4.19 Eigen values . . . . . . . . . . . . . . . . . . 26
Exa 4.20 Eigen values . . . . . . . . . . . . . . . . . . 26
Exa 4.21 determinant . . . . . . . . . . . . . . . . . . 27
Exa 4.22 eigen values . . . . . . . . . . . . . . . . . . 27
Exa 5.1 gradient . . . . . . . . . . . . . . . . . . . . 28
Exa 5.2 Steep descent . . . . . . . . . . . . . . . . . 29
Exa 6.1 uniform aproximation . . . . . . . . . . . . 30
Exa 6.2 distance at infinity . . . . . . . . . . . . . . 30
Exa 6.3 aproximation . . . . . . . . . . . . . . . . . 31
Exa 6.5 approximate . . . . . . . . . . . . . . . . . . 33
Exa 6.11 polynomial of degree lessthan 3 . . . . . . . 34
Exa 6.12 Least squares approximation . . . . . . . . . 35
Exa 7.1 integration . . . . . . . . . . . . . . . . . . . 38
Exa 7.8 adaptive quadrature . . . . . . . . . . . . . 38
Exa 8.1 Taylor series . . . . . . . . . . . . . . . . . . 42
Exa 8.5 Adamsbashforth3 . . . . . . . . . . . . . . . 43
Exa 8.6 Modified euler method . . . . . . . . . . . . 43

5
Chapter 1

Number systems and Errors

Scilab code Exa 1.1 number system

1
2 // Example ( pg no . 2 0 )
3
4 // t h e number %pi /4 i s t h e v a l u e o f i n f i n i t e
series
5 // sum ( ( ( − 1 ) ˆ i ) / ( 2 ∗ i +1) )=1−sum ( 2 / ( 1 6 ∗ ( j ) ˆ 2 ) −1)
6 // The s e q u e n c e a l p h a 1 , a l p h a 2 , . . . . . . . . i s monotone−
decreasing
7 // t o i t s l i m i t %pi /4
8 // 0< =( a l p h a ) n − %pi /4 <= ( 1 / ( 4 ∗ n+3) ) n
=1 ,2 ,.....
9 // To c a l c u l a t e %pi /4 c o r r e c t t o w i t h i n 10ˆ( −6)
using t h i s sequence
10 // we would n e e d 1 0 ˆ ( 6 ) <=4∗n+3
11 n =(10^(6) -3) /4
12 // r o u g h l y n =250 ,000

6
Chapter 2

Interpolation by polynomials

Scilab code Exa 2.1 shifted power form

1 // Example 2 . 1
2
3 // Given p ( 6 0 0 0 ) =1/3 , p ( 6 0 0 1 ) =−2/3
4 // From p ( x )=a0+a1 ∗ x , by s u b s t i t u t i n g x =6000 & x
=6001
5 // we g e t e q u a t i o n s a0+a1 ∗ ( 6 0 0 0 ) =1/3 & a0+a1 ∗ ( 6 0 0 1 )
=−2/3
6 // s o l v i n g t h e a b o v e e q u a t i o n s we g e t
7 a0 =6000.3
8 a1 = -1
9 deff ( ’ [ y ]= f ( x ) ’ , ’ y =6000.3 − x ’ )
10 f (6000)
11 f (6001)
12 // y =6000.3 − x , e q u a t i o n r e c o v e r s o n l y o n l y t h e
f i r s t d i g i t of the
13 // g i v e n f u n c t i o n v a l u e s , a l o s s o f f o u r d e c i m a l
digits
14 // remedy f o r s u c h l o s s o f s i g n i f i c a n c e i s t h e u s e
o f SHIFTED POWER FORM
15 // p ( x )=a0 + a1 ∗ ( x−c ) + a2 ∗ ( x−c ) ˆ2 + . . . . . . . + an ∗ ( x−
c ) ˆn

7
16 // i f we c h o o s e t h e c e n t e r c t o be 6 0 0 0
17
18 deff ( ’ [ y ]= p ( x ) ’ , ’ y =0.33333 −( x −6000) ’ )
19 p (6000)
20 p (6001)

Scilab code Exa 2.2 second degree interpolating polynomial

1 // Example 2 . 2
2
3 K (1) =1.5709
4 K (4) =1.5727
5 K (6) =1.5751
6 l0 (3.5) =[(3.5 -4) *(3.5 -6) ]/[(1 -4) *(1 -6) ]
7 l1 (3.5) =[(3.5 -1) *(3.5 -6) ]/[(4 -1) *(4 -6) ]
8 l2 (3.5) =[(3.5 -1) *(3.5 -4) ]/[(6 -1) *(6 -4) ]
9 K (3.5) = l0 (3.5) * K (1) + l1 (3.5) * K (4) + l2 (3.5) * K (6) ;
10 K (3.5)

Scilab code Exa 2.3 determine polynomial by newton formula

1 // Example 2 . 3
2 // U s i n g Newton f o r m u l a
3
4 x0 =1
5 x1 =4
6 x2 =6
7 P2 (1) =1.5709
8 P2 (4) =1.5727
9 P2 (6) =1.5751
10 K1 =[ P2 (1) - P2 (4) ]/(1 -4)
11 K2 =[ P2 (4) - P2 (6) ]/(4 -6)
12 K3 ={ K1 - K2 }/(1 -6)

8
13 // Where a s K1 = K [ 1 , 4 ] , K2 = [ 4 , 6 ] , K3 =
[1 ,4 ,6]
14 deff ( ’ [ y ]= f ( x ) ’ , ’ y=P2 ( x0 )+K1 ∗ ( x−x0 )+K3 ∗ (
x−x0 ) ∗ ( x−x1 ) ’ )
15 funcprot (0)
16 x = poly (0 , ” x ” )
17 y = P2 ( x0 ) + K1 *( x - x0 ) + K3 *( x - x0 ) *( x - x1 )
18 x =3.5
19 f

Scilab code Exa 2.5 Newtons formula

1 // Example 2 . 5
2
3 x0 =1
4 x1 =4
5 x2 =6
6 x3 =0
7 x4 =3.5
8 K (1) =1.5709
9 K (4) =1.5727
10 K (6) =1.5751
11 P2 (1) =1.5709
12 P2 (4) =1.5727
13 P2 (6) =1.5751
14
15 p0 = K (1)
16 U0 =1 //U0=U0 ( x ’ )
17 K1 =[ P2 (1) - P2 (4) ]/(1 -4)
18 // Where a s K1 = K [ 1 , 4 ]
19 U1 =( x4 - x0 ) * U0 //U1=U1 ( x ’ )
20 p1 = p0 + U1 * K1 // p1=p1 ( x ’ )
21
22 // a d d i n g t h e p o i n t x2=6
23 K2 =[ P2 (4) - P2 (6) ]/(4 -6)

9
24 // Where a s K2 =K [ 4 , 6 ]
25
26 K3 ={ K1 - K2 }/(1 -6)
27 // Where a s K1 = K [ 1 , 4 ] , K2 =K [ 4 , 6 ] , K3 = K
[1 ,4 ,6]
28 U2 =( x4 - x0 ) *( x4 - x1 ) //U2=U2 ( x ’ )
29 p2 = p1 + U2 * K3 // p2=p2 ( x ’ )
30
31 // t o c h e c k e r r o r a p p r o x i m a t i o n f o r k ( 3 . 5 ) we add
p o i n t x3=0
32 // K( 0 ) =1.5708= a
33 // p2 ( 0 ) =1.5708=K( 0 )
34 a =1.5708
35 K4 =[ P2 (6) -a ]/(6 -0)
36 //K4=K [ x2 , x3 ] = [ 6 , 0 ]
37 K5 = -0.000001
38 //K5=K [ x0 , x1 , x2 , x3 ]
39 U3 =( x4 - x2 ) *( x4 - x1 ) *( x4 - x0 ) //U3=U3 ( x ’ )
40
41 p3 = p2 + U3 * K5
42 // p3=p3 ( x ’ )

Scilab code Exa 2.7 find N

1
2 // Example 2 . 7
3
4 deff ( ’ [ y ]= f ( x ) ’ , ’ y=(x ) ˆ ( 1 / 2 ) ’ )
5 x0 =1
6 x1 =2
7
8 // a b s ( f ( x ’ )−p2 ( x ’ ) ) <= ( ( x1−x0 ) ˆ 2 ) ∗M/8
9 // a b s ( f ( x ’ )−p2 ( x ’ ) ) <= 2 ∗ ( ( h ) ˆ 3 ) / ( 3 ∗ ( 3 ) ˆ ( 1 / 3 ) )
10
11 h =(5*((10) ^( -8) ) *24*((3) ^(1/2) ) ) ^(1/3)

10
12
13 // h i s a p p r o x i m a t e l y 0 . 0 1 2 8
14 // h=(x1−x0 ) /N
15
16 N ={( x1 - x0 ) / h }
17 //N i s a p r o x i m a t e l y 79

11
Chapter 3

The solution of nonlinear


equations

check Appendix AP 4 for dependency:

bisect.sce

check Appendix AP 6 for dependency:

newt.sce

check Appendix AP 7 for dependency:

regulfalsi.sce

check Appendix AP 3 for dependency:

secantm.sce

Scilab code Exa 3.1 Root finding

1 // Example ( 3 . 1 )
2
3 // f i n d i n g r o o t s u s i n g b i s e c t i o n method
4
5 deff ( ’ [ y ]= f ( x ) ’ , ’ y=x −0.2∗ s i n ( x ) −0.5 ’ )

12
6 bisection (0.5 ,1.0 , f )
7
8
9 // r e g u l a f a l s i method
10
11 deff ( ’ [ y ]= f ( x ) ’ , ’ y=x −0.2∗ s i n ( x ) −0.5 ’ )
12 regularfalsi (0.5 ,1.0 , f )
13
14 // s e c a n t method
15
16 deff ( ’ [ y ]= f ( x ) ’ , ’ y=x −0.2∗ s i n ( x ) −0.5 ’ )
17 secant (0.5 ,1.0 , f )
18
19
20 // newton r a p s o n method
21
22
23 x =(0.5+1) /2
24 deff ( ’ [ y ]= f ( x ) ’ , ’ y=x −0.2∗ s i n ( x ) −0.5 ’ )
25 deff ( ’ [ y ]= g ( x ) ’ , ’ y =1 −0.2∗ c o s ( x ) ’ )
26 x = newton (x ,f , g )

check Appendix AP 4 for dependency:

bisect.sce

Scilab code Exa 3.2.a finding roots

1 // // Example ( 3 . 2 a )
2 //
3 // // f i n d i n g r o o t s u s i n g b i s e c t i o n method
4 //
5 //
6 // d e f f ( ’ [ y ]= f ( x ) ’ , ’ y=xˆ3−x −1 ’)
7 // bisection (0 ,10 , f ) ;
8 //

13
9 //
10 // // r e g u l a f a l s i method
11 //
12 // d e f f ( ’ [ y ]= f ( x ) ’ , ’ y=xˆ3−x −1 ’)
13 // r e g u l a r f a l s i ( 0 , 1 0 , f ) ;
14 //
15 //
16 //
17 // newton r a p s o n method
18 //
19 // x =(0+10) /2
20 // d e f f ( ’ [ y ]= f ( x ) ’ , ’ xˆ3−x −1 ’)
21 // d e f f ( ’ [ y ]= g ( x ) ’ , ’ y=2∗x ˆ2 −1 ’)
22 // x=newton ( x , f , g ) ;

check Appendix AP 5 for dependency:

fixedp.sce

check Appendix AP 6 for dependency:

newt.sce

check Appendix AP 7 for dependency:

regulfalsi.sce

Scilab code Exa 3.3 Fixed point iteration

1 // e x a m p l e ( 3 . 3 )
2
3 // h e r e f ( x )=e ˆ(−x )− s i n ( x ) , a c c o r d i n g
t o f i x e d p o i n t i t e r a t i o n we t a k e
g ( x )=x=x+e ˆ(−x )− s i n ( x ) ;
4 // so , xn=g ( xn )
5 deff ( ’ [ y ]= g ( x ) ’ , ’ y=x + ( 2 . 7 1 8 ) ˆ(−x )− s i n ( x ) ’ )
6 x =0.6

14
7 for n =1:1:18
8 g(x);
9 x=g(x)
10 end

Scilab code Exa 3.4 Fixed point iteration

1 // e x a m p l e ( 3 . 4 )
2
3 // h e r e f ( x ) =1.5∗ x−t a n ( x ) −0.1=0 , a c c o r d i n g t o
f i x e d p o i n t i t e r a t i o n we g e t x =(0.1+ t a n ( x ) )
/1.5
4 // where g ( x )=x =(0.1+ t a n ( x ) ) / 1 . 5
5 //& xn=g ( xn )
6 deff ( ’ [ y ]= g ( x ) ’ , ’ y =(0.1+ t a n ( x ) ) / 1 . 5 ’ )
7 x =0
8
9 for n =1:1:10
10 g(x);
11 x=g(x)
12 end

check Appendix AP 3 for dependency:

secantm.sce

Scilab code Exa 3.5 Secant method

1 // e x a m p l e ( 3 . 5 )
2
3 deff ( ’ [ y ]= f ( x ) ’ , ’ y=xˆ3−x−1 ’ )
4 secant (1.0 ,1.5 , f )

15
Scilab code Exa 3.8.a Roots of a polynomial equation

1 // e x a m p l e ( pg no . 1 1 1 )
2
3
4 // a , b & f a r e t h e modulus c o e f f o f x ˆ 0 , x ˆ 1 , x ˆ5
5 c =[ -6.8 10.8 -10.8 7.4 -3.7 1]
6 a =6.8;
7 b =10.8;
8 f =1;
9 n =5
10 p5 = poly (c , ’ x ’ , ’ c o e f f ’ )
11 p=n*a/b
12 q = a / f ^(1/ n )
13 roots ( p4 )
14
15 xset ( ’ window ’ ,0) ;
16 x = -2:.01:2.5;
//
d e f i n i n g the range of x .
17 deff ( ’ [ y ]= f ( x ) ’ , ’ y=x ˆ5 −3.7∗ x ˆ 4 + 7 . 4 ∗ x ˆ3 −10.8∗ x
ˆ 2 + 1 0 . 8 ∗ x −6.8 ’ ) ; // d e f i n i n g t h e
cunction
18 y = feval (x , f ) ;
19
20 a = gca () ;
21
22 a . y_location = ” o r i g i n ” ;
23
24 a . x_location = ” o r i g i n ” ;
25 plot (x , y )

// i n s t r u c t i o n t o p l o t t h e g r a p h
26

16
27 title ( ’ y = 8∗ x ˆ3 −12∗ xˆ2−2∗x+3 ’ )

Scilab code Exa 3.8 Polynomial equations real roots

1 // e x a m p l e ( 3 . 8 )
2
3
4
5 // a , b & e a r e t h e modulus c o e f f o f x ˆ 0 , x ˆ 1 , x ˆ4
6 c =[ -1 1 -1 -1 1]
7 a =1;
8 b =1;
9 e =1;
10 n =4
11 p4 = poly (c , ’ x ’ , ’ c o e f f ’ )
12 p=n*a/b
13 q =( a / e ) ^(1/ n )
14 roots ( p4 )
15 // from h e r e we f o u n d t h a t o n l y 2 r e a l r o o t s ,
o t h e r two a r e c o m p l e x r o o t s
16 xset ( ’ window ’ ,0) ;
17 x = -2:0.1:3;
//
d e f i n i n g the range of x .
18 deff ( ’ [ y ]= f ( x ) ’ , ’ y=xˆ4−xˆ3−xˆ2+x−1 ’ ) ;
// d e f i n i n g t h e f u n c t i o n
19 y = feval (x , f ) ;
20
21 a = gca () ;
22
23 a . y_location = ” o r i g i n ” ;
24
25 a . x_location = ” o r i g i n ” ;
26 plot (x , y )

17
// i n s t r u c t i o n t o p l o t t h e g r a p h
27
28 title ( ’ y =xˆ4−xˆ3−xˆ2+x−1 ’ )

Scilab code Exa 3.10 Roots of polynomial equation

1 // e x a m p l e ( 3 . 1 0 )
2
3 c =[ -3 1 0 1]
4 p3 = poly (c , ’ x ’ , ’ c o e f f ’ )
5 roots ( p3 )
6 // h e r e
7 xset ( ’ window ’ ,0) ;
8 x = -2:.01:2.5;
//
d e f i n i n g the range of x .
9 deff ( ’ [ y ]= f ( x ) ’ , ’ y=xˆ3+x−3 ’ ) ; //
d e f i n i n g the cunction
10 y = feval (x , f ) ;
11
12 a = gca () ;
13
14 a . y_location = ” o r i g i n ” ;
15
16 a . x_location = ” 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 h e g r a p h
18
19 title ( ’ y =xˆ3+x−3 ’ )

Scilab code Exa 3.11 Roots of polynomial equation

18
1 // e x a m p l e ( 3 . 1 1 )
2 c =[ -6.8 10.8 -10.8 7.4 -3.7 1]
3 p5 = poly (c , ’ y ’ , ’ c o e f f ’ )
4 roots ( p5 )

Scilab code Exa 3.12 Roots of polynomial equation

1 // e x a m p l e ( 3 . 1 2 )
2
3 c =[ -5040 13068 -13132 6769 -1960 322 -28 1]
4 p7 = poly (c , ’ y ’ , ’ c o e f f ’ )
5 roots ( p7 )

Scilab code Exa 3.14 Roots of a polynomial equation

1 // e x a m p l e ( 3 . 1 4 )
2
3 c =[ -3 1 0 1]
4 p3 = poly (c , ’ y ’ , ’ c o e f f ’ )
5 roots ( p3 )

Scilab code Exa 3.15 Roots of a polynomial equation

1 // e x a m p l e ( 3 . 1 5 )
2
3 c =[ -6.8 10.8 -10.8 7.4 -3.7 1]
4 p5 = poly (c , ’ x ’ , ’ c o e f f ’ )
5 roots ( p5 )

19
Scilab code Exa 3.16 Roots of polynomial equation

1 // e x a m p l e ( 3 . 1 6 )
2
3 c =[ -5040 13068 -13132 6769 -1960 322 -28 1]
4 p7 = poly (c , ’ x ’ , ’ c o e f f ’ )
5 roots ( p7 )

Scilab code Exa 3.17.a Roots of a polynomial equation

1 // e x a m p l e ( 3 . 1 7 )
2
3 c =[51200 0 -39712 0 7392 0 -170 0 1 ]
4 p8 = poly (c , ’ x ’ , ’ c o e f f ’ )
5 roots ( p8 )
6
7 xset ( ’ window ’ ,0) ;
8 x = -11:01:11;
//
d e f i n i n g the range of x .
9 deff ( ’ [ y ]= f ( x ) ’ , ’ y=x ˆ8 −170∗ x ˆ6+7392∗ x ˆ4 −39712∗ x
ˆ2+51200 ’ ) ; // d e f i n i n g t h e
cunction
10 y = feval (x , f ) ;
11
12 a = gca () ;
13
14 a . y_location = ” o r i g i n ” ;
15
16 a . x_location = ” o r i g i n ” ;
17 plot (x , y )

20
// i n s t r u c t i o n t o p l o t t h e g r a p h
18
19 title ( ’ y =x ˆ8 −170∗ x ˆ6+7392∗ x ˆ4 −39712∗ x ˆ2+51200 ’ )

21
Chapter 4

Matrices and Systems of linear


Equations

Scilab code Exa 4.1 matrix multiplication

1
2 // Example ( pg no . 1 3 0 )
3
4 A =[3 0 2;1 2 0;0 1 1]
5 B =[2 1;0 1;1 0]
6 A*B

Scilab code Exa 4.2 matrix multiplication

1 // Example ( pg no . 1 3 0 )
2
3
4 A =[2 1;1 3]
5 B =[2 1;0 1]
6 A*B
7 B*A

22
8 // m a t r i x m u l t i p l i c a t i o n i s n o t c o m m u t a t i v e
9 // s o A∗B!=B∗A

Scilab code Exa 4.3 properties of matrices

1 // Example ( pg no . 1 3 3 )
2
3 A =[1 1;0 1]
4 inv ( A )
5 B =[1 0;1 1]
6 inv ( B )
7 A*B
8 inv ( A * B )
9 inv ( A ) * inv ( B )
10 // i n v (A∗B)=
i n v (B) ∗ i n v
(A)
11 inv ( B ) * inv ( A )
12 // Hence i n v
(A) ∗ i n v (
B) = i n v
(A) ∗ i n v (
B)
13 I = eye (3 ,3)
14 C =( A * B ) *( inv ( A ) * inv ( B ) )
15 //C!= I
16 // so , i n v (A
) ∗ i n v (B)
cannot
be t h e
inverse
o f (A∗B)

23
Scilab code Exa 4.4 matrix

1
2 // Example ( pg no . 1 3 6 )
3
4 // x1 + 2 ( x2 ) = 3
5 // 2 ( x1 ) + 4 ( x2 ) = 6
6
7 A =[1 2;2 4]
8 // c o e f f i c i e n t m a t r i x o f a b o v e e q u a t i o n s
9 b =[3 6] ’
10 x=A\b
11 // f o r c o r r e s p o n d i n g homogenous s y s t e m
12 // x1 + 2 ( x2 ) = 0
13 // 2 ( x1 ) + 4 ( x2 ) = 0
14 A =[1 2;2 4]
15 // c o e f f i c i e n t m a t r i x o f a b o v e e q u a t i o n s
16 b =[0 0] ’
17 x=A\b

Scilab code Exa 4.6 determinant

1 // Example ( pg no . 1 4 0 )
2
3 A =[1 2;2 4]
4 det ( A )
5 // Here A i s a s i n g u l a r
m a t r i x i . e , d e t (A) =0
6 // i n v (A) =( a d j (A) ) / d e t (A)
7 // s o A i s n o t i n v e r t i b l e

Scilab code Exa 4.7 matrix

24
1 // Example ( pg no . 1 4 4 )
2
3 A = [1 2 3;4 5 6;7 8 9]
4 [L ,U , E ] = lu ( A )
5 // L i s l o w e r t r i a n g u l a r m a t r i x ( mxn )
6 // U i s u p p e r t r i a n g u l a r m a t r i x ( mxmin (m, n ) )
7 // E i s p e r m u t a t i o n m a t r i x ( min (m, n ) xn )
8 A*E

Scilab code Exa 4.8 Backward substitution

1 // e x a m p l e 4 . 1 ( pg 1 4 9 )
2
3 // 2 x1 + 3 x2 − x3 = 5
4 //−2x2 − x3 = −7
5 //−5x3 = −15
6
7 A = [2 3 -1;0 -2 -1;0 0 -5]
8 b = [5 -7 -15] ’
9 a =[ A b ]
10 [ nA , mA ]= size ( A )
11 n = nA
12
13 // Backward s u b s t i t u t i o n
14
15 x (3) = a (n , n +1) / a (n , n ) ;
16
17 for i = n -1: -1:1
18 sumk =0;
19 for k = i +1: n
20 sumk = sumk + a (i , k ) * x ( k ) ;
21 end
22 x ( i ) =( a (i , n +1) - sumk ) / a (i , i ) ;
23 end
24 disp ( x )

25
Scilab code Exa 4.14 norm

1 // e x a m p l e ( 3 . 1 4 )
2
3 c =[ -3 1 0 1]
4 p3 = poly (c , ’ y ’ , ’ c o e f f ’ )
5 roots ( p3 )

Scilab code Exa 4.16 determinant

1 // Example ( pg no . 1 8 6 )
2
3
4 // ( 1 )
5 A =[1 4;2 3]
6 det ( A )
7
8 // ( 2 )
9 A =[4 1;3 2]
10 det ( A )

Scilab code Exa 4.17 determinant

1 // Example ( pg no . 1 8 6 )
2
3 A =[3.1 4;3.2 3]
4 det ( A )

26
Scilab code Exa 4.18 determinant

1 // Example ( pg no . 1 8 6 )
2
3 A =[1 2;2 2]
4 B =[1 2;1 1]
5 det ( A ) + det ( B )
6 C =[1 2; 3 3]
7 det ( C )
8 // d e t (A)+d e t (B)=d e t (C)

Scilab code Exa 4.19 Eigen values

1 // Example ( 4 . 1 1 ) ( pg no . 1 9 1 )
2
3 B =[1 2 0;2 1 0;0 0 -1]
4 lam = spec ( B )

Scilab code Exa 4.20 Eigen values

1 // Example ( 4 . 1 4 ) ( pg no . 2 0 1 )
2
3
4 B =[1 2 0;2 1 0;0 0 -1]
5 lam = spec ( B )
6 norm ( B )
7 // Each e i g e n v a l u e o f t h e m a t r i x must have
absolute value
8 // no b i g g e r t h a n t h e norm o f t h a t
matrix

27
Scilab code Exa 4.21 determinant

1 // Example ( 4 . 1 5 ) ( pgno 2 0 2 )
2
3 B =[1 2 0;2 1 0;0 0 -1]
4 I =[1 0 0;0 1 0;0 0 1]
5 // h e r e we a r e t a k i n g lamda=a
6 // d e t (B−a ) ∗ I =0 i s c h a r a c t e r i s t i c equation
t o g e t lamda
7 deff ( ’ [ y ]= pa ’ , ’ pa=d e t (B−a ) ∗ I ’ )
8 pa =0

Scilab code Exa 4.22 eigen values

1 // Example ( 4 . 1 6 ) ( p a g e no . 2 0 3 )
2
3 A =[4 -1 -1 -1; -1 4 -1 -1; -1 -1 4 -1; -1 -1 -1 4]
4 spec ( A )

28
Chapter 5

systems of equations and


unconstraned optimization

Scilab code Exa 5.1 gradient

1
2 // Example 5 . 1
3
4 deff ( ’ y=f ( x ) ’ , ’ y =(( x1 ) ˆ 3 ) +(( x2 ) ˆ 3 ) −2∗(( x1 ) ˆ 2 ) +3∗(( x2
) ˆ 2 ) −8 ’ )
5 funcprot (0)
6 deff ( ’ y=g ( x ) ’ , ’ y =3∗(( x1 ) ˆ 2 ) −4∗( x1 ) +3∗(( x2 ) ˆ 2 ) +6∗( x2 )
’)
7 // f 1 =( d f / dx1 ) ( x ) =0 , f 2 =( d f / dx2 ) ( x ) =0
8 deff ( ’ y=f p ( x ) ’ , ’ y =3∗(( x1 ) ˆ 2 ) −4∗( x1 ) ’ )
9 deff ( ’ y=f p p ( x ) ’ , ’ y =3∗(( x2 ) ˆ 2 ) +6∗( x2 ) ’ )
10 x1 = poly (0 , ” x1 ” )
11 fp =(3*(( x1 ) ^2) -4*( x1 ) )
12 p = roots ( fp )
13
14 x2 = poly (0 , ” x12 ” )
15 fpp =3*(( x2 ) ^2) +6*( x2 )
16 p = roots ( fpp )

29
Scilab code Exa 5.2 Steep descent

1
2 // Example 5 . 2
3
4 deff ( ’ [ y ]= f ( x1 , x2 ) ’ , ’ y =(( x1 ) ˆ 3 ) +(( x2 ) ˆ 3 ) −2∗(( x1 ) ˆ 2 )
+3∗(( x2 ) ˆ 2 ) −8 ’ )
5 // x1=1 , x2=−1
6 // ( d e l ) f (X( 0 ) ) = [ 3 ∗ ( ( x1 ) ˆ 2 ) −4∗x1 , 3 ∗ ( ( x2 ) ˆ 2 ) +6∗ x2
] ’=[ −1 , −3] ’
7 // Thus , i n t h e f i r s t s t e p o f s t e e p d e s c e n t ,
8 // we l o o k f o r a minimum o f t h e f u n c t i o n
9 funcprot (0)
10 deff ( ’ [ y ]= g ( t ) ’ , ’ y =((1+ t ) ˆ 3 ) +((−1+3∗ t ) ˆ 3 ) −2∗((1+ t )
ˆ 2 ) +3∗(( −1+3∗ t ) ˆ 2 ) −8 ’ )
11 // g ’ ( t ) =3∗((1+ t ) ˆ 2 ) +3∗3∗(( −1+3∗ t ) ˆ 2 ) −4∗(1+ t )
+3∗2∗( −1+3∗ t )
12 t = poly (0 , ” t ” )
13 y =3*((1+ t ) ^2) +3*3*(( -1+3* t ) ^2) -4*(1+ t ) +3*2*3*( -1+3* t
)
14 p = roots ( y )
15 // We c h o o s e t h e p o s i t i v e r o o t t =1/3
16 t =1/3;
17 x1 =1+ t
18 x2 = -1+3* t
19 a =3*(( x1 ) ^2) -4* x1
20 b =3*(( x2 ) ^2) +6* x2
21 funcprot (0)
22 deff ( ’ [ y ]= f p ( x1 ) ’ , ’ y = ( 3 ∗ ( ( x1 ) ˆ 2 ) −4∗( x1 ) ) ’ )
23
24 x1 = poly (0 , ” x1 ” )
25 fp =(3*(( x1 ) ^2) -4*( x1 ) )
26 p = roots ( fp )

30
Chapter 6

Approximation

Scilab code Exa 6.1 uniform aproximation

1 // Example 6 . 1
2
3 deff ( ’ [ y ]= f ( x ) ’ , ’ y=exp ( x ) ’ )
4 x0 = -1
5 x1 =0
6 x2 =1
7 // F=f ( x0 , x1 , x2 )=f ( − 1 , 0 , 1 )
8 F = f ( x0 ) /[( x1 - x0 ) *( x2 - x0 ) ]+ f ( x1 ) /[( x0 - x1 ) *( x2 - x1 ) ]+ f (
x2 ) /[( x0 - x2 ) *( x1 - x2 ) ]
9 // W( − 1 , 0 , 1 ) =2 and so , f o r a<= −1 ,1 <=b
10 // | f [ − 1 , 0 , 1 ] | / 2 <= d i s t ( a t i n f i n i t y ) ( f , p i 1 ) ∗∗∗∗∗
11 // d i s t ( exp ( x ) , p i 1 ) >= 0 . 2 7 1 5 4

Scilab code Exa 6.2 distance at infinity

1 // Example 6 . 2
2
3 deff ( ’ [ y ]= f ( x ) ’ , ’ y =t a n ( ( %pi / 4 ) ∗ x ) ’ )

31
4
5 // on s t d i n t e r v a l −1 <= x <= 1 from p i 3
6 // t h i s i s an odd f u n c t i o n f (−x )=f ( x )
7 n =4
8 p = (1/(2*( n +1) ) ) *( f (1) -2* f ( cos ( %pi /( n +1) ) ) +2* f ( cos
(2* %pi /( n +1) ) ) -2* f ( cos (3* %pi /( n +1) ) ) +2* f ( cos (4*
%pi /( n +1) ) ) -f ( -1) )
9 // 0 . 0 0 2 0 3 <= d i s t ( a t i n f i n i t y ) ( f , p i 4 )=p = 0 . 0 0 4 1

Scilab code Exa 6.3 aproximation

1 // Example 6 . 3
2
3 deff ( ’ [ y ]= f ( x ) ’ , ’ y=exp ( x ) ’ )
4 xset ( ’ window ’ ,0) ;
5 x = -1:.01:1; // d e f i n i n g t h e r a n g e o f
x.
6 y = feval (x , f ) ;
7
8 a = gca () ;
9
10 a . y_location = ” o r i g i n ” ;
11
12 a . x_location = ” o r i g i n ” ;
13 plot (x , y ) // i n s t r u c t i o n to plot the
graph
14
15
16
17 // p o s s i b l e approximation
18 // y = q1 ( x )
19
20 // L e t e ( x ) = exp ( x ) − [ a0+a1 ∗ x ]
21 // q1 ( x ) & exp ( x ) must be e q u a l a t two p o i n t s i n
[ − 1 , 1 ] , s a y a t x1 & x2

32
22 // s i g m a 1 = max ( a b s ( e ( x ) ) )
23 // e ( x1 ) = e ( x2 ) = 0 .
24 // By a n o t h e r argument b a s e d on s h i f t i n g t h e
g r a p h o f y = q1 ( x ) ,
25 // we c o n c l u d e t h a t t h e maximum e r r o r s i g m a 1 i s
attained at e x a c t l y 3 points .
26 // e ( −1) = s i g m a 1
27 // e ( 1 ) = sigma1
28 // e ( x3 ) = −s i g m a 1
29 // x1 < x3 < x2
30 // S i n c e e ( x ) h a s a r e l a t i v e minimum a t x3 , we
have e ’ ( x ) = 0
31 // Combining t h e s e 4 e q u a t i o n s , we have . .
32 // exp ( −1) − [ a0−a1 ] = s i g m a 1 −−−−−−−−−−−−−−−−−−(
i)
33 // exp ( 1 ) − [ a0+a1 ] = p1 −−−−−−−−−−−−−−−−−−−−−−−(
ii )
34 // exp ( x3 ) − [ a0+a1 ∗ x3 ] = −s i g m a 1 −−−−−−−−−−−−−−(
iii )
35 // exp ( x3 ) − a1 = 0 −−−−−−−−−−−−−−−−−−−−−−−−−−−−(
iv )
36
37 // These have t h e s o l u t i o n
38
39 a1 = ( exp (1) - exp ( -1) ) /2
40 x3 = log ( a1 )
41 sigma1 = 0.5* exp ( -1) + x3 *( exp (1) - exp ( -1) ) /4
42 a0 = sigma1 + (1 - x3 ) * a1
43
44 x = poly (0 , ” x ” ) ;
45 // Thus ,
46 q1 = a0 + a1 * x
47
48 deff ( ’ [ y1 ]= f ( x ) ’ , ’ y1 = 1 . 2 6 4 3 + 1 . 1 7 5 2 ∗ x ’ )
49
50 xset ( ’ window ’ ,0) ;
51 x = -1:.01:1; // d e f i n i n g t h e r a n g e o f
x.

33
52 y = feval (x , f ) ;
53
54 a = gca () ;
55
56 a . y_location = ” o r i g i n ” ;
57
58 a . x_location = ” o r i g i n ” ;
59 plot (x , y ) // i n s t r u c t i o n t o p l o t t h e
graph

Scilab code Exa 6.5 approximate

1 // Example 6 . 5
2
3 // xn=10+(n−1) /5
4 // A c c o r d i n g l y we c h o o s e
5 // p h i 1 ( x ) =1 , p h i 2 ( x )=x , p h i 3 ( x ) =(x ) ˆ3
6 A =[6 63 662.2; 63 662.2 6967.8; 662.2 6967.8
73393.5664]
7 norm (A , ’ i n f ’ )
8 x =[10.07 -2 0.099] ’
9 A*x
10 norm ( A *x , ’ i n f ’ )
11 norm ( A * x )
12 a =( norm ( x ) ) / norm (( A ) ^( -1) )
13
14 // norm (A∗ x ) >=norm ( x ) / norm ( (A) ˆ( −1) )
15 // norm (Aˆ( −1) , ’ i n f ’ ) >= 7 . 8
16
17 cond ( A )
18
19 // t h e c o n d i t i o n number o f A i s much l a r g e r t h a n
1 0 ˆ 5 , s o we t a k e
20 deff ( ’ [ y ]= f ( x ) ’ , ’ y=10−2∗x + ( ( ( x ) ˆ 2 ) ) /2 ’ )
21 // f ( x ) i s a p o l y n o m i a l o f d e g r e e 2 , F ∗ ( x ) s h o u l d be

34
f (x) i t s e l f
22
23 c1 =10
24 c2 = -2
25 c3 =0.1
26
27 // by u s i n g e l i m i n a t i o n a l g o r i t h m ( Gauss e l i m i n a t i o n
) , we g e t
28 c1 =9.99999997437
29 c2 = -1.999999951
30 c3 =0.099999976
31 // by 14− d e c i m a l d i g i t f l o a t i n g p o i n t a r i t h m e t i c
method
32 c1 =6.035
33 c2 = -1.243
34 c3 =0.0639
35 // c a l c u l a t i o n c a r r i e d o u t i n s e v e n d e c i m a l d i g i t
f l o a t i n g point arithmetic
36 c1 =8.492
37 c2 = -1.712
38 c3 =0.0863

Scilab code Exa 6.11 polynomial of degree lessthan 3

1
2 // Example 6 . 1 1
3
4 x0 = -1
5 x1 =18
6 // p i=<f , p i >
7 p0 = integrate ( ’ exp ( x ) ’ , ’ x ’ ,x0 , x1 )
8
9 p1 = integrate ( ’ x ∗ exp ( x ) ’ , ’ x ’ ,x0 , x1 )
10
11 p2 = integrate ( ’ ( exp ( x ) ) ∗ ( ( x ˆ 2 ) −(1/3) ) ’ , ’ x ’ ,x0 , x1 )

35
12
13 p3 = integrate ( ’ ( exp ( x ) ) ∗ ( ( x ˆ 3 ) −3∗x / 5 ) ’ , ’ x ’ ,x0 , x1 )
14
15 // f o r l e g e n d r e p o l y n o m i a l s one can show
16 // s i = <p i , p i > = 2 / ( 2 ∗ i +1)
17 s0 =2/(2*0+1)
18 s1 =2/(2+1)
19 s2 =2/(2*2+1)
20 s3 =2/(2*3+1)
21
22 // d i ∗=<f , p i >/ s i
23 // p ∗ ( x )=y=d0∗1+d1 ∗ x+d2 ∗ ( 3 / 2 ) ∗ ( ( x ˆ 2 ) −(1/3) )+d3 ∗ ( ( x ˆ 3 )
−3∗x / 5 ) ∗ ( 5 / 2 )
24 // p ∗ ( x )=y=(p0 / s 0 ) ∗1+( p1 / s 1 ) ∗ x+(p2 / s 2 ) ∗ ( 3 / 2 ) ∗ ( ( x ˆ 2 )
−(1/3) ) +(p3 / s 3 ) ∗ ( ( x ˆ 3 ) −3∗x / 5 ) ∗ ( 5 / 2 )
25 poly (0 , ” x ” )
26 y =1.17552011*(1) +(1.103638324) *( x ) +(0.3578143506)
*(3/2) *(( x ^2) -(1/3) ) +(0.07045563367) *(( x ^3) -3* x
/5) *(5/2)
27 //On ( − 1 , 1 ) , t h i s p o l y n o m i a l a maximum d e v i a t i o n
from exp ( x ) o f a b o u t 0 . 0 1

Scilab code Exa 6.12 Least squares approximation

1 // Example 6 . 1 2
2
3 // L e a s t s q u a r e s a p p r o x i m a t i o n
4
5 deff ( ’ [ y ]= f ( x ) ’ , ’ y=10−2∗x +(( x ˆ 2 ) / 1 0 ) ’ )
6 // we s e e k t h e p o l y n o m i a l o f d e g r e e & l t ;= 2 which
minimizes
7 // sum ( n=1 t o 9 ) [ fn −p ( xn ) ] ˆ 2
8 // we a r e d e a l i n g w i t h s c a l a r p r o d u c t w i t h w( x ) =1
9 P0x =1
10 // h e n c e

36
11 s0 =0;
12 B =0;
13 A1 =0;
14 s1 =0;
15 for n =1:1:6
16
17 s0 = s0 +1
18 B =[10+( n -1) /5]+ B
19
20 A1 =[10+[ n -1]/5]*{[(( n -1) /5) -0.5]^2}+ A1
21
22 s1 ={[(( n -1) /5) -0.5]^2}+ s1
23
24 end
25 B0 = B / s0
26
27 B1 = A1 / s1
28 C1 = s1 / s0
29
30 x = poly (0 , ” x ” )
31 y1 =x - B0
32 x = poly (0 , ” x ” )
33 y2 =(( x - B0 ) ^2) -0.1166667
34 // s i m i l a r l y c a l c u l a t e s 2
35 s2 =0.05973332
36 // p ∗ ( x ) =(d0 ∗ ) ∗ P0x+(d1 ∗ ) ∗P1 ( x ) +(d2 ∗ ) ∗P2 ( x )
37 // d0∗=d0 , d1∗=d1 , d2∗=d2 a r e l e a s t s q u a r e s
approximation
38 // d0∗=d0=s i g m a ( n=1 t o 6 ) [ f n / 6 ] where f n=f ( xn )
39
40 d0 =0.03666667
41 d1 =0.1
42 d2 =0.0999999
43
44 x = poly (0 , ” x ” )
45 p = d0 + d1 *( x - B0 ) + d2 *{[( x - B0 ) ^2] - C1 }
46 // c 1=c 1 ∗ , c 2=c 2 ∗ , c 3=c 3 ∗
47 c1 =9.99998

37
48 c2 = -1.9999998
49 c3 =0.0999999

38
Chapter 7

differentiation and integration

Scilab code Exa 7.1 integration

1 // I= i n t e g r a l ( exp ˆ(−x ˆ 2 ) dx )
2
3 deff ( ’ y=f ( x ) ’ , ’ y=exp ( −( x ˆ 2 ) ) ’ )
4 a =0 , b =1
5 c =( a + b ) /2
6 deff ( ’ y=g ( x ) ’ , ’ y=−2∗x ∗ exp ( −( x ˆ 2 ) ) ’ )
7 f(a)
8 f(b)
9 f(c)
10 g(a)
11 g(b)
12 g(c)
13 R =( b - a ) * f ( a )
14 M =( b - a ) * f ( c )
15 T =( b - a ) *[ f ( a ) + f ( b ) ]/2
16 S =( b - a ) *{ f ( a ) +4* f ( c ) + f ( b ) }/6
17 CT =[( b - a ) /2]*[ f ( a ) + f ( b ) ]+[( b - a ^2) /12]*[ g ( a ) -g ( b ) ]

Scilab code Exa 7.8 adaptive quadrature

39
1 // Example 7 . 8
2
3 // True v a l u e o f t h e i n t e g r a l
4 x0 =0
5 x1 =1
6 I = integrate ( ’ s q r t ( x ) ’ , ’ x ’ ,0 ,1)
7
8 // u s i n g a d a p t i v e q u a d r a t u r e b a s e d on s i m p s o n s r u l e
9
10 deff ( ’ [ y ]= f ( x ) ’ , ’ y = [ ( x ) ˆ ( 1 / 2 ) ] ’ )
11 x =1:1:10
12 plot (x , f )
13
14 x2 =( x0 + x1 ) /2;
15 h =1/2
16 // c o n s i d e r i n g t h e i n t e r v a l [ x2 , x1 ] = [ 1 / 2 , 1 ]
17
18 s = h /6.*{ f ( x2 ) +4* f (( x2 ) + h /2) + f ( x1 ) }
19 p = h /12*{ f ( x2 ) +4* f (( x2 ) + h /4) +2* f (( x2 ) + h /2) +4* f ( x2 +3* h
/4) + f ( x1 ) }
20 E =(1/15) *( p - s )
21 // // E r r o r c r i t e r i o n i s c l e a r l y s a t i s f i e d , h e n c e we
put v a l u e o f p t o SUM r e g i s t e r t o o b t a i n p a r t i a l
approximation
22 // c o n s i d e r i n g t h e i n t e r v a l [ x2 , x1 ] = [ 0 , 1 / 2 ]
23
24 s1 = h /6.*{ f ( x0 ) +4* f (( x0 ) + h /2) + f ( x2 ) }
25 p1 = h /12.*{ f ( x0 ) +4* f (( x0 ) + h /4) +2* f (( x0 ) + h /2) +4* f ( x0
+3* h /4) + f ( x2 ) }
26 E1 =1/15.*[ p1 - s1 ]
27
28 // Here s i n c e e r r o r i s n o t l e s s t h a n 0 . 0 0 0 2 5 we have
to d i v i d e i n t e r v a l [ 0 , 1 / 2 ] to [0 ,1/4]& [ 1 / 4 , 1 / 2 ]
29 h =1/4
30 // c o n s i d e r i n g i n t e r v a l [ 1 / 4 , 1 / 2 ]
31
32 x3 =1/4
33

40
34 s2 = h /6.*{ f ( x3 ) +4* f (( x3 ) + h /2) + f ( x2 ) }
35 p2 = h /12.*{ f ( x3 ) +4* f (( x3 ) + h /4) +2* f (( x3 ) + h /2) +4* f ( x3
+3* h /4) + f ( x2 ) }
36 E2 =1/15.*[ p2 - s2 ]
37
38 // E2 < ( 0 . 0 0 0 5 ) /4
39 // E r r o r c r i t e r i o n i s c l e a r l y s a t i s f i e d , h e n c e we
add v a l u e o f p2 t o SUM r e g i s t e r t o o b t a i n p a r t i a l
approximation
40 sum = p + p2
41 funcprot (0)
42 // A p p l y i n g a g a i n a b o v e b a s i c f o r m u l a s
43
44 // w i t h h=1/4 , in interval [0.1/4]
45 // we g e t
46
47 s3 =0.07975890
48 p3 =0.08206578
49 E3 =0.0001537922
50 // Here s i n c e e r r o r i s n o t l e s s t h a n 0 . 0 0 0 1 2 5 we
have t o d i v i d e i n t e r v a l
51 // [ 0 , 1 / 4 ] i n t o [ 0 , 1 / 8 ] & [ 1 / 8 , 1 / 4 ] w i t h h=1/8
52 h =1/8
53
54 // f o r i n t e r v a l [ 1 / 8 , 1 / 4 ]
55
56 s4 =0.05386675
57 p4 =0.05387027
58 E4 =0.0000002346
59
60
61 // E4 < ( 0 . 0 0 0 5 ) ∗h = ( 0 . 0 0 0 5 ) /8 = 0 . 0 0 0 0 6 2 5
62 // E r r o r c r i t e r i o n i s c l e a r l y s a t i s f i e d , h e n c e we
add v a l u e o f p4 t o
63 //SUM r e g i s t e r t o o b t a i n p a r t i a l a p p r o x i m a t i o n
64 sum = p + p2 + p4
65
66

41
67 // c o n s i d e r i n t e r v a l [ 0 , 1 / 8 ]
68
69 s5 =0.02819903
70 p5 =0.02901464
71 E5 =0.00005437
72
73 // E5 < 0 . 0 0 0 0 6 2 5
74
75 // S i n c e t h e e r r o r t e s t i s p a s s e d on b o t h i n t e r v a l s ,
we can add t h e s e v a l u e s i n t o sUM r e g i s t e r t o
get
76 sum = p + p2 + p4 + p5
77
78 // s i n c e t h e e x a c t v a l u e o f I i s 0 . 6 6 6 6 6 6 6 6 6
79 abs ( sum - I ) <0.0005 // o v e r t h e i n t e r v a l [ 0 , 1 ]

42
Chapter 8

THE SOLUTION OF
DIFFERENTIAL
EQUATIONS

Scilab code Exa 8.1 Taylor series

1 // Example 8 . 1
2
3 deff ( ’ [ v ]= f ( x , y ) ’ , ’ v=1−(y / x ) ’ )
4 funcprot (0)
5 deff ( ’ [ v ]= f p ( x , y ) ’ , ’ v=−( f ( x ) / x ) +(y / ( x ˆ 2 ) ) ’ )
6 funcprot (0)
7 deff ( ’ [ v ]= f p p ( x , y ) ’ , ’ v=−( f p ( x ) / x ) +2∗( f ( x ) / ( x ˆ 2 ) ) −2∗(
y /( x ˆ3) ) ’ )
8 funcprot (0)
9 deff ( ’ [ v ]= g ( x , y ) ’ , ’ v=−( f p p ( x ) / x ) +3∗( f p ( x ) / ( x ˆ 2 ) ) −6∗(
f ( x ) / ( x ˆ 3 ) ) +6∗( y / ( x ˆ 4 ) ) ’ )
10 funcprot (0)
11 x1 =2
12 y1 =2
13 x =2.1
14 y2 = y1 +( x -2) * f ( x1 , y1 ) +(( x -2) ^2) * fp ( x1 , y1 ) / factorial
(2) +(( x -2) ^3) * fpp ( x1 , y1 ) / factorial (3) +(( x -2) ^4) * g

43
( x1 , y1 ) / factorial (4)

check Appendix AP 2 for dependency:

adamsbash.sce

Scilab code Exa 8.5 Adamsbashforth3

1 // Example 8 . 5
2
3 deff ( ’ [ v ]= f ( x , y ) ’ , ’ v=x+y ’ )
4
5 [y , x ]= adamsbashforth3 (0 ,0 ,1 ,1/32 , f )

check Appendix AP 1 for dependency:

modifiedEuler.sce

Scilab code Exa 8.6 Modified euler method

1 // Example 8 . 6
2
3 // M o d i f i e d E u l e r s method
4
5 deff ( ’ [ v ]= f ( x , y ) ’ , ’ v=x −(1/ y ) ’ )
6
7 [y , x ] = modifiedeuler (1 ,0 ,0.2 ,0.1 , f )

44
Appendix

Scilab code AP 1 Modified euler method

1 function [u , t ] = modifiedeuler ( u0 , t0 , tn ,h , f )
2
3 // m o d i f i e d e u l e r 1 s t o r d e r method s o l v i n g ODE
4 // du / d t = f ( u , t ) , w i t h i n i t i a l
5 // c o n d i t i o n s u=u0 a t t=t 0 . The
6 // s o l u t i o n i s o b t a i n e d f o r t = [ t 0 : h : t n ]
7 // and r e t u r n e d i n u
8
9 umaxAllowed = 1 e +100;
10
11 t = [ t0 : h : tn ]; u = zeros ( t ) ; n = length ( u ) ; u (1) =
u0 ;
12
13 for j = 1: n -1
14 k1 = h * f ( t ( j ) ,u ( j ) ) ;
15 k2 = h * f ( t ( j ) + h /2 , u ( j ) + k1 /2) ;
16 u ( j +1) = u ( j ) + k2 ;
17 if u ( j +1) > umaxAllowed then
18 disp ( ’ E u l e r 1 − WARNING: underflow or
o v e r f l o w ’ );
19 disp ( ’ S o l u t i o n s o u g h t i n t h e f o l l o w i n g
range : ’ );
20 disp ([ t0 h tn ]) ;
21 disp ( ’ S o l u t i o n e v a l u a t e d i n t h e f o l l o w i n g
range : ’ );
22 disp ([ t0 h t ( j ) ]) ;

45
23 n = j ; t = t (1 ,1: n ) ; u = u (1 ,1: n ) ;
24 break ;
25 end ;
26 end ;
27
28 endfunction

Scilab code AP 2 adamsbashforth

1
2 function [u , t ] = adamsbashforth3 ( u0 , t0 , tn ,h , f )
3
4 // a d a m s b a s h f o r t h 3 3 r d o r d e r method s o l v i n g ODE
5 // du / d t = f ( u , t ) , w i t h i n i t i a l
6 // c o n d i t i o n s u=u0 a t t=t 0 . The
7 // s o l u t i o n i s o b t a i n e d f o r t = [ t 0 : h : t n ]
8 // and r e t u r n e d i n u
9
10 umaxAllowed = 1 e +100;
11
12 t = [ t0 : h : tn ]; u = zeros ( t ) ; n = length ( u ) ; u (1) =
u0 ;
13 for j = 1: n -1
14 if j <3 then
15 k1 = h * f ( t ( j ) ,u ( j ) ) ;
16 k2 = h * f ( t ( j ) +h , u ( j ) + k1 ) ;
17 u ( j +1) = u ( j ) + ( k2 + k1 ) /2;
18 end ;
19
20 if j >=2 then
21 u ( j +2) = u ( j +1) + ( h /12) *(23* f ( t ( j +1) ,u ( j +1) )
-16* f ( t ( j ) ,u ( j ) ) +5* f ( t (j -1) ,u (j -1) ) ) ;
22 end ;
23 end ;
24 endfunction

Scilab code AP 3 secant mehod

46
1 function [ x ]= secant (a ,b , f )
2 N =100; // d e f i n e max . no . i t e r a t i o n s
t o be p e r f o r m e d
3 PE =10^ -4 // d e f i n e t o l e r a n c e f o r
convergence
4 for n =1:1: N // i n i t i a t i n g f o r l o o p
5 x =a -( a - b ) * f ( a ) /( f ( a ) -f ( b ) ) ;
6 disp ( x )
7 if abs ( f ( x ) ) <= PE then break ; // c h e c k i n g f o r
the required condition
8 else a = b ;
9 b=x;
10 end
11 end
12 disp (n , ” no . o f i t e r a t i o n s =” ) //
13 endfunction

Scilab code AP 4 bisection method

1 function x = bisection (a ,b , f )
2 N =100; //
d e f i n e max . number o f i t e r a t i o n s
3 PE =10^ -4 //
define tolerance
4 if ( f ( a ) * f ( b ) > 0) then
5 error ( ’ no r o o t p o s s i b l e f ( a ) ∗ f ( b ) > 0 ’)
// c h e c k i n g i f t h e d e c i d e d r a n g e is
containing a root
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 p p r o x i m a t e root
at a ,
10 abort ;
11 end ;
12 if ( abs ( f ( b ) ) < PE ) then //
s e e i n g i f t h e r e i s an a p p r o x i m a t e r o o t at b ,

47
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 ’ for ’ loop ,
18 p=f(a)*f(x)
19 if p <0 then b = x ,x =( a + x ) /2;
// c h e c k i n g f o r t h e r e q u i r e d c o n d i t i o n s ( f
( x ) ∗ f ( a ) <0) ,
20 else
21 a=x
22 x =( x + b ) /2;
23 end
24 if abs ( f ( x ) ) <= PE then break
// i n s t r u c t i o n t o come o u t o f t h e l o o p
a f t e r the r e q u i r e d c o n d i t i o n i s achived ,
25 end
26 end
27 disp (n , ” no . o f i t e r a t i o n s =” )
// d i s p l a y t h e no . o f i t e r a t i o n s t o o k t o
achive required condition ,
28 endfunction

Scilab code AP 5 fixed point

1 function [ x ]= fixedp ( x0 , f )
2 // f i x e d −p o i n t i t e r a t i o n
3 N = 100; eps = 1. e -5; // d e f i n e max . no . iterations
and e r r o r
4 maxval = 10000.0; // d e f i n e value for divergence
5 a = x0 ;
6 while (N >0)
7 xn = f ( a ) ;
8 if ( abs ( xn - a ) < eps ) then
9 x = xn
10 disp (100 - N ) ;
11 return ( x ) ;

48
12 end ;
13 if ( abs ( f ( x ) ) > maxval ) then
14 disp (100 - N ) ;
15 error ( ’ S o l u t i o n d i v e r g e s ’ ) ;
16 abort ;
17 end ;
18 N = N - 1;
19 xx = xn ;
20 end ;
21 error ( ’ No c o n v e r g e n c e ’ ) ;
22 abort ;
23 // end f u n c t i o n

Scilab code AP 6 newton method

1 function x = newton (x ,f , fp )
2 R =100;
3 PE =10^ -8;
4 maxval =10^4;
5
6 for n =1:1: R
7 x =x - f ( x ) / fp ( x ) ;
8 if abs ( f ( x ) ) <= PE then break
9 end
10 if ( abs ( f ( x ) ) > maxval ) then error ( ’ S o l u t i o n
d i v e r g e s ’ );
11 abort
12 break
13 end
14 end
15 disp (n , ” no . o f i t e r a t i o n s =” )
16 endfunction

Scilab code AP 7 Regular falsi method

1 function [ x ]= regularfalsi (a ,b , f )
2 N =100;
3 PE =10^ -5;

49
4 for n =2:1: N
5 x =a -( a - b ) * f ( a ) /( f ( a ) -f ( b ) ) ;
6 disp ( x )
7 if abs ( f ( x ) ) <= PE then break ;
8 elseif ( f ( a ) * f ( x ) <0) then b = x ;
9 else a = x ;
10 end
11 end
12 disp (n , ” no . o f i t t i r a t i o n s =” )
13 endfunction

50

You might also like