Numerical Methods - Principles, Analysis, and Algorithms - S. Pal
Numerical Methods - Principles, Analysis, and Algorithms - S. Pal
1 Funded
Book Description
Title: Numerical Methods: Principles, Analysis, And Algorithms
Author: S. Pal
Publisher: Oxford University Press
Edition: 1
Year: 2009
ISBN: 9780195693751
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.
Contents
List of Scilab Codes
36
38
42
45
86
97
124
135
143
162
13 Numerical Differentiation
174
14 Numerical Integration
183
3
273
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
1.10
1.13
1.14
1.15
1.16
1.17
1.18
1.19
1.20
1.23
1.24
1.25
1.26
1.29
1.30
1.31
1.32
1.33
1.34
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5
6
7
7
8
9
9
10
11
11
12
14
15
17
19
20
22
23
25
26
27
29
31
32
32
33
33
34
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
1.35
1.36
2.4
2.6
3.1
3.2
3.3
3.4
3.5
4.1
4.2
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
5.12
5.13
5.14
5.15
5.19
5.20
5.21
5.22
5.23
5.24
5.25
5.26
5.27
5.28
5.29
5.31
Multiplication . . . . . . . . . . . . . .
Division . . . . . . . . . . . . . . . . . .
Solving Simultaneous Linear Equation .
Integration . . . . . . . . . . . . . . . .
Limiting Error . . . . . . . . . . . . . .
Known Error . . . . . . . . . . . . . . .
Absolute Relative and Percentage Errors
Absolute Relative and Percentage Errors
Absolute Relative and Percentage Errors
Quadratic Equation . . . . . . . . . . .
Database Management . . . . . . . . . .
Bisection Method . . . . . . . . . . . .
Bisection Method . . . . . . . . . . . .
Regula Falsi Method . . . . . . . . . . .
Ridders Method . . . . . . . . . . . . .
General Iterative Method . . . . . . . .
Linear Iterative Method . . . . . . . . .
Aitkens Method . . . . . . . . . . . . .
Newton Raphson Method . . . . . . . .
Modified Newton Raphson Method . . .
Newton Raphson Method . . . . . . . .
Newton Raphson Method . . . . . . . .
Newton Raphson Method . . . . . . . .
Secant Method . . . . . . . . . . . . . .
Kizner Method . . . . . . . . . . . . . .
Brent Method . . . . . . . . . . . . . .
Horner Method . . . . . . . . . . . . . .
Laguerre Method . . . . . . . . . . . . .
Mullers Method . . . . . . . . . . . . .
Mullers Method . . . . . . . . . . . . .
Bairstow Hitchcock Method . . . . . . .
Bernoulli Method . . . . . . . . . . . .
Graeffe Method . . . . . . . . . . . . . .
QD Method . . . . . . . . . . . . . . . .
Linear Iteration Method . . . . . . . . .
Aitkens Method . . . . . . . . . . . . .
Newton Raphson Method . . . . . . . .
Secant Method . . . . . . . . . . . . . .
6
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
34
35
36
37
38
38
39
39
40
42
42
45
46
47
48
49
49
50
51
52
52
53
54
55
56
57
57
58
59
62
65
67
67
69
70
71
71
72
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
5.32
5.33
5.34
5.35
5.36
5.37
5.38
5.39
5.40
6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10
7.1
7.2
7.3
7.4
7.5
7.6
8.1
8.2
8.3
8.4
8.5
8.6
8.7
8.8
9.1
9.2
9.3
Exa 9.4
73
77
79
80
81
82
83
84
85
86
87
88
89
90
92
93
94
95
95
97
101
102
104
105
106
107
108
109
110
112
115
116
120
124
124
127
128
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
9.5
9.6
9.7
9.8
10.1
10.2
10.3
10.4
10.5
10.6
10.7
10.8
11.1
11.2
11.3
11.4
11.5
11.6
11.7
11.8
11.9
11.10
11.11
12.1
12.2
12.3
12.4
12.5
12.6
12.7
12.8
13.1
13.2
13.3
13.4
13.5
13.6
13.7
Vector Computation . . . . . . . . . . . . . . .
House Holder Transformation . . . . . . . . . .
Givens QR Method . . . . . . . . . . . . . . .
Recursive Least Square Method . . . . . . . . .
System of Non Linear Equations . . . . . . . .
Contraction Method and Seidel Method . . . .
Non Linear System of Equation . . . . . . . . .
Newton Method . . . . . . . . . . . . . . . . .
Newton Raphshon Method . . . . . . . . . . .
Newton Method . . . . . . . . . . . . . . . . .
Iterative Method . . . . . . . . . . . . . . . . .
Steepest Descent . . . . . . . . . . . . . . . . .
Eigenvalues and Eigenvectors . . . . . . . . . .
Leverriers Method . . . . . . . . . . . . . . . .
Danilevsky Method . . . . . . . . . . . . . . .
Power Method . . . . . . . . . . . . . . . . . .
Inverse Power Method . . . . . . . . . . . . . .
Rayleigh Quotient . . . . . . . . . . . . . . . .
Jacobi Method . . . . . . . . . . . . . . . . . .
Recursive Formula . . . . . . . . . . . . . . . .
QR Method . . . . . . . . . . . . . . . . . . . .
LU Method . . . . . . . . . . . . . . . . . . . .
Generalized Eigenvalue Problem . . . . . . . .
Linear Interpolation Technique . . . . . . . . .
Lagarangian Method . . . . . . . . . . . . . . .
Aitken Nevilles Method . . . . . . . . . . . . .
Newtons Divided Difference Interpolation . . .
Interpolation Methods . . . . . . . . . . . . . .
Chebyshev Interpolating Polynomial . . . . . .
Double Interpolation . . . . . . . . . . . . . . .
Spline Interpolation . . . . . . . . . . . . . . .
Differentiation . . . . . . . . . . . . . . . . . .
Calculation of x coordinate of Minimum Point
Newton Forward Difference Formula . . . . . .
Newton Backward Difference Formula . . . . .
Stirlings Central Difference Derivatives . . . . .
Extrapolation . . . . . . . . . . . . . . . . . . .
Richardson Extrapolation . . . . . . . . . . . .
8
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
130
131
132
133
135
136
136
137
138
139
141
141
143
144
145
148
149
150
151
153
153
156
158
162
163
164
165
166
169
171
172
174
174
176
177
179
180
180
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
13.8
14.2
14.3
14.5
14.7
14.8
14.9
14.10
14.11
14.14
14.15
14.16
14.18
14.19
15.1
15.6
15.7
15.8
15.9
15.10
15.11
15.12
15.13
15.14
15.15
15.16
15.17
15.18
15.19
15.20
15.21
15.22
15.23
15.24
15.25
15.26
15.27
15.28
Application . . . . . . . . . . . . . . . . . . . . . . . .
Simpsons 1 3rd Rule . . . . . . . . . . . . . . . . . . .
Trapezoidal Rule and Simpsons Rule . . . . . . . . . .
Romberg Method . . . . . . . . . . . . . . . . . . . . .
Gaussian Quadrature Formula . . . . . . . . . . . . .
Gauss Legendre Two Point Rule . . . . . . . . . . . .
Gauss Legendre Three Point Rule . . . . . . . . . . .
Spline Integration Method . . . . . . . . . . . . . . . .
Trapezoidal Rule . . . . . . . . . . . . . . . . . . . . .
Trapezoidal and Simpsons Rule . . . . . . . . . . . . .
Trapezoidal and Simpsons Rule . . . . . . . . . . . . .
Multiple Integration with Variable Limits . . . . . . .
Integration . . . . . . . . . . . . . . . . . . . . . . . .
Integration . . . . . . . . . . . . . . . . . . . . . . . .
Ordinary Differential Equation . . . . . . . . . . . . .
Taylor Method . . . . . . . . . . . . . . . . . . . . . .
Picard Method . . . . . . . . . . . . . . . . . . . . . .
Euler Method . . . . . . . . . . . . . . . . . . . . . . .
Trapezium Method . . . . . . . . . . . . . . . . . . . .
Heun Method . . . . . . . . . . . . . . . . . . . . . . .
Midpoint Method . . . . . . . . . . . . . . . . . . . .
Modified Midpoint Method . . . . . . . . . . . . . . .
Single Step Method . . . . . . . . . . . . . . . . . . .
Second Order Runge Kutta Method . . . . . . . . . .
Third Order Runge Kutta Method . . . . . . . . . . .
Fourth Order Runge Kutta Method . . . . . . . . . .
New Variant of Runge Kutta Method . . . . . . . . .
Runge Kutta Merson Method . . . . . . . . . . . . . .
Runge Kutta Fehlberg Method . . . . . . . . . . . . .
Carp Karp Runge Kutta Method . . . . . . . . . . . .
Implicit Runge Kutta Method . . . . . . . . . . . . . .
Linear Multi Step Method . . . . . . . . . . . . . . . .
Milne Simpson Predictor Corrector Method . . . . . .
Improved Milne Simpson Predictor Corrector Method
Hamming Predictor Corrector Method . . . . . . . . .
Multi Valued Method . . . . . . . . . . . . . . . . . .
First order ODE . . . . . . . . . . . . . . . . . . . . .
Differential Equation . . . . . . . . . . . . . . . . . . .
9
181
183
184
186
187
187
188
188
189
190
191
193
194
194
195
195
196
197
197
198
199
199
200
201
202
202
203
204
204
205
206
207
208
208
210
211
213
214
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
16.1
16.2
16.3
16.4
16.5
16.6
18.4
18.5
18.6
18.7
18.8
19.3
19.4
19.5
19.6
19.7
19.8
19.9
19.10
19.11
19.12
19.13
20.1
20.2
20.3
20.4
20.5
20.6
20.7
20.8
21.1
21.2
21.3
Exa 21.4
Exa 21.5
Exa 22.1
216
217
219
221
222
223
225
226
227
229
230
235
236
237
238
239
241
244
247
248
250
250
254
255
256
257
258
260
261
262
265
266
267
269
270
273
Exa
Exa
Exa
Exa
Exa
22.2
22.3
22.4
22.5
22.7
MLP . . . . . . . . . . .
Bisection Method . . . .
Hopfield Neural Network
RBF Network . . . . . . .
First Order ODE . . . . .
11
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
274
275
276
277
278
Chapter 1
Background to Numerical
Methods
12
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
xd = xd *10;
s = s +( ceil ( xd ) / b ^( i ) )
xd = xd - fix ( xd )
if ( xd ==0)
break
end
end
endfunction
// c o n v e r s i o n from h e x a d e c i m a l t o d e c i m a l s y s t e m
disp ( hex2dec ( 1A2C ) , 1A2C= ) ;
// i n b u i t f u n c t i o n
// c o n v e r s i o n from h e x a d e c i m a l t o d e c i m a l s y s t e m
disp ( bas2dec (428.5 ,8) , 4 2 8 . 5 = )
// i n l i n e f u n c t i o n
// c o n v e r s i o n from h e x a d e c i m a l t o d e c i m a l s y s t e m
disp ( bas2dec (120.1 ,3) , 1 2 0 . 1 = )
// i n l i n e
function
13
10101101110 i s = );
7
8
9
// i n b u i l t f u n c t i o n
// c o n v e r s i o n from b i n a r y t o h e x a d e c i m a l
disp ( dec2hex ( bin2dec ( 1 0 1 0 1 1 0 1 1 1 0 ) ) , H e x a d e c i m a l
form o f 1 0 1 0 1 1 0 1 1 1 0 i s = ) ;
// i n b u i l t f u n c t i o n
10
11 // c o n v e r s i o n from b i n a r y t o o c t a l
12 s = dec2oct ( bin2dec ( 1 0 1 1 ) ) ;
13 s1 = dec2oct ( bin2dec ( 1 1 0 0 1 1 0 1 0 1 0 0 ) ) ;
// i n b u i l t
function
14 printf ( \n O c t a l form o f 1 0 1 1 . 1 1 0 0 1 1 0 1 0 1 i s = \n\n
%s . %s ,s , s1 )
15
16 // c o n v e r s i o n from b i n a r y t o h e x a d e c i m a l
17 s = dec2hex ( bin2dec ( 1 0 1 1 ) ) ;
18 s1 = dec2hex ( bin2dec ( 1 1 0 0 1 1 0 1 0 1 0 0 ) ) ;
// i n b u i l t
19
function
printf ( \n\n H e x a d e c i m a l form o f 1 0 1 1 . 1 1 0 0 1 1 0 1 0 1 i s
= \n\n %s . %s ,s , s1 )
// Example 1 . 4
// C o n v e r s i o n t o B i n a r y System
// Page no . 6
14
i s = );
7
8
9
// i n b u i l t f u n c t i o n
// c o n v e r s i o n from o c t a l t o b i n a r y
disp ( dec2bin ( hex2dec ( A478 ) ) , B i n a r y form o f A478
i s = );
// i n b u i l t f u n c t i o n
10
11 // c o n v e r s i o n from o c t a l t o b i n a r y
12 s = dec2bin ( oct2dec ( 3 ) ) ;
13 s1 = dec2bin ( oct2dec ( 154 ) ) ;
// i n b u i l t f u n c t i o n
14 printf ( \n O c t a l form o f 3 . 1 5 4 i s = \n\n %s . 0 0 %s ,s ,
s1 )
// i n b u i l t
function
b = dec2hex ( oct2dec ( 1 7 5 3 ) )
disp (b , H e x a d e c i m a l form o f 1 7 5 3 i s = ) ;
//
inbuilt function
// c o n v e r s i o n from o c t a l t o b i n a r y
b = dec2bin ( hex2dec ( A478 ) )
disp (b , B i n a r y form o f A478 i s = ) ;
// i n b u i l t
function
b = dec2oct ( hex2dec ( A478 ) )
15
14
15
16
17
18
19
20
21
// i n b u i l t
function
16
//
10101101110 i s = );
7
8
9
// i n b u i l t f u n c t i o n
// c o n v e r s i o n from b i n a r y t o h e x a d e c i m a l
disp ( dec2hex ( bin2dec ( 1 0 1 0 1 1 0 1 1 1 0 ) ) , H e x a d e c i m a l
form o f 1 0 1 0 1 1 0 1 1 1 0 i s = ) ;
// i n b u i l t f u n c t i o n
10
11 // c o n v e r s i o n from b i n a r y t o o c t a l
12 s = dec2oct ( bin2dec ( 1 0 1 1 ) ) ;
13 s1 = dec2oct ( bin2dec ( 1 1 0 0 1 1 0 1 0 1 0 0 ) ) ;
14
// i n b u i l t
function
printf ( \n O c t a l form o f 1 0 1 1 . 1 1 0 0 1 1 0 1 0 1 i s = \n\n
%s . %s ,s , s1 )
15
16 // c o n v e r s i o n from b i n a r y t o h e x a d e c i m a l
17 s = dec2hex ( bin2dec ( 1 0 1 1 ) ) ;
18 s1 = dec2hex ( bin2dec ( 1 1 0 0 1 1 0 1 0 1 0 0 ) ) ;
// i n b u i l t
function
19 printf ( \n\n H e x a d e c i m a l form o f 1 0 1 1 . 1 1 0 0 1 1 0 1 0 1 i s
= \n\n %s . %s ,s , s1 )
17
i s = );
7
8
9
// i n b u i l t f u n c t i o n
// c o n v e r s i o n from o c t a l t o b i n a r y
disp ( dec2bin ( hex2dec ( A478 ) ) , B i n a r y form o f A478
i s = );
// i n b u i l t f u n c t i o n
10
11 // c o n v e r s i o n from o c t a l t o b i n a r y
12 s = dec2bin ( oct2dec ( 3 ) ) ;
13 s1 = dec2bin ( oct2dec ( 154 ) ) ;
// i n b u i l t f u n c t i o n
14 printf ( \n O c t a l form o f 3 . 1 5 4 i s = \n\n %s . 0 0 %s ,s ,
s1 )
18
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
disp (b , B i n a r y form o f 1 7 5 3 i s = ) ;
// i n b u i l t
function
b = dec2hex ( oct2dec ( 1 7 5 3 ) )
disp (b , H e x a d e c i m a l form o f 1 7 5 3 i s = ) ;
//
inbuilt function
// c o n v e r s i o n from o c t a l t o b i n a r y
b = dec2bin ( hex2dec ( A478 ) )
disp (b , B i n a r y form o f A478 i s = ) ;
// i n b u i l t
function
b = dec2oct ( hex2dec ( A478 ) )
disp (b , O c t a l form o f A478 i s = ) ;
// i n b u i l t
function
// c o n v e r s i o n from o c t a l t o b i n a r y
s = dec2bin ( oct2dec ( 3 ) ) ;
s1 = dec2bin ( oct2dec ( 154 ) ) ;
// i n b u i l t f u n c t i o n
printf ( \n O c t a l form o f 3 . 1 5 4 i s = \n\n %s . 0 0 %s ,s ,
s1 )
s = dec2hex ( oct2dec ( 3 ) ) ;
s1 = dec2hex ( oct2dec ( 154 ) ) ;
// i n b u i l t f u n c t i o n
printf ( \n\n H e x a d e c i m a l form o f 3 . 1 5 4 i s = \n\n %s .
%s ,s , s1 )
compliment
for i =8: -1:1
x = x /10;
19
for 1s
9
10
11
12
13
14
15
16
17
18
xd =x - fix ( x )
if ( floor (( xd *10) +0.1) ==1)
x1 (1 , i ) =0;
else
x1 (1 , i ) =1;
end
x =x - xd ;
end
endfunction
function [ x1 ]= com2 ( x )
// f u n c t i o n f o r 2 s
compliment ( )
for i =8: -1:1
x = x /10;
xd =x - fix ( x )
if ( int (( xd *10) +0.1) ==1)
x1 (1 , i ) =0;
else
x1 (1 , i ) =1;
end
end
for i =8: -1:1
if ( x1 (1 , i ) ==0) then
x1 (1 , i ) =1;
break ;
else
x1 (1 , i ) =0;
end
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
end
37 endfunction
38 a
20
44
printf ( \n\n )
45 end
// f u n c t i o n f o r 1 s
compliment
for i =8: -1:1
x = x /10;
xd =x - fix ( x )
if ( floor (( xd *10) +0.1) ==1)
x1 (1 , i ) =0;
else
x1 (1 , i ) =1;
end
x =x - xd ;
end
endfunction
a
=[00010011 ,01110110 ,11101101 ,10000001 ,10000000 ,00000000];
19 for i =1:6
20
printf ( 1 s Compliment o f %. 8 i = ,a ( i ) ) ;
21
disp ( com1 ( a ( i ) ) )
22
printf ( \n\n )
23 end
21
// f u n c t i o n
for addition of binaries
c =0;
for i =1:10
x1 (1 , i ) =0
end
for i =10: -1:1
x = x /10;
xd =x - fix ( x )
x =x - xd ;
y = y /10;
yd =y - fix ( y )
y =y - yd ;
if c ==1 then
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
x1 (1 , i ) =1; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =1; c =0;
else
x1 (1 , i ) =0; c =0;
end
else
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
22
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
x1 (1 , i ) =0; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =0; c =0;
else
x1 (1 , i ) =1; c =0;
end
end
end
disp ( x1 , A d d i t i o n o f 173 and 141= )
endfunction
function [ x1 ]= sub (x , y )
// f u n c t i o n f o r
subtraction of binaries
c =0;
for i =1:10
x1 (1 , i ) =0
end
for i =10: -1:1
x = x /10;
xd =x - fix ( x )
x =x - xd ;
y = y /10;
yd =y - fix ( y )
y =y - yd ;
if c ==1 then
if floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==1 then
x1 (1 , i ) =0; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor
(( yd *10) +0.1) ==0
x1 (1 , i ) =1; c =0;
elseif floor (( xd *10) +0.1) ==1 & floor
(( yd *10) +0.1) ==1
x1 (1 , i ) =1; c =1;
elseif floor (( xd *10) +0.1) ==1 & floor
(( yd *10) +0.1) ==0
x1 (1 , i ) =0; c =0;
end
23
59
60
61
62
63
64
65
66
else
if floor (( xd *10) +0.1) ==1 & floor (( yd
*10) +0.1) ==1 then
x1 (1 , i ) =1; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor
(( yd *10) +0.1) ==0
x1 (1 , i ) =0; c =0;
elseif floor (( xd *10) +0.1) ==1 & floor
(( yd *10) +0.1) ==0
x1 (1 , i ) =1; c =0;
elseif floor (( xd *10) +0.1) ==0 & floor
(( yd *10) +0.1) ==1
x1 (1 , i ) =1; c =1;
end
67
68
69
end
70
end
71
disp ( x1 , S u b t r a c t i o n
72 endfunction
73 add (10101101 ,10001101)
74 sub (11100100 ,00101101)
o f 45 from 228= )
// f u n c t i o n
for addition of binaries
c =0;
printf ( A d d i t i o n o f %. 4 i and %. 4 i = ,x , y )
for i =1:4
24
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
x1 (1 , i ) =0
end
for i =4: -1:1
x = x /10;
xd =x - fix ( x )
x =x - xd ;
y = y /10;
yd =y - fix ( y )
y =y - yd ;
if c ==1 then
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
x1 (1 , i ) =1; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =1; c =0;
else
x1 (1 , i ) =0; c =1;
end
else
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
x1 (1 , i ) =0; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =0; c =0;
else
x1 (1 , i ) =1; c =0;
end
end
end
disp ( x1 )
endfunction
add (0010 ,0101) ;
add (1110 ,1011) ;
add (1110 ,0101) ;
add (0010 ,1011) ;
25
// f u n c t i o n
for addition of binaries
c =0;
printf ( A d d i t i o n o f %. 4 i and %. 4 i = ,x , y )
for i =1:5
x1 (1 , i ) =0
end
for i =5: -1:1
x = x /10;
xd =x - fix ( x )
x =x - xd ;
y = y /10;
yd =y - fix ( y )
y =y - yd ;
if c ==1 then
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
x1 (1 , i ) =1; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =1; c =0;
else
x1 (1 , i ) =0; c =1;
26
26
27
28
end
else
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
x1 (1 , i ) =0; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =0; c =0;
else
x1 (1 , i ) =1; c =0;
end
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
end
end
disp ( x1 )
endfunction
add (0010 ,0101) ;
add (1101 ,1010) ;
add (1101 ,0101) ;
add (0010 ,1010) ;
add (1101 ,0010) ;
add (1111 ,0000) ;
// f u n c t i o n
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
c =0;
printf ( A d d i t i o n o f %. 4 i and %. 4 i = ,x , y )
for i =1:5
x1 (1 , i ) =0
end
for i =5: -1:1
x = x /10;
xd =x - fix ( x )
x =x - xd ;
y = y /10;
yd =y - fix ( y )
y =y - yd ;
if c ==1 then
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
x1 (1 , i ) =1; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =1; c =0;
else
x1 (1 , i ) =0; c =1;
end
else
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
x1 (1 , i ) =0; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =0; c =0;
else
x1 (1 , i ) =1; c =0;
end
end
end
disp ( x1 )
endfunction
28
// f u n c t i o n
25
26
27
28
x1 (1 , i ) =0; c =1;
end
else
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
x1 (1 , i ) =0; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =0; c =0;
else
x1 (1 , i ) =1; c =0;
end
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
end
end
disp ( x1 )
endfunction
add (0010 ,0101) ;
add (11110 ,11011) ;
add (1000 ,0101) ;
add (00010 ,11011) ;
add (11110 ,00010) ;
add (11111 ,0000) ;
// f u n c t i o n
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
c =0;
for i =1:5
x1 (1 , i ) =0
end
for i =5: -1:1
x = x /10;
xd =x - fix ( x )
x =x - xd ;
y = y /10;
yd =y - fix ( y )
y =y - yd ;
if c ==1 then
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
x1 (1 , i ) =1; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =1; c =0;
else
x1 (1 , i ) =0; c =0;
end
else
if floor (( xd *10) +0.1) ==1 & floor (( yd *10)
+0.1) ==1 then
x1 (1 , i ) =0; c =1;
elseif floor (( xd *10) +0.1) ==0 & floor (( yd
*10) +0.1) ==0
x1 (1 , i ) =0; c =0;
else
x1 (1 , i ) =1; c =0;
end
end
end
disp ( x1 , A d d i t i o n o f 173 and 141= )
endfunction
29
30
31
32
33
34
35
36
37
38 add (0100 ,1011) ;
39 add (1100 ,0101) ;
31
32
30
i =i -1; c =1;
31
end
32
end
33
end
34
disp ( x1 )
35 endfunction
36 mul (1110 ,1011) ;
33
24
end
25
else
26
if xd ==0 | xd ==1 then
27
x1 (1 , i ) = xd ; c =0
28
elseif xd ==2
29
x1 (1 , i ) =0;
30
i =i -1; c =1;
31
end
32
end
33
end
34
disp ( x1 )
35 endfunction
36 mul (1110 ,1011) ;
// f u n c t i o n f o r
34
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
subtraction of binaries
c =0; m =0;
for i =1:5
x1 (1 , i ) =0
end
for i =5: -1:1
if c ==1 then
if x ( i ) ==0 & y ( i ) ==1 then
x1 (1 , i ) =0; c =1;
elseif x ( i ) ==0 & y ( i ) ==0
x1 (1 , i ) =1; c =0;
elseif x ( i ) ==1 & y ( i ) ==1
x1 (1 , i ) =1; c =1;
elseif x ( i ) ==1 & y ( i ) ==0
x1 (1 , i ) =0; c =0;
end
else
if x ( i ) ==1 & y ( i ) ==1 then
x1 (1 , i ) =0; c =0;
elseif x ( i ) ==0 & y ( i ) ==0
x1 (1 , i ) =0; c =0;
elseif x ( i ) ==1 & y ( i ) ==0
x1 (1 , i ) =1; c =0;
elseif x ( i ) ==0 & y ( i ) ==1
x1 (1 , i ) =1; c =1;
end
end
end
disp ( x1 , Remainder = )
endfunction
d1 =11011001; d2 =01011; d22 =[0 ,0 ,0 ,0 ,0]
for i =8: -1:1
d3 = d1 /10;
div (1 , i ) = int (10*( d3 - int ( d3 ) ) )
d1 = d1 /10
end
for i =5: -1:1
d3 = d2 /10;
35
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
36
1 // Example 1 . 2 6
2 // M u l t i p l i c a t i o n
3 // Page no . 19
4 clc ; clear ; close ;
5
6
7 function [ x1 ]= mul (x , y )
8
for i =1:8
9
x1 (1 , i ) =0
10
end
11
printf ( M u l t i p l i c a t i o n o f %. 4 i and %. 4 i = ,x , y )
12
x=x*y;
13
c =0;
14
for i =10: -1:1
15
x = x /10;
16
xd = floor (( x - fix ( x ) ) *10+0.1)
17
if c ==1 then
18
if xd ==0 then
19
x1 (1 , i ) =1; c =0
20
elseif xd ==1
21
x1 (1 , i ) =0;
22
c =1;
23
elseif xd ==2
24
x1 (1 , i ) =1; c =1;
25
end
26
else
27
if xd ==0 | xd ==1 then
28
x1 (1 , i ) = xd ; c =0
29
elseif xd ==2
30
x1 (1 , i ) =0;
31
i =i -1; c =1;
32
end
33
end
34
end
35
for i =1:10
36
if x1 (1 , i ) ==1 then
37
x1 (1 ,i -1) =1;
38
break
37
39
end
40
end
41
disp ( x1 )
42 endfunction
43 mul (1110 ,1011) ;
%i \ n N o r m a l i z e d F l o a t i n g P o i n t
R e p r e s e n t a t i o n o f %g = %. 4 f x 10 ,i , x1 , x
)
16
17
18
19
20
21
else
for i =1:10
x = x *10
if ceil ( x ) ~=0 then
break
end
38
22
23
24
end
x = x /10; i =i -1;
printf ( \n
%i \ n N o r m a l i z e d F l o a t i n g P o i n t
R e p r e s e n t a t i o n o f %g = %. 4 f x 10 ,i , x1 , x
)
25
end
26
27 endfunction
28
29 x =[25.12 , -0.00287 ,87000];
30 for i =1:3
31
fp ( x ( i ) )
32 end
// Example 1 . 3 1
39
2 //Add
3 // Page no . 26
4 clc ; clear ; close ;
5 a =0.4532 e5 ; b =0.5427 e7 ;
6 c=a+b
7 printf ( A d d i t i o n o f %. 4 g and %. 6 g = %. 6 g ,a ,b , c )
i ) ,b ( i ) ,c ( i ) )
40
10 end
,a ( i ) ,b ( i ) ,c ( i ) )
9 end
\n ,a ( i ) ,b ( i ) ,c ( i ) )
9 end
41
o f %. 3 g and %. 2 g = %. 6 g
e99 ,0.1000 e3 ];
for i =1:3
c(i)=a(i)/b(i)
printf ( \ n D i v i s i o n o f %. 2 g by %. 3 g= %. 3 g \n ,a ( i )
,b ( i ) ,c ( i ) )
end
42
Chapter 2
Scope of Numerical and
Mathematical Methods
// Example 2 . 4
// S o l v i n g S i m u l t a n e o u s L i n e a r E q u a t i o n
// Page 36
clc ; close ; clear ;
// e q 1= 5x 331y =3.5
// e q 2= 6x 397y =5.2
43
V a l u e= ) ;
// p e r f o r m i n g i n t e g r a t i o n w i t h
r e s p e c t t o dx
44
Chapter 3
Errors and Their Propagation
45
5 l =28; d =5;
6 v = %pi * l *( d /2) ^2;
7 printf ( \ nVolume o f C y l i n d e r= %f cu . cm ,v ) ;
8 re_d =0.1; re_l = -0.5;
9 re_v =2* re_d + re_l ;
// r e l a t i v e e r r o r
computation
10 printf ( \n\ n R e l a t i v e e r r o r i n volume= %f %% , re_v ) ;
// Example 3 . 3
// A b s o l u t e , R e l a t i v e and P e r c e t a g e E r r o r s
// Page no . 48
clc ; clear ; close ;
x =0.00006; x1 =0.00005;
ex =x - x1 ;
// a b s o l u t e e r r o r
Ex = ex / x1 ;
// r e l a t i v e e r r o r
px =100* Ex ;
// p e r c e n t a g e e r r o r
printf ( \ n A b s o l u t e E r r o r= %f\ n R e l a t i v e E r r o r= %f\
n P e r c e n t a g e E r r o r= %f %% ,ex , Ex , px ) ;
46
7 Ex = ex / x1 ;
// r e l a t i v e e r r o r
8 px =100* Ex ;
// p e r c e n t a g e e r r o r
9 printf ( \ n A b s o l u t e E r r o r= %f\ n R e l a t i v e E r r o r= %f\
n P e r c e n t a g e E r r o r= %f %% ,ex , Ex , px ) ;
// Example 3 . 5
// A b s o l u t e , R e l a t i v e and P e r c e n t a g e E r r o r s
// Page no . 52
clc ; clear ; close ;
x =9.12345; y =7.654321;
x1 =9.1234; y1 =7.6543;
// on a 5 d e c i m a l c o m p u t e r
ex =x - x1 ;
// a b s o l u t e e r r o r o f x
ey =y - y1 ;
// a b s o l u t e e r r o r o f y
z1 = x1 + y1 ;
printf ( \ n A b s o l u t e E r r o r i n x= %f , ex ) ;
printf ( \ n A b s o l u t e E r r o r i n y= %f , ey ) ;
printf ( \ n A d d i t i o n on a 5 d e c i m a l c o m p u t e r y i e l d s = %
. 5 g , z1 ) ;
z2 =16.777;
printf ( \ n A b s o l u t e T o t a l E r r o r= %f ,x +y - z2 ) ;
printf ( \ n A b s o l u t e P r o p a g a t e d E r r o r= %f ,x +y - z1 ) ;
printf ( \ n A b s o l u t e Round o f f E r r o r= %. 4 g ,z1 - z2 ) ;
printf ( \ n R e a l t i v e T o t a l E r r o r= %. 4 g ,( x +y - z2 ) /( x + y )
);
printf ( \ n R e l a t i v e P r o p a g a t e d E r r o r= %. 2 g ,( x +y - z1 )
/( x + y ) ) ;
printf ( \ n R e l a t i v e Round o f f E r r o r= %. 3 g ,( z1 - z2 ) /( x
+y));
printf ( \ nBound on t h e p r o p a g a t e d r e l a t i v e e r r o r= %f
,2*10^ -4) ;
printf ( \ nBound on t h e t o t a l r e l a t i v e e r r o r= %f
47
,3*10^ -4) ;
22 printf ( \ nAs we can s e e t h a t b o t h t h e p r o p a g a t e d and
t o t a l r e l a t i v e e r r o r a r e l e s s t h a n t h e i r bound
values )
48
Chapter 4
Programming Tools and
Techniques
// Example 4 . 1
// Q u a d r a t i c E q u a t i o n
// Page no . 96
clc ; clear ; close ;
a = input ( E n t e r v a l u e o f a= ) ;
b = input ( E n t e r v l a u e o f b= ) ;
c = input ( E n t e r v a l u e o f c= ) ;
x1 =( -1* b + sqrt (( b ^2) -4* a * c ) ) /(2* a ) ;
x2 =( -1* b - sqrt (( b ^2) -4* a * c ) ) /(2* a ) ;
printf ( \ n 1 s t Root= %f , x1 ) ;
printf ( \ n2nd Root= %f , x2 ) ;
// 1 s t r o o t
// 2 nd r o o t
1 // Example 4 . 2
2 // D a t a b a s e Management
3 // Page no . 112
4 clc ; clear ; close ;
5 M
=[12 ,25 ,21 ,35;25 ,7 ,23 ,29;10 ,27 ,7 ,36;26 ,26 ,26 ,35;29 ,0 ,23 ,30];
// marks
6
7 // c a l c u l a t i o n o f c o m p o s i t e s c o r e
8 for i =1:5 ,
9
j =1; k =0;
10
max1 = M (i , j ) ;
11
if ( max1 < M (i , j +1) )
12
max1 = M (i , j +1)
13
else
14
k =1;
15
end ,
16
17
if ( M (i , j +2) >M (i , j + k ) )
18
max2 = M (i , j +2) ;
19
else
20
max2 = M (i , j ) ;
21
end ,
22
CS (i ,1) = max1 + max2 + M (i ,4) ;
23 end
24
25 I =[ Reg . No . , Name o f S t u d e n t s , T e s t 1 , T e s t 2 ,
26
27
28
29
30
31
32
33
34
35
Test 3 , F inal ;
CS/01 , C . V . Rajan , 12 , 25 , 21 , 35 ;
CS/02 , B . X . Roy , 25 , 07 , 23 , 29 ;
CS/03 , P . C . S a s i k u m a r , 10 , 27 , 07 , 36 ;
CS/04 , B . D . Box , 26 , 26 , 26 , 35 ;
CS/05 , K . K . M u k h e r j e e , 29 , 0 , 23 , 30 ;]
printf ( \n )
for i =1:6
for j =1:6
if (j >2)
printf ( \ t )
50
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
end
printf ( %s ,I (i , j ) ) ;
if ( i ~=1)
if (j >2)
printf ( \ t )
end
printf (
)
end
if ( i ==1 & j ==6)
printf ( C o m p o s i t e S c o r e \n )
end
end
if ( i ~=1)
printf ( %i \n , CS (i -1 ,1) ) ;
end
end
// d i s p ( CS , C o m p o s i t e S c o r e , I ) ;
max1 = CS (1 ,1) ; j =1;
for i =2:5
if ( max1 < CS (i ,1) )
max1 = CS (i ,1) ; j = i ;
end ,
end
printf ( \n\ nTopper i s : \ n%s\ t%s \ t%s ,I (1 ,1) ,I (1 ,2) ,
Composite S c o r e )
65 printf ( \nCS/0 %i \ t \ t%s \ t \ t \ t%i ,j , I ( j +1 ,2) , CS (j ,1) )
51
Chapter 5
Solutions of Algebraic and
Transcendental Equations
9
10
11
12
13
14
n )
while abs ( x1 - x2 ) >e
z =( x1 + x2 ) /2
printf (
%i \ t \ t%f \ t%f \ t%f \ t%f \n ,i , x1 , x2 ,z , f
(z))
if f ( z ) * f ( x1 ) >0
x1 = z
else
52
15
x2 = z
16
end
17
i = i +1
18 end
19 printf ( \n\ nThe s o l u t i o n
o f t h i s e q u a t i o n i s %g
a f t e r %i I t e r a t i o n s ,z ,i -1)
9
10
11
12
13
14
15
16
17
18
19
n )
while abs ( x1 - x2 ) >e
z =( x1 + x2 ) /2
printf (
%i \ t \ t%f \ t%f \ t%f \ t%f \n ,i , x1 , x2 ,z , f
(z))
if f ( z ) * f ( x1 ) >0
x1 = z
else
x2 = z
end
i = i +1
end
printf ( \n\ nThe s o l u t i o n o f t h i s e q u a t i o n i s %g
a f t e r %i I t e r a t i o n s ,z ,i -1)
20
53
21
9
10
11
12
13
14
15
16
17
18
19
20
21
)
printf ( \n
n )
for i =0:19
x3 = x2 * f ( x1 ) /( f ( x1 ) -f ( x2 ) ) + x1 * f ( x2 ) /( f ( x2 ) -f ( x1 ) )
printf ( %i \ t%f \ t%f \ t%f \ t%f \ t%f \ t%f \n ,i , x1 , f ( x1
) ,x2 , f ( x2 ) ,x3 , f ( x3 ) )
if f ( x1 ) * f ( x3 ) >0 then
x1 = x3
else
x2 = x3
end
if abs ( f ( x3 ) ) <e then
break
end
end
printf ( \n\ n T h e r e f o r e t h e s o l u t i o n i s %. 1 0 g , x3 )
54
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
\t
s i g n \ t x4 )
printf ( \n
n )
for i =0:8
x3 =( x1 + x2 ) /2
a = f ( x1 ) -f ( x2 ) ;
s = a * abs (1/ a )
x4 = x3 +( x3 - x2 ) *( s * f ( x3 ) ) / sqrt ( f ( x3 ) -f ( x1 ) * f ( x2 ) )
printf ( %i \ t%f \ t%f \ t%f \ t%f \ t%f \ t%f
%i \ t%f \n
,i , x1 , f ( x1 ) ,x2 , f ( x2 ) ,x3 , f ( x3 ) ,s , x4 )
if f ( x1 ) * f ( x4 ) >0 then
x1 = x4
else
x2 = x4
end
if abs ( f ( x4 ) ) <e then
break
end
end
printf ( \n\ nThe s o l u t i o n o f t h i s e q u a t i o n i s %g
a f t e r %i I t e r a t i o n s ,x4 , i )
printf ( \n\n\ nThere a r e c o m p u t a t i o n e r r o r i n t h e
55
a n s w e r s g i v e n by t h e book i n t h i s e x a m p l e \n\n (
v a l u e o f x1 i s u s e d i n s t e a d o f x2 ) )
// Example 5 . 5
// G e n e r a l I t e r a t i v e Method
// Page no . 154
clc ; clear ; close ;
deff ( x=f ( x ) , x=s q r t (3+5/ x ) )
printf ( n\ t x \ t \ t f ( x ) \n )
printf ( \n )
x =2;
for i =1:8
printf ( %i \t% . 1 0 f \t% . 1 0 f \n ,i ,x , f ( x ) )
x=f(x);
end
printf ( \n\ nThe s o l u t i o n o f t h i s e q u a t i o n a f t e r %i
I t e r a t i o n s i s %. 1 0 f ,i , x )
56
8 x =0;
9 for i =1:7
10
printf ( %i \t% . 1 0 f \t% . 1 0 f \n ,i ,x , f ( x ) )
11
x=f(x);
12 end
13 printf ( \n\ nThe s o l u t i o n o f t h i s e q u a t i o n a f t e r %i
I t e r a t i o n s i s %. 1 0 f ,i , x )
8
9
10
11
12
13
14
n )
x0 =0.5; e =0.0001
for i =1:3
x1 = f ( x0 ) ; x2 = f ( x1 ) ; x3 = f ( x2 ) ;
y = x3 -(( x3 - x2 ) ^2) /( x3 -2* x2 + x1 )
dx0 =y - x0 ;
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \
t% . 1 0 f \n ,i , x0 , x1 , x2 , x3 ,y , dx0 )
x0 = y ;
if abs ( x0 ) <e then
break ;
end
15
16
17
18
19 end
20 printf ( \n\ nThe s o l u t i o n
o f t h i s e q u a t i o n a f t e r %i
57
I t e r a t i o n s i s %. 1 0 f ,i , y )
9
10
11
12
13
14
15
16
17
18
19
)
printf (
n )
x0 =0.5; e =0.00001
for i =1:4
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n
,i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ nThe s o l u t i o n o f t h i s e q u a t i o n a f t e r %i
I t e r a t i o n s i s %. 1 0 f ,i , x1 )
58
9
10
11
12
13
14
15
16
17
18
19
)
printf (
n )
x0 =0; e =0.00001
for i =1:4
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n
,i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ n T h e r e f o r e , t h e r o o t i s %. 1 0 f , x1 )
59
6
7
8
9
10
11
12
13
14
15
16
17
18
19
n )
x0 =2; e =0.00001
for i =1:11
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n
,i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ n T h e r e f o r e , t h i s i s n o t c o n v e r g e n t ( i . e . )
divergent )
)
8 printf (
n )
60
9 x0 =0; e =0.00001
10 for i =1:11
11
x1 = x0 - f ( x0 ) / f1 ( x0 )
12
e1 = abs ( x0 - x1 )
13
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n
,i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
14
15
16
17
18 end
19 printf ( \n\ n T h e r e f o r e ,
it
is
c y c l i c in nature )
9
10
11
12
13
14
)
printf (
n )
x0 =1.45; e =0.00001
for i =1:12
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
printf ( %i \t% . 5 g
\ t \t% . 5 g \ t \t% . 5 g \ t \t% . 5 g \ t
\t% . 5 g \n ,i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
61
15
if abs ( x0 ) <e then
16
break ;
17
end
18 end
19 printf ( \n\ n T h e r e f o r e ,
it
is divergent )
9
10
11
12
13
14
15
16
17
18
19
20
t E r r o r \n )
printf (
n )
x0 =0.567123008; x1 =1; e =0.00001
for i =1:9
x2 = x1 - f ( x1 ) *( x1 - x0 ) /( f ( x1 ) -f ( x0 ) )
e1 = abs ( x0 - x2 )
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \
t% . 1 0 f \n ,i -1 , x0 , f ( x0 ) ,x1 , f ( x1 ) ,x2 , e1 )
x0 = x1 ;
x1 = x2
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ n T h e r e f o r e , t h e r o o t i s %. 1 0 f , x2 )
62
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// f u n c t i o n f o r
differentiation
printf ( n\ t h \ t c \ t x n \ t \ t f ( xn ) \ t \ tF ( xn ) \ t \ t k 1 \ t \ t v \ t \
tXn+1\n )
printf (
n )
x0 =2; e =0.00001; h =0.5; c =0.5;
for i =1:11
h1 = - f ( x0 ) ;
F = f1 ( f ( x0 ) ,f ( x0 + h2 ) )
k1 = h1 * F /2;
v = h * f ( x0 ) /( c *( f ( x0 + c + h ) -f ( x0 + c ) ) ) - k1 / c ;
a =0;
for j =0:3
a = a +( v ^ j ) / factorial ( j +1)
end
x1 = x0 + k1 * a
printf ( %i \ t%g \ t%g \t% . 6 f \t% . 6 f \t% . 6 f \t% . 8 f \ t %
. 5 f \t% . 6 f \n ,i -1 ,h ,c , x0 , f ( x0 ) ,F , k1 ,v , x1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
63
26 end
27 printf ( \n\ n T h e r e f o r e ,
t h e s o l u t i o n i s %. 1 0 f , x1 )
// Example 5 . 1 5
// B r e n t Method
// Page no . 173
clc ; clear ; close ;
deff ( y=f ( x ) , y=x2+x2 )
x1 =0; x2 =0.5; x3 =2;
r = f ( x2 ) / f ( x3 ) ; s = f ( x2 ) / f ( x1 ) ; t = f ( x1 ) / f ( x3 ) ;
q =( t -1) *( r -1) *( s -1) ;
p = r * t *( s -1) *( x2 - x3 ) -s *(1 - r ) *( x2 - x1 ) +( t *s - r ) * x2
printf ( Root i s : %. 1 0 g , x2 +( p / q ) )
// Example 5 . 1 9
// H o r n e r Method
// Page no . 177
clc ; clear ; close ;
deff ( y=f ( x , a1 , a2 , a3 , a4 ) , y=a1 x3+ a2 x2+ a3 x+a4 )
k =1; m =2;
a =[4; -13; -31; -275];
for i =1:10
s =1;
64
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 end
32 printf ( The p o s i t i v e r o o t
33 for i =2:10
34
printf ( %i ,d ( i ) )
35 end
i s %i . ,d (1) )
// Example 5 . 2 0
// L a g u e r r e Method
// Page no . 180
65
11
12
13
14
15
16
17
18
t \ t N r o o t \n )
printf (
n )
xi =1
for i =0:9
Proot = xi -( n * f ( xi ) ) /( f1 ( xi ) + sqrt (( n -1) * f1 ( xi ) ^2 - n
* f ( xi ) * f2 ( xi ) ) )
Nroot = xi -( n * f ( xi ) ) /( f1 ( xi ) - sqrt (( n -1) * f1 ( xi ) ^2 - n
* f ( xi ) * f2 ( xi ) ) )
printf ( %i \ t%i \ t%f \ t%f \ t%f \ t%f \ t%f \ t%f \n ,i ,n ,
xi , f ( xi ) , f1 ( xi ) , f2 ( xi ) , Proot , Nroot )
xi = Proot
end
printf ( \n\ n P r o o t = %f\n\ nNroot = %f , Proot , Nroot )
z r+ , z r ]
66
18
19
20
21
22
23
24
25
26
27
28
29
30 end
31 for i =1:12
32
if i ==1 then
33
printf ( s ( i ) )
34
for j =1:5
35
printf ( \ t \ t \ t%i ,j -1)
36
end
37
elseif i <=4
38
printf ( \n %s ,s ( i ) )
39
for j =1:5
40
printf ( \ t \t% . 1 0 f , zi (i -1 , j ) )
41
end
67
42
elseif i <=7
43
printf ( \n %s ,s ( i ) )
44
for j =1:5
45
printf ( \ t \t% . 1 0 f
46
end
47
elseif i <=8
48
printf ( \n %s ,s ( i ) )
49
for j =1:5
50
printf ( \ t \t% . 1 0 f
51
end
52
elseif i <=9
53
printf ( \n %s ,s ( i ) )
54
for j =1:5
55
printf ( \ t \t% . 1 0 f
56
end
57
elseif i <=10
58
printf ( \n %s ,s ( i ) )
59
for j =1:5
60
printf ( \ t \t% . 1 0 f
61
end
62
elseif i <=11
63
printf ( \n %s ,s ( i ) )
64
for j =1:5
65
printf ( \ t \t% . 1 0 f
66
end
67
elseif i <=12
68
printf ( \n %s ,s ( i ) )
69
for j =1:5
70
printf ( \ t \t% . 1 0 f
71
end
72
end
73 end
74 printf ( \n\ nAt t h e end o f t h e
, fz (i -4 , j ) )
, li ( j ) )
, di ( j ) )
, gi ( j ) )
,z ( j ) )
, zi ( j ) )
%i i t e r a t i o n , t h e r o o t
o f t h e e q u a t i o n i s %. 1 0 f ,j -2 , z ( j ) )
68
18
19
20
21
22
23
24
l i +1 , h i , h i +1 , z i +1 ,D+ , D ]
li (1) =( zi (3 ,1) - zi (2 ,1) ) /( zi (2 ,1) - zi (1 ,1) )
hi (1) = zi (3 ,1) - zi (2 ,1) ;
for i =2:6
for j =1:3
fz (j ,i -1) = f ( zi (j ,i -1) )
end
di (i -1) =1+ li (i -1)
gi (i -1) = fz (1 ,i -1) * li (i -1) ^2 - fz (2 ,i -1) * di (i -1) ^2+
fz (3 ,i -1) *( li (i -1) + di (i -1) )
D1 (i -1) = gi (i -1) + sqrt ( gi (i -1) ^2 -4* fz (3 ,i -1) * di (i
-1) * li (i -1) *( fz (1 ,i -1) * li (i -1) - fz (2 ,i -1) * di (i
-1) + fz (3 ,i -1) ) )
D2 (i -1) = gi (i -1) - sqrt ( gi (i -1) ^2 -4* fz (3 ,i -1) * di (i
-1) * li (i -1) *( fz (1 ,i -1) * li (i -1) - fz (2 ,i -1) * di (i
-1) + fz (3 ,i -1) ) )
if abs ( D1 (i -1) ) > abs ( D2 (i -1) ) then
li ( i ) = -2* fz (3 ,i -1) * di (i -1) / D1 (i -1)
else
li ( i ) = -2* fz (3 ,i -1) * di (i -1) / D2 (i -1)
end
hi ( i ) = li ( i ) * hi (i -1) ;
69
25
z (i -1) = zi (3 ,i -1) + hi ( i )
26
for j =1:2
27
zi (j , i ) = zi ( j +1 ,i -1)
28
end
29
zi (3 , i ) = z (i -1)
30 end
31 for i =1:16
32
if i ==1 then
33
printf ( s ( i ) )
34
for j =1:5
35
printf ( \ t \ t \ t%i ,j -1)
36
end
37
elseif i <=4
38
printf ( \n %s ,s ( i ) )
39
for j =1:5
40
printf ( \ t \t% . 1 0 f , zi (i -1 , j ) )
41
end
42
elseif i <=7
43
printf ( \n %s ,s ( i ) )
44
for j =1:5
45
printf ( \ t \t% . 1 0 f , fz (i -4 , j ) )
46
end
47
elseif i <=8
48
printf ( \n %s ,s ( i ) )
49
for j =1:5
50
printf ( \ t \t% . 1 0 f , li ( j ) )
51
end
52
elseif i <=9
53
printf ( \n %s ,s ( i ) )
54
for j =1:5
55
printf ( \ t \t% . 1 0 f , di ( j ) )
56
end
57
elseif i <=10
58
printf ( \n %s ,s ( i ) )
59
for j =1:5
60
printf ( \ t \t% . 1 0 f , gi ( j ) )
61
end
62
elseif i <=11
70
63
printf ( \n %s ,s ( i ) )
64
for j =1:5
65
printf ( \ t \t% . 1 0 f
66
end
67
elseif i <=12
68
printf ( \n %s ,s ( i ) )
69
for j =1:5
70
printf ( \ t \t% . 1 0 f
71
end
72
elseif i <=13
73
printf ( \n %s ,s ( i ) )
74
for j =1:5
75
printf ( \ t \t% . 1 0 f
76
end
77
elseif i <=14
78
printf ( \n %s ,s ( i ) )
79
for j =1:5
80
printf ( \ t \t% . 1 0 f
81
end
82
elseif i <=15
83
printf ( \n %s ,s ( i ) )
84
for j =1:5
85
printf ( \ t \t% . 1 0 f
86
end
87
elseif i <=16
88
printf ( \n %s ,s ( i ) )
89
for j =1:5
90
printf ( \ t \t% . 1 0 f
91
end
92
end
93 end
94 printf ( \n\ nAt t h e end o f t h e
, li ( j +1) )
, hi ( j ) )
, hi ( j +1) )
,z ( j ) )
, D1 ( j ) )
, D2 ( j ) )
%ith i t e r a t i o n , t h e
r o o t o f t h e e q u a t i o n i s %. 1 0 f ,j -1 , z ( j ) )
71
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Example 5 . 2 3
// B a i r s t o w H i t c h c o c k Method
// Page no . 187
clc ; clear ; close ;
deff ( y=f ( x , p , q ) , y=x2+p z+q )
a =[1 , -1 ,1 , -1 ,1]
a =a ; a =[ a ,a ,a ,a , a ]
printf ( I t e r a t i o n > )
for i =1:5
printf ( \ t%i \ t ,i )
end
printf ( \n
)
p (1 ,1) = -1.2; q (1 ,1) =0.95;
s =[ b1 , b2 , b3 , b4 , c 1 , c 2 , c 3 , c 4 , c , dp ,
dq , p , q ]
// s 1 =[ b1 ; b2 ; b3 ; b4 ; c 1 ; c 2 ; c 3 ; c 4 ; c ; dp ; dq ; p ; q ]
for i =1:5
b (1 , i ) =0; b (2 , i ) = a (1 , i ) ; c (1 , i ) =0; c (2 , i ) = a (1 , i ) ;
for k =1:4
b ( k +2 , i ) = a ( k +1 , i ) -p (1 , i ) * b ( k +1 , i ) -q (1 , i ) *
b (k , i )
c ( k +2 , i ) = b ( k +2 , i ) -p (1 , i ) * c ( k +1 , i ) -q (1 , i ) *
c (k , i )
end
cb (1 , i ) = c (6 , i ) -b (6 , i ) ;
dq (1 , i ) =( b (6 , i ) * c (4 , i ) -b (5 , i ) * cb (1 , i ) ) /( c (4 , i )
^2 - cb (1 , i ) * c (3 , i ) )
dp (1 , i ) =( b (5 , i ) * c (4 , i ) -b (6 , i ) * c (3 , i ) ) /( c (4 , i )
^2 - cb (1 , i ) * c (3 , i ) )
p (1 , i +1) = p (1 , i ) + dp (1 , i ) ; q (1 , i +1) = q (1 , i ) + dq (1 , i )
;
end
for j =1:13
printf ( \n
%s\ t \ t ,s ( j ) )
72
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
if j <5 then
for i =1:5
printf ( %. 9 f \ t ,b ( j +2 , i ) )
end
elseif j <9 then
for i =1:5
printf ( %. 9 f \ t ,c (j -2 , i ) )
end
elseif j <10
for i =1:5
printf ( %. 9 f \ t , cb (1 , i ) )
end
elseif j <11
for i =1:5
printf ( %. 9 f \ t , dp (1 , i ) )
end
elseif j <12
for i =1:5
printf ( %. 9 f \ t , dq (1 , i ) )
end
elseif j <13
for i =1:5
printf ( %. 9 f \ t ,p (1 , i +1) )
end
else
for i =1:5
printf ( %. 9 f \ t ,q (1 , i +1) )
end
end
end
z = poly (0 , z ) ;
a = f (z , p (1 , i +1) ,q (1 , i +1) ) ;
printf ( \n\ nRoots f o r Q u a d r a t i c E q u a t i o n Q = )
disp ( a )
a = roots ( a )
printf ( \n\ t a r e \n )
disp ( a (1) )
disp ( a (2) )
73
// Example 5 . 2 4
// B e r n o u l l i Method
// Page no . 189
clc ; clear ; close ;
a =[1 , -8 , -15 ,10];
for i =1:2
c ( i ) =0;
end
c (3) =1;
for k =4:13
c ( k ) = -( a (2) * c (k -1) + a (3) * c (k -2) + a (4) * c (k -3) )
r (k -3) = c ( k ) / c (k -1)
end
disp (c , Ck V a l u e s )
disp (r , Rk V a l u e s )
disp ( r (k -3) , T h e r e f o r e t h e e x a c t r o o t i s = )
74
) * a (k -1 , i + j )
14
break
15
j = j +1;
16
end
17
end
18 end
19 printf ( \ t \ t \ t \ t a 1 \ t \ t \ t \ t a 2 \ t \ t \ t \ t a 3 \n k \ t a 0 \ t a 1 \ t
20
21
22
23
24
25
26
27
\ t \t \ t a 2 \ t \ t \t \ t a 3 \ t \ t \t \n\ t \ t \ t \ t a 0 \ t \ t \ t \
ta1 \ t \ t \ t \ ta2 )
printf ( \n
n )
for i =1:4
printf ( %i \ t%g \t% . 4 g \ t \t% . 5 g \ t \t% . 9 g \ t \t% . 8 g \
t%g \ t \t% . 1 0 g \n ,i -1 , a (i ,1) ,a (i ,2) , abs ( a (i ,2) /
a (i ,1) ) ^(1/(2^( i -1) ) ) ,a (i ,3) , abs ( a (i ,3) / a (i
,2) ) ^(1/(2^( i -1) ) ) ,a (i ,4) , abs ( a (i ,4) / a (i ,3) )
^(1/(2^( i -1) ) ) )
end
for i =5:6
printf ( %i \ t%g \t% . 4 g \t% . 5 g \ t \t% . 9 g \t% . 8 g \t% . 7 g \
t% . 1 0 g \n ,i -1 , a (i ,1) ,a (i ,2) , abs ( a (i ,2) / a (i ,1)
) ^(1/(2^( i -1) ) ) ,a (i ,3) , abs ( a (i ,3) / a (i ,2) )
^(1/(2^( i -1) ) ) ,a (i ,4) , abs ( a (i ,4) / a (i ,3) )
^(1/(2^( i -1) ) ) )
end
printf ( \n\ nThe A b s o l u t e V a l u e s o f t h e r o o t s a r e %g ,
%. 8 g and %g , abs ( a (i ,2) / a (i ,1) ) ^(1/(2^( i -1) ) ) ,
abs ( a (i ,3) / a (i ,2) ) ^(1/(2^( i -1) ) ) , abs ( a (i ,4) / a (i
,3) ) ^(1/(2^( i -1) ) ) )
75
25
26
27
28
// Example 5 . 2 6
//QD Method
// Page no . 194
clc ; clear ; close ;
n )
for i =1:14
for j =1:3
printf ( \ t \t% . 1 0 f \ t ,q (i , j ) )
end
76
29
printf ( \n )
30
for j =1:4
31
printf ( %. 1 0 f \ t \ t \ t ,e (i , j ) )
32
end
33
printf ( \n )
34 end
35 printf ( \ t \t% . 1 0 f \ t \ t \t% . 1 0 f \ t \ t \t% . 1 0 f \n ,q (15 ,1) ,q
and
%. 1 0
// Example 5 . 2 7
// L i n e a r I t e r a t i o n Method
// Page no . 198
clc ; clear ; close ;
deff ( x=f ( x ) , x =20/( x 2+2 x +10) )
printf ( n\ t x \ t \ t f ( x ) \n )
printf ( \n )
x =1;
for i =1:19
printf ( %i \t% . 1 0 f \t% . 1 0 f \n ,i ,x , f ( x ) )
x1 = x ;
x=f(x);
end
printf ( \n\ nx = %. 1 0 f , x1 )
77
9
10
11
12
13
14
15
n )
x0 =1; e =0.0001
for i =1:3
x1 = f ( x0 ) ; x2 = f ( x1 ) ; x3 = f ( x2 ) ;
y = x3 -(( x3 - x2 ) ^2) /( x3 -2* x2 + x1 )
dx0 =y - x0 ;
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \
t% . 1 0 f \n ,i , x0 , x1 , x2 , x3 ,y , dx0 )
x0 = y ;
if abs ( x0 ) <e then
break ;
end
16
17
18
19
20 end
21 printf ( \n\ nThe s o l u t i o n
o f t h i s e q u a t i o n a f t e r %i
I t e r a t i o n s i s %. 1 0 f ,i , y )
// Example 5 . 2 9
// Newton Raphson Method
// Page no . 199
78
9
10
11
12
13
14
15
16
17
18
19
)
printf (
n )
x0 =01; e =0.00001
for i =1:4
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n
,i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ nThe s o l u t i o n o f t h i s e q u a t i o n a f t e r %i
I t e r a t i o n s i s %. 1 0 f ,i , x1 )
log (x) )
6 printf ( n\ t x n \ t \ t f ( xn ) \ t \ tXn+1\ t \ t f ( Xn+1) \ t \ tXn+2\ t \
t E r r o r \n )
7 printf (
79
8
9
10
11
12
13
14
15
16
17
18
19
n )
x0 =0.1; x1 =1.2; e =0.00001
for i =1:7
x2 = x1 - f ( x1 ) *( x1 - x0 ) /( f ( x1 ) -f ( x0 ) )
e1 = abs ( x0 - x2 )
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \
t% . 1 0 f \n ,i -1 , x0 , f ( x0 ) ,x1 , f ( x1 ) ,x2 , e1 )
x0 = x1 ;
x1 = x2
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ n T h e r e f o r e , t h e r o o t i s %. 1 0 f , x2 )
Scilab code Exa 5.32 Regula Falsi Newton Raphson and Mullers Method
1 // Example 5 . 3 2
2 // R e g u l a F a l s i , Newton Raphson and M u l l e r s Method
3 // Page no . 201
4 clc ; clear ; close ;
5 deff ( x=f ( x ) , x=x 5 3.7 x 4 + 7 . 4 x 3 10.8 x 2 + 1 0 . 8 x
6
7
8
9
10
6.8 )
deff ( x=f 1 ( x ) , x=5x 4 43.7 x 3 + 3 7 . 4 x 2 21.6 x
+10.8 )
// newton r a p h s o n
printf ( n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( xn ) \ t \ tXn+1\ t \ t E r r o r \n
)
printf (
n )
x0 =1.5; e =0.00001
80
11 for i =1:4
12
x1 = x0 - f ( x0 ) / f1 ( x0 )
13
e1 = abs ( x0 - x1 )
14
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n
,i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
15
16
17
18
19 end
20 printf ( \n\ nThe s o l u t i o n
o f t h i s e q u a t i o n by newton
r a p h s h o n a f t e r %i I t e r a t i o n s i s %. 1 0 f \n\n\n ,i , x1
)
21
22 // r e g u l a f a l s i
23 x1 =1; x2 =2; e =0.00001
24 printf ( n\ t x 1 \ t \ t f ( x1 ) \ t \ t x 2 \ t \ t f ( x2 ) \ t \ t x 3 \ t \ t f ( x3 )
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
)
printf ( \n
n )
for i =0:7
x3 = x2 * f ( x1 ) /( f ( x1 ) -f ( x2 ) ) + x1 * f ( x2 ) /( f ( x2 ) -f ( x1 ) )
printf ( %i \ t%f \ t%f \ t%f \ t%f \ t%f \ t%f \n ,i , x1 , f ( x1
) ,x2 , f ( x2 ) ,x3 , f ( x3 ) )
if f ( x1 ) * f ( x3 ) >0 then
x1 = x3
else
x2 = x3
end
if abs ( f ( x3 ) ) <e then
break
end
end
printf ( \n\ n T h e r e f o r e t h e s o l u t i o n by r e g u l a f a l s i
method a f t e r %i i t e r a t i o n s i s %. 1 0 g ,i , x3 )
// m u l l e r s method
81
41 zi =[1;2;3];
42 s =[ i , z 0 , z 1 , z 2 , f 0 , f 1 , f 2 , l i , d i , g i ,
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
l i +1 , h i , h i +1 , z i +1 ,D+ , D ]
li (1) =( zi (3 ,1) - zi (2 ,1) ) /( zi (2 ,1) - zi (1 ,1) )
hi (1) = zi (3 ,1) - zi (2 ,1) ;
for i =2:6
for j =1:3
fz (j ,i -1) = f ( zi (j ,i -1) )
end
di (i -1) =1+ li (i -1)
gi (i -1) = fz (1 ,i -1) * li (i -1) ^2 - fz (2 ,i -1) * di (i -1) ^2+
fz (3 ,i -1) *( li (i -1) + di (i -1) )
D1 (i -1) = gi (i -1) + sqrt ( gi (i -1) ^2 -4* fz (3 ,i -1) * di (i
-1) * li (i -1) *( fz (1 ,i -1) * li (i -1) - fz (2 ,i -1) * di (i
-1) + fz (3 ,i -1) ) )
D2 (i -1) = gi (i -1) - sqrt ( gi (i -1) ^2 -4* fz (3 ,i -1) * di (i
-1) * li (i -1) *( fz (1 ,i -1) * li (i -1) - fz (2 ,i -1) * di (i
-1) + fz (3 ,i -1) ) )
if abs ( D1 (i -1) ) > abs ( D2 (i -1) ) then
li ( i ) = -2* fz (3 ,i -1) * di (i -1) / D1 (i -1)
else
li ( i ) = -2* fz (3 ,i -1) * di (i -1) / D2 (i -1)
end
hi ( i ) = li ( i ) * hi (i -1) ;
z (i -1) = zi (3 ,i -1) + hi ( i )
for j =1:2
zi (j , i ) = zi ( j +1 ,i -1)
end
zi (3 , i ) = z (i -1)
end
printf ( \n\n )
for i =1:16
if i ==1 then
printf ( s ( i ) )
for j =1:5
printf ( \ t \ t \ t %i ,j -1)
end
printf ( \n
82
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
)
elseif i <=4
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f , zi (i -1 , j ) )
end
elseif i <=7
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f , fz (i -4 , j ) )
end
elseif i <=8
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f , li ( j ) )
end
elseif i <=9
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f , di ( j ) )
end
elseif i <=10
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f , gi ( j ) )
end
elseif i <=11
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f , li ( j +1) )
end
elseif i <=12
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f , hi ( j ) )
end
elseif i <=13
83
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
end
129 end
130 printf (
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f
end
elseif i <=14
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f
end
elseif i <=15
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f
end
elseif i <=16
printf ( \n %s ,s ( i ) )
for j =1:5
printf ( \ t \t% . 1 0 f
end
, hi ( j +1) )
,z ( j ) )
, D1 ( j ) )
, D2 ( j ) )
84
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// newton r a p h s o n
x9 =[1.5 ,2.5 ,2.7 ,3.1;4 ,5 ,14 ,10]
for h =1:4
x0 = x9 (1 , h ) ; e =0.00001
for i =1: x9 (2 , h )
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
end
printf ( \ nThe s o l u t i o n o f t h i s e q u a t i o n by newton
r a p h s h o n a f t e r %i I t e r a t i o n s i s %. 5 f \n ,i , x1 )
21 end
22
23 // m u l l e r s method
24 zx =[1 ,2 ,2.7 ,3.1;2 ,3 ,3.7 ,4.1;3 ,4 ,4.7 ,5.1]
25 zi =[1;2;3];
26 s =[ i , z 0 , z 1 , z 2 , f 0 , f 1 , f 2 , l i , d i , g i ,
l i +1 , h i , h i +1 , z i +1 ,D+ , D ]
27 li (1) =( zi (3 ,1) - zi (2 ,1) ) /( zi (2 ,1) - zi (1 ,1) )
28 hi (1) = zi (3 ,1) - zi (2 ,1) ;
29 for i =2:4
30
for j =1:3
31
fz (j ,i -1) = f ( zi (j ,i -1) )
32
end
33
di (i -1) =1+ li (i -1)
34
gi (i -1) = fz (1 ,i -1) * li (i -1) ^2 - fz (2 ,i -1) * di (i -1) ^2+
35
36
37
38
li ( i ) = -2* fz (3 ,i -1) * di (i -1) / D1 (i -1)
39
else
40
li ( i ) = -2* fz (3 ,i -1) * di (i -1) / D2 (i -1)
41
end
42
hi ( i ) = li ( i ) * hi (i -1) ;
43
z (i -1) = zi (3 ,i -1) + hi ( i )
44
for j =1:2
45
zi (j , i ) = zi ( j +1 ,i -1)
46
end
47
zi (3 , i ) = z (i -1)
48 end
49 printf ( \n\ nAt t h e end o f t h e %ith i t e r a t i o n by
m u l l e r s method , t h e r o o t o f t h e e q u a t i o n i s %. 1 0 f
,j +2 , z ( j ) )
// Example 5 . 3 4
//QD Method
// Page no . 202
clc ; clear ; close ;
a =[1 ,2 ,10 , -20]
for i =1:5
e (i ,1) =0;
e (i ,4) =0;
end
q (1 ,1) = - a (2) / a (1) ;
q (1 ,2) =0; q (1 ,3) =0;
e (1 ,2) = a (3) / a (2) ;
e (1 ,3) = a (4) / a (3) ;
for i =2:7
for j =1:3
q (i , j ) = e (i -1 , j +1) + q (i -1 , j ) -e (i -1 , j )
86
17
end
18
for
19
20
end
21 end
22 printf (
23 printf (
24
25
26
27
28
29
30
31
32
33
34
35
j =1:2
e (i , j +1) = e (i -1 , j +1) * q (i , j +1) / q (i , j )
e 0 \ t \ t q 1 \ t \ t e 1 \ t \ t q 2 \ t \ t e 2 \ t \ t q 3 \ t \ t e 3 \n )
n )
for i =1:7
for j =1:3
printf ( \ t \t% . 1 0 f \ t ,q (i , j ) )
end
printf ( \n )
for j =1:4
printf ( %. 1 0 f \ t \ t \ t ,e (i , j ) )
end
printf ( \n )
end
printf ( \ t \t% . 1 0 f \ t \ t \t% . 1 0 f \ t \ t \t% . 1 0 f \n ,q (7 ,1) ,q
(7 ,2) ,q (7 ,3) )
printf ( \ nThe e x a c t r o o t s a r e \t% . 1 0 f
and
%. 1 0
f ,q (7 ,1) ,q (7 ,3) )
87
8
9
10
11
12
13
14
15
16
17
18
19
20
printf ( n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( xn ) \ t \ tXn+1\ t \ t E r r o r \n
)
printf (
n )
x0 =10; e =0.00001
for i =1:4
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n
,i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ nThus t h e b a l l i s submerged u p t o h e i g h t
o f %. 1 0 f cm\n\n\n , x1 )
/ ( a+x ) )
7
8
88
10
11
12
13
14
15
16
17
18
19
20
21
n )
x0 =20000; x1 =25000; e =0.00001
for i =1:8
x2 = x1 - f ( x1 ) *( x1 - x0 ) /( f ( x1 ) -f ( x0 ) )
e1 = abs ( x0 - x2 )
printf ( %i \ t%f \t% . 1 0 f \ t%f \t% . 1 0 f \ t%f \t% . 1 0 f \n ,
i -1 , x0 , f ( x0 ) ,x1 , f ( x1 ) ,x2 , e1 )
x0 = x1 ;
x1 = x2
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ n T h e r e f o r e , Rb = %. 1 0 f ohm , x2 )
10
11
12
13
)
printf (
n )
x0 =0.1; e =0.00001
for i =1:10
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
89
14
15
16
17
18
19 end
20 printf ( \n\ n T h e r e f o r e , Volume v = %. 1 0 f
l t r , x1 )
9
10
11
12
13
14
15
16
17
18
)
printf (
n )
x0 =6; e =0.00001
for i =1:10
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n
,i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
end
90
19
9
10
11
12
13
14
15
16
17
18
19
)
printf (
n )
x0 =10; e =0.00001
for i =1:10
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n
,i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ n T h e r e f o r e , s i d e s a r e = %. 5 f m x %. 5 f m
x %. 5 f m ,x1 , x1 ,20/ x1 ^2)
91
9
10
11
12
13
14
15
16
17
18
19
)
printf (
n )
x0 =1; e =0.00001
for i =1:10
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
printf ( %i \t% . 1 0 f \t% . 6 f \t% . 5 f \t% . 1 0 f \t% . 1 0 f \n ,
i -1 , x0 , f ( x0 ) , f1 ( x0 ) ,x1 , e1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ t \ t \ t \ t \ t 2 \n T h e r e f o r e , M a g n e t i c Flux = %
. 5 f Wb m , x1 )
92
Chapter 6
Numerical Methods of Linear
Equations Direct Methods
augmented m a t r i x
7
8 // t r i a n g u l a r i z a t i o n
9 for i =1:4
10
B (1 , i ) = A (1 , i )
11
B (2 , i ) = A (2 , i ) -( A (2 ,1) / A (1 ,1) ) * A (1 , i )
12
B (3 , i ) = A (3 , i ) -( A (3 ,1) / A (1 ,1) ) * A (1 , i )
13 end
14 disp (A , Augmented M a t r i x= )
15 disp (B , T r i a n g u l a t e d M a t r i x= )
16 // back s u b s t i t u t i o n
93
//
Scilab code Exa 6.2 Gaussian Elimination Method for TriDiagonal System
// Example 6 . 2
// G a u s s i a n E l i m i n a t i o n Method f o r Tri D i a g o n a l
System
3 // Page no . 222
4 clc ; clear ; close ;
1
2
5
6
7
8
9
10
11
12
13
// e q u a t i o n m a t r i x
A =[1 ,2 ,0 ,0;2 ,3 , -1 ,0;0 ,4 ,2 ,3;0 ,0 ,2 , -1];
K =[5;5;11;10]; i =1;
// i n i t i a l i z a t i o n
w (1) = A (1 ,2) / A (1 ,1) ;
g (1) = K (1) / A (1 ,1) ;
printf ( \nw ( %i )=%f ,i , w ( i ) ) ; printf ( \ ng ( %i )=%f ,i , g (
i))
14
15 // c o m p u t a t i o n
16 for i =2:3
17
w ( i ) =( A (i , i +1) ) /( A (i , i ) -A (i ,i -1) * w (i -1) )
94
18
19
20
21
22
23
24
end
i =4
m = -2
g ( i ) = m *( K ( i ) -A (i ,i -1) * g (i -1) ) /( A (i , i ) -A (i ,i -1) * w (i
-1) )
25 x ( i ) = g ( i )
26 printf ( \ ng ( %i )=%f ,i , g ( i ) )
27 printf ( \n\ nx ( %i )=%f ,i , x ( i ) )
28
29 // s o l u t i o n
30 for i =3: -1:1
31
x ( i ) = g ( i ) -w ( i ) * x ( i +1)
32
printf ( \n\ nx ( %i )=%f ,i , x ( i ) )
33 end
matrix
8
9 for i =1:3
10
j=i
11
while ( A (i , i ) ==0 & j <=3)
95
// augmented
12
for k =1:4
13
B (1 , k ) = A ( j +1 , k )
14
A ( j +1 , k ) = A (i , k )
15
A (i , k ) = B (1 , k )
16
end
17
disp ( A )
18
j = j +1
19
end
20
disp ( A )
21
for k =4: -1: i
22
A (i , k ) = A (i , k ) / A (i , i )
23
end
24
disp ( A )
25
for k =1:3
26
if ( k ~= i ) then
27
l = A (k , i ) / A (i , i )
28
for m = i :4
29
A (k , m ) = A (k , m ) -l * A (i , m )
30
end
31
end
32
33
end
34
disp ( A )
35 end
36
37 for i =1:3
38
printf ( \ nx ( %i ) = %g\n ,i , A (i ,4) )
39 end
// Example 6 . 4
// G a u s s i a n E l i m i n a t i o n Method w i t h o u t P i v o t i n g
96
3 // Page no . 227
4 clc ; clear ; close ;
5
6 A =[0.3*10^ -11 ,1 ,0.7;1 ,1 ,0.9];
matrix
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// t r i a n g u l a r i z a t i o n
for i =1:3
B (1 , i ) = A (1 , i )
B (2 , i ) = A (2 , i ) -( A (2 ,1) / A (1 ,1) ) * A (1 , i )
end
disp (A , Augmented M a t r i x= )
disp (B , T r i a n g u l a t e d M a t r i x= )
// back s u b s t i t u t i o n
x (2) = B (2 ,3) / B (2 ,2) ;
printf ( \ nx ( 2 )=%f\n ,x (2) )
for i =1: -1:1
k =0
for j = i +1:2
k = k + B (i , j ) * x ( j )
end
x ( i ) =(1/ B (i , i ) ) *( B (i ,3) -k )
printf ( \ nx ( %i )=%f\n ,i , x ( i ) )
end
97
// augmented
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
for j =1:3
printf ( %. 2 f \ t ,U (i , j ) )
end
if ( i ==2)
printf ( =
)
else
printf ( \ t )
end
for j =1:3
98
44
printf ( %. 2 f \ t ,A (i , j ) )
45
end
46
printf ( \n )
47 end
// Example 6 . 6
// T r a n g u l a r i z a t i o n Method
// Page no . 236
clc ; clear ; close ;
A =[2 ,1 ,1;1 ,3 ,1;1 ,1 ,4];
B =[7;10;15];
printf ( A can be f a c t o r i z a e d a s f o l l o w s : \ n )
printf ( \ tL \ t \ t \ t \tU\ t \ t =\ t \tA )
U (2 ,1) =0; U (3 ,1) =0; U (3 ,2) =0;
L (1 ,2) =0; L (1 ,3) =0; L (2 ,3) =0;
for i =1:3
L (i , i ) =1
end
for i =1:3
U (1 , i ) = A (1 , i )
end
L (2 ,1) =1/ U (1 ,1) ;
for i =2:3
U (2 , i ) = A (2 , i ) -U (1 , i ) * L (2 ,1) ;
end
L (3 ,1) =1/ U (1 ,1) ;
L (3 ,2) =( A (3 ,2) -U (1 ,2) * L (3 ,1) ) / U (2 ,2) ;
U (3 ,3) = A (3 ,3) -U (1 ,3) * L (3 ,1) -U (2 ,3) * L (3 ,2) ;
printf ( \n )
for i =1:3
99
27
for j =1:3
28
printf ( %. 2 f \ t ,L (i , j ) )
29
end
30
31
if ( i ==2)
32
printf (
)
33
else
34
printf ( \ t )
35
end
36
37
for j =1:3
38
printf ( %. 2 f \ t ,U (i , j ) )
39
end
40
if ( i ==2)
41
printf ( =
)
42
else
43
printf ( \ t )
44
end
45
for j =1:3
46
printf ( %. 2 f \ t ,A (i , j ) )
47
end
48
printf ( \n )
49 end
50 printf ( \nY=UX )
51
Y = inv ( L ) * B
52
X = inv ( U ) * Y
53 printf ( \n\nX= )
54 for i =1:3
55
printf ( \n
%i ,X (i ,1) )
56 end
100
1 // Example 6 . 7
2 // W i l k i n s o n Method
3 // Page no . 240
4 clc ; clear ; close ;
5
6 A
// Example 6 . 8
// C h o l e s k y s F a c t o r i z a t i o n
// Page no . 243
clc ; clear ; close ;
A =[1 ,2 ,3;2 ,5 ,8;3 ,8 ,22];
U (2 ,1) =0; U (3 ,1) =0; U (3 ,2) =0;
for i =1:3
for j =1:3
if ( i == j )
k =0;
for m =1: i -1
k = k + U (m , i ) ^2;
end
U (i , j ) = sqrt ( A (i , j ) -k )
end
if (j > i )
k =0;
for m =1: i -1
101
20
k = k + U (m , j ) * U (m , i ) ;
21
end
22
U (i , j ) =( A (i , j ) -k ) / U (i , i )
23
end
24
end
25 end
26 disp (U , R e q u i r e d M a t r i x (U)= )
// Example 6 . 9
// Complex System o f L i n e a r E q u a t i o n
// Page no . 244
clc ; clear ; close ;
for i =1:7
s =0;
for j =1:7
A (i , j ) = j ^ i
s = s +( -1) ^( j +1) * A (i , j )
end
B (i ,1) = s ;
end
X = inv ( A ) * B
disp (X , The S o l u t i o n = )
// Example 6 . 1 0
102
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// S o l v i n g M a t r i c e s
// Page no . 244
clc ; close ; clear ;
warning ( o f f )
for i =1:7
s =0;
for j =1:7
A (i , j ) =360360/( i + j )
end
B (i ,1) =1;
end
X = inv ( A ) * B
disp (360360* X , The S o l u t i o n by 3 6 0 3 6 0 X= )
disp (X , F i n a l S o l u t i o n = )
103
Chapter 7
Numerical Solutions for Matrix
Inversion
// Example 7 . 1
// GaussJ o r d a n Two Array Method
// Page no . 254
clc ; clear ; close ;
A =[2 ,6 ,1;3 ,9 ,2;0 , -1 ,3];
C = eye (3 ,3) ;
for i =1:3
and 2
B (1 , i ) = A (1 , i ) ;
A (1 , i ) = A (2 , i ) ;
A (2 , i ) = B (1 , i ) ;
B (2 , i ) = C (1 , i ) ;
C (1 , i ) = C (2 , i ) ;
C (2 , i ) = B (2 , i ) ;
end
printf ( \n )
// m a t r i x
// U n i t M a t r i x
// i n t e r c h a n g e o f row 1
104
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// p r i n t i n g o f m a t r i c e s A and C
for i =1:3
for j =1:3
printf ( %f\ t ,A (i , j ) )
end
printf ( | \ t ) ;
for j =1:3
printf ( %f\ t ,C (i , j ) )
end
printf ( \n )
end
printf ( \n\n ) ;
for i =1:3
A (1 , i ) = A (1 , i ) /3;
C (1 , i ) = C (1 , i ) /3;
end
// p r i n t i n g o f m a t r i c e s A and C
for i =1:3
for j =1:3
printf ( %f\ t ,A (i , j ) )
end
printf ( | \ t ) ;
for j =1:3
printf ( %f\ t ,C (i , j ) )
end
printf ( \n )
end
printf ( \n\n ) ;
for i =1:3
A (2 , i ) = A (2 , i ) -2* A (1 , i ) ;
C (2 , i ) = C (2 , i ) -2* C (1 , i ) ;
end
105
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// p r i n t i n g o f m a t r i c e s A and C
for i =1:3
for j =1:3
printf ( %f\ t ,A (i , j ) )
end
printf ( | \ t ) ;
for j =1:3
printf ( %f\ t ,C (i , j ) )
end
printf ( \n )
end
printf ( \n\n ) ;
for i =1:3
// i n t e r c h a n g e o f row 2 and 3
B (1 , i ) = A (2 , i ) ;
A (2 , i ) = A (3 , i ) ;
A (3 , i ) = B (1 , i ) ;
B (2 , i ) = C (2 , i ) ;
C (2 , i ) = C (3 , i ) ;
C (3 , i ) = B (2 , i ) ;
end
// p r i n t i n g o f m a t r i c e s A and C
for i =1:3
for j =1:3
printf ( %f\ t ,A (i , j ) )
end
printf ( | \ t ) ;
for j =1:3
printf ( %f\ t ,C (i , j ) )
end
printf ( \n )
end
printf ( \n\n ) ;
for i =1:3
A (2 , i ) = -1* A (2 , i ) ;
C (2 , i ) = -1* C (2 , i ) ;
106
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
end
for i =1:3
A (1 , i ) = A (1 , i ) -3* A (2 , i ) ;
C (1 , i ) = C (1 , i ) -3* C (2 , i ) ;
end
// p r i n t i n g o f m a t r i c e s A and C
for i =1:3
for j =1:3
printf ( %f\ t ,A (i , j ) )
end
printf ( | \ t ) ;
for j =1:3
printf ( %f\ t ,C (i , j ) )
end
printf ( \n )
end
printf ( \n\n ) ;
for i =1:3
A (3 , i ) = -3* A (3 , i ) ;
C (3 , i ) = -3* C (3 , i ) ;
end
// p r i n t i n g o f m a t r i c e s A and C
for i =1:3
for j =1:3
printf ( %f\ t ,A (i , j ) )
end
printf ( | \ t ) ;
for j =1:3
printf ( %f\ t ,C (i , j ) )
end
printf ( \n )
end
printf ( \n\n ) ;
for i =1:3
107
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
A (1 , i ) = A (1 , i ) -A (3 , i ) *(29/3) ;
C (1 , i ) = C (1 , i ) -29* C (3 , i ) /3;
end
for i =1:3
A (2 , i ) = A (2 , i ) + A (3 , i ) *3;
C (2 , i ) = C (2 , i ) + C (3 , i ) *3;
end
// p r i n t i n g o f m a t r i c e s A and C
for i =1:3
for j =1:3
printf ( %f\ t ,A (i , j ) )
end
printf ( | \ t ) ;
for j =1:3
printf ( %f\ t ,C (i , j ) )
end
printf ( \n )
end
printf ( \n\n ) ;
disp (C , I n v e r s e M a t r i x o f A )
// Example 7 . 2
// I n v e r s e i n P l a c e w i t h o u t P i v o t i n g
// Page no . 256
clc ; clear ; close ;
// m a t r i x
// c o p i e d m a t r i x
9
printf ( \n\ n S t a g e %i ,i ) ;
10
for j =1:3
11
if ( i == j )
12
B (i , j ) =1/ B (i , j ) ;
13
else
14
B (i , j ) = A (i , j ) / A (i , i ) ;
15
end ,
16
end
17
disp ( B )
18
for j =1:3
19
for k =1:3
20
if ( i ~= j )
21
B (j , k ) = A (j , k ) -A (j , i ) * B (i , k ) ;
22
end ,
23
end
24
end
25
disp ( B )
26
for j =1:3
27
if ( i ~= j )
28
B (j , i ) = -1* A (j , i ) * B (i , i ) ;
29
end ,
30
31
end
32
disp ( B )
33
A=B;
34 end
35 disp (B , I n v e r s e o f M a t r i x A= )
// Example 7 . 3
// I n v e r s e i n P l a c e w i t h P i v o t i n g
// Page no . 258
109
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
for i =1:3
printf ( \n\ n S t a g e %i ,i )
if (i <3)
for j =1:3
// i n t e r c h a n g e o f r o w s
C (i , j ) = A (i , j ) ;
A (i , j ) = A ( i +1 , j ) ;
A ( i +1 , j ) = C (i , j ) ;
C (i , j ) = B (i , j ) ;
B (i , j ) = B ( i +1 , j ) ;
B ( i +1 , j ) = C (i , j ) ;
end
end
disp ( B )
for j =1:3
if ( i == j )
B (i , j ) =1/ B (i , j ) ;
else
B (i , j ) = A (i , j ) / A (i , i ) ;
end ,
end
for j =1:3
for k =1:3
if ( i ~= j )
B (j , k ) = A (j , k ) -A (j , i ) * B (i , k ) ;
end ,
end
end
for j =1:3
if ( i ~= j )
B (j , i ) = -1* A (j , i ) * B (i , i ) ;
end ,
end
110
42
43
44
45
46
47
48
49
50
51
52
53
54
55
disp ( B )
A=B;
end
for j =1:3
// i n t e r c h a n g e o f column 2 and 3
C (j ,1) = A (j ,2) ;
A (j ,2) = A (j ,3) ;
A (j ,3) = C (j ,1) ;
end
for j =1:3
// i n t e r c h a n g e o f column 2 and 1
C (j ,1) = A (j ,2) ;
A (j ,2) = A (j ,1) ;
A (j ,1) = C (j ,1) ;
end
disp (A , I n v e r s e o f M a t r i x A= )
R
7 for i =4: -1:1
8
for j =4: -1:1
9
if (i > j )
10
Y (i , j ) =0;
11
end
12
if ( i == j )
13
Y (i , j ) =1/ R (i , j ) ;
14
end
15
if (i < j )
111
// m a t r i x
16
l =0;
17
for k = i +1: j
18
l =l - R (i , k ) * Y (k , j ) ;
19
end
20
Y (i , j ) = l / R (i , i ) ;
21
end
22
end
23 end
24 disp (Y , I n v e r s e o f M a t r i x R= )
// Example 7 . 5
// I n v e r s e o f Complex M a t r i c e s
// Page no . 262
clc ; clear ; close ;
112
// Example 7 . 6
// I t e r a t i v e P r o c e d u r e
// Page no . 265
clc ; clear ; close ;
A =[3 ,1 ,3/2; -5/4 , -1/4 , -3/4; -1/4 , -1/4 , -1/4];
disp (A , M a t r i x A= ) ;
B =[1 ,1 ,3.5;1 ,3 , -3; -2 , -3 , -4];
disp (B , Assumed M a t r i x B= ) ;
e =0.1;
// i t e r a t i o n s
E1 = e ; k =1;
while ( E1 >= e )
printf ( \n\n\ n I t e r a t i o n %i \n ,k )
C = B *(2* eye (3 ,3) -A * B ) ; disp (C , M a t r i x C= ) ;
E = A *C - eye (3 ,3) ; disp (E , M a t r i x E= ) ;
B = C ; printf ( \ n I n v e r s e o f M a t r i x A a f t e r %i
i t e r a t i o n s = ,k ) ; disp ( B ) ;
E1 =0;
for i =1:3
for j =1:3
E1 = E1 + E (i , j ) ^2;
end
end
E1 = sqrt ( E1 ) ;
k = k +1;
end
113
Chapter 8
Numerical Solutions of Linear
Systems of Equations Iterative
Methods
// Example 8 . 1
// J a c o b i Method
// Page no . 273
clc ; clear ; close ;
A =[8 , -3 ,2;4 ,11 , -1;6 ,3 ,12];
B =[20;33;36]
for i =0:19
X ( i +1 ,1) = i ;
end
for i =2:4
X (1 , i ) =0;
end
for r =1:19
for i =1:3
114
// e q u a t i o n m a t r i x
// s o l u t i o n m a t r i x
16
17
18
19
20
21
22
23
end
24 end
25 printf (
26 printf (
27 disp ( X )
28 printf (
k =0;
for j =1:3
if ( i ~= j )
k =k - A (i , j ) * X (r , j +1) ;
end
end
X ( r +1 , i +1) =( k + B (i ,1) ) / A (i , i ) ;
r\t
x ( r ) \ t \ ty ( r ) \ t
z ( r ) );
\n
)
\n\ n A f t e r 18 i t e r a t i o n s e x a c t s o l u t i o n i s : \
nx=%i \ t y=%i \ t z=%i ,X (19 ,2) ,X (19 ,3) ,X (19 ,4) )
// Example 8 . 2
// GaussS e i d e l Method
// Page no . 274
clc ; clear ; close ;
A =[8 , -3 ,2;4 ,11 , -1;6 ,3 ,12];
B =[20;33;36]
for i =0:10
X ( i +1 ,1) = i ;
end
for i =2:4
X (1 , i ) =0;
end
for r =1:10
for i =1:3
k1 =0;
115
// e q u a t i o n m a t r i x
// s o l u t i o n m a t r i x
17
18
19
20
21
22
23
24
25
26
27
28
29
end
30 end
31 printf (
32 printf (
33 disp ( X )
34 printf (
for j =1: i -1
k1 = k1 - A (i , j ) * X ( r +1 , j +1) ;
end
k2 =0;
for j = i +1:3
k2 = k2 - A (i , j ) * X (r , j +1) ;
end
X ( r +1 , i +1) =( k1 + k2 + B (i ,1) ) / A (i , i ) ;
r\t
x ( r ) \ t \ ty ( r ) \ t
z ( r ) );
\n
)
\n\ n A f t e r 9 i t e r a t i o n s e x a c t s o l u t i o n i s : \ nx
=%i \ t y=%i \ t z=%i ,X (10 ,2) ,X (10 ,3) ,X (10 ,4) )
// Example 8 . 3
//SOR Method
// Page no . 275
clc ; clear ; close ;
A =[5 ,2 ,1; -1 ,4 ,2;2 , -3 ,10];
B =[ -12;20;3];
w =0.9;
for i =0:13
X ( i +1 ,1) = i ;
end
116
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
X (1 ,2) = -2.4;
X (1 ,3) =5;
X (1 ,4) =0.3;
for r =1:13
for i =1:3
k1 =0;
for j =1: i -1
k1 = k1 - A (i , j ) * X ( r +1 , j +1) ;
end
k2 =0;
for j = i +1:3
k2 = k2 - A (i , j ) * X (r , j +1) ;
end
X ( r +1 , i +1) =(1 - w ) * X (r , i +1) +( w * k1 + w * k2 + w * B (i
,1) ) / A (i , i ) ;
30
end
31 end
32 printf (
r\t
x ( r ) \ t \ ty ( r ) \ t
z ( r ) );
33 printf ( \n
)
;
34 disp ( X ) ;
35 printf ( \n\ n A f t e r 12 i t e r a t i o n s e x a c t s o l u t i o n i s : \
nx=%i \ t y=%i \ t z=%i ,X (13 ,2) ,X (13 ,3) ,X (13 ,4) ) ;
// Example 8 . 4
// GaussS e i d e l P o i n t I t e r a t i v e Method
// Page no . 278
117
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
118
39
end
40
X ( r +1 , i +1) =( k1 + k2 + B (i ,1) ) / A (i , i ) ;
41
end
42 end
43 printf (
r
);
44 for i =1:6
45
printf ( x%i
,i ) ;
46 end
47 printf ( \n
48
49
50
51
52
)
disp ( X )
printf ( \n\ n A f t e r 6 i t e r a t i o n s e x a c t s o l u t i o n i s : \ n
);
for i =1:6
printf ( x%i=%f
,i , X (7 , i +1) ) ;
end
equation matrix
7 B =[3;2;1; -4];
// s o l u t i o n m a t r i x
8
9 // t r a n s f o r m a t i o n o f t h e e q u a t i o n s
10 for i =1:4
11
A1 (1 , i ) = A (4 , i ) ;
12
B1 (1 ,1) = B (4 ,1) ;
119
//
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
end
for i =1:4
A1 (3 , i ) = A (2 , i ) ;
B1 (3 ,1) = B (2 ,1) ;
end
for i =1:4
A1 (2 , i ) = A (1 , i ) -A (2 , i ) ;
B1 (2 ,1) = B (1 ,1) -B (2 ,1) ;
end
for i =1:4
A1 (4 , i ) =2* A (1 , i ) -A (2 , i ) +2* A (3 , i ) -A (4 , i ) ;
B1 (4 ,1) =2* B (1 ,1) -B (2 ,1) +2* B (3 ,1) -B (4 ,1) ;
end
// p r i n t i n g o f t r a n s f o r m e d e q u a t i o n s
printf ( \ n T r a n s f o r m e d E q u a t i o n s a r e =\n\n )
for i =1:4
for j =1:4
printf ( ( %ix ( %i ) ) , A1 (i , j ) ,j ) ;
if (j <4)
printf ( + )
end
end
printf ( = %i \n , B1 (i ,1) ) ;
end
for i =1:4
for j =1:4
if ( A (j , j ) ==0)
for k =1:4
C (j , k ) = A (j , k ) ;
A (j , k ) = A ( j +1 , k ) ;
A ( j +1 , k ) = C (j , k ) ;
end
end
end
end
for i =0:12
120
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
X ( i +1 ,1) = i ;
end
for i =2:5
X (1 , i ) =0;
end
for r =1:12
for i =1:4
k1 =0;
for j =1: i -1
k1 = k1 - A1 (i , j ) * X ( r +1 , j +1) ;
end
k2 =0;
for j = i +1:4
k2 = k2 - A1 (i , j ) * X (r , j +1) ;
end
X ( r +1 , i +1) =( k1 + k2 + B1 (i ,1) ) / A1 (i , i ) ;
end
end
printf ( \n\n
r
);
for i =1:4
printf ( x%i
,i ) ;
end
printf ( \n
)
disp ( X )
printf ( \n\ n A f t e r 11 i t e r a t i o n s e x a c t s o l u t i o n i s : \ n
);
for i =1:4
printf ( x%i=%f
,i , X (12 , i +1) ) ;
end
121
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
end
29
B2 (i ,1) = B ( i +3 ,1) ;
30 end
31 disp ( B2 , B2= ,B1 , B1= ,A22 , A22= ,A21 , A21= ,A12 ,
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
123
1 // Example 8 . 7
2 // B l o c k GaussS e i d e l Method
3 // Page no . 283
4 clc ; clear ; close ;
5
6 A
for i =1:2
for j =1:2
A11 (i , j ) = A (i , j ) ;
end
B1 (i ,1) = B (i ,1) ;
end
for i =1:2
for j =1:2
A12 (i , j ) = A (i , j +2) ;
end
B2 (i ,1) = B ( i +2 ,1) ;
end
for i =1:2
for j =1:2
A13 (i , j ) = A (i , j +4) ;
end
B3 (i ,1) = B ( i +4 ,1) ;
end
for i =1:2
for j =1:2
A21 (i , j ) = A ( i +2 , j ) ;
end
end
for i =1:2
for j =1:2
A22 (i , j ) = A ( i +2 , j +2) ;
124
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
end
end
for i =1:2
for j =1:2
A23 (i , j ) = A ( i +2 , j +4) ;
end
end
for i =1:2
for j =1:2
A31 (i , j ) = A ( i +4 , j ) ;
end
end
for i =1:2
for j =1:2
A32 (i , j ) = A ( i +4 , j +2) ;
end
end
for i =1:2
for j =1:2
A33 (i , j ) = A ( i +4 , j +4) ;
end
end
disp ( B3 , B3= ,B2 , B2= ,B1 , B1= ,A33 , A33= ,A32 , A32=
,A31 , A31= ,A23 , A23= ,A22 , A22= ,A21 , A21= ,A13
, A13= ,A12 , A12= ,A11 , A11= ) ;
A11_1 = inv ( A11 ) ; A22_1 = inv ( A22 ) ; A33_1 = inv ( A33 ) ;
disp ( A33_1 , I n v e r s e o f M a t r i x A33= , A22_1 , I n v e r s e
o f M a t r i x A22= , A11_1 , I n v e r s e o f M a t r i x A11= ) ;
for i =1:2
X1 (i ,1) =0;
X2 (i ,1) =0;
X3 (i ,1) =0;
end
for i =1:6
X (i ,1) =i -1;
end
for i =2:7
X (1 , i ) =0;
125
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
end
for r =1:5
X11 = A11_1 *( -1* A12 * X2 +( -1) * A13 * X3 + B1 ) ;
X22 = A22_1 *( -1* A21 * X11 +( -1) * A23 * X3 + B2 ) ;
X33 = A33_1 *( -1* A31 * X11 +( -1) * A32 * X22 + B3 ) ;
X1 = X11 ;
X2 = X22 ;
X3 = X33 ;
disp ( X3 , X3= ,X2 , X2= ,X1 , X1= )
for i =2:7
if (i <4)
X ( r +1 , i ) = X1 (i -1 ,1) ;
end
if (i <6 & i >3)
X ( r +1 , i ) = X2 (i -3 ,1) ;
end
if (i <8 & i >5)
X ( r +1 , i ) = X3 (i -5 ,1) ;
end
end
end
printf ( \n\ n I t e r a t i o n ) ;
for i =1:6
printf (
x%i
,i ) ;
end
printf ( \n
)
disp ( X )
printf ( \n\ n A f t e r 4 i t e r a t i o n s e x a c t s o l u t i o n i s : \ n
);
for i =1:6
printf ( x%i=%f
,i , X (5 , i +1) ) ;
end
126
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
end
for i =1:2
for j =1:2
A21 (i , j ) = A ( i +2 , j ) ;
end
end
for i =1:2
for j =1:2
A22 (i , j ) = A ( i +2 , j +2) ;
end
end
for i =1:2
for j =1:2
A23 (i , j ) = A ( i +2 , j +4) ;
end
end
for i =1:2
for j =1:2
A31 (i , j ) = A ( i +4 , j ) ;
end
end
for i =1:2
for j =1:2
A32 (i , j ) = A ( i +4 , j +2) ;
end
end
for i =1:2
for j =1:2
A33 (i , j ) = A ( i +4 , j +4) ;
end
end
disp ( B3 , B3= ,B2 , B2= ,B1 , B1= ,A33 , A33= ,A32 , A32=
,A31 , A31= ,A23 , A23= ,A22 , A22= ,A21 , A21= ,A13
, A13= ,A12 , A12= ,A11 , A11= ) ;
60 A11_1 = inv ( A11 ) ; A22_1 = inv ( A22 ) ; A33_1 = inv ( A33 ) ;
61 disp ( A33_1 , I n v e r s e o f M a t r i x A33= , A22_1 , I n v e r s e
o f M a t r i x A22= , A11_1 , I n v e r s e o f M a t r i x A11= ) ;
62 for i =1:2
128
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
X1 (i ,1) =0;
X2 (i ,1) =0;
X3 (i ,1) =0;
end
for i =1:7
X (i ,1) =i -1;
end
for i =2:7
X (1 , i ) =0;
end
for r =1:6
X11 = A11_1 *((1 - w ) * X1 +( -1) * w * A12 * X2 +( -1) * w * A13 * X3 +
w * B1 ) ;
X22 = A22_1 *((1 - w ) * X2 +( -1) * w * A21 * X11 +( -1) * w * A23 * X3
+ w * B2 ) ;
X33 = A33_1 *((1 - w ) * X3 +( -1) * w * A31 * X11 +( -1) * w * A32 *
X22 + w * B3 ) ;
X1 = X11 ;
X2 = X22 ;
X3 = X33 ;
disp ( X3 , X3= ,X2 , X2= ,X1 , X1= )
for i =2:7
if (i <4)
X ( r +1 , i ) = X1 (i -1 ,1) ;
end
if (i <6 & i >3)
X ( r +1 , i ) = X2 (i -3 ,1) ;
end
if (i <8 & i >5)
X ( r +1 , i ) = X3 (i -5 ,1) ;
end
end
end
printf ( \n\ n I t e r a t i o n ) ;
for i =1:6
printf (
x%i
,i ) ;
end
printf ( \n
129
98
99
100
101
102
)
disp ( X )
printf ( \n\ n A f t e r 5 i t e r a t i o n s e x a c t s o l u t i o n i s : \ n
);
for i =1:6
printf ( x%i=%f
,i , X (6 , i +1) ) ;
end
130
Chapter 9
Linear Least Squares Problem
// Example 9 . 1
// MooreP e n r o s e G e n e r a l i z e d I n v e r s e
// Page no . 292
clc ; clear ; close ;
1
2
3
4
5
6
7
8
9
// Example 9 . 2
// Curve F i t t i n g
// Page no . 293
clc ; clear ; close ;
x (1) =0.25;
for i =2:6
x (1 , i ) = x (1 ,i -1) +0.25;
end
// x v a l u e s
y (1 ,1) =3.1; y (1 ,2) =1.7; y (1 ,3) =1; y (1 ,4) =0.68; y (1 ,5)
=0.42; y (1 ,6) =0.26; // y v a l u e s
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// c o n s t r u c t i o n o f n o r m a l e q u a t i o n s
for i =1:6
Y (1 , i ) = log10 ( y (1 , i ) ) ;
end
Ex =0;
for i =1:6
Ex = Ex + x (1 , i ) ;
end
EY =0;
for i =1:6
EY = EY + Y (1 , i ) ;
end
Ex2 =0;
for i =1:6
Ex2 = Ex2 + x (1 , i ) ^2;
end
ExY =0;
for i =1:6
ExY = ExY + x (1 , i ) * Y (1 , i ) ;
end
printf ( E x ( k ) \ t \ t y ( k ) \ t \ tE Y( k ) \ t \ tE x2 ( k ) \ t \ tE x (
k ) Y( k ) )
32 printf ( \n
)
33 for i =1:6
34
printf ( \ n%f \ t%f \ t%f \ t%f \ t%f ,x (1 , i ) ,y (1 , i ) ,Y (1 ,
132
i ) ,x (1 , i ) ^2 , x (1 , i ) * Y (1 , i ) )
35 end
36 printf ( \n
37
38
)
printf ( \ n%f \ t%f \ t%f \ t%f \ t%f ,Ex ,0 , EY , Ex2 , ExY )
printf ( \n
n\n )
A =[6 , Ex ; Ex , Ex2 ];
// s y s t e m o f n o r m a l e q u a t i o n s
B =[ EY ; ExY ];
X = inv ( A ) * B ;
a = exp ( X (1 ,1) ) ;
b = -1* X (2 ,1) ;
for i =1:2
for j =1:2
printf ( %f
,A (i , j ) )
end
if ( i ==1)
printf ( )
end
39
40
41
42
43
44
45
46
47
48
49
50
51
52
printf ( \ t a % i ,i ) ;
53
if ( i ==1)
54
printf ( = )
55
end
56
57
printf ( \ t%f \n ,B (i ,1) )
58 end
59 printf ( \n\ na1=%f\ na2=%f\n\ na=%f\ nb=%f\n\n ,X (1 ,1) ,X
(2 ,1) ,a , b )
60 printf ( The f i t t e d c u r v e i s : \ n
e ,b , a )
133
%fx \ ny=%f
// Example 9 . 3
//GramSchmidt O r t h o g o n a l i z a t i o n / O r t h o n o r m a l i z a t i o n
Process
// Page no . 294
clc ; clear ; close ;
deff ( y=f ( x , a ) , y=s q r t ( x ( 1 , a ) 2+x ( 2 , a ) 2+x ( 3 , a ) 2+x
( 4 , a ) 2) );
deff ( y=f 1 ( g , a , h , b ) , y=g ( 1 , a ) h ( 1 , b )+g ( 2 , a ) h ( 2 , b )+
g ( 3 , a ) h ( 3 , b )+g ( 4 , a ) h ( 4 , b ) ) ;
7
8 U =[1/ sqrt (3) , -2/ sqrt (7) ,1 ,0 ,0 ,0;0 ,1/ sqrt (7)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
,0 ,1 ,0 ,0;1/ sqrt (3) ,1/ sqrt (7) ,0 ,0 ,1 ,0; -1/ sqrt (3)
, -1/ sqrt (7) ,0 ,0 ,0 ,1];
for i =1:4
V (i ,1) = U (i ,1) ;
end
for i =1:4
if ( f (V ,1) ~=0)
W (i ,1) = V (i ,1) / f (V ,1) ;
else
W (i ,1) =0;
end
end
for j =2:6
for i =1:4
for l =1:4
k (l ,1) =0;
end
for l =1: j -1
for m =1:4
w (m ,1) = W (m , l ) ;
end
k =k -( f1 (U ,j ,W , l ) ) * w ;
end
V (i , j ) = U (i , j ) + k (i ,1) ;
134
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
end
for i =1:4
if ( j ~=4)
if ( f (V , j ) ~=0)
W (i , j ) = V (i , j ) / f (V , j ) ;
else
W (i , j ) =0;
end
else
W (i , j ) =0;
end
end
end
disp (U , U= )
disp ( W= )
printf ( \n )
for i =1:4
for j =1:6
printf ( %. 4 f \ t \ t ,W (i , j ) )
end
printf ( \n )
end
disp ( V= )
printf ( \n )
for i =1:4
for j =1:6
printf ( %. 4 f \ t \ t ,V (i , j ) )
end
printf ( \n )
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Example 9 . 4
//QR D e c o m p o s i t i o n
// Page no . 296
clc ; clear ; close ;
A =[2 ,1 ,1;1 ,3 ,1;1 ,1 ,4];
B = A *A ;
disp (B , ATA= )
// c h o l e s k y f a c t o r i z a t i o n t o f i n d R
R (2 ,1) =0; R (3 ,1) =0; R (3 ,2) =0;
for i =1:3
for j =1:3
if ( i == j )
k =0;
for m =1: i -1
k = k + R (m , i ) ^2;
end
R (i , j ) = sqrt ( B (i , j ) -k )
end
if (j > i )
k =0;
for m =1: i -1
k = k + R (m , j ) * R (m , i ) ;
end
R (i , j ) =( B (i , j ) -k ) / R (i , i )
end
end
end
// c h o l e s k y f a c t o r i z a t i o n end
disp (R , Upper T r i a n g u l a r M a t r i x (R)= )
R_1 = inv ( R ) ;
disp ( R_1 , I n v e r s e o f R )
Q = A * R_1 ;
disp (Q , O r t h o g o n a l M a t r i x Q= )
136
// Example 9 . 5
// V e c t o r Computation
// Page no . 299
clc ; clear ; close ;
X =[2 ,3 ,0 ,1];
n = X (1) ;
for i =2:4
if (n < X ( i ) )
n=X(i);
end
end
printf ( \nMaximum V a l u e ( n )=%i \n ,n )
for i =1:4
X(i)=X(i)/n;
end
disp (X , N o r m a l i z e d X= )
k =0;
for i =1:4
k = k + X ( i ) ^2;
end
sigma = X (1) * abs (1/ X (1) ) * sqrt ( k ) ;
printf ( \ n s i g m a=%f\n , sigma ) ;
X (1) = X (1) + sigma ;
printf ( \ n M o d i f i e d x1 = %g\n ,X (1) )
for i =1:4
U (1 , i ) = X ( i ) ;
end
disp (U , U= )
p = sigma * X (1) ; sigma = n * sigma ;
137
31
32
// Example 9 . 6
// House H o l d e r T r a n s f o r m a t i o n
// Page no . 300
clc ; clear ; close ;
A =[4 ,2 ,1;2 ,5 , -2;1 , -2 ,7]
disp (A , A= )
k =0;
for j =2:3
k = k + A (j ,1) ^2;
end
a = A (2 ,1) * abs (1/ A (2 ,1) ) * sqrt ( k ) ;
disp (a , a l p h a= )
U =[0; a + A (2 ,1) ; A (3 ,1) ];
disp (U , U= )
U1 =U * U ;
disp ( U1 , UTU= )
U2 = U *U ;
disp ( U2 , UUT= )
P = eye (3 ,3) -(2* U2 ) / U1 ;
disp (P , P= ) ;
B=P*A*P;
disp (B , B= ) ;
printf ( \n\n\ nThere a r e c o m p u t a t i o n e r r o r i n t h e
a n s w e r s g i v e n by t h e book i n t h i s e x a m p l e \n\n ( a22
v a l u e e r r o r i n UUT) )
138
)
deff ( y=s ( i , j ) , y=A( i , j ) / s q r t ( (A( i , j ) 2+A( j , j ) 2 ) )
)
disp (A , A= )
R = A ; Q = eye (3 ,3) ;
m =1;
for j =1:2
for i = j +1:3
for k =1:3
//C m a t r i x e v a l u a t i o n
for l =1:3
if ( k == l )
if ( k == i | k == j )
C (k , l ) = c (i , j )
else
C (k , l ) =1
end
end
if (k > l )
if ( k == i & l == j )
C (k , l ) = -1* s (i , j )
else
C (k , l ) =0
end
139
29
end
30
if (k < l )
31
if ( k == j & l == i )
32
C (k , l ) = s (i , j )
33
else
34
C (k , l ) =0
35
end
36
end
37
end
38
end
39
printf ( \n\n I t e r a t i o n %i ,m )
40
m = m +1
41
disp (C , C= ) ;
42
R=C*R;
43
Q = Q *C ;
44
disp (Q , Q= ,R , R= )
45
end
46 end
47 disp ( Q *R , QR=A= )
// v e r i f i c a t i o n
// Example 9 . 8
// R e c u r s i v e L e a s t S q u a r e Method
// Page no . 308
clc ; clear ; close ;
A0 =[3 ,0;0 ,3;3 ,3];
B0 =[2;2;2];
A1 =[6 ,3]; B1 =[6];
A0T = A0 ;
G0 = A0T * A0 ;
disp ( G0 , G0= )
140
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
G0_1 = inv ( G0 ) ;
disp ( G0_1 , I n v e r s e o f G0= )
X0 = G0_1 * A0T * B0 ;
disp ( X0 , X0= )
// by r e c u r s i v e l e a s t s q u a r e a l g o r i t h m
G1 = G0 + A1 * A1 ;
disp ( G1 , G1= ) ;
G1_1 = inv ( G1 ) ;
disp ( G1_1 , I n v e r s e o f G1 )
X1 = X0 + G1_1 * A1 *( B1 - A1 * X0 ) ;
disp ( X1 , X1= )
// v e r i f i c a t i o n
A =[3 ,0;0 ,3;3 ,3;6 ,3];
B =[2;2;2;6];
AT =A ;
G = AT * A ;
disp (G , G= )
G_1 = inv ( G ) ;
disp ( G_1 , I n v e r s e o f G= )
X = G_1 * AT * B ;
disp (X , X= )
disp ( Thus X and X1 a r e Same )
141
Chapter 10
Numerical Solutions of System
of Non Linear Equations
// Example 1 0 . 1
// System o f Non L i n e a r E q u a t i o n s
// Page no . 311
clc ; clear ; close ;
deff ( y=f ( x ) , y=x2 exp ( 2 x ) 4 )
deff ( y=f 1 ( x ) , y=2x2 exp ( 2 x ) )
x0 =0; e =0.00001
for i =1:10
x1 = x0 - f ( x0 ) / f1 ( x0 )
e1 = abs ( x0 - x1 )
x0 = x1 ;
if abs ( x0 ) <e then
break ;
end
end
printf ( \n\ nThe s o l u t i o n o f t h i s e q u a t i o n a f t e r %i
142
I t e r a t i o n s by newton r a p h s h o n method i s %. 1 0 f ,i ,
x1 )
7
8
9
10
11
12
13
14
15
16
17
\n 0\ t%f \ t%f \n ,x
(1) ,y (1) )
for i =2:9
x ( i ) = sin ( x (i -1) + y (i -1) )
y ( i ) = cos ( x (i -1) -y (i -1) )
printf ( %i \ t%f \ t%f \n ,i -1 , x ( i ) ,y ( i ) )
end
printf ( \n\n\n ( b ) S e i d e l Method \n\ nn \ t x n \ t \ t y n \n
\n 0\ t%f \ t%f \n ,x
(1) ,y (1) )
for i =2:9
x ( i ) = sin ( x (i -1) + y (i -1) )
y ( i ) = cos ( x ( i ) -y (i -1) )
printf ( %i \ t%f \ t%f \n ,i -1 , x ( i ) ,y ( i ) )
end
143
1 // Example 1 0 . 3
2 // Non L i n e a r System o f E q u a t i o n
3 // Page no . 315
4 clc ; clear ; close ;
5 x (1 ,1) =1; x (1 ,2) =0; x (1 ,3) =0;
6 y (1 ,1) =0; y (1 ,2) =2; y (1 ,3) =2;
7 printf ( Case >\t \ t I \ t \ t \ t \ t \ I I \ t \ t \ t \ t I I I \n
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
n I t e r a t i o n \ t x \ t \ t y \ t \ t x \ t \ t y \ t \ t x \ t \ t y \n\n
0\ t
\ t%f \ t%f \ t%f \ t%f \ t%f \ t%f \n ,x (1 ,1) ,y (1 ,1) ,x (1 ,2) ,
y (1 ,2) ,x (1 ,3) ,y (1 ,3) )
for i =2:9
printf (
%i \ t ,i -1)
for j =1:3
if j ==1 | j ==2 then
x (i , j ) =( - y (i -1 , j ) ^2 -4* x (i -1 , j ) ^2+8* x (i
-1 , j ) +4) /8
y (i , j ) =(2* y (i -1 , j ) ^2 -2* x (i -1 , j ) +1) /4
printf ( \ t%f \ t%f ,x (i , j ) ,y (i , j ) )
end
if j ==3 then
x (i , j ) =( - y (i -1 , j ) ^2 -4* x (i -1 , j ) ^2+11* x (i
-1 , j ) +4) /11
y (i , j ) =( -2* y (i -1 , j ) ^2+8* y (i -1 , j ) -2* x (i -1 , j )
-1) /4
printf ( \ t%f \ t%f ,x (i , j ) ,y (i , j ) )
end
end
printf ( \n )
end
printf ( \n\n\n\ nNote : There a r e c o m p u t a t i o n a l
e r r o r s i n t h i s e x a m p l e g i v e n by t h e book )
144
// Example 1 0 . 5
// Newton Raphshon Method
145
3 // Page no . 320
4 clc ; clear ; close ;
5
6 deff ( y=f 1 ( x , y ) , y=x33x y2+1 )
7 deff ( y=f 2 ( x , y ) , y=3x 2 yy 3 )
8 deff ( y=f 1 1 ( x , y ) , y=3x26y 2 )
9 deff ( y=f 1 2 ( x , y ) , y=6x y )
10 deff ( y=f 2 1 ( x , y ) , y=6x y )
11 deff ( y=f 2 2 ( x , y ) , y=3x23y 2 )
12 x =[0;1];
13 printf ( \ nx ( 0 ) = %g\ ny ( 0 ) = %g\n ,x (1) ,x (2) )
14 for i =1:3
15
fx =[ f1 ( x (1) ,x (2) ) ; f2 ( x (1) ,x (2) ) ]
16
printf ( \n f x ( %i ) = \n ,i )
17
disp ( fx )
18
J =[ f11 ( x (1) ,x (2) ) , f12 ( x (1) ,x (2) ) ; f21 ( x (1) ,x (2) ) ,
19
20
21
22
23
24
25
26
27
28
29
30 end
146
1 // Example 1 0 . 6
2 // Newton Method
3 // Page no . 322
4 clc ; clear ; close ;
5
6 deff ( y=f 1 ( x , y , z ) , y=x 0.1 y 2 + 0 . 0 5 z 2 0.7 )
7 deff ( y=f 2 ( x , y , z ) , y=y +0.3 x 2 0.1 x z 0.5 )
8 deff ( y=f 3 ( x , y , z ) , y=z +0.4 y 2 + 0 . 1 x y 1.2 )
9 deff ( y=f 1 1 ( x , y , z ) , y=1 )
10 deff ( y=f 1 2 ( x , y , z ) , y = 0.2 y )
11 deff ( y=f 1 3 ( x , y , z ) , y =0.1 z )
12 deff ( y=f 2 1 ( x , y , z ) , y =0.6 x 0.1 z )
13 deff ( y=f 2 2 ( x , y , z ) , y=1 )
14 deff ( y=f 2 3 ( x , y , z ) , y = 0.1 x )
15 deff ( y=f 3 1 ( x , y , z ) , y =0.1 y )
16 deff ( y=f 3 2 ( x , y , z ) , y =0.8 y +0.1 x )
17 deff ( y=f 3 3 ( x , y , z ) , y=1 )
18 x =[0;0;0];
19 printf ( n\ t x n \ t \ t y n \ t \ t z n \n
20
21
22
23
24
25
26
27
28
29
\
n )
for i =1:6
fx =[ f1 ( x (1) ,x (2) ,x (3) ) ; f2 ( x (1) ,x (2) ,x (3) ) ; f3 ( x
(1) ,x (2) ,x (3) ) ]
J =[ f11 ( x (1) ,x (2) ,x (3) ) , f12 ( x (1) ,x (2) ,x (3) ) , f13 ( x
(1) ,x (2) ,x (3) ) ; f21 ( x (1) ,x (2) ,x (3) ) , f22 ( x (1) ,x
(2) ,x (3) ) , f23 ( x (1) ,x (2) ,x (3) ) ; f31 ( x (1) ,x (2) ,x
(3) ) , f32 ( x (1) ,x (2) ,x (3) ) , f33 ( x (1) ,x (2) ,x (3) ) ]
J_1 = inv ( J )
printf ( %i \ t%f \ t%f \ t%f \n ,i -1 , x (1) ,x (2) ,x (3) )
x =x - J_1 * fx
end
printf ( \n\ nThe s o l u t i o n i s x = %f , y = %f and z =
%f ,x (1) ,x (2) ,x (3) )
printf ( \n\n\ nNote : There a r e c o m p u t a t i o n e r r o r s i n
c a l c u l a t i o n g i v e n by t h e book )
147
8
9
10
11
12
13
14
\
n )
for i =1:7
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n ,i -1 , x (1) ,x
(2) ,x (3) )
x (1) =0.7+0.1* x (2) ^2 -0.05* x (3) ^2
x (2) =0.5 -0.3* x (1) ^2+0.1* x (1) * x (3)
x (3) =1.2 -0.4* x (2) ^2 -0.1* x (1) * x (2)
end
printf ( \n\ nThe s o l u t i o n i s x = %. 1 0 f , y = %. 1 0 f and
z = %. 1 0 f ,x (1) ,x (2) ,x (3) )
148
9
10
11
12
13
14
15
16
\
n )
for i =1:11
Fx =[ f ( x (1) ,x (2) ) ];
J =[4*( x (1) -2) ^3 ,6*( x (2) +3) ];
u =( Fx * J *J * Fx ) /( J *J * Fx * J *J * Fx )
printf ( %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n ,i -1 , x (1) ,x
(2) , Fx )
x =x - u *J * Fx
end
printf ( \n\ n T h i s shows t h a t t h e s o l u t i o n t e n d s t o x1
= %i
and
x2 = %i , ceil ( x (1) ) , floor ( x (2) ) )
149
Chapter 11
Eigenvalues and Eigenvectors
=[0.2; -0.6];
T =[1.1 , -0.3; -0.3 ,1.9];
B1 = T * A1 ; B2 = T * A2 ; B3 = T * A3 ; B4 = T * A4 ;
disp ( B4 , B3 , B2 , B1 , The t r a n s f o r m e d v e c t o r s a r e : )
disp ( These p o i n t s l i e on t h e e l l i p s e : )
printf (
2
2\ n ( x3y ) +(3 x+y ) \n \
n 16
4\ n\n )
A5 =[0;2/ sqrt (10) ];
disp ( The v e c t o r ( 0 , 2 / 1 0 ( 1 / 2 ) ) l i e s on t h e c i r c l e :
)
printf ( 2
2\ nx + y = 4\ n
\n
10\ n\n
)
B5 = T * A5 ;
150
16
17
disp ( A l s o l i e s on t h e same e l l i p s e , B5 )
printf ( \n\nWe can s e e t h a t t h e r e i s a l i n e a r
r e l a t i o n s h i p b e t w e e n t h e f i r s t 4 v e c t o r s and
t h e i r r e s p e c t i v e transformend v e c t o r s through the
s c a l a r s known a s e i g e n v e c t o r s and e i g e n v a l u e s
respectively )
// Example 1 1 . 2
// L e v e r r i e r s Method
// Page no . 337
clc ; close ; clear ;
A =[2 ,2 ,2;2 ,5 ,5;2 ,5 ,1];
A1 = A ;
C (1) =0;
for j =1:3
for k =1:3
if ( j == k )
C (1) = C (1) + A1 (j , k )
end
end
end
disp (A , A= )
disp ( A1 , A1= )
printf ( \nC1= )
disp ( C (1) ) ;
for i =2:3
A2 = A *( A1 - C (i -1) * eye (3 ,3) ) ;
printf ( \n\n\nA%i= ,i )
disp ( A2 ) ;
C ( i ) =0;
151
25
for j =1:3
26
for k =1:3
27
if ( j == k )
28
C ( i ) = C ( i ) + A2 (j , k ) / i
29
end
30
end
31
end
32
printf ( \nC%i= ,i )
33
disp ( C ( i ) )
34
A1 = A2 ;
35 end
36 printf ( \n\n\ n T h e r e f o r e t h e c h a r a c t e r i s t i c
polynomial i s :\ n 3
(1) ,C (2) ,C (3) )
2\ nx %ix %ix %i = 0 ,C
37
38 // v e r i f i c a t i o n
39 printf ( \n\ n V e r i f i c a t i o n : )
40 s = poly (0 , s ) ;
41 p = poly (A , x ) ;
42 A =A - eye (3 ,3) * %s ;
43 disp (p , = ,A )
152
153
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// p a r t i t i o n g
for i =4:6
for j =1:3
T (i -3 , j ) = G (i , j )
end
end
disp (T , T= )
// e i g e n v a l u e s c o m p u t a t i o n
printf ( \n\n\ n C h a r a t e r i s t i c p o l y n o m i a l : )
p = poly (A , x )
disp ( p )
printf ( \n\n\ n E i g e n v a l u e s : )
a = roots ( p )
disp (a )
// e i g e n v e c t o r s c o m p u t a t i o n
for k =1:3
m =2
for l =1:3
y (l , k ) = a (k ,1) ^( m )
m =m -1;
end
end
printf ( \n\n )
disp (y , y= )
// e i g e n v e c t o r c o m p u t a t i o n
for k =1:3
for l =1:3
y1 (l ,1) = y (l ,1)
y2 (l ,1) = y (l ,2)
y3 (l ,1) = y (l ,3)
end
x1 = T * y3 ;
x2 = T * y2 ;
x3 = T * y1 ;
end
154
86 printf ( \n\ n E i g e n v e c t o r s : \ n )
87 for i =1:3
88
printf ( |%. 1 f | \ t \ t |%. 1 f | \ t \ t |%. 1 f | , x1 (i ,1) , x2 (i
,1) , x3 (i ,1) )
89
printf ( \n )
90 end
// Example 1 1 . 4
// Power Method
// Page no . 345
clc ; close ; clear ;
A =[1 ,2;3 ,4];
e =0.001;
q0 =[1;1];
for i =1:5
q1 = A * q0 ;
a = max ( q1 )
for j =1:2
q2 ( j ) = q1 ( j ) / a ;
end
printf ( \ nq ( %i ) = %. 4 f
q ( %i ) = %. 4 f \n
a = %. 4 f
%. 4 f
Scaled
%i \n\n ,
l a r g e s t e i g e n v a l u e i s %. 4 f w i t h
t h e c o r r e s p o n d i n g e i g e n v e c t o r a s %. 4 f \n
155
%i ,a , q0 (1) , q0 (2) )
// Example 1 1 . 5
// I n v e r s e Power Method
// Page no . 347
clc ; close ; clear ;
A =[7 ,6 , -3; -12 , -20 ,24; -6 , -12 ,16];
e =10^ -6;
X =[1;1;1];
B =0;
Y =[0;0;0]
a =0; l =0;
for i =1:2
printf ( When a=%i \n ,a ) ;
C =A - a * eye () ;
disp (C , C= )
C_1 = inv ( C ) ;
disp ( C_1 , I n v e r s e o f C= ) ;
printf ( \n\ n I t r
lambda
X)
19
20
21
22
23
24
25
printf ( \n
n )
for j =1:10
printf ( \ n%i
%f
%f
%f
%f ,j -1 ,l , X (1) ,X (2) ,X (3) ) ;
Y = C_1 * X ;
B = max ( Y ) ;
e1 = abs (l - B ) ;
X=Y/B;
156
26
m =0;
27
for k =1:3
28
m = m +( Y ( k ) -X ( k ) ) ^2;
29
end
30
e2 = sqrt ( m ) ;
31
er = max ( e1 , e2 ) ;
32
if ( er < e )
33
break
34
end
35
l=B;
36
37
end
38
a = -3;
39
printf ( \n\n\n\n )
40 end
41 printf ( \n\n\ nNote : Computation o f Y i s wrong g i v e n
i n t h e book )
// Example 1 1 . 6
// R a y l e i g h Q u o t i e n t
// Page no . 348
clc ; close ; clear ;
A =[10 ,7 ,8 ,7;7 ,5 ,6 ,5;8 ,6 ,10 ,9;7 ,5 ,9 ,10];
q0 =[1;1;1;1];
for i =0:4
X =( A ^ i ) * q0 ;
l =( X * A * X ) /( X * X )
printf ( \nLambda ( %i ) = %f\n ,i +1 , l )
end
printf ( \n\ nDominant E i g e n v a l u e = %f\n\n\n ,l )
157
14
15 e =0.001;
16 for i =1:5
17
q1 = A * q0 ;
18
a = max ( q1 )
19
for j =1:4
20
q2 ( j ) = q1 ( j ) / a ;
21
end
22
23
q1 = q2 ;
24
q0 = q1 ;
25 end
26 disp ( q2 , C o r r e s p o n d i n g E i g e n v e c t o r = )
// Example 1 1 . 7
// J a c o b i s Method
// Page no . 355
clc ; close ; clear ;
A =[1 ,1 ,1/2;1 ,1 ,1/4;1/2 ,1/4 ,2];
C=A;
V =[ sqrt (2) ,0 ,1/2; sqrt (2) ,0 ,1/4;3/(4* sqrt (2) ) , -1/(4*
sqrt (2) ) ,2]
S = eye (3 ,3)
disp (A , A = )
VI =0;
for i =1:3
for j =1:3
if ( i ~= j )
VI = VI + A (i , j ) ^2
// i n i t i a l o f f d i a g norm
158
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
end
end
end
VI = sqrt ( VI ) ;
VF = VI *10^ -7;
// f i n a l t h r e s h o l d
V1 = VI /3;
o = poly (0 , o ) ;
for i =1:3
for q =2:3
for p =q -1: -1:1
if ( A (p , q ) > V1 )
a = - A (p , q ) ;
b =( A (p , p ) -A (q , q ) ) /2
if ( b ~=0)
w = b * abs (1/ b ) *( a / sqrt ( a ^2+ b ^2) ) ;
else
w =( a / sqrt ( a ^2+ b ^2) ) ;
end
sin0 = w / sqrt (2*(1+ sqrt (1 - w ^2) ) ) ;
cos0 = sqrt (1 - sin0 ^2)
end
B (p , p ) = A (p , p ) * cos0 ^2+ A (q , q ) * sin0 ^2 -2* A (p , q ) *
sin0 * cos0
B (q , q ) = A (p , p ) * sin0 ^2+ A (q , q ) * cos0 ^2+2* A (p
, q ) * sin0 * cos0
B (p , q ) =( A (p , p ) -A (q , q ) ) * sin0 * cos0 + A (p , q )
*( cos0 ^2 - sin0 ^2)
S (i , i ) = S (i , i )
S (i , p ) = S (i , p ) * cos0 - S (i , q ) * sin0
S (i , q ) = S (i , p ) * sin0 + S (i , q ) * cos0
40
41
42
43
44
end
45 end
46 end
47 disp (B , B = )
48 disp (S , S = )
49 printf ( \n\n\ nComputation e r r o r
p r o v i d e d by book )
159
in the s o l u t i o n
// Example 1 1 . 8
// R e c u r s i v e Formula
// Page no . 357
clc ; close ; clear ;
A =[2 , -1 ,0 ,0; -1 ,2 , -1 ,0;0 , -1 ,2 , -1;0 ,0 , -1 ,2];
l = poly (0 , l ) ;
p0 =1;
p1 = A (1 ,1) -l ;
for i =2:4
p2 =( A (i , i ) -l ) * p1 - A (i ,i -1) ^2* p0 ;
p0 = p1 ;
p1 = p2 ;
printf ( \n\ np%i ( l ) = ,i ) ;
disp ( p2 )
end
160
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
C (k , l ) =1
end
end
if (k > l )
if ( k == i & l == j )
C (k , l ) = -1* s2 (i , j )
else
C (k , l ) =0
end
end
if (k < l )
if ( k == j & l == i )
C (k , l ) = s2 (i , j )
else
C (k , l ) =0
end
end
end
end
R=C*R;
Q = Q *C ;
end
end
disp (Q , Q= ,R , R= )
disp ( Q *R , QR= )
A=R*Q;
disp (A , A= )
end
l1 = l0 + s ;
for i =2:3
for j =2:3
V (i -1 ,j -1) = A (i , j )
end
end
disp (V , V= )
p = poly (V , x ) ;
162
80
81
82
83
84
85
86
87
88
disp ( =0 ,p ) ;
a = roots ( p ) ;
for j =1:2
printf ( \ na ( %i ) = %f ,j , a ( j ) )
end
l2 = l1 + a (1)
l3 = l1 + a (2)
disp ( l3 , l 3= ,l2 , l 2= ,l1 , l 1= )
printf ( \n\n\ nNote : V a l u e s o f V v a r i e s i n e a c h s t e p
r e s u l t i n g i n d i f f e r e n t r e s u l t s due t o e r r o r i n
book c a l c u l a t i o n )
=[120 ,80 ,40 , -16;80 ,120 ,16 , -40;40 ,16 ,120 , -80; -16 , -40 , -80 ,120];
7 disp (A , A = )
8 L = eye (4 ,4) ;
9 for l =1:20
10 for j =1:4
11
for i =1: j
12
k =0
13
for p =1: i -1
14
k =k - A (i , p ) * A (p , j )
15
end
16
A (i , j ) = A (i , j ) + k
17
end
163
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
for i = j +1:4
k =0;
for p =1: j -1
k =k - A (i , p ) * A (p , j )
end
A (i , j ) =( A (i , j ) + k ) / A (j , j )
end
end
disp (A , M o d i f i e d A = )
for i =1:4
for j =1:4
if i > j then
L (i , j ) = A (i , j )
else
U (i , j ) = A (i , j )
end
end
end
disp (U , U = ,L , L = )
A=U*L;
printf ( \n\ n A f t e r %i i t e r a t i o n s , m a t r i x A =\n\n ,l )
for i =1:4
for j =1:4
printf (
%. 2 f \ t ,A (i , j ) )
end
printf ( \n )
end
end
printf ( \n\ n T h e r e f o r e t h e e i g e n v a l u e s a r e t h e
diagonal elements f the transformed t r i a n g u l a r
m a t r i x a r e : \ n\n )
47 for i =1:4
48
printf ( %. 2 f , ,A (i , i ) )
49 end
164
// Example 1 1 . 1 1
// G e n e r a l i z e d E i g e n v a l u e Problem
// Page no . 365
clc ; close ; clear ;
A =[1 ,1 ,0.5;1 ,1 ,0.25;0.5 ,0.25 ,2]
B =[2 ,2 ,2;2 ,5 ,5;2 ,5 ,11]
disp (B , B = ,A , A = )
for i =1:3
G (i , i ) = sqrt ( B (i , i ) )
end
G =[ B ; eye (3 ,3) ];
// t r a n s f o r m a t i o n t o f r o b e n i u s m a t r i x
for k =3: -1:2
g ( k ) =0;
for j =1: k -1
if ( g ( k ) <G (k , j ) )
g ( k ) = G (k , j )
p=j;
end
end
if ( g ( k ) ~=0)
for j =1:3
r (1 , j ) = G (k , j )
end
for i =1:6
G (i ,k -1) = G (i ,k -1) / g ( k )
end
for j =1:3
165
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
if ( j ~= k -1)
l = G (k , j )
for i =1:6
G (i , j ) = G (i , j ) -l * G (i ,k -1)
end
end
end
end
for j =1:3
for i =1:3
c (i ,1) = G (i , j )
end
G (k -1 , j ) =0
for i =1:3
G (k -1 , j ) = G (k -1 , j ) + r (1 , i ) * c (i ,1)
end
end
end
// p a r t i t i o n g
for i =4:6
for j =1:3
T (i -3 , j ) = G (i , j )
end
end
// e i g e n v a l u e s c o m p u t a t i o n
p = poly (B , x )
a = roots ( p )
printf ( \n\ n D i a g o n a l i z e d M a t r i x B = \n\n )
for i =1:3
for j =1:3
if i ~= j then
B (i , j ) =0
else
B (i , j ) = a ( i )
end
end
166
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
end
disp ( B )
// e i g e n v e c t o r s c o m p u t a t i o n
for k =1:3
m =2
for l =1:3
y (l , k ) = a ( k ) ^( m )
m =m -1;
end
end
printf ( \n\n )
for k =1:3
for l =1:3
y1 (l ,1) = y (l ,1)
y2 (l ,1) = y (l ,2)
y3 (l ,1) = y (l ,3)
end
x1 = T * y3 ;
x2 = T * y2 ;
x3 = T * y1 ;
end
printf ( \n\ n E i g e n v e c t o r s o f B a r e : \ n\n )
for i =1:3
printf ( |%. 5 f | \ t \ t |%. 5 f | \ t \ t |%. 5 f | , x3 (i ,1) , x2 (i
,1) , x1 (i ,1) )
95
printf ( \n )
96 end
97 x =[ x3 , x2 , x1 ]
98
99
100
101
102
103 B =[2 ,2 ,2;2 ,5 ,5;2 ,5 ,11]
104 G =0
105 for i =1:3
167
106
for j =1:3
107
if i == j then
108
G (i , j ) = sqrt ( B (i , j ) )
109
else
110
G (i , j ) =0;
111
end
112
end
113 end
114
115 B = inv ( G ) *x * A * x * inv ( G )
116 disp (B , E i g e n v e c t o r s o f A = )
117
118 printf ( \n\n\ nNote : Computation E r r o r i n book i n
c a c u l a t i o n of e i g e n v e c t o r of B thus f o r A )
168
Chapter 12
Interpolation and
Extrapolation
// Example 1 2 . 1
// L i n e a r I n t e r p o l a t i o n T e c h n i q u e
// Page no . 372
clc ; close ; clear ;
printf ( x :
)
f =[1 ,4 ,9 ,16 ,25];
for i =1:5
printf ( %i \ t ,i )
end
printf ( \ n f ( x ) :
)
for i =1:5
printf ( %i \ t ,f ( i ) )
end
x =2.5;
x1 =2; x2 =3; printf ( \n\ n f o r ( 2 , 4 ) and ( 3 , 9 ) )
f (2.5) = f ( x1 ) +( f ( x2 ) -f ( x1 ) ) *( x - x1 ) /( x2 - x1 )
169
18
19
20
21
22
23
24
25
26
27
28
29
30
printf ( \ n f ( 2 . 5 ) = %. 1 f ,f (2.5) )
x =2.5;
x1 =2; x2 =4; printf ( \n\ n f o r ( 2 , 4 ) and ( 4 , 1 6 ) )
f (2.5) = f ( x1 ) +( f ( x2 ) -f ( x1 ) ) *( x - x1 ) /( x2 - x1 )
printf ( \ n f ( 2 . 5 ) = %. 1 f ,f (2.5) )
x =2.5;
x1 =1; x2 =3; printf ( \n\ n f o r ( 1 , 1 ) and ( 3 , 9 ) )
f (2.5) = f ( x1 ) +( f ( x2 ) -f ( x1 ) ) *( x - x1 ) /( x2 - x1 )
printf ( \ n f ( 2 . 5 ) = %. 1 f ,f (2.5) )
printf ( \n\ nExact v a l u e = %. 2 f ,2.5^2)
// Example 1 2 . 2
// L a g a r a n g i a n Method
// Page no . 373
clc ; close ; clear ;
xk =[ -1 ,0 ,2 ,5];
yk =[10 ,7 ,7 ,22];
P =0;
x = poly (0 , x ) ;
for k =0:3
p = yk ( k +1)
for j =0:3
if ( j ~= k )
p = p *(( x - xk ( j +1) ) /( xk ( k +1) - xk ( j +1) ) )
end
end
170
18
P=P+p;
19 end
20 disp (P , P= )
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// f u n c t i o n f o r
exchanging values
z=a;y=b;x=z;
endfunction
deff ( y=P( a , b , c , d , e ) , y=( c ( d ) b ( d+1)c ( d+e ) b ( d ) ) / (
a ( d+e )a ( d ) ) ) // f u n c t i o n f o r f i n d i n g
polynomials
xi =[0.8 ,1 ,1.2 ,1.4 ,1.6];
yi =[2.2255 ,2.7183 ,3.3201 ,4.0552 ,4.9530];
x =1.23
[ xi (5) , xi (1) ,a ]= tran ( xi (1) , xi (5) )
[ xi (4) , xi (1) ,a ]= tran ( xi (1) , xi (4) )
[ xi (3) , xi (2) ,a ]= tran ( xi (2) , xi (3) )
[ xi (2) , xi (1) ,a ]= tran ( xi (1) , xi (2) )
[ yi (5) , yi (1) ,a ]= tran ( yi (1) , yi (5) )
[ yi (4) , yi (1) ,a ]= tran ( yi (1) , yi (4) )
[ yi (3) , yi (2) ,a ]= tran ( yi (2) , yi (3) )
[ yi (2) , yi (1) ,a ]= tran ( yi (1) , yi (2) )
for i =1:5
x_xi ( i ) =x - xi ( i ) ;
end
printf ( x i
xx i
y i \n )
171
25 printf ( \n )
26 for i =1:5
27
printf ( %. 1 f
%. 2 f \ t%f \n , xi ( i ) , x_xi ( i ) , yi ( i )
)
28 end
29 printf ( \n\ n P o l y n o m i a l s \n )
30 printf ( \n )
31 for i =1:4
32
for j =1:5 - i
33
printf ( %f\n ,P ( xi , yi , x_xi ,j , i ) )
34
yi ( j ) = P ( xi , yi , x_xi ,j , i )
35
end
36
printf ( \n\n\n )
37 end
// Example 1 2 . 4
// Newton s D i v i d e d D i f f e r e n c e I n t e r p o l a t i o n
// Page no . 381
clc ; close ; clear ;
x =[0 ,1 ,2 ,3 ,4 ,5]
y =[1 ,2 ,5 ,10 ,17 ,26];
y1 = y ;
deff ( y i=P( a , b , d , e ) , y i =(b ( d+1)b ( d ) ) / ( a ( d+e )a ( d ) )
) // f u n c t i o n f o r f i n d i n g p o l y n o m i a l s
for i =1:3
for j =1:6 - i
z (j , i ) = P (x ,y ,j , i )
y ( j ) = z (j , i )
end
end
172
16 z (6 ,1) =0;
17 printf ( x
18
19
20
21
22
23
24
y
f ( x0 , x1 )
f ( x0 , x1 , x3 )
f
( x0 , x1 , x2 , x3 ) \n )
printf (
\
n )
for j =1:6
printf ( %i
%i \ t%i \ t \ t%i \ t \ t%i \n ,x (1 , j )
, y1 (1 , j ) ,z (j ,1) ,z (j ,2) ,z (j ,3) )
end
x1 =2.6;
f = y1 (4) +( x1 - x (4) ) *( z (4 ,1) ) +( x1 - x (4) ) *( x1 - x (5) ) * z
(4 ,2)
printf ( \n\ n f ( 2 . 6 ) =%. 2 f ,f )
// Example 1 2 . 5
// I n t e r p o l a t i o n Methods
// Page no . 403
clc ; close ; clear ;
x =[0 ,1 ,2 ,3 ,4];
y =[0 ,1 ,8 ,27 ,64];
// I n v e r s e l a g r a n g e Method
P =0;
y1 =20;
for k =0:4
p = x ( k +1)
for j =0:4
if ( j ~= k )
p = p *(( y1 - y ( j +1) ) /( y ( k +1) -y ( j +1) ) )
173
17
end
18
end
19
P=P+p;
20 end
21 disp (P , I n v e r s e L a g r a n g e i n t e r p o l a t i o n x= )
22
23
24 // Newton s d i v i d e d i f f e r e n c e i n t e r p o l a t i o n
25 x1 = x ;
26 deff ( x i=P( a , b , d , y ) , x i =(b ( d+1)b ( d ) ) / ( a ( d+y )a ( d ) )
27
28
29
30
31
32
33
34
35
36
37
38
39
40
) // f u n c t i o n f o r f i n d i n g p o l y n o m i a l s
for i =1:2
for j =1:5 - i
z (j , i ) = P (y ,x ,j , i )
x ( j ) = z (j , i )
end
end
z (5 ,1) =0;
printf ( \n\n y \ t x
f ( y0 , y1 )
f ( y0 , y1 , y3 ) \n
)
printf ( \n
)
for j =1:5
printf ( %i \ t%i \ t%i \ t \ t%i \ t \n ,y (1 , j ) , x1 (1 ,
j ) ,z (j ,1) ,z (j ,2) )
end
y1 =20;
f = x1 (4) +( y1 - y (4) ) *( z (4 ,1) ) +( y1 - y (4) ) *( y1 - y (5) ) * z
(4 ,2)
printf ( \n\ nNewton D i v i d e D i f f e r e n c e x ( 2 0 )=%. 2 f ,f )
41
42
43 x = x1 ;
44 // I t e r a t e d L i n e a r I n t e r p o l a t i o n
45 function [x ,y , z ]= tran (a , b )
46
47
48
// f u n c t i o n f o r
exchanging values
z=a;y=b;x=z;
endfunction
deff ( y=P( a , b , c , d , e ) , y=( c ( d ) b ( d+1)c ( d+e ) b ( d ) ) / (
174
a ( d+e )a ( d ) ) )
polynomials
49 y1 =20
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// f u n c t i o n f o r f i n d i n g
84 end
85 printf ( \n\n\n x \ t y \ t d y \ t d 2 y \ t d 3 y \ t d 4 y \n )
86 printf (
\n )
87 for i =1:5
88
printf ( %i \ t%i \ t%i \ t%i \ t%i \ t%i \n ,x ( i ) , y1 ( i ) ,z (
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
176
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Example 1 2 . 6
// Chebyshev I n t e r p o l a t i n g P o l y n o m i a l
// Page no . 407
clc ; close ; clear ;
deff ( y=f ( x ) , y=1/(1+ exp (x ) ) ) ;
a = -2; b =2; n =3;
D = %pi /(2* n +2)
for k =0: n
t ( k +1) = - cos ( D *(2* k +1) )
x ( k +1) =(( a + b ) /2) +( b - a ) * t ( k +1) /2
y ( k +1) = f ( x ( k +1) )
C ( k +1) =0
end
for j =0: n
for k =0: n
L =(2* k +1) * D
C ( j +1) = C ( j +1) + y ( k +1) * cos ( j * L )
end
end
C (1) = C (1) /( n +1) ;
for j =1: n
C ( j +1) =2* C ( j +1) /( n +1)
end
x = poly (0 , x )
T (1) =1; T (2) = x ;
for j =1: n -1
T ( j +2) =2* x * T ( j +1) -T ( j )
end
P = C (1) * T (1)
for j =1: n
P = P + C ( j +1) * T ( j +1)
end
disp (P , P3 ( x )= )
printf ( \n\n\ nNote : Book h a s C a l c u l a t i o n e r r o r s i n
calculation of c o e f f i c i e n t s )
177
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Example 1 2 . 7
// Double I n t e r p o l a t i o n
// Page no . 409
clc ; close ; clear ;
x =[0 ,1 ,2 ,3 ,4];
y =[0 ,1 ,2 ,3 ,4];
z
=[0 ,1 ,8 ,27 ,64;1 ,3 ,11 ,31 ,69;4 ,7 ,16 ,37 ,76;9 ,13 ,23 ,45 ,85;16 ,21 ,32 ,55
printf ( y / x )
for i =1:5
printf ( \ t%i ,x ( i ) )
end
for i =1:5
printf ( \n %i ,y ( i ) )
for j =1:5
printf ( \ t%i ,z (j , i ) )
end
end
printf ( \n\n\n )
for i =1:5
x =2.5;
x1 =2; x2 =3;
z1 (1 , i ) = z (i , x1 +1) +( z (i , x2 +1) -z (i , x1 +1) ) *( x - x1 ) /(
x2 - x1 )
end
printf ( V a l u e s o f z a t x = 2 . 5 : \ n\n y )
for i =1:5
printf ( \ t%i ,y ( i ) )
178
28
29
30
31
32
33
34
35
36
end
printf ( \n z )
for i =1:5
printf ( \ t%g , z1 ( i ) )
end
y =1.5;
y1 =1; y2 =2;
z2 = z1 ( y1 +1) +( z1 ( y2 +1) - z1 ( y1 +1) ) *( y - y1 ) /( y2 - y1 )
printf ( \n\ nValue o f z a t x =2.5 and y =1.5 : %g , z2 )
) /(4+ pi (i -1) )
14
15
16
17
18
end
si2 (8) =0;
si2 (1) =0; si1 (8) =0;
si1 (1) =0;
for i =7: -1:2
179
19
si2 ( i ) = pi ( i ) * si2 ( i +1) + qi ( i )
20 end
21 for i =2:8
22
si1 ( i ) = si1 (i -1) + h *( si2 ( i ) + si2 (i -1) ) /2
23 end
24 printf ( \n i \ t x i \ t f i \ t
pi \t\t
qi \t\t
25
26
27
28
29
30
31
32
si2 \t\t
si1 )
printf ( \n
)
for i =1:8
printf ( \n %i \ t%g \ t%g \ t%f \ t%f \ t%f \ t%f ,i , xi ( i ) ,
yi ( i ) , pi ( i ) , qi ( i ) , si2 ( i ) , si1 ( i ) )
end
x =0.1325;
i =4;
s = yi ( i ) +( x - xi ( i ) ) * si1 ( i ) +( si2 ( i ) *( x - xi ( i ) ) ^2) /2+((
si2 ( i +1) - si2 ( i ) ) /( xi ( i +1) - xi ( i ) ) ) *(( x - xi ( i ) ) ^3) /6
printf ( \n\ n S p l i n e I n t e r p o l a t e d V a l u e o f s ( 0 . 1 3 2 5 )
i s : %f ,s )
180
Chapter 13
Numerical Differentiation
// Example 1 3 . 1
// D i f f e r e n t i a t i o n
// Page no . 420
clc ; close ; clear ;
deff ( y=f ( x ) , y=x2+5 )
deff ( y=f 1 ( x , h ) , y=( f ( x+h ) f ( x ) ) / h )
h =0.01; x =2.4
d = f1 (x , h )
d1 =( f1 ( x +h , h ) - f1 ( x ) ) / h
printf ( dy \n = %g\n dx ,d )
printf ( \n\n\n d2y \n = %g\n dx2 , d1 )
1
2
3
4
5
6
7
8
9
10
11
12
13
// Example 1 3 . 2
// C a l c u l a t i o n o f xc o o r d i n a t e o f Minimum P o i n t
// Page no . 422
clc ; close ; clear ;
for i =1:7
for j =1:6
z (i , j ) =0
end
end
h =0.2
printf (
x
y
d
d2
d3
d4 \n )
printf (
)
for i =1:7
z (i ,1) = i /5;
end
z (1 ,2) =2.10022
z (2 ,2) =1.98730
z (3 ,2) =1.90940
z (4 ,2) =1.86672
z (5 ,2) =1.85937
z (6 ,2) =1.88755
z (7 ,2) =1.95147
for i =3:6
for j =1:9 - i
z (j , i ) = z ( j +1 ,i -1) -z (j ,i -1)
end
end
disp ( z )
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 s = poly (0 , s )
32 p = z (5 ,2) ; k =4;
33 for i =3:5
34
r =1;
35
for j =1: i -2
182
36
r = r *( s +( j -1) )
37
end
38
r = r * z (k , i ) / factorial ( j ) ;
39
k =k -1;
40
p=p+r;
41
42 end
43 disp ( p )
44 s =( - z (4 ,3) + z (3 ,4) /2) / z (3 ,4)
45 disp (s , s= )
46 x = z (5 ,1) + s * h
47 disp (x , x= )
d3 \ t
\t
d4 \n )
printf (
)
7 h =0.05;
8 z
=[1.00 ,1.00000;1.05 ,1.02470;1.10 ,1.04881;1.15 ,1.07238;1.20 ,1.09544
6
183
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
for j =1:9 - i
z (j , i ) = z ( j +1 ,i -1) -z (j ,i -1)
end
end
printf ( \n )
for i =1:7
for j =1:6
if z (i , j ) ==0 then
printf ( \ t )
else
printf ( %. 7 f \ t ,z (i , j ) )
end
end
printf ( \n )
end
s = poly (0 , s )
p = z (5 ,2) ; k =4;
for i =3:5
r =1;
for j =1: i -2
r = r *( s +( j -1) )
end
r = r * z (k , i ) / factorial ( j ) ;
k =k -1;
p=p+r;
end
disp (p , y ( s ) = )
printf ( \n\ ny1 ( 1 ) = %g , f1 (1 ,0) )
printf ( \n\ ny2 ( 1 ) = %g , f2 (1 ,0) )
printf ( \n\ ny3 ( 1 ) = %g , f3 (1 ,0) )
printf ( \n\ ny1 ( 1 . 0 2 5 ) = %g , f1 (1 ,0.5) )
184
d3 \ t
\t
d4 \n )
6 printf (
)
7 h =0.02;
8 z
=[0.96 ,1.8025;0.98 ,1.7939;1.00 ,1.7851;1.02 ,1.7763;1.04 ,1.7673];
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
185
7
8
9
10
11
d3 \n
)
printf (
)
h =0.01; s =0.5;
deff ( y=f 1 ( x , s ) , y =(( z ( x , 3 ) +z ( x 1 ,3) ) /2+ s z ( x 1 ,4)
+( z ( x 1 ,5)+z ( x 2 ,5) ) ( 3 s 2 1) / 1 2 ) / h )
deff ( y=f 2 ( x , s ) , y=( z ( x 1 ,4) ) /h 2 )
deff ( y=f 3 ( x , s ) , y=( z ( x 1 ,5)+z ( x 2 ,5) ) / ( 2 h 3 ) )
z
=[1.00 ,1.00000;1.01 ,1.00499;1.02 ,1.00995;1.03 ,1.01489;1.04 ,1.01980
12 for i =3:5
13
for j =1:19 - i
14
z (j , i ) = z ( j +1 ,i -1) -z (j ,i -1)
15
end
16 end
17 printf ( \n )
18 for i =1:17
19
for j =1:5
20
if z (i , j ) ==0 then
21
printf ( \ t )
22
else
23
printf ( %. 7 f \ t ,z (i , j ) )
24
end
186
25
end
26
printf ( \n )
27 end
28 printf ( \n\ ny1 ( 1 . 1 2 5 ) = %g
0 . 4 7 7 1 4 0 4 ) , f1 (13 ,0.5) )
29 printf ( \n\ ny2 ( 1 . 1 2 5 ) = %g
0 . 2 0 9 5 1 ) , f2 (13 ,0.5) )
30 printf ( \n\ ny3 ( 1 . 1 2 5 ) = %g
, f3 (13 ,0.5) )
( exact value =
( exact value =
( exact value = 0.27935)
// Example 1 3 . 7
187
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// R i c h a r d s o n E x t r a p o l a t i o n
// Page no . 431
clc ; close ; clear ;
deff ( y=f ( x ) , y=exp ( 2 x ) )
e =10^ -4; h =0.8;
D1 =0;
for i =1:4
printf ( \n )
for j =1: i
if j ==1 then
D (i , j ) =( f ( h ) -f ( - h ) ) /(2* h )
else
D (i , j ) = D (i ,j -1) +( D (i ,j -1) -D (i -1 ,j -1) )
/(2^(2*( j -1) ) -1)
end
printf ( %g\ t \ t ,D (i , j ) )
end
h = h /2
end
printf ( \n\n\ t \ t \ t \ t \ t \ t 2 x \ nHence , t h e d e r i v a t i v e
of the function y = f ( x ) = e
a t x=0 i s D( 3 , 3 ) =
%g ,D (i , j ) )
188
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
N =4;
h =( b - a ) /( N +1) ;
for j =1: N
s(j)=f(a+j*h)
end
for i =1: N
for j =1: N
if abs (i - j ) ==1 then
A (i , j ) = -1
end
if i == j then
A (i , j ) =2+ s ( i ) * h ^2
end
end
if i ==1 then
k (i ,1) = s ( i ) + a1 / h ^2
elseif i == N
k (i ,1) = s ( i ) + b1 / h ^2
else
k (i ,1) = s ( i )
end
end
disp (A , A = )
disp (k , k = )
189
Chapter 14
Numerical Integration
// Example 1 4 . 2
// S i m p s o n s 1/3 r d Rule
// Page no 442
clc ; clear ; close ;
x (1 ,1) =0
for i =2:9
x (1 , i ) = x (1 ,1) +( i -1) *10
end
y
=[30 ,31.63 ,33.44 ,35.47 ,37.75 ,40.33 ,43.29 ,46.69 ,50.67]
11
12 // t r a p e z o i d a l r u l e
13 S =0;
14 h =( x (9) -x (1) ) /8
15 for j =1:9
16
S =0;
17
for i =1: j
190
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
if ( i ==1 | i == j )
S=S+y(i)
else
S = S +2* y ( i )
end
end
S = S * h /2
printf ( \n V e l o c i t y a t t ( %i ) = %. 2 f ,x ( j ) ,S )
y1 ( j ) = S
end
y1 (1) =0;
// S i m p s o n s 1/3 r d Rule
S =0;
h =( x (9) -x (1) ) /8
for i =1:9
if ( i ==1 | i ==9)
S = S + y1 ( i )
elseif ((( i ) /2) - fix (( i ) /2) ==0)
S = S +4* y1 ( i )
else
S = S +2* y1 ( i )
end
end
S = S * h /3;
S = S /1000
printf ( \n\ n S i m p s o n s 1/3 r d Rule Sum = %g km ,S )
// Example 1 4 . 3
// T r a p e z o i d a l Rule and S i m p s o n s Rule
// Page no . 442
191
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
end
43 end
44 S = S * h /3
45 printf ( \n\ n S i m p s o n s 1/3 r d Rule Sum = %g ,S )
193
27 end
28 for i =2:3
29
for k =1:4 - i
30
z ( k *2+ i -2 , i ) = z (2* k -1+ i ,i -1) +( z (2* k -1+ i ,i -1) -z (2*
k -3+ i ,i -1) ) /3
31 end
32 end
33
34 printf ( \n\n )
35 disp (z , The T a b l e o f
values : )
l o g ( x ) , x ,0 ,1)
10 end
11 disp (s , T i l l 1 0 0 0 t e r m s
....
I =)
// Example 1 4 . 8
194
I = %g
,s )
,s )
// Example 1 4 . 1 0
// S p l i n e I n t e g r a t i o n Method
195
3 // Page no . 478
4 clc ; close ; clear ;
5
6 deff ( y=f ( x ) , y=s i n d ( %pi x ) )
7 deff ( y=f 1 ( x , h ) , y=( f ( x+h ) f ( x ) ) / h )
8 h =0.01;
9 n =2; h =0.5; a =0; b =1;
10 disp ( integrate ( f ( x ) , x ,0 ,1) , I = )
// Example 1 4 . 1
// T r a p e z o i d a l Rule
// Page no 440
clc ; clear ; close ;
x1 =1.46
for i =1:6
x (1 , i ) = x1 + i /100
end
y =[3.86 ,3.90 ,3.96 ,4.02 ,4.06 ,4.12]
// t r a p e z o i d a l r u l e
S =0;
h =( x (6) - x1 ) /6
for i =1:6
if ( i ==1 | i ==6)
S=S+y(i)
else
S = S +2* y ( i )
end
end
S = S * h /2
printf ( \n I = %g ,S )
196
// Example 1 4 . 1 4
// T r a p e z o i d a l and S i m p s o n s Rule
// Page no . 486
clc ; close ; clear ;
x (1) =0.5; y (1) =0.5; h =0.25
for i =2:3
x ( i ) = x (i -1) + h
y ( i ) = y (i -1) + h
end
printf ( y / x \ t | \ t%g \ t%g \ t%g ,x (1) ,x (2) ,x (3) )
printf ( \n| )
for i =1:3
printf ( \n%g\ t | \ t ,y ( i ) )
for j =1:3
z (i , j ) = x ( j ) * y ( i )
printf ( %g\ t ,z (i , j ) )
end
end
// t r a p e z o i d a l r u l e
s =0;
for i =1:3
for j =1:3
if i ==1 & j ==1 then
s = s + z (i , j )
elseif i ==3 & j ==3
s = s + z (i , j )
else
s = s +2* z (i , j )
197
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
end
end
end
s =( s *( h ^2) ) /4
printf ( \n\n )
disp (s , T r a p e z o i d a l Rule Sum = )
printf ( \n\n )
// s i m p s o n s r u l e
s =0;
for i =1:3
for j =1:3
if i /2 - int ( i /2) ==0 & j /2 - int ( j /2) ==0 then
s = s +16* z (i , j )
elseif i /2 - int ( i /2) ~=0 & j /2 - int ( j /2) ~=0
s = s + z (i , j )
else
s = s +4* z (i , j )
end
end
end
s =( s *( h ^2) ) /9
disp (s , S i m p s o n s Rule Sum = )
198
9
y ( i ) = y (i -1) + h
10 end
11 printf ( y / x \ t | \ t%g \ t%g \ t%g \ t%g \ t%g ,x (1) ,x (2) ,x (3) ,
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
x (4) ,x (5) )
printf ( \n
|
)
for i =1:5
printf ( \n%g\ t | \ t ,y ( i ) )
for j =1:5
z (i , j ) = x ( j ) * y ( i )
printf ( %g\ t ,z (i , j ) )
end
end
// t r a p e z o i d a l r u l e
s =0;
for i =1:5
for j =1:5
if i ==1 & j ==1 then
s = s + z (i , j )
elseif i ==5 & j ==5
s = s + z (i , j )
else
s = s +2* z (i , j )
end
end
end
s =( s *( h ^2) ) /4
printf ( \n\n )
disp (s , T r a p e z o i d a l Rule Sum = )
printf ( \n\n )
// s i m p s o n s r u l e
s =0;
for i =1:5
for j =1:5
if i /2 - int ( i /2) ==0 & j /2 - int ( j /2) ==0 then
199
44
if i == j then
45
s = s +16* z (i , j )
46
else
47
s = s +4* z (i , j )
48
end
49
50
elseif i /2 - int ( i /2) ~=0 & j /2 - int ( j /2) ~=0
51
s = s + z (i , j )
52
else
53
s = s +4* z (i , j )
54
end
55
end
56 end
57 s =( s *( h ^2) ) /9
58 disp (s , S i m p s o n s Rule Sum = )
// Example 1 4 . 1 6
// M u l t i p l e I n t e g r a t i o n w i t h V a r i a b l e L i m i t s
// Page no . 491
clc ; close ; clear ;
deff ( z=f ( x ) , z=x+1 )
deff ( z=f 1 ( y ) , z =(y +1) 3 ( y +3) 2 )
s =5/9* f ( - sqrt (3/5) ) +8/9* f (0) +5/9* f ( sqrt (3/5) )
s = s *5/9* f1 ( - sqrt (3/5) ) +8/9* f1 (0) +5/9* f1 ( sqrt (3/5) )
s = s /256;
disp (s , I = )
200
201
Chapter 15
Numerical Solutions of
Ordinary Differential Equations
Initial Value Problem
// Example 1 5 . 6
202
2 // T a y l o r Method
3 // Page no . 510
4 clc ; clear ; close ;
5
6 deff ( y=f 1 ( x , y ) , y=x2+y 2 )
7 deff ( y=f 2 ( x , y ) , y=2x+2y f 1 ( x , y ) )
8 deff ( y=f 3 ( x , y ) , y=2+2 f 1 ( x , y ) 2+2 y f 2 ( x , y ) )
9 deff ( y=f 4 ( x , y ) , y=6 f 1 ( x , y ) f 2 ( x , y ) +2y f 3 ( x , y ) )
10 h =0.2;
11 for l =1:2
12
a =0; y =0; x =0;
13
printf ( \n\nh = %g\n
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
\n ,h )
for i =1:4
x = a +( i -1) * h
k =0;
for j =1:4
if j ==1 then
k = k +( h ^ j ) * f1 (x , y ) / factorial ( j )
elseif j ==2
k = k +( h ^ j ) * f2 (x , y ) / factorial ( j )
elseif j ==3
k = k +( h ^ j ) * f3 (x , y ) / factorial ( j )
else
k = k +( h ^ j ) * f4 (x , y ) / factorial ( j )
end
end
y=y+k;
printf ( \ nx = %g\n\ ny (%g) = %g\n\n ,x , x +0.2 , y )
end
h = h +0.2;
end
203
// Example 1 5 . 7
// P i c a r d Method
// Page no . 511
clc ; clear ; close ;
deff ( y=f ( x , y ) , y=x2+y 2 )
y (1) =0;
for i =1:2
y ( i +1) = y (1) + integrate ( f ( x , y ( i ) ) , x ,0 , i /10)
printf ( \n y (%g) = %g\n ,i /10 , y ( i +1) )
end
// Example 1 5 . 8
// E u l e r Method
// Page no . 513
clc ; clear ; close ;
deff ( y=f ( x , y ) , y=x+y )
y (1) =1;
h =0.1;
for i =1:6
printf ( \ ny (%g) = %g\n ,(i -1) /10 , y ( i ) )
y ( i +1) = y ( i ) + h * f (( i -1) /10 , y ( i ) )
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Example 1 5 . 9
// Trapezium Method
// Page no . 516
clc ; clear ; close ;
deff ( y=f ( x , y ) , y=x y 2 )
y =1;
h =0.2;
y2 = poly (0 , y2 )
for i =1:2
x =( i -1) * h ;
x1 = x + h
y1 = roots ( - y2 + y + h *( f (x , y ) + f ( x1 , y2 ) ) /2)
printf ( \n Y( %i ) = %g o r %g\n ,i , y1 (1) , y1 (2) )
end
// Example 1 5 . 1 0
// Heun Method
// Page no . 517
clc ; clear ; close ;
deff ( y=f ( x , y ) , y=y 2/ x )
y =2;
h =0.25;
for i =1:4
x =1+( i -1) * h
x1 = x + h
ye = y + h * f (x , y )
y = y + h *( f (x , y ) + f ( x1 , ye ) ) /2
printf ( \n y (%g) = %g\n ,x1 , y )
end
205
9
10
11
12
13
14
15
16
\n
)
for i =1:3
x =( i -1) * h
s1 = f (x , y ) ;
s2 = f ( x + h /2 , y + s1 * h /2) ;
printf ( %i \ t%g \ t%g \ t%g \ t%g ,i -1 ,x ,y , s1 , s2 )
y = y + s2 * h ;
printf ( \ t%g \n ,y )
end
206
6
7
8
9
10
11
12
13
14
15
y =1;
h =0.2;
Z (1) = y ;
Z (2) = Z (1) + h * f (0 , Z (1) )
printf ( Z ( %i ) = %g ,1 , Z (2) )
for i =2:5
x =( i -1) * h ;
Y (i -1) =( Z ( i ) + Z (i -1) + h * f (x , Z ( i ) ) ) /2
Z ( i +1) = Z (i -1) +2* h * f (x , Z ( i ) )
printf ( \n Y( %i ) = %g\n\n\n Z ( %i ) = %g ,i -1 , Y (i
-1) ,i , Z ( i +1) )
16 end
17 printf ( \n\n\n y4 = %g ,(4* Y (4) -Y (2) ) /3)
10
11
12
13
computed ) \n
n )
for i =1:6
x =( i -1) * h
if i <6 then
printf ( %i \t% . 2 f \t% . 5 f \ t \ t \ t \t% . 5 f \n ,i -1 ,x
,y , f1 ( x ) )
207
14
else
15
printf ( %i \t% . 2 f \t% . 5 f \ t \ t \ t \ t \n ,i -1 ,x , y )
16
end
17
y=y+h*f(y);
18 end
// Example 1 5 . 1 4
// S e c o n d Order Runge Kutta Method
// Page no . 525
clc ; clear ; close ;
deff ( y=f ( x , y ) , y=xy )
y =1; x =1; h =0.1;
// s i m p l e r u n g e k u t t a method
K1 = h * f (x , y ) ;
K2 = h * f ( x +h , y + K1 ) ;
y1 = y +( K1 + K2 ) /2
printf ( \ ny ( 1 . 1 ) by s i m p l e r u n g e k u t t a method = %g\n
\n , y1 )
// e u l e r c a u c h y method
K1 = h * f (x , y ) ;
K2 = h * f ( x + h /2 , y + K1 /2) ;
y1 = y +( K1 + K2 )
printf ( y ( 1 . 1 ) by e u l e r c a u c h y method = %g\n\n , y1 )
// o p t i m a l method
K1 = h * f (x , y ) ;
K2 = h * f ( x +2* h /3 , y +2* K1 /3) ;
y1 = y +( K1 +3* K2 ) /4
printf ( y ( 1 . 1 ) by o p t i m a l method = %g , y1 )
208
// Example 1 5 . 1 5
// T h i r d Order Runge Kutta Method
// Page no . 526
clc ; clear ; close ;
deff ( y=f ( x , y ) , y=xy )
y =1; x =1; h =0.1;
// scheme 1
K1 = h * f (x , y ) ;
K2 = h * f ( x + h /2 , y + K1 /2) ;
K3 = h * f ( x + h /2 ,y - K1 +2* K2 ) ;
y1 = y +( K1 +4* K2 + K3 ) /6
printf ( \ ny ( 1 . 1 ) by scheme 1 = %g\n\n , y1 )
// scheme 2
K1 = h * f (x , y ) ;
K2 = h * f ( x + h /3 , y + K1 /3) ;
K3 = h * f ( x +2* h /3 , y +2* K2 /3) ;
y1 = y +( K1 +3* K3 ) /4
printf ( \ ny ( 1 . 1 ) by scheme 2 = %. 7 f \n\n , y1 )
// Example 1 5 . 1 6
// F o u r t h Order Runge Kutta Method
// Page no . 528
209
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
210
8
9
10
11
12
13
14
15
16
17
18
\n
)
for i =0:14
K1 = h * f (x , y ) ;
K2 = h * f ( x + h /3 , y + K1 /3) ;
K3 = h * f ( x + h /3 , y +( K1 + K2 ) /6) ;
K4 = h * f ( x + h /2 , y +( K1 +3* K3 ) /8) ;
K5 = h * f ( x +h , y +( K1 -3* K3 +4* K4 ) /2)
y1 = y +( K1 +4* K4 + K5 ) /6
printf ( \n %i \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t%
. 3 f \t% . 3 f ,i ,x ,y , K1 , K2 , K3 , K4 , K5 , y1 )
y = y1 ;
x=x+h;
end
211
7
8
9
10
11
12
13
14
15
16
17
K1 = h * f (x , y ) ;
K2 = h * f ( x + h /4 , y + K1 /4) ;
K3 = h * f ( x +3* h /8 , y +3*( K1 +3* K2 ) /32) ;
K4 = h * f ( x +12* h /13 , y +1932* K1 /2197 -7200* K2 /2197+7296* K3
/2197) ;
K5 = h * f ( x +h , y +439* K1 /216 -8* K2 +3680* K3 /513 -845* K4
/4104)
K6 = h * f ( x + h /2 ,y -8* K1 /27+2* K2 -3544* K3 /2565+1859* K4
/4104 -11* K5 /40)
disp ( K6 , K6 = ,K5 , K5 = ,K4 , K4 = ,K3 , K3 = ,K2 , K2
= ,K1 , K1 = )
y1 = y +(25* K1 /216+1408* K3 /2565+2197* K4 /4104 - K5 /5)
y11 = y +(16* K1 /135+6656* K3 /12825+28561* K4 /56430 -9* K5
/50+2* K6 /55)
printf ( \ ny ( 1 . 1 ) = %. 9 f \n\n , y1 )
printf ( \ ny ( 1 . 1 ) = %. 9 f \n\n , y11 )
// Example 1 5 . 2 0
// Carp Karp Runge Kutta Method
// Page no . 537
clc ; clear ; close ;
deff ( y=f ( x , y ) , y=xy )
y =1; x =1; h =0.1; printf ( \n )
U =[0 ,1/5 ,3/10 ,3/5 ,1 ,7/8];
v
=[0 ,0 ,0 ,0 ,0;1/5 ,0 ,0 ,0 ,0;3/40 ,9/40 ,0 ,0 ,0;3/10 , -9/10 ,6/5 ,0 ,0; -11/54
212
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
for l =1:5
K (1) = h * f (x , y ) ;
for i =2:6
k =0;
for j =1: i -1
k = k + v (i , j ) * K ( j )
end
K ( i ) = h * f ( x + U ( i ) *h , y + k )
end
k =0;
for i =1:6
k=k+a(i)*K(i)
end
y1 = y + k ;
k =0;
for i =1:6
k = k + a1 ( i ) * K ( i )
end
y11 = y + k ;
for i =1:6
printf ( K%i = %. 9 f \n ,i , K ( i ) )
end
printf ( \ ny ( 1 . 1 ) = Y%i = %. 9 f \n ,l , y1 )
printf ( y ( 1 . 1 ) = Y%i = %. 9 f \n ,l , y11 )
y = y1 ;
printf ( \n\n\n )
end
// Example 1 5 . 2 1
// I m p l i c i t Runge Kutta Method
// Page no . 539
213
4
5
6
7
8
9
10
11
12
13
8
9
10
11
12
\n
0\ t%g \ t \t% . 3 f \ t \t% . 3 f \n ,x (1) ,y (1) ,f ( x (1) ,y (1) ) )
;
for i =2:11
x ( i ) =( i -1) * h ;
y ( i +1) =( - y ( i ) -y (i -1) + h *( f ( x ( i ) ,y ( i ) ) + f ( x (i -1) ,y (
i -1) ) ) ) /2;
printf ( %i \t% . 3 f \ t \t% . 3 f \ t \t% . 3 f \n ,i -1 , x ( i ) ,y (
i ) ,f ( x ( i ) ,y ( i ) ) )
end
214
// Example 1 5 . 2 3
// M i l n e Simpson P r e d i c t o r C o r r e c t o r Method
// Page no . 544
clc ; clear ; close ;
deff ( y=f ( x , y ) , y=y+exp ( x ) )
h =0.5;
y =[1 ,1.824 ,3.718 ,7.722]
for i =1:4
x =( i -1) * h ;
f1 ( i ) = f (x , y ( i ) ) ;
printf ( \ n f % i = %g ,i -1 , f1 ( i ) )
end
y41 = y (1) +4* h *(2* f1 (4) - f1 (3) +2* f1 (2) ) /3
f4 = f ( x +h , y41 ) ;
y4 = y (3) + h *( f4 +4* f1 (4) + f1 (3) ) /3
printf ( \n\n\ n P r e d i c t o r = %. 9 f \n\n , y41 )
printf ( E v a l u a t o r = %. 9 f \n\n , f4 )
printf ( C o r r e c t o r = %. 9 f , y4 )
Scilab code Exa 15.24 Improved Milne Simpson Predictor Corrector Method
1 // Example 1 5 . 2 4
2 // Improved M i l n e Simpson P r e d i c t o r
3 // Page no . 546
4 clc ; clear ; close ;
5
215
C o r r e c t o r Method
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
+1) \n
n )
f1 (1) = f (x , y (1) ) ;
for i =1:3
K1 = h * f (x , y ( i ) ) ;
K2 = h * f ( x +2* h /3 , y ( i ) +2* K1 /3) ;
y ( i +1) = y ( i ) +( K1 +3* K2 ) /4
printf ( %i \t% . 3 f \t% . 3 f \t% . 3 f \n ,i -1 ,x , y ( i ) , f1 ( i
))
x=x+h
f1 ( i +1) = f (x , y ( i +1) )
end
Y31 =0
for i =3:10
Y41 = y (i -2) +4* h *(2* f1 (4) - f1 (3) +2* f1 (2) ) /3
//
predictor
m4 = Y41 +28*( y ( i +1) - Y31 ) /29
// m o d i f i e r
v4 = f ( x +h , m4 )
// e v a l u a t o r
Y4 = y ( i ) + h *( v4 +4* f1 (4) + f1 (3) ) /3
// c o r r e c t o r
printf ( %i \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \
t% . 3 f \t% . 3 f \n ,i ,x , y ( i +1) , f1 (4) ,Y31 , y ( i +1) ,
Y41 , m4 , v4 )
y ( i +2) = Y4
Y31 = Y41 ;
f1 (2) = f1 (3) ;
f1 (3) = f1 (4) ;
f1 (4) = f ( x +h , y ( i +2) )
x=x+h
end
216
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
n )
f1 (1) = f (x , y (1) ) ;
for i =1:3
K1 = h * f (x , y ( i ) ) ;
K2 = h * f ( x +2* h /3 , y ( i ) +2* K1 /3) ;
y ( i +1) = y ( i ) +( K1 +3* K2 ) /4
printf ( %i \t% . 3 f \t% . 3 f \t% . 3 f \n ,i -1 ,x , y ( i ) , f1 ( i
))
x=x+h
f1 ( i +1) = f (x , y ( i +1) )
end
Y31 = y (4) ; Yc =0
for i =3:10
Y41 = y (i -2) +4* h *(2* f1 (4) - f1 (3) +2* f1 (2) ) /3 //
predictor
m4 = Y41 +112*( Y31 - Yc ) /121
// m o d i f i e r
v4 = f ( x +h , m4 )
// e v a l u a t o r
Y4c =(9* y ( i +1) -y (i -1) ) /8+3* h *( v4 +2* f1 (4) - f1 (3) ) /8
// c o r r e c t o r
Y4 = Y4c +9*( Y41 - Y4c ) /121
// f i n a l v a l u e
printf ( %i \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \
t% . 3 f \t% . 3 f \t% . 3 f \n ,i ,x , y ( i +1) , f1 (4) ,Y31 , Yc ,
Y41 , m4 , v4 , Y4c )
y ( i +2) = Y4
Y31 = Y41 ;
f1 (2) = f1 (3) ;
217
29
30
31
32
33 end
f1 (3) = f1 (4) ;
f1 (4) = f ( x +h , y ( i +2) )
Yc = Y4c
x=x+h
17
18
19
20
21
22
23
// Example 1 5 . 2 6
// M u l t i Valued Method
// Page no . 553
clc ; clear ; close ;
deff ( y=f 1 ( x , y ) , y=2x2y )
h =0.1; x =0; y = -1;
deff ( y=f 2 ( x , y ) , y=4xf 1 ( x , y ) )
deff ( y=f 3 ( x , y ) , y=4 f 2 ( x , y ) )
B =[1 ,1 ,1 ,1;0 ,1 ,2 ,3;0 ,0 ,1 ,3;0 ,0 ,0 ,1];
y0 =[ y ; h * f1 (x , y ) ; h ^2* f2 (x , y ) /2; h ^3* f3 (x , y ) /6]
y01 = y0 ;
r =[0;1;3/4;1/6]
disp (r , I f r = )
printf ( \n\n
nx = 0\ t \ t \ t x = 0 . 1 \ t \ t \ t \ t x = 0 . 2 \ n\ t )
for i =1:2
y11 = B * y01
s ( i ) = h *( f1 ( x +h , y11 (1) ) ) - y11 (2)
y1 = y11 + s ( i ) * r
if i ==2 then
break
end
218
24
y2 = y1 ;
25
y22 = y11 ;
26
y01 = y1
27 end
28 printf ( \ t
( s = %. 5 g ) \ t \ t
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
( s = %. 9 f ) \n
n
Y0\ t \ t
Y i \ t \ t
Y1\ t \ t
Y 2 \ t \ t
Y2\n
n ,s (1) ,s (2) )
for i =1:4
printf ( %. 5 f \t% . 5 f \t% . 5 f \t% . 5 f \t% . 5 f \n , y0 ( i
) , y22 ( i ) , y2 ( i ) , y11 ( i ) , y1 ( i ) )
end
y0 =[ y ; h * f1 (x , y ) ; h ^2* f2 (x , y ) /2; h ^3* f3 (x , y ) /6]
y01 = y0 ;
r =[5/12;1;3/4;1/6]
disp (r , I f r = )
printf ( \n\n
nx = 0\ t \ t \ t x = 0 . 1 \ t \ t \ t \ t x = 0 . 2 \ n\ t )
for i =1:2
y11 = B * y01
s ( i ) = h *( f1 ( x +h , y11 (1) ) ) - y11 (2)
y1 = y11 + s ( i ) * r
if i ==2 then
break
end
y2 = y1 ;
y22 = y11 ;
y01 = y1
end
printf ( \ t
( s = %. 5 g ) \ t \ t
( s = %. 9 f ) \n
n
Y0\ t \ t
Y i \ t \ t
Y1\ t \ t
Y 2 \ t \ t
Y2\n
n ,s (1) ,s (2) )
for i =1:4
printf ( %. 5 f \t% . 5 f \t% . 5 f \t% . 5 f \t% . 5 f \n , y0 ( i
219
) , y22 ( i ) , y2 ( i ) , y11 ( i ) , y1 ( i ) )
51 end
\n )
11 Y =[ y1 ; y2 ]
12 for i =1:8
13
14
F =[ f1 (x , Y (1) ,Y (2) ) ; f2 (x , Y (1) ,Y (2) ) ]
15
Y1 = Y + h * F
16
x=x+h;
17
F1 =[ f1 (x , Y1 (1) , Y1 (2) ) ; f2 (x , Y1 (1) , Y1 (2) ) ]
18
Y = Y +( h /2) *( F + F1 )
19
printf ( %g\t% . 3 f \t% . 3 f \n ,x -h , Y (1) ,Y (2) )
20
21 end
22
23 // c l a s s i c a l r u n g e k u t t a method
24 printf ( \n\n\ n C l a s s i c a l Runge Kutta Method : \ n\n n\ t x
n )
220
25 Y =[ y1 ; y2 ]; x =0;
26 for i =1:6
27
K1 = h *[ f1 (x , Y (1) ,Y (2) ) ; f2 (x , Y (1) ,Y (2) ) ]
28
K2 = h *[ f1 ( x + h /2 , Y (1) + K1 (1) /2 , Y (2) + K1 (2) /2) ; f2 ( x + h
29
30
31
32
33
34
35 end
// Example 1 5 . 2 8
// D i f f e r e n t i a l E q u a t i o n
// Page no . 562
clc ; clear ; close ;
deff ( y=f ( x , y ) , y=2y 2/(1+ x ) )
h =0.1; z (1) = -1;
for i =1:11
printf ( \nZ (%g) = %g\n ,(i -1) /10 , z ( i ) )
z ( i +1) = z ( i ) + h * f (( i -1) /10 , z ( i ) )
end
221
222
Chapter 16
Numerical Solutions of
Ordinary Differential Equations
Boundary Value Problems
\n ,n -1)
K1 (1) = h * Z0 ;
223
15
printf ( \n K11 = %g , K1 (1) ) ;
16
K1 (2) = h * f ( Y0 ) ;
17
printf ( \n K12 = %g , K1 (2) ) ;
18
K2 = h * f ( Y0 + K1 (2) )
19
printf ( \n K22 = %g , K2 ) ;
20
Z0 = Z0 +( K1 (2) + K2 ) /2
21
printf ( \n Z%i = %g ,n , Z0 ) ;
22
K2 = h * Z0 ;
23
printf ( \n K21 = %g , K2 ) ;
24
Y0 = Y0 +( K1 (1) + K2 ) /2
25
printf ( \n Y%i = %g ,n , Y0 ) ;
26
printf ( \n\n\n )
27
if n ==1 then
28
Y2 = Y0
29
end
30
end
31
printf ( \n\n\n )
32 end
33 printf ( Hence t h e s o l u t i o n i s y (%g) = %i , y (%g) = %
.4 f
and
224
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
h =( b - a ) / n
y = y0 ; y01 =0; x = a ;
for i =0:5
yi1 (1 , i +1) = y
K1 = h * y01 ;
R1 = h * F1 (x ,y , y01 ) ;
K2 = h *( y + R1 /2) ;
R2 = h * F1 ( x + h /2 , y + K1 /2 , y01 + R1 /2)
K3 = h *( y01 + R2 /2)
R3 = h * F1 ( x + h /2 , y + K2 /2 , y01 + R2 /2)
K4 = h *( y + R3 )
R4 = h * F1 ( x +h , y + K3 , y01 + R3 )
y = y +( K1 +2* K2 +2* K3 + K4 ) /6
y01 = y01 +( R1 +2* R2 +2* R3 + R4 ) /6
x=x+h
end
y =0; y01 =1; x = a ;
for i =0:5
yi2 (1 , i +1) = y
K1 = h * y01 ;
R1 = h * F2 (x ,y , y01 ) ;
K2 = h *( y + R1 /2) ;
R2 = h * F2 ( x + h /2 , y + K1 /2 , y01 + R1 /2)
K3 = h *( y01 + R2 /2)
R3 = h * F2 ( x + h /2 , y + K2 /2 , y01 + R2 /2)
K4 = h *( y + R3 )
R4 = h * F2 ( x +h , y + K3 , y01 + R3 )
y = y +( K1 +2* K2 +2* K3 + K4 ) /6
y01 = y01 +( R1 +2* R2 +2* R3 + R4 ) /6
x=x+h
end
for i =1:6
yi ( i ) = yi1 (1 , i ) +(( y1 - yi1 (6) ) / yi2 (6) ) * yi2 ( i )
end
y =1; x = a ; y01 = y1
for i =0:5
yir (1 , i +1) = y ;
K1 = h * y01 ;
225
49
R1 = h * f1 (x ,y , y01 ) ;
50
K2 = h *( y + R1 /2) ;
51
R2 = h * f1 ( x + h /2 , y + K1 /2 , y01 + R1 /2)
52
K3 = h *( y01 + R2 /2)
53
R3 = h * f1 ( x + h /2 , y + K2 /2 , y01 + R2 /2)
54
K4 = h *( y + R3 )
55
R4 = h * f1 ( x +h , y + K3 , y01 + R3 )
56
y = y +( K1 +2* K2 +2* K3 + K4 ) /6
57
y01 = y01 +( R1 +2* R2 +2* R3 + R4 ) /6
58
x=x+h
59 end
60 x = a ;
61 printf ( \n
62
63
64
65
66
67
68
69
70
71
72
73
74
75
n\ t x )
for i =1:6
printf ( \t% . 1 f \ t ,x )
x=x+h
end
printf ( \n\ t y )
for i =1:6
printf ( \t% . 4 f \ t , yi ( i ) )
end
printf ( \n
by RK )
for i =1:6
printf ( \t% . 4 f \ t , yir ( i ) )
end
printf ( \n
)
printf ( \n\n\ nNote : Computation e r r o r i n c a l c u l a t i o n
o f v a l u e s by RK method p e r f o r m e d i n book )
226
// Example 1 6 . 3
// M u l t i p l e S h o o t i n g Method
// Page no . 577
clc ; close ; clear ;
h =0.25; x =0; y1 =0;
deff ( y=f ( x ) , y=(4h 2 ) /(1+ x ) 2 )
deff ( y=f 1 ( x ) , y=2(1+(h 2 ) /(1+ x ) 2 ) )
for i =1:4
x=x+h
B(i)=f(x);
for j =1:4
if i ==4 & i == j
A (i , j ) = f1 ( x ) +1/4
A (i ,j -1) =2
elseif j == i then
A (i , j ) = f1 ( x )
A (i , j +1) =1
if j -1~=0 then
A (i ,j -1) =1
end
end
end
end
y = inv ( A ) * B
disp (B , B = ,A , A = )
printf ( \n\n\n x : )
for i =1:5
printf ( \t% . 2 f ,x )
x=x+h
end
x =0; printf ( \n y : \ t% . 2 f , y1 ) ;
for i =1:4
printf ( \t% . 4 f ,y ( i ) )
end
227
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
\n
)
for i =1:5
r ( i ) = - x ^2
if i >1 & i <5 then
printf ( %i \ t%g \ t%s \ t%g \ t%i \ t%g \n ,i -1 ,x , ?
,x ,q , r ( i ) )
else
printf ( %i \ t%g \ t%g \ t%g \ t%i \ t%g \n ,i -1 ,x , Y ( i
) ,x ,q , r ( i ) )
end
x=x+h
end
x =0;
printf (
\n
)
for i =1:3
x=x+h
for j =1:3
if i == j then
A (i , j ) =2+ h ^2* q
elseif i < j & abs (i - j ) ~=2
228
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
A (i , j ) = -1+ h * x /2
elseif i > j & abs (i - j ) ~=2
A (i , j ) = -1 - h * x /2
end
end
if i ==3 then
B ( i ) = - h ^2* r ( i +1) +( - h * x /2+1) * Y (1+2*( i -1) )
else
B ( i ) = - h ^2* r ( i +1) +( h * x /2+1) * Y (1+2*( i -1) )
end
B ( i ) =( -1) ^( i +1) * B ( i )
end
disp (B , B = ,A , A = )
y = inv ( A ) * B
for i =1:3
Y ( i +1) = y ( i )
end
x =0;
disp ( The S o l u t i o n i s : ,B , B = ,A , A = )
printf ( x : )
for i =1:5
printf ( \ t %. 2 f ,x )
x=x+h
end
x =0; printf ( \n y : ) ;
for i =1:5
printf ( \t% . 3 f ,Y ( i ) )
end
// Example 1 6 . 5
// Non L i n e a r Problem
229
3 // Page no . 584
4 clc ; close ; clear ;
5
6 deff ( y=f ( x ) , y=2/(1+x ) )
7 Y =[1 ,0.75 ,0.75 ,0.75 ,0.5]; h =0.25
8 A =[ -2 ,1 ,0;1 , -2 ,1;0 ,1 , -2]; A_1 = inv ( A )
9 disp ( A_1 , I n v e r s e o f A = ,A , A = )
10 printf ( \ nThe S o l u t i o n o f t h e s y s t e m
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
i s : \n\n
I t e r a t i o n \ t Y0\ t \ t Y1\ t \ t Y2\ t \ t Y3\ t \ t Y4\n
)
for i =0:6
printf ( \n
%i ,i )
for j =1:5
if j <4 & i ~=0 then
Y ( j +1) = y ( j )
end
printf ( \ t \t% . 4 f ,Y ( j ) )
end
x =0;
for j =1:3
x=x+h
if j ~=2 then
B ( j ) = h ^2* f ( x ) * Y ( j +1) ^2 - Y (1+2*( j -1) )
else
B ( j ) = h ^2* f ( x ) * Y ( j +1) ^2
end
end
y = A_1 * B
end
230
1 // Example 1 6 . 6
2 // C o l l o c a t i o n Method
3 // Page no . 589
4 clc ; close ; clear ;
5
6 h1 =0.000001; h =0.25; x =0;
7 Y (1) =0; Y (5) =0;
8 deff ( y=p ( x ) , y=1 )
9 deff ( y=q ( x ) , y=2/(1+x ) 2 )
10 deff ( y=f ( x ) , y =(2 x 4) /(1+ x ) 4 )
11 deff ( y= f i ( x , j ) , y=(1x ) x j )
12 deff ( y=f 1 ( x , y ) , y=(x+y ) / h1 )
// f u n c t i o n
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
for
differentiation
for i =1:4
x=x+h
for j =1:4
A (i , j ) = p ( x ) * f1 ( f1 ( fi (x , j ) , fi ( x + h1 , j ) ) , f1 ( fi (
x + h1 , j ) , fi ( x +2* h1 , j ) ) ) + f1 ( p ( x ) ,p ( x + h1 ) ) *
f1 ( fi (x , j ) , fi ( x + h1 , j ) ) + q ( x ) * fi (x , j )
end
end
x =0;
for i =1:4
x=x+h
B(i)=f(x)
end
disp (B , B = ,A , A = )
C = inv ( A ) * B
x =0;
for i =2:4
x=x+h;
for j =1:4
Y ( i ) = Y ( i ) + C ( j ) * fi (x , j )
end
end
disp (Y , S o l u t i o n M a t r i x Y = )
231
Chapter 18
Numerical Solutions of
Parabolic Partial Differential
Equations
// Example 1 8 . 4
// Forward D i f f e r e n c e Method
// Page no . 624
clc ; clear ; close ;
h =0.2; k =0.02;
r = k / h ^2;
printf ( \n j \ t t \ t | \ t i >\t )
for i =0:5
printf ( %i \ t ,i )
end
printf ( \n | \ t | \ t | \ t x >\t )
for i =0:5
printf ( %. 3 f \ t ,( i ) /5)
end
232
16
17
18
19
20
21
22
23
24
25
26
27
28
29
printf ( \n
)
for j =1:6
printf ( \n %i \t% . 3 f \ t | \ t \ t ,j -1 ,( j -1) /50)
for i =1:6
if i ==1 | i ==6 then
u (j , i ) =0;
elseif j ==1 then
u (j , i ) = sin ( %pi *( i -1) /5)
else
u (j , i ) =( u (j -1 ,i -1) + u (j -1 , i +1) ) /2
end
printf ( %. 3 f \ t ,u (j , i ) )
end
end
// Example 1 8 . 5
// Bender Schmidt Method
// Page no . 625
clc ; clear ; close ;
h =0.1; k =0.005;
r = k / h ^2;
printf ( \n j
|\ ti
for i =0:10
printf ( %i \ t
end
printf ( \n |
| \ tx
for i =0:10
printf ( %. 3 f \ t
>\t )
,i )
>\t )
,( i ) /10)
233
15 end
16 printf ( \n
17
18
19
20
21
22
23
24
25
26
27
28
29
)
for j =1:9
printf ( \n %i | \ t \ t ,j -1)
for i =1:11
if i ==1 | i ==11 then
u (j , i ) =0;
elseif j ==1 then
u (j , i ) = sin ( %pi *( i -1) /10)
else
u (j , i ) = u (j -1 , i ) /2+( u (j -1 ,i -1) + u (j -1 , i +1)
) /4
end
printf ( %. 3 f \ t ,u (j , i ) )
end
end
// Example 1 8 . 6
// Crank N i c o l s o n Method
// Page no . 631
clc ; clear ; close ;
h =1/2; k =1/8;
r = k / h ^2;
for i =1:2:3
for j =1:9
if i ==1 | j ==1 then
u (i , j ) =0;
end
if i ==3 then
234
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
u (i , j ) =( j -1) /8
end
end
end
a =[3 , -1 ,0; -1 ,3 , -1;0 , -1 ,3];
a = inv ( a ) ;
for j =2:9
u (2 , j ) =( u (1 ,j -1) +2* u (2 ,j -1) + u (3 ,j -1) + u (1 , j ) +
u (3 , j ) ) /6
end
u =u
printf ( \ n f o r h = 1/ 2\ n\n )
printf ( i \\ j > )
for i =1:3
printf ( \ t%i \ t ,i )
end
printf ( \n
n )
for i =1:9
printf ( \n %i ,i )
for j =1:3
printf ( \ t
%. 9 f ,u (i , j ) )
end
end
29
30
31
32
33
34
35
36
37
38
39 h =1/4; k =1/8;
40 r = k / h ^2;
41 for i =1:4:5
42
for j =1:9
43
if i ==1 | j ==1 then
44
u (i , j ) =0;
45
end
46
if i ==5 then
47
u (i , j ) =( j -1) /8
235
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
end
end
end
a =[3 , -1 ,0; -1 ,3 , -1;0 , -1 ,3];
a = inv ( a ) ;
for j =2:9
b =[ u (1 ,j -1) -u (2 ,j -1) + u (3 ,j -1) + u (1 , j ) ; u (2 ,j
-1) -u (3 ,j -1) + u (4 ,j -1) ; u (3 ,j -1) -u (4 ,j -1) + u
(5 ,j -1) + u (5 , j ) ]
x=a*b
u (2 , j ) = x (1) ; u (3 , j ) = x (2) ; u (4 , j ) = x (3) ;
end
u =u
printf ( \n\n\n\n\ n f o r h = 1/ 4\ n\n )
printf ( i \\ j > )
for i =1:5
printf ( \ t%i \ t ,i )
end
printf ( \n
n )
for i =1:9
printf ( \n %i ,i )
for j =1:5
printf ( \ t
%. 9 f ,u (i , j ) )
end
end
// Example 1 8 . 7
// Gauss S e i d e l Method
// Page no . 637
236
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
)
for k =1:7
printf ( \n %i \ t | \ t \ t ,k -1)
for i =1:6
if i ==1 | i ==6 then
u (k , i ) =0;
elseif k ==1 then
u (k , i ) = f (( i -1) /5)
else
u (k , i ) =( u (k -1 ,i -1) + u (k -1 , i +1) ) /2
end
printf ( %. 2 f \ t ,u (k , i ) )
end
end
// Example 1 8 . 8
//ADI Method
237
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Page no . 642
clc ; clear ; close ;
for i =1:4
for j =1:5
P (i , j ) =20
end
end
r =1; k =0;
for i =1:6
v1 ( i ) =20
u1 ( i ) =20
end
P1
=[25 ,30 ,35 ,50 ,60;35 ,0 ,0 ,0 ,70;45 ,0 ,0 ,0 ,80;60 ,70 ,80 ,100 ,90]
for i =1:4
printf ( \n )
for j =1:5
printf ( %i \ t ,P (i , j ) )
end
if i ==2 then
printf ( > )
end
printf ( \ t )
for j =1:5
printf ( %i \ t , P1 (i , j ) )
if i >1 & i <4 & j >1 & j <5 then
P1 (i , j ) = P (i , j )
end
end
end
P1v = P1 ; P1h = P1 ;
for i =1:6
for j =1:6
if i == j then
Av (i , j ) =1+2* r
elseif abs (i - j ) ==1 & i + j ~=5 & i + j ~=9
238
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Av (i , j ) = - r
end
end
end
for i =1:6
for j =1:6
if i == j then
Ah (i , j ) =1+2* r
elseif abs (i - j ) ==1 & i + j ~=7
Ah (i , j ) = - r
end
end
end
n =8
for l =1: n
k =0;
for j =0:2
for i =1:2
if i ==1 then
Bv ( i + j + k ) = r * P1h ( i +1 , j +1) +(1 -2* r ) * P1h ( i
+1 , j +2) + r * P1h ( i +1 , j +3) + r * P1h (i , j +1)
else
Bv ( i + j + k ) = r * P1h ( i +1 , j +1) +(1 -2* r ) * P1h ( i
+1 , j +2) + r * P1h ( i +1 , j +3) + r * P1h ( i +2 , j +1)
end
end
k = k +1;
end
k =0;
Bh =[ r *30+(1 -2* r ) * v1 (1) + r * v1 (4) + r *35; r *35+(1 -2* r ) * v1
(3) + r * v1 (5) ; r * v1 (1) +(1 -2* r ) * v1 (2) + r * v1 (3) + r *(70) ;
r * v1 (1) +(1 -2* r ) * v1 (2) + r *(70+45) ; r * v1 (3) +(1 -2* r ) *
v1 (4) + r *80; r * v1 (5) +(1 -2* r ) * v1 (6) + r *(100+80) ]
for i =1:6
v (i , l ) = v1 ( i )
end
for i =1:6
u (i , l ) = u1 ( i )
239
72
end
73
v1 = inv ( Av ) * Bv
74
u1 = inv ( Ah ) * Bh
75
k =1;
76
for i =2:3
77
for j =2:4
78
P1h (i , j ) = u1 ( i + j +k -4)
79
end
80
k = k +2
81
end
82
k =0;
83
for j =2:4
84
for i =2:3
85
P1v (i , j ) = v1 ( i + j +k -3)
86
end
87
k = k +1
88
end
89 end
90 printf ( \n\n\n\ n R e s u l t s f o r V e r t i c a l
Transverse in
Celsius :\n )
91 for i =1:7
92
printf ( \n )
93
if i ==1 then
94
printf ( I t r > )
95
for j =1: n
96
printf ( \ t %i ,j -1)
97
end
98
printf ( \n
)
else
printf ( v%i ,i -1)
for j =1: n
printf ( \t% . 2 f ,v (i -1 , j ) )
end
end
99
100
101
102
103
104
105 end
106 printf ( \n\n\n\ n R e s u l t s
Celsius :\n )
107 for i =1:7
108
printf ( \n )
109
if i ==1 then
110
printf ( I t r > )
111
for j =1: n
112
printf ( \ t %i ,j -1)
113
end
114
printf ( \n
)
115
else
116
printf ( u%i ,i -1)
117
for j =1: n
118
printf ( \t% . 2 f ,u (i -1 , j ) )
119
end
120
end
121 end
241
Chapter 19
Numerical Solutions of
Hyperbolic Partial Differential
Equations
// Example 1 9 . 3
// S i m p l e E x p l i c i t Method
// Page no . 658
clc ; clear ; close ;
c = -2; dt =0.07; dx =0.2;
r = abs ( c ) * dt / dx ;
printf ( \n x \ t i \ t | \ t j >\t )
for i =0:6
printf ( %i \ t ,i )
end
printf ( \n | \ t | \ t | \ t t >\t )
for i =0:6
printf ( %. 3 f \ t ,i * dt )
end
242
16
17
18
19
20
21
22
23
24
25
26
27
28
29
printf ( \n
)
for j =1:6
printf ( \n %. 1 f \ t%i \ t | \ t \ t ,(j -1) * dx ,j -1)
for i =1:7
if i ==1 then
u (j , i ) =0;
elseif j ==1 then
u (j , i ) =1
else
u (j , i ) =(1 - r ) * u (j ,i -1) + r * u (j -1 ,i -1)
end
printf ( %. 3 f \ t ,u (j , i ) )
end
end
// Example 1 9 . 4
// S i m p l e I m p l i c i t Method
// Page no . 659
clc ; clear ; close ;
c = -2; dt =0.07; dx =0.2;
r = abs ( c ) * dt / dx ;
printf ( \n x \ t i \ t | \ t j >\t )
for i =0:6
printf ( %i \ t ,i )
end
printf ( \n | \ t | \ t | \ t t >\t )
for i =0:6
printf ( %. 3 f \ t ,i * dt )
243
15 end
16 printf ( \n
17
18
19
20
21
22
23
24
25
26
27
28
29
)
for j =1:6
printf ( \n %. 1 f \ t%i \ t | \ t \ t ,(j -1) * dx ,j -1)
for i =1:7
if i ==1 then
u (j , i ) =0;
elseif j ==1 then
u (j , i ) =1
else
u (j , i ) =(1/(1+ r ) ) * u (j ,i -1) + r * u (j -1 , i ) /(1+
r)
end
printf ( %. 3 f \ t ,u (j , i ) )
end
end
// Example 1 9 . 5
// Lax W e n d r o f f Method
// Page no . 660
clc ; clear ; close ;
c = -2; dt =0.07; dx =0.2;
r = abs ( c ) * dt / dx ;
printf ( \n x \ t i \ t | \ t j >\t )
for i =0:6
printf ( %i \ t ,i )
end
printf ( \n | \ t | \ t | \ t t >\t )
244
13 for i =0:6
14
printf ( %. 3 f \ t ,i * dt )
15 end
16 i =1;
17 printf ( \n
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
)
for j =1:7
for i =1:6
if j ==1 then
u (i , j ) =0;
u ( i +1 , j ) =0;
elseif i ==1 then
u (i , j ) =1
else
u (i , j ) = r *( r -1) * u ( i +1 ,j -1) /2+(1 - r ^2) * u (i ,
j -1) + r *(1+ r ) * u (i -1 ,j -1) /2
end
end
end
for i =1:6
printf ( \n %. 1 f \ t%i \ t | \ t \ t ,(i -1) * dx ,i -1)
for j =1:7
printf ( %. 3 f \ t ,u (i , j ) )
end
end
245
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
)
for i =1:6
printf ( \n %. 1 f \ t%i \ t | \ t \ t ,(i -1) *h ,i -1)
for j =1:7
if j ==1 then
u (i , j ) =0;
elseif i ==1 then
u (i , j ) =1
else
u (i , j ) = u (i -1 ,j -1) +( u (i ,j -1) -u (i -1 , j ) ) / a
end
printf ( %. 3 f \ t ,u (i , j ) )
end
end
// Example 1 9 . 7
// L e a p f r o g Method
// Page no . 662
246
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 for j =1:7
19
for i =1:6
20
if j ==1 | j ==2 & i ~=1 then
21
u (i , j ) =0;
22
u ( i +1 , j ) =0;
23
elseif i ==1 then
24
u (i , j ) =1
25
else
26
u (i , j ) = u (i ,j -2) -r *( u ( i +1 ,j -1) -u (i -1 ,j -1)
)
27
end
28
end
29 end
30 for i =1:6
31
printf ( \n %. 1 f \ t%i \ t | \ t \ t ,(i -1) *h ,i -1)
32
for j =1:7
33
printf ( %. 3 f \ t ,u (i , j ) )
34
end
35 end
247
19
20
21
22
23
24
25
26
27
28
// Example 1 9 . 8
// V a r i a b l e C o e f f i c i e n t s
// Page no . 663
clc ; clear ; close ;
// s i m p l e e x p l i c i t method
printf ( \n\nBy S i m p l e E x p l i c i t Method : \ n\n )
dt =0.05; dx =0.2;
x =0;
printf ( \n i \ t x \ t r \ t | \ t j >\t )
for i =0:6
printf ( %i \ t ,i )
end
printf ( \n \ t \ t \ t | \ t t >\t )
for i =0:6
printf ( %. 3 f \ t ,i * dt )
end
printf ( \n
)
for j =1:6
r = sqrt (1+2* x ) * dt / dx ;
printf ( \n %i \t% . 3 f \t% . 3 f \ t | \ t \ t ,(j -1) ,x , r )
for i =1:7
if i ==1 then
u (j , i ) =0;
elseif j ==1 then
u (j , i ) =1
else
u (j , i ) =(1 - r ) * u (j ,i -1) + r * u (j -1 ,i -1)
248
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
end
printf ( %. 3 f \ t ,u (j , i ) )
end
x = x + dx
end
// s i m p l e i m p l i c i t method
printf ( \n\n\nBy S i m p l e I m p l i c i t Method : \ n )
c = -2; dt =0.05; dx =0.2; x =0
printf ( \n i \ t x \ t r \ t | \ t j >\t )
for i =0:6
printf ( %i \ t ,i )
end
printf ( \n \ t \ t \ t | \ t t >\t )
for i =0:6
printf ( %. 3 f \ t ,i * dt )
end
printf ( \n
)
for j =1:6
r = sqrt (1+2* x ) * dt / dx ;
printf ( \n %i \t% . 3 f \t% . 3 f \ t | \ t \ t ,(j -1) ,x , r )
for i =1:7
if i ==1 then
u (j , i ) =0;
elseif j ==1 then
u (j , i ) =1
else
u (j , i ) =(1/(1+ r ) ) * u (j ,i -1) + r * u (j -1 , i ) /(1+
r)
end
printf ( %. 3 f \ t ,u (j , i ) )
end
x = x + dx
end
249
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// w e n d r o f f method
printf ( \n\n\nBy W e n d r o f f Method : \ n )
k =0.05; h =0.2;
x =0.1;
printf ( \n i \ t x \ t c \ t a \ t | \ t j >\t )
for i =0:6
printf ( %i \ t ,i )
end
printf ( \n \ t \ t \ t \ t | \ t t >\t )
for i =0:6
printf ( %. 3 f \ t ,i * k )
end
printf ( \n
)
for i =1:6
c = sqrt (1+2* x ) ;
a =( h + k * c ) /( h - k * c )
printf ( \n %i \t% . 3 f \t% . 3 f \t% . 3 f \ t | \ t \ t ,(i -1) ,x h /2 ,c , a )
for j =1:7
if j ==1 then
u (i , j ) =0;
u ( i +1 , j ) =0;
elseif i ==1 then
u (i , j ) =1
else
u (i , j ) = u (i -1 ,j -1) +( u (i ,j -1) -u (i -1 , j ) ) / a
end
printf ( %. 3 f \ t ,u (i , j ) )
end
x=x+h
end
250
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// s i m p l e e x p l i c i t method
printf ( \n\nBy S i m p l e E x p l i c i t Method : \ n )
c = -2; dt =0.07; dx =0.2;
r = abs ( c ) * dt / dx ;
printf ( \n i \ t x \ t | \ t j >\t )
for i =0:6
printf ( %i \ t ,i )
end
printf ( \n | \ t | \ t | \ t t >\t )
for i =0:6
printf ( %. 3 f \ t ,i * dt )
end
printf ( \n
)
x =0;
for j =1:6
printf ( \n %i \t% . 1 f \ t | \ t \ t ,j -1 , x )
for i =1:7
if i ==1 then
u (j , i ) = exp ( - x ) ;
elseif j ==1 then
u (j , i ) =1
251
27
28
else
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
end
printf ( %. 3 f \ t ,u (j , i ) )
49
50
51
52
53
54
55
56
57
58
59
60
end
x = x + dx
end
// s i m p l e i m p l i c i t method
printf ( \n\n\nBy S i m p l e I m p l i c i t Method : \ n )
c = -2; dt =0.07; dx =0.2;
r = abs ( c ) * dt / dx ;
printf ( \n i \ t x \ t | \ t j >\t )
for i =0:6
printf ( %i \ t ,i )
end
printf ( \n | \ t | \ t | \ t t >\t )
for i =0:6
printf ( %. 3 f \ t ,i * dt )
end
printf ( \n
)
x =0;
for j =1:6
printf ( \n %i \t% . 1 f \ t | \ t \ t ,j -1 , x )
for i =1:7
if i ==1 then
u (j , i ) = exp ( - x ) ;
elseif j ==1 then
u (j , i ) =1
else
u (j , i ) =(1/(1+ r ) ) * u (j ,i -1) + r * u (j -1 , i ) /(1+
r ) + dt *2* x
end
printf ( %. 3 f \ t ,u (j , i ) )
252
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
end
x = x + dx
end
// w e n d r o f f method
printf ( \n\n\nBy W e n d r o f f Method : \ n )
c =2; k =0.07; h =0.2;
a =( h + k * c ) /( h - k * c )
printf ( \n x \ t i \ t | \ t j >\t )
for i =0:6
printf ( %i \ t ,i )
end
printf ( \n | \ t | \ t | \ t t >\t )
for i =0:6
printf ( %. 3 f \ t ,i * k )
end
printf ( \n
)
x =0;
for i =1:6
printf ( \n %. 1 f \ t%i \ t | \ t \ t ,x ,i -1)
for j =1:7
if j ==1 then
u (i , j ) = exp ( - x ) ;
elseif i ==1 then
u (i , j ) =1
else
u (i , j ) = u (i -1 ,j -1) +( u (i ,j -1) -u (i -1 , j ) ) / a
+(2* h * k ) *( x + h /2) /( a *( h + c * k ) )
end
printf ( %. 3 f \ t ,u (i , j ) )
end
x=x+h
end
253
Scilab code Exa 19.10 Non Linear 1st Order Hyperboolic Differential Equation
// Example 1 9 . 1 0
// Non L i n e a r 1 s t Order H y p e r b o o l i c D i f f e r e n t i a l
Equation
3 // Page no . 667
4 clc ; clear ; close ;
1
2
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
)
for j =1:7
for i =1:6
if j ==1 then
u (i , j ) = exp ( - x ) ;
u ( i +1 , j ) = exp ( -( x + h ) ) ;
elseif i ==1 then
u (i , j ) =1
else
254
27
28
29
30
31
32
33
34
35
36
37
38
39
u (i , j ) = u (i ,j -1) -k *( u ( i +1 ,j -1) ^2 - u (i -1 ,j
-1) ^2) /(4* h ) + k ^2*(( u ( i +1 ,j -1) + u (i ,j
-1) ) *( u ( i +1 ,j -1) ^2 - u (i ,j -1) ^2) -( u (i ,j
-1) + u (i -1 ,j -1) ) *( u (i ,j -1) ^2 - u (i -1 ,j
-1) ^2) ) /(8* h ^2)
end
x=x+h
end
end
x =0;
for i =1:6
printf ( \n %i \t% . 1 f \ t | \ t \ t ,i -1 , x )
for j =1:7
printf ( %. 3 f \ t ,u (i , j ) )
end
x=x+h
end
// Example 1 9 . 1 1
// F i n i t e D i f f e r e n c e Method
// Page no . 670
clc ; clear ; close ;
deff ( y=f ( x ) , y=s i n ( %pi x ) )
deff ( y=g ( x ) , y=0 )
a =1; b =1; c =1; n =5; m =10;
h=a/n;k=b/m;r=c*k/h;
r1 = r ^2; r2 = r1 /2; s1 =1 - r1 ; s2 =2*(1 - r2 )
printf ( \n i )
for i =1: n
printf ( \ t %i ,i )
end
255
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
printf ( \n
\
nfi )
for i =1: n
f1 ( i ) = f ( h *( i -1) )
printf ( \t% . 3 f , f1 ( i ) )
end
printf ( \ n g i )
for i =1: n
g1 ( i ) = g ( h *( i -1) )
printf ( \ t %g , g1 ( i ) )
end
printf ( \n\n\n i / j > )
for i =1: m
printf ( \ t %i ,i )
end
printf ( \n
)
for j =1: m
for i =1: n
if i ==1 | i == n then
u (i , j ) =0;
elseif j ==1
u (i , j ) = f1 ( i )
elseif j ==2
u (i , j ) = s1 * f1 ( i ) + k * g1 ( i ) + r2 *( f1 ( i +1) + f1 (i
-1) )
else
u (i , j ) = s2 * u (i ,j -1) + r1 * u (i -1 ,j -1) + u ( i +1 ,j
-1) -u (i ,j -2)
end
39
40
41
end
42 end
43 for i =1: n
44
printf ( \n %i \ t ,i )
45
for j =1: m
256
46
printf ( \t% . 3 f ,u (i , j ) )
47
end
48 end
// Example 1 9 . 1 2
// H y p e r b o l i c P a r t i a l D i f f e r e n t i a l E q u a t i o n s
// Page no . 673
clc ; clear ; close ;
deff ( y=f ( x ) , y=12x )
Ua (1) =0.25;
Ua (2) =0.75
A =[1 , -2;1 ,2];
x1 = inv ( A ) * Ua ;
printf ( Xb = %g
and
Tb = %g , x1 (1) , x1 (2) )
A =[2 , -1;2 ,1];
B =[ -7.5; -8.5];
x2 = inv ( A ) * B ;
printf ( \n\n Pb = %g
and
Qb = %g , x2 (1) , x2 (2) )
x1 (1) = x1 (1) - Ua (1)
du = x1 * x2
printf ( \n\n dU = %g , du )
Ub = f ( Ua (1) ) + du ;
printf ( \n\n M o d i f i e d Ub = %g , Ub )
// Example 1 9 . 1 3
257
2 // H y p e r b o l i c D i f f e r e n t i a l E q u a t i o n s i n 2D o r 3D
3 // Page no . 675
4 clc ; clear ; close ;
5
6 deff ( y=f ( x , y ) , y=x (2 x ) y (2 y ) )
7 c2 =3; k =0.4; h =0.4; c2 =3; s2 =0.5
8 for l =0:11
9
if l ==0 then
10
printf ( \n t = %i \n\n i \ t x \ t | \ t j >\t ,l )
11
for i =0:5
12
printf ( %i \ t ,i )
13
end
14
printf ( \n | \ t
| \ t | \ t y >\t )
15
for i =0:5
16
printf ( %. 3 f \ t ,i * k )
17
end
18
x =0;
19
printf ( \n
)
for i =1:6
y =0;
printf ( \n %i \t% . 3 f \ t | \ t \ t ,i -1 , x )
for j =1:6
if i ==1 | i ==6 then
u (i , j ) =0;
elseif j ==1 | j ==6 then
u (i , j ) =0
else
u (i , j ) = f (x , y )
end
printf ( %. 3 f \ t ,u (i , j ) )
y=y+k;
end
x=x+h
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 end
36 u2 = u ;
37 else
258
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
)
for i =1:6
y =0;
printf ( \n %i \t% . 3 f \ t | \ t \ t ,i -1 , x )
for j =1:6
if i ==1 | i ==6 then
u (i , j ) =0;
elseif j ==1 | j ==6 then
u (i , j ) =0
elseif l ==1
u (i , j ) = s2 *( u1 ( i +1 , j ) + u1 (i -1 , j ) + u1 (i ,
j +1) + u1 (i ,j -1) -4* u1 (i , j ) ) +2* u1 (i ,
j)
else
u (i , j ) = s2 *( u1 ( i +1 , j ) + u1 (i -1 , j ) + u1 (i ,
j +1) + u1 (i ,j -1) -4* u1 (i , j ) ) +2* u1 (i ,
j ) - u2 (i , j )
end
printf ( %. 4 f \ t ,u (i , j ) )
y=y+k;
end
x=x+h
end
60
61
62
63
64
65
66 end
67 if l >1 then
68
u2 = u1
69 end
259
70 u1 = u ;
71 end
260
Chapter 20
Numerical Solutions of
Elliptical Partial Differential
Equations
// Example 2 0 . 1
// D i r e c t Method
// Page no . 682
clc ; clear ; close ;
h =1/3;
A =[ -4 ,1 ,1 ,0;1 , -4 ,0 ,1;1 ,0 , -4 ,1;0 ,1 ,1 , -4]
x =0;
for i =1:4
x=x+h
if i ==4 then
B (i ,1) =0
else
B (i ,1) = -1* sin ( x * %pi ) ^2
end
end
261
16 disp (A , A = )
17 disp (B , B = )
18 U = inv ( A ) * B
19 disp (U , U = )
// Example 2 0 . 2
// F i v e P o i n t Formula
// Page no . 683
clc ; clear ; close ;
A =[ -4 ,1 ,1 ,0;1 ,0 , -4 ,1;1 , -4 ,0 ,1;0 ,1 ,1 , -4];
B =[ -25; -150; -25; -150];
u1 = inv ( A ) * B ;
j =0; k =1
for i =1:4
j = j +1;
printf ( \ nu%i%i = %g\n ,k ,j , u1 ( i ) )
if i ==2 then
j =0; k =2
end
end
printf ( \n\n U = \n )
for i =1:4
printf ( \n )
for j =1:4
if j ==1 then
u (i , j ) =0
elseif j ==4
u (i , j ) =100
elseif ( i ==1 | i ==4) & j ==2
u (i , j ) =25
262
27
28
29
30
31
32
33
end
34 end
7
8
9
10
11
12
13
14
15
16
17
18
19
20
n )
for i =1:4
for j =1:4
if j ==1 then
u (i , j ) =0
elseif j ==4
u (i , j ) =100
elseif ( i ==1 | i ==4) & j ==2
u (i , j ) =25
elseif i ==1 | i ==4
u (i , j ) = u (i ,j -1) *2
else
u (i , j ) =0
end
end
263
21 end
22 for k =0:17
23
printf (
24
25
26
27
28
29
30
31
32
33
34 end
35 disp (u , U = )
6
7
8
9
10
11
n )
for i =1:4
for j =1:4
for k =3: -1:1
if k ==3 then
u (i ,j , k ) =100
elseif ( i ==1 | i ==4 | j ==1 | j ==4) & k
264
12
13
14
15
16
17
18
19
20
end
21
end
22 end
23 k =2
24 for l =0:14
25
printf (
26
27
28
29
30
31
32
33
34
35
36 end
==2
u (i ,j , k ) =300
elseif k ==2
u (i ,j , k ) =0
elseif ( i ==1 | i ==4 | j ==1 | j ==4) & k
==1
u (i ,j , k ) =500
else
u (i ,j , k ) =700
end
// Example 2 0 . 5
265
2 // Nine P o i n t Formula
3 // Page no . 688
4 clc ; clear ; close ;
5
6 printf ( I t r \ t \ t U11\ t \ t U12\ t \ t U21\ t \ t U22\n
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
n )
for i =1:4
for j =1:4
if j ==1 then
u (i , j ) =0
elseif j ==4
u (i , j ) =100
elseif ( i ==1 | i ==4) & j ==2
u (i , j ) =25
elseif i ==1 | i ==4
u (i , j ) = u (i ,j -1) *2
else
u (i , j ) =0
end
end
end
for k =0:17
printf ( %i \ t \t% . 3 f \ t \t% . 3 f \ t \t% . 3 f \ t \t% . 3 f \n ,
k , u (3 ,2) ,u (2 ,2) ,u (3 ,3) ,u (2 ,3) )
for i =3: -1:2
for j =2:3
u1 (i , j ) =( u ( i +1 ,j -1) + u (i -1 ,j -1) + u ( i +1 , j
+1) + u (i -1 , j +1) +4*( u (i , j +1) + u (i ,j -1) + u
(i -1 , j ) + u ( i +1 , j ) ) ) /20
end
end
for i =3: -1:2
for j =2:3
u (i , j ) = u1 (i , j )
end
end
end
266
35
// Example 2 0 . 6
// F i v e P o i n t Formula
// Page no . 689
clc ; clear ; close ;
h =0.25; k =0.25; y =1; x =0;
deff ( x=f ( y ) , x=y 3 )
for i =1:5
x =0;
printf ( \n%g\ t | ,y )
for j =1:5
if ( i ==1 | i ==5)
u (i , j ) = f ( x )
elseif j ==5
u (i , j ) = f ( x )
else
u (i , j ) =0
end
x=x+k;
printf ( %f\ t ,u (i , j ) )
end
y =y - h
end
printf ( \n\ t
n )
26 x =0;
27
for j =1:5
267
28
29
30
31
printf ( \ t
x=x+k
%g\ t ,x )
end
printf ( \n\n\n I t r \ t U11\ t U12\ t U13\ t U21\ t U22\ t
U23\ t U31\ t U32\ t U33\n
n )
32
33 for l =0:20
34
y =0;
35
printf (
36
37
38
39
40
41
42
43
44
45
46
47 end
268
5
6 dr =3; r0 =4; dth = %pi /4;
7 deff ( y=f ( u1 , u2 , u3 , u4 ) , y=(u1+u3+( d r ( u3u1 ) ) / ( 2 r 0
8
9
10
11
)
for l =0:15
printf ( \n %i ,l )
for i =1:8
if i ==1 then
u1 ( i ) = f (100 , U (8) ,40 , U ( i +1) )
elseif i ==8
u1 ( i ) = f (100 , U (i -1) ,40 , U (1) )
else
u1 ( i ) = f (100 , U (i -1) ,40 , U ( i +1) )
end
12
13
14
15
16
17
18
19
20
21
22
23
end
24
for i =1:8
25
U ( i ) = u1 ( i )
26
printf ( \t% . 3 f ,U ( i ) )
27
end
28 end
// Example 2 0 . 8
269
2 // S p h e r i c a l C o o r d i n a t e System
3 // Page no . 697
4 clc ; clear ; close ;
5 deff ( y=c o t ( x ) , y=1/ t a n ( x ) )
6 dr =5; r0 =50; dth = %pi /4; dfi = %pi /4; N = -10; Z =60; Nb =0; Zt
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
=70;
deff ( y=f ( u1 , u2 , u3 , u4 , u5 , u6 , t h 0 ) , y =(( u1+u3 ) / d r 2+(
u3u1 ) / ( r 0 d r ) +(u2+u4 ) / ( r 0 dth ) 2+( u2 c o t ( t h 0 ) / (
r 0 2 dth ) +(u5+u6 ) / ( r 0 s i n ( t h 0 ) d f i ) 2 ) ) / ( 2 / d r
2+2/( r 0 dth ) 2+ c o t ( t h 0 ) / ( r 0 2 dth ) +2/( r 0 s i n ( t h 0
) d f i ) 2) )
// l a p l a c e d i s t r i b u t i o n i n
spherical coordinate
T1 =40; T2 =20; H1 =35; H2 =10; B1 =5; B2 =0; t1 =240; t2 =180; b1
=100; b2 =80; h1 =210; h2 =150
printf ( \n
n )
s =[ T ,H ,B , t , h , b ];
for i =1:8
if i <4 | i >6 then
T (1 , i ) = T1 ;
H (1 , i ) = H1 ;
B (1 , i ) = B1 ;
b (1 , i ) = b1 ;
t (1 , i ) = t1 ;
h (1 , i ) = h1 ;
else
T (1 , i ) = T2 ;
H (1 , i ) = H2 ;
b (1 , i ) = b2 ;
B (1 , i ) = B2 ;
t (1 , i ) = t2 ;
h (1 , i ) = h2 ;
end
end
h (1) =0; h (2) =0;
Al =[ T ; H ; B ; t ; h ; b ]
for i =1:6
270
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
if i ==1 then
printf ( T e m p e r a t u r e D i s t r i b u t i o n i n Outer
S p h e r e \n )
printf (
n )
end
if i ==4 then
printf ( \ n T e m p e r a t u r e D i s t r i b u t i o n i n I n n e r
S p h e r e \n )
printf (
n )
end
printf ( \ n P o i n t : )
for j =1:8
printf ( \ t%s%i ,s ( i ) ,j )
end
printf ( \ n T e m p e r a t u r e : )
for j =1:8
if ( j ==1 | j ==2) & i ==5 then
printf ( \ t%s , ? )
else
printf ( \ t%i , Al (i , j ) )
end
end
printf ( \n
52 end
53 th0 =10^ -30
54 Uh1 = f (1000 , Al (5 ,8) , Al (2 ,1) , Al (5 ,2) , Al (6 ,1) , Al (4 ,1) ,
th0 )
55 disp ( Uh1 , Uh1 = )
56 th0 = %pi /4;
57 Uh2 = f (1000 , Uh1 , Al (2 ,2) , Al (5 ,3) , Al (6 ,2) , Al (4 ,2) , th0 )
58 disp ( Uh2 , Uh2 = )
271
Chapter 21
Advances in Numerical
Methods Using Parallel
Computing Paradigm
\ t x 5 \n\ t \ t \ t \ t \ t y 0 \ t \ t y 1 \ t \ t y 2 \ t \ t y 3 \ t \ t y 4 \ t \ t y 5
)
9 printf ( \n
n )
10 while ( abs (a - b ) >= e )
11
272
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 end
h =( b - a ) /5;
y (1) = f ( a ) ;
x (1) = a ;
printf ( %i \ t%g \ t%g \ t%f \ t%f ,i ,a ,b ,h , x (1) )
for j =2:6
x ( j ) = x (j -1) + h ;
y(j)=f(x(j));
if ( y (j -1) * y ( j ) <0)
a = x (j -1) ;
b=x(j);
end
printf ( \ t%f ,x ( j ) )
end
printf ( \n\ t \ t \ t \ t \ t )
for j =1:6
printf ( %f\ t ,y ( j ) )
end
printf ( \n )
i = i +1;
// Example 2 1 . 2
// L a g r a n g e I n t e r p o l a t i o n i n P a r a l l e l Computing
// Page no . 723
clc ; close ; clear ;
xi =[ -1 ,0 ,2 ,5];
yi =[9 ,5 ,3 ,15];
s =[ x=1 , n=4 , Data : , ( 1 , 9 ) , ( 0 , 5 ) , ( 2 , 3 ) ,
273
(5 ,1 5) ]
for i =1:4
printf ( \ t P r o c e s s o r \ t )
end
printf ( \n )
for i =1:4
printf ( \ t
N%i\ t \ t ,i )
end
printf ( \n )
for i =1:7
for j =1:4
printf (
%s\ t \ t ,s ( i ) )
end
printf ( \n )
end
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 x =1; T =0;
25 for k =0:3
26
p = yi ( k +1)
27
for j =0:3
28
if ( j ~= k )
29
p = p *(( x - xi ( j +1) ) /( xi ( k +1) - xi ( j +1) ) )
30
end
31
end
32
T=T+p;
33
printf ( \nT ( %i ) = %g ,k +1 , p )
34 end
35 printf ( \n\nT = %g ,T )
Scilab code Exa 21.3 Trapezoidal Rule and Simpsons Rule in Parallel Computing
1
// Example 2 1 . 3
274
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
S = S +4* y ( i )
40
else
41
S = S +2* y ( i )
42
end
43 end
44 S = S * h /3
45 printf ( \n\ n S i m p s o n s 1/3 r d Rule Sum = %g ,S )
// Example 2 1 . 4
// P a r a l l e l GaussS e i d e l Method
// Page no . 730
clc ; close ; clear ;
A =[3 ,2;6 ,2];
B =[2;3];
x (1) =1/4;
x (2) =1/5;
e =0.002;
old (1) = x (1) ;
old (2) = x (2) ;
new (1) = old (1) ;
new (2) = old (2) ;
printf ( \ t \ t P r o c e s s 1\ t \ t P r o c e s s 2\ n I t r \ t \ t o l d \
tnew1 \ t \ t o l d 2 \ tnew2 \n\n )
printf ( %i \ t \ t%g \ t%g \ t \ t%g \ t%g \n ,0 , old (1) , new (1) ,
old (2) , new (2) )
for i =1:4
printf ( %i ,i )
for j =1:2
k =0;
for l =1: j -1
276
22
23
24
25
26
27
28
29
30
31
32
33
34 end
k =k -( A (j , l ) * old ( l ) ) ;
end
m =0;
for l = j +1:2
m =m -( A (j , l ) * old ( l ) ) ;
end
new ( j ) =( B ( j ) + k + m ) / A (j , j )
printf ( \ t \t% . 5 g \t% . 5 g , old ( j ) , new ( j ) )
end
printf ( \n )
old (1) = new (1)
old (2) = new (2)
// Example 2 1 . 5
// P o i s s o n s P a r t i a l D i f f e r e n t i a l E q u a t i o n
// Page no . 733
clc ; clear ; close ;
s =[ s t , nd , r d ]
for i =4:20
s(i)= th
end
h =0.25; deff ( y=f ( x ) , y=x 3 ) ; y =1; x =0;
for i =1:6
if i ~=6 then
printf ( %g\ t | ,y )
y =y - h ;
x =0;
277
18
19
20
21
22
23
24
25
26
27
28
for j =1:5
if i ==1 | i ==5 | j ==5 then
P (i , j ) = f ( x )
else
P (i , j ) =0
end
printf ( %f\ t ,P (i , j ) )
x=x+h;
end
else
printf (
n\ t )
x =0;
for j =1:5
printf (
%g\ t \ t ,x )
x=x+h
end
29
30
31
32
33
34
end
35
printf ( \n )
36 end
37
38 printf ( \n\n\n )
39
40 for l =0:17
41
y =1;
42
if l ~=0 then
43
printf ( A f t e r t h e %i%s I t e r a t i o n
44
45
46
47
48
49
50
51
: \n
\
n %i ,l , s ( l ) ,l )
for i =1:6
if i ~=6 then
printf ( \ t%g ,y )
y =y - h
for j =1:5
printf ( \t% . 3 f ,P (i , j ) )
end
else
278
52
53
54
55
56
57
58
59
60
61
x =0;
printf ( \ t )
for j =1:5
printf ( \ t%g ,x )
x=x+h
end
end
printf ( \n )
end
printf (
\
n )
62 end
63 y =0;
64
for i =4: -1:2
65
y=y+h
66
for j =2:4
67
P1 (i , j ) =( P (i , j +1) + P (i ,j -1) + P (i -1 , j ) + P ( i
+1 , j ) -h ^2* y ) /4
68
end
69
end
70
for i =4: -1:2
71
for j =2:4
72
P (i , j ) = P1 (i , j )
73
end
74
end
75 end
279
Chapter 22
Numerical Methods Using
Neural Networks
// Example 2 2 . 1
//MLP A l g o r i t h m
// Page no . 748
clc ; clear ; close ;
deff ( y=f ( x ) , y=1/(1+ exp (x ) ) )
Wih =[0.1 , -0.3;0.3 ,0.4];
Who =[0.4;0.5]
i =[0.2 ,0.6];
t =0.7;
a =10;
for k =1:3
printf ( \n\n\ n A f t e r I t e r a t i o n %i : \ n\n ,k )
disp ( Wih , Wih = )
disp ( Who , Who = )
a1 = i * Wih ;
disp ( a1 , a = )
h =[ f ( a1 (1) ) ,f ( a1 (2) ) ]
280
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
disp (h , h = )
b1 = h * Who
disp ( b1 , b1 = )
o = f ( b1 )
disp (o , o = )
d = o *(1 - o ) *( t - o )
disp (d , d = )
for j =1:2
e (1 , j ) = h ( j ) *(1 - h ( j ) ) * d * Who ( j )
end
disp (e , e = )
dWho = a *h * d ;
disp ( dWho , dWho = )
Who = Who + dWho ;
dWih = a *i * e ;
disp ( dWih , dWih = )
Wih = Wih + dWih ;
end
Output \ t \ t Output \n
\
n )
8 in
9 n
( in ( i ) ) ,n ( i ) ,n ( i ) -f ( in ( i ) ) )
12 end
tN42 \n
n )
8 N01
=[0 ,1 ,0.5 ,0.75 ,0.875 ,0.938 ,0.969 ,0.984 ,0.992 ,0.996 ,0.998 ,0.999 ,1 ,1
9
10
11
12
13
14
15
16
17
N02 (1) =2
for i =2:13
N02 ( i ) =1;
end
for i =1:13
net31 ( i ) = f ( N01 ( i +1) ) * f ( N01 ( i ) )
if net31 ( i ) >0 then
O31 ( i ) =1;
else
282
18
19
20
21
22
23
24
25
O31 ( i ) =0;
end
N41 ( i ) =(1 - O31 ( i ) ) *( N01 ( i ) ) + O31 ( i ) * N01 ( i +1)
N42 ( i ) =(1 - O31 ( i ) ) * N01 ( i +1) + O31 ( i ) * N02 ( i )
if i ==2 then
printf ( %. 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \
t% . 3 f \t% . 3 f \t% . 3 f \n ,0 , N02 ( i ) ,f ( N01 ( i ) ) ,
N01 ( i +1) ,f ( N01 ( i +1) ) , net31 ( i ) , O31 ( i ) , N41 (
i ) , N42 ( i ) )
else
printf ( %. 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f
\t% . 3 f \t% . 3 f \n , N01 ( i ) , N02 ( i ) ,f ( N01 ( i ) ) , N01 ( i
+1) ,f ( N01 ( i +1) ) , net31 ( i ) , O31 ( i ) , N41 ( i ) , N42 ( i )
)
26 end
27
28 end
29 printf ( \n\ n T h e r e f o r e t h e
s o l u t i o n i s %. 3 f , N42 (13) )
283
13
end
14
T (i , j ) = k ;
15
end
16 end
17 disp (T , T = )
Output \ t \ t Output \n
\
n )
8 in
=[6.4828 ,6.4883 ,6.5164 ,9.9786 ,9.9816 ,9.9944 ,7.7926 ,7.7718 ,7.7180 , 10 for i =1:18
11
printf ( %. 4 f \ t \t% . 4 f \ t \t% . 4 f \ t \t% . 4 f \n , in ( i ) ,f
( in ( i ) ) ,n ( i ) ,f ( in ( i ) ) -n ( i ) )
12 end
284
S o l u t i o n \ t E r r o r \n
x \ t \ twa ( x ) \ t \ t \ twt ( x ) \ t \ t \ tdw
( x ) \n
n )
8 x
\ t \t% . 4 f \ t \ t \t% . 4 f \ t \n ,x (
i ) ,(1 - f ( x ( i ) ) ) ,e ( i ) ,-e ( i ) +(1 - f ( x ( i ) ) ) )
12 end
13 printf ( \n\n\ n E x p e r i m e n t a l
r e s u l t v a r y i n g from
calculated result )
285