Scilab Textbook Companion for Numerical Methods: Principles, Analysis, And Algorithms by S. Pal1 Created by Saurav Suman B.Tech Others NIT Jamshedpur College Teacher NA Cross-Checked by
May 23, 2016
1
Funded by a grant from the National Mission on Education through ICT, http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab codes written in it can be downloaded from the ”Textbook Companion Project” section at the website http://scilab.in
Book Description Title: Numerical Numerical Methods: Principles, Principles, Analysis, Analysis, And Algorithms Algorithms Author: S. Pal Publisher: Oxford University Press Edition: 1 Year: 2009 ISBN: 9780195693751
1
Scilab numbering policy used in this document and the relation to the above book. Exa Example (Solved example) Eqn Equation (Particular equation of the above book) AP Appendix to Example(Scilab Code that is an Appednix to a particular
Example of the above book) For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means a scilab code whose theory is explained in Section 2.3 of the book.
2
Contents Listt of Scilab Codes Lis
4
1 Bac Backgr kgroun ound d to Numeri Numerical cal Method Methodss
5
2 Sco Scope pe of Numerical Numerical and Mathe Mathemat matica icall Methods Methods
36
3 Erro Errors rs and and Their Their Prop Propaga agatio tion n
38
4 Progra Programming mming Tools and Techn Techniques iques
42
5 Solut Solutions ions of Algebrai Algebraic c and Transcen Transcendent dental al Equations Equations
45
6 Num Numeri erical cal Methods Methods of Linear Linear Equation Equationss Direct Direct Methods Methods
86
7 Num Numeri erical cal Soluti Solutions ons for Matrix Matrix Inversi Inversion on
97
8 Numeri Numerical cal Soluti Solutions ons of Linear Linear Syst Systems ems of Equations Equations Iter Iteraative Methods 107 9 Lin Linear ear Least Least Squa Squares res Prob Problem lem
124
10 Numerical Solutions of System of Non Linear Equations
135
11 Eigenvalues and Eigenvectors
143
12 Interpolation and Extrapolation
162
13 Numerical Differentiation
174
14 Numerical Integration
183
3
15 Numerical Solutions of Ordinary Differential Equations Initial Value Problem 195 16 Numerical Solutions of Ordinary Differential Equations Boundary Value Problems 216 18 Numerical Solutions of Parabolic Partial Differential Equations 225 19 Numerical Solutions of Hyperbolic Partial Differential Equations 235 20 Numerical Solutions of Elliptical Partial Differential Equations 254 21 Advances in Numerical Methods Using Parallel Computing Paradigm 265 22 Numerical Methods Using Neural Networks
4
273
List of Scilab Codes Exa 1.1 Exa 1.2 Exa 1.3 Exa 1.4 Exa 1.5 Exa 1.6 Exa 1.7 Exa 1.8 Exa 1.9 Exa 1.10 Exa 1.13 Exa 1.14 Exa 1.15 Exa 1.16 Exa 1.17 Exa 1.18 Exa 1.19 Exa 1.20 Exa 1.23 Exa 1.24 Exa 1.25 Exa 1.26 Exa 1.29 Exa 1.30 Exa 1.31 Exa 1.32 Exa 1.33 Exa 1.34
Conversion to Decimal System . . . . . . . . Conversion Using Shortcut Method . . . . . . Conversion to Base B from Decimal System . Conversion to Binary System . . . . . . . . . Conversion to Binary System . . . . . . . . . Conversion to Decimal Number . . . . . . . . Conversion to Decimal Number . . . . . . . . Conversion to Base B from Binary System . . Conversion to Binary System . . . . . . . . . Conversion to Binary System and to Base N . 1s compliment and 2s compliment . . . . . . 1s compliment . . . . . . . . . . . . . . . . . Addition and Subtraction . . . . . . . . . . . Addition . . . . . . . . . . . . . . . . . . . . Addition . . . . . . . . . . . . . . . . . . . . Addition . . . . . . . . . . . . . . . . . . . . Addition . . . . . . . . . . . . . . . . . . . . Subtraction . . . . . . . . . . . . . . . . . . . Multiplication . . . . . . . . . . . . . . . . . Multiplication . . . . . . . . . . . . . . . . . Division . . . . . . . . . . . . . . . . . . . . . Multiplication . . . . . . . . . . . . . . . . . Normalized Floating Point Representation . . Add . . . . . . . . . . . . . . . . . . . . . . . Add . . . . . . . . . . . . . . . . . . . . . . . Add . . . . . . . . . . . . . . . . . . . . . . . Add . . . . . . . . . . . . . . . . . . . . . . . Subtraction . . . . . . . . . . . . . . . . . . . 5
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
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 1.35 Exa 1.36 Exa 2.4 Exa 2.6 Exa 3.1 Exa 3.2 Exa 3.3 Exa 3.4 Exa 3.5 Exa 4.1 Exa 4.2 Exa 5.1 Exa 5.2 Exa 5.3 Exa 5.4 Exa 5.5 Exa 5.6 Exa 5.7 Exa 5.8 Exa 5.9 Exa 5.10 Exa 5.11 Exa 5.12 Exa 5.13 Exa 5.14 Exa 5.15 Exa 5.19 Exa 5.20 Exa 5.21 Exa 5.22 Exa 5.23 Exa 5.24 Exa 5.25 Exa 5.26 Exa 5.27 Exa 5.28 Exa 5.29 Exa 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 5.32 Exa 5.33 Exa 5.34 Exa 5.35 Exa 5.36 Exa 5.37 Exa 5.38 Exa 5.39 Exa 5.40 Exa 6.1 Exa 6.2 Exa 6.3 Exa 6.4 Exa 6.5 Exa 6.6 Exa 6.7 Exa 6.8 Exa 6.9 Exa 6.10 Exa 7.1 Exa 7.2 Exa 7.3 Exa 7.4 Exa 7.5 Exa 7.6 Exa 8.1 Exa 8.2 Exa 8.3 Exa 8.4 Exa 8.5 Exa 8.6 Exa 8.7 Exa 8.8 Exa 9.1 Exa 9.2 Exa 9.3 Exa 9.4
Regula Falsi Newton Raphson and Mullers Method . . Newton Raphson and Mullers Method . . . . . . . . . QD Method . . . . . . . . . . . . . . . . . . . . . . . . Newton Raphson Method . . . . . . . . . . . . . . . . Secant Method . . . . . . . . . . . . . . . . . . . . . . Newton Raphson Method . . . . . . . . . . . . . . . . Newton Raphson Method . . . . . . . . . . . . . . . . Newton Raphson Method . . . . . . . . . . . . . . . . Newton Raphson Method . . . . . . . . . . . . . . . . Gaussian Elimination Method . . . . . . . . . . . . . . Gaussian Elimination Method for TriDiagonal System Gauss Jordan Method . . . . . . . . . . . . . . . . . . Gaussian Elimination Method without Pivoting . . . . Dolittle Factorization Method . . . . . . . . . . . . . . Trangularization Method . . . . . . . . . . . . . . . . Wilkinson Method . . . . . . . . . . . . . . . . . . . . Choleskys Factorization . . . . . . . . . . . . . . . . . Complex System of Linear Equation . . . . . . . . . . Solving Matrices . . . . . . . . . . . . . . . . . . . . . Gauss Jordan Two Array Method . . . . . . . . . . . Inverse in Place without Pivoting . . . . . . . . . . . . Inverse in Place with Pivoting . . . . . . . . . . . . . . Inverse of Triangular Matrices . . . . . . . . . . . . . . Inverse of Complex Matrices . . . . . . . . . . . . . . Iterative Procedure . . . . . . . . . . . . . . . . . . . . Jacobi Method . . . . . . . . . . . . . . . . . . . . . . Gauss Seidel Method . . . . . . . . . . . . . . . . . . . SOR Method . . . . . . . . . . . . . . . . . . . . . . . Gauss Seidel Point Iterative Method . . . . . . . . . . Gauss Seidel Point Iterative Method . . . . . . . . . . Block Jacobi Method . . . . . . . . . . . . . . . . . . Block Gauss Seidel Method . . . . . . . . . . . . . . . Block SOR Method . . . . . . . . . . . . . . . . . . . Moore Penrose Generalized Inverse . . . . . . . . . . . Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . Gram Schmidt Orthogonalization or Orthonormalization Process . . . . . . . . . . . . . . . . . . . . . . . QR Decomposition . . . . . . . . . . . . . . . . . . . . 7
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 9.5 Exa 9.6 Exa 9.7 Exa 9.8 Exa 10.1 Exa 10.2 Exa 10.3 Exa 10.4 Exa 10.5 Exa 10.6 Exa 10.7 Exa 10.8 Exa 11.1 Exa 11.2 Exa 11.3 Exa 11.4 Exa 11.5 Exa 11.6 Exa 11.7 Exa 11.8 Exa 11.9 Exa 11.10 Exa 11.11 Exa 12.1 Exa 12.2 Exa 12.3 Exa 12.4 Exa 12.5 Exa 12.6 Exa 12.7 Exa 12.8 Exa 13.1 Exa 13.2 Exa 13.3 Exa 13.4 Exa 13.5 Exa 13.6 Exa 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 13.8 Exa 14.2 Exa 14.3 Exa 14.5 Exa 14.7 Exa 14.8 Exa 14.9 Exa 14.10 Exa 14.11 Exa 14.14 Exa 14.15 Exa 14.16 Exa 14.18 Exa 14.19 Exa 15.1 Exa 15.6 Exa 15.7 Exa 15.8 Exa 15.9 Exa 15.10 Exa 15.11 Exa 15.12 Exa 15.13 Exa 15.14 Exa 15.15 Exa 15.16 Exa 15.17 Exa 15.18 Exa 15.19 Exa 15.20 Exa 15.21 Exa 15.22 Exa 15.23 Exa 15.24 Exa 15.25 Exa 15.26 Exa 15.27 Exa 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 16.1 Exa 16.2 Exa 16.3 Exa 16.4 Exa 16.5 Exa 16.6 Exa 18.4 Exa 18.5 Exa 18.6 Exa 18.7 Exa 18.8 Exa 19.3 Exa 19.4 Exa 19.5 Exa 19.6 Exa 19.7 Exa 19.8 Exa 19.9 Exa 19.10 Exa 19.11 Exa 19.12 Exa 19.13 Exa 20.1 Exa 20.2 Exa 20.3 Exa 20.4 Exa 20.5 Exa 20.6 Exa 20.7 Exa 20.8 Exa 21.1 Exa 21.2 Exa 21.3 Exa 21.4 Exa 21.5 Exa 22.1
Outline of Linear Shooting Method . . . . . . . . . . . Linear Shooting Method . . . . . . . . . . . . . . . . . Multiple Shooting Method . . . . . . . . . . . . . . . . Finite Difference Method . . . . . . . . . . . . . . . . Non Linear Problem . . . . . . . . . . . . . . . . . . . Collocation Method . . . . . . . . . . . . . . . . . . . Forward Difference Method . . . . . . . . . . . . . . . Bender Schmidt Method . . . . . . . . . . . . . . . . . Crank Nicolson Method . . . . . . . . . . . . . . . . . Gauss Seidel Method . . . . . . . . . . . . . . . . . . . ADI Method . . . . . . . . . . . . . . . . . . . . . . . Simple Explicit Method . . . . . . . . . . . . . . . . . Simple Implicit Method . . . . . . . . . . . . . . . . . Lax Wendroff Method . . . . . . . . . . . . . . . . . . Wendroff Method . . . . . . . . . . . . . . . . . . . . Leapfrog Method . . . . . . . . . . . . . . . . . . . . . Variable Coefficients . . . . . . . . . . . . . . . . . . . Inhomogeneous 1st Order Hyperboolic Differential Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . Non Linear 1st Order Hyperboolic Differential Equation Finite Difference Method . . . . . . . . . . . . . . . . Hyperbolic Partial Differential Equations . . . . . . . Hyperbolic Differential Equations in 2D or 3D . . . . . Direct Method . . . . . . . . . . . . . . . . . . . . . . Five Point Formula . . . . . . . . . . . . . . . . . . . Finite Difference Method . . . . . . . . . . . . . . . . Seven Point Formula . . . . . . . . . . . . . . . . . . . Nine Point Formula . . . . . . . . . . . . . . . . . . . Five Point Formula . . . . . . . . . . . . . . . . . . . Laplace Distribution . . . . . . . . . . . . . . . . . . . Spherical Coordinate System . . . . . . . . . . . . . . Parallel Bisection Method . . . . . . . . . . . . . . . . Lagrange Interpolation in Parallel Computing . . . . . Trapezoidal Rule and Simpsons Rule in Parallel Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . Parallel Gauss Seidel Method . . . . . . . . . . . . . . Poissons Partial Differential Equation . . . . . . . . . MLP Algorithm . . . . . . . . . . . . . . . . . . . . . 10
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 22.2 Exa 22.3 Exa 22.4 Exa 22.5 Exa 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
Scilab code Exa 1.1 Conversion to Decimal System
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/ / E xa mp le 1 . 1 / / C o n v e r s i o n t o D e ci m a l S ys te m / / Pa ge no . 4 clc ; close ; clear ; function [ s ] = b a s 2 d e c ( x , b ) xi = int ( x ) x d = x - int ( x ) s =0 for i = 1 : 1 0 xi=xi/10 s = s + ( 1 0 * ( x i -fix ( x i ) ) * b ^ ( i - 1 ) ) xi = int ( x i ) if ( x i = = 0 ) break end end for i = 1 : 1
12
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
xd=xd*10; s = s + ( ceil ( x d ) / b ^ ( i ) ) x d = x d - fix ( x d ) if ( x d = = 0 ) break end
end endfunction
/ / c o n v e r s i o n f ro m h e xa d ec i ma l t o d e c im a l s ys te m // i n b u i t f u n c t i o n disp ( hex2dec ( ’ 1A2C ’ ) , ’ 1A2C= ’ ) ; / / c o n v e r s i o n f ro m h e xa d ec i ma l t o d e c im a l s ys te m // i n l i n e f u n c t i o n disp ( b a s 2 d e c ( 4 2 8 . 5 , 8 ) , ’ 428 .5= ’ ) / / c o n v e r s i o n f ro m h e xa d ec i ma l t o d e c im a l s ys te m disp ( b a s 2 d e c ( 1 2 0 . 1 , 3 ) , ’ 120 .1= ’ ) // i n l i n e function
Scilab code Exa 1.2 Conversion Using Shortcut Method
1 2 3 4 5 6 7
/ / E xa mp le 1 . 2 / / C o n v e r s i o n U s i n g S h o r t c u t Meth od / / Pa ge no . 4 clc ; close ; clear ; A=10;C=12; d=(((1)*16+A)*16+2)*16+C; disp (d , ’ D ec im a l f or m o f 1A2C i s = ’ ) ;
13
Scilab code Exa 1.3 Conversion to Base B from Decimal System
1 2 3 4 5 6
/ / E xa mp le 1 . 3 / / C o n v er s i o n t o B as e B f ro m D ec im a l S ys te m / / Pa ge no . 5 clc ; close ; clear ;
/ / c o n v e rs i o n fro m b i na r y t o o c t a l disp ( d e c 2 o c t ( b i n 2 d e c ( ’ 1 0 1 0 1 1 0 1 1 1 0 ’ ) ) , ’ O c ta l f or m o f 1 01 01 10 11 10 i s = ’ ) ; // i n b u i l t f u n c t i o n
7 8 / / c o n v e r s i o n f ro m b i n a r y t o h e xa d ec i ma l 9 disp ( dec2hex ( b i n 2 d e c ( ’ 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 01 01 10 11 10 i s = ’ ) ; // i n b u i l t f u n c t i o n 10 11 / / c o n v e rs i o n fro m b i na r y t o o c t a l 12 s = d e c 2 o c t ( b i n 2 d e c ( ’ 101 1 ’ ) ) ; 13 s 1 = d e c 2 o c t ( b i n 2 d e c ( ’ 1 1 0 0 1 1 0 1 0 1 0 0 ’ ) ) ; // i n b u i l t
function 14 printf ( ’ \n O ct al form o f 1 0 1 1 . 11 0 0 1 1 0 1 0 1 i s = \n\n %s . %s ’ , s , s 1 ) 15 16 / / c o n v e r s i o n f ro m b i n a r y t o h e xa d ec i ma l 17 s = dec2hex ( b i n 2 d e c ( ’ 101 1 ’ ) ) ; 18 s1 = dec2hex ( b i n 2 d e c ( ’ 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 ex ad ec im al fo rm o f 1 0 11 . 11 0 01 1 01 0 1 i s = \n\n %s . %s ’ , s , s 1 )
Scilab code Exa 1.4 Conversion to Binary System
1 / / E xa mp le 1 . 4 2 / / C o n v er s i o n t o B i n ar y S ys te m 3 / / Pa ge no . 6
14
4 clc ; close ; clear ; 5 / / c o n v e rs i o n fro m o c t a l t o b i na r y 6 disp ( d e c 2 b i n ( o c t 2 d e c ( ’ 175 3 ’ ) ) , ’ B i na ry f or m o f 1 75 3 i s = ’ ); // i n b u i l t f u n c t i o n 7 8 / / c o n v e rs i o n fro m o c t a l t o b i na r y 9 disp ( d e c 2 b i n ( hex2dec ( ’ A478 ’ ) ) , ’ B i n ar y f or m 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 rs i o n fro m o c t a l t o b i na r y 12 s = d e c 2 b i n ( o c t 2 d e c ( ’ 3 ’ ) ) ; 13 s 1 = d e c 2 b i n ( o c t 2 d e c ( ’ 154 ’ ) ) ; // i n b u i l t f u n c ti 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 )
Scilab code Exa 1.5 Conversion to Binary System
1 2 3 4 5 6 7 8 9 10 11 12 13
/ / E xa mp le 1 . 5 / / C o n v er s i o n t o B i n ar y S ys te m / / Pa ge no . 6 clc ; close ; clear ;
/ / c o n v e rs i o n fro m o c t a l t o b i na r y b = d e c 2 b i n ( o c t 2 d e c ( ’ 175 3 ’ ) ) disp (b , ’ B in ar y fo rm o f 1 75 3 i s = ’ ) ; // i n b u i l t function b = dec2hex ( o c t 2 d e c ( ’ 175 3 ’ ) ) // disp (b , ’ H ex ad ec im al f or m o f 1 75 3 i s = ’ ) ; i n b u i l t f u nc t io n / / c o n v e rs i o n fro m o c t a l t o b i na r y b = d e c 2 b i n ( hex2dec ( ’ A478 ’ ) ) disp (b , ’ B in ar y fo rm o f A478 i s = ’ ) ; // i n b u i l t function b = d e c 2 o c t ( hex2dec ( ’ A478 ’ ) ) 15
14 disp (b , ’ O ct al form o f A478 i s = ’ ) ;
// i n b u i l t
function 15 / / c o n v e rs i o n fro m o c t a l t o b i na r y 16 s = d e c 2 b i n ( o c t 2 d e c ( ’ 3 ’ ) ) ; 17 s 1 = d e c 2 b i n ( o c t 2 d e c ( ’ 154 ’ ) ) ; // i n b u i l t f u n c ti o n 18 printf ( ’ \n O c t a l form o f 3 .1 5 4 i s = \n\n %s . 0 0 %s ’ ,s , s1 ) 19 s = dec2hex ( o c t 2 d e c ( ’ 3 ’ ) ) ; 20 s1 = dec2hex ( o c t 2 d e c ( ’ 154 ’ ) ) ; // i n b u i l t f u n c ti o n 21 printf ( ’ \n\n H e xa de c i ma l fo rm o f 3 . 15 4 i s = \n\n % s . %s ’ , s , s 1 )
Scilab code Exa 1.6 Conversion to Decimal Number
1 2 3 4 5 6
/ / E xa mp le 1 . 6 / / C o n v e r s i o n t o D e c i m a l Number / / Pa ge no . 7 clc ; close ; clear ; disp ( d e c 2 b i n ( 1 8 2 ) , ’ B i n a r y o f 182= ’ )
function
Scilab code Exa 1.7 Conversion to Decimal Number
1 2 3 4 5
/ / E xa mp le 1 . 7 / / C o n v e r s i o n t o D e c i m a l Number / / Pa ge no . 7 clc ; close ; clear ;
16
// i n b u i l t
6 disp ( d e c 2 o c t ( 4 6 7 ) , ’ O c t a l o f 467= ’ )
//
i n b u i l t f u nc t io n
Scilab code Exa 1.8 Conversion to Base B from Binary System
1 2 3 4 5 6
/ / E xa mp le 1 . 8 / / C o n v er s i o n t o B as e B f ro m B i na r y S ys te m / / Pa ge no . 8 clc ; close ; clear ;
/ / c o n v e rs i o n fro m b i na r y t o o c t a l disp ( d e c 2 o c t ( b i n 2 d e c ( ’ 1 0 1 0 1 1 0 1 1 1 0 ’ ) ) , ’ O c ta l f or m o f 1 01 01 10 11 10 i s = ’ ) ; // i n b u i l t f u n c t i o n
7 8 / / c o n v e r s i o n f ro m b i n a r y t o h e xa d ec i ma l 9 disp ( dec2hex ( b i n 2 d e c ( ’ 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 01 01 10 11 10 i s = ’ ) ; // i n b u i l t f u n c t i o n 10 11 / / c o n v e rs i o n fro m b i na r y t o o c t a l 12 s = d e c 2 o c t ( b i n 2 d e c ( ’ 101 1 ’ ) ) ; 13 s 1 = d e c 2 o c t ( b i n 2 d e c ( ’ 1 1 0 0 1 1 0 1 0 1 0 0 ’ ) ) ; // i n b u i l t
function 14 printf ( ’ \n O ct al form o f 1 0 1 1 . 11 0 0 1 1 0 1 0 1 i s = \n\n %s . %s ’ , s , s 1 ) 15 16 / / c o n v e r s i o n f ro m b i n a r y t o h e xa d ec i ma l 17 s = dec2hex ( b i n 2 d e c ( ’ 101 1 ’ ) ) ; 18 s1 = dec2hex ( b i n 2 d e c ( ’ 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 ex ad ec im al fo rm o f 1 0 11 . 11 0 01 1 01 0 1 i s = \n\n %s . %s ’ , s , s 1 )
17
Scilab code Exa 1.9 Conversion to Binary System
1 2 3 4 5 6
/ / E xa mp le 1 . 9 / / C o n v er s i o n t o B i n ar y S ys te m / / Pa ge no . 8 clc ; close ; clear ;
/ / c o n v e rs i o n fro m o c t a l t o b i na r y disp ( d e c 2 b i n ( o c t 2 d e c ( ’ 175 3 ’ ) ) , ’ B i na ry f or m o f 1 75 3 i s = ’ ); // i n b u i l t f u n c t i o n
7 8 / / c o n v e rs i o n fro m o c t a l t o b i na r y 9 disp ( d e c 2 b i n ( hex2dec ( ’ A478 ’ ) ) , ’ B i n ar y f or m 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 rs i o n fro m o c t a l t o b i na r y 12 s = d e c 2 b i n ( o c t 2 d e c ( ’ 3 ’ ) ) ; 13 s 1 = d e c 2 b i n ( o c t 2 d e c ( ’ 154 ’ ) ) ; // i n b u i l t f u n c ti 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 )
Scilab code Exa 1.10 Conversion to Binary System and to Base N
1 2 3 4 5 6
/ / E x am pl e 1 . 1 0 / / C o n v er s i o n t o B i n ar y S ys te m and t o B as e N / / Pa ge no . 9 clc ; close ; clear ; b = d e c 2 b i n ( o c t 2 d e c ( ’ 175 3 ’ ) )
18
7 disp (b , ’ B in ar y fo rm o f 1 75 3 i s = ’ ) ;
// i n b u i l t
function 8 b = dec2hex ( o c t 2 d e c ( ’ 175 3 ’ ) ) 9 disp (b , ’ H ex ad ec im al f or m o f 1 75 3 i s = ’ ) ; 10 11 12 13 14 15 16 17 18
//
i n b u i l t f u nc t io n / / c o n v e rs i o n fro m o c t a l t o b i na r y b = d e c 2 b i n ( hex2dec ( ’ A478 ’ ) ) disp (b , ’ B in ar y fo rm o f A478 i s = ’ ) ; // i n b u i l t function b = d e c 2 o c t ( hex2dec ( ’ A478 ’ ) ) // i n b u i l t disp (b , ’ O ct al form o f A478 i s = ’ ) ; function / / c o n v e rs i o n fro m o c t a l t o b i na r y s = d e c 2 b i n ( o c t 2 d e c ( ’ 3 ’ ) ) ; s 1 = d e c 2 b i n ( o c t 2 d e c ( ’ 154 ’ ) ) ; // i n b u i l t f u n c ti 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 ) 19 s = dec2hex ( o c t 2 d e c ( ’ 3 ’ ) ) ; 20 s1 = dec2hex ( o c t 2 d e c ( ’ 154 ’ ) ) ; // i n b u i l t f u n c ti o n 21 printf ( ’ \n\n H e xa de c i ma l fo rm o f 3 . 15 4 i s = \n\n % s . %s ’ , s , s 1 )
Scilab code Exa 1.13 1s compliment and 2s compliment
1 2 3 4 5 6
/ / E x am pl e 1 . 1 3 / / 1 s c om p li me n t and 2 s c om p li m en t / / P ag e n o . 1 1 clc ; close ; clear ;
// f u n c ti o n f o r 1 s
function [ x 1 ] = c o m 1 ( x )
compliment 7 8
for i = 8 : - 1 : 1 x=x/10;
19
9 x d = x - fix ( x ) if ( floor ( ( x d * 1 0 ) + 0 . 1 ) = = 1 ) 10 11 x1(1,i)=0; else 12 13 x1(1,i)=1; end 14 15 x=x-xd; end 16 17 e n d f u n c t i o n 18 function [ x 1 ] = c o m 2 ( x ) // f u n c t i o n f o r 2 s
compliment () 19 for i = 8 : - 1 : 1 x=x/10; 20 21 x d = x - fix ( x ) if ( int ( ( x d * 1 0 ) + 0 . 1 ) = = 1 ) 22 23 x1(1,i)=0; else 24 25 x1(1,i)=1; end 26 27 end 28 for i = 8 : - 1 : 1 29 if ( x 1 ( 1 , i ) = = 0 ) then 30 x1(1,i)=1; break ; 31 32 else x1(1,i)=0; 33 34 end 35 36 end 37 e n d f u n c t i o n 38 a = [ 0 0 0 1 0 0 1 1 , 0 1 1 1 0 1 1 0 , 1 1 1 0 1 1 0 1 , 1 0 0 0 0 0 0 1 , 1 0 0 0 0 0 0 0 , 0 0 0 00 0 0 0 ] ; 39 for i = 1 : 6 40 printf ( ’ 1 s Co mp li me nt o f %. 8 i = ’ , a ( i ) ) ; 41 disp ( c o m 1 ( a ( i ) ) ) printf ( ’ 2 s Co mp li me nt o f %. 8 i = ’ , a ( i ) ) ; 42 43 disp ( c o m 2 ( a ( i ) ) )
20
44 printf ( ’ \n\n ’ ) 45 end
Scilab code Exa 1.14 1s compliment
1 2 3 4 5 6
/ / E x am pl e 1 . 1 4 / / 1 s c o mp l i me n t / / P ag e n o . 1 2 clc ; close ; clear ;
// f u n c ti o n f o r 1 s
function [ x 1 ] = c o m 1 ( x )
compliment for i = 8 : - 1 : 1 7 8 x=x/10; x d = x - fix ( x ) 9 10 if ( floor ( ( x d * 1 0 ) + 0 . 1 ) = = 1 ) 11 x1(1,i)=0; 12 else 13 x1(1,i)=1; end 14 15 x=x-xd; end 16 17 e n d f u n c t i o n 18 a = [ 0 0 0 1 0 0 1 1 , 0 1 1 1 0 1 1 0 , 1 1 1 0 1 1 0 1 , 1 0 0 0 0 0 0 1 , 1 0 0 0 0 0 0 0 , 0 0 0 00 0 0 0 ] ; 19 for i = 1 : 6 20 printf ( ’ 1 s Co mp li me nt o f %. 8 i = ’ , a ( i ) ) ; 21 disp ( c o m 1 ( a ( i ) ) ) 22 printf ( ’ \n\n ’ ) 23 end
21
Scilab code Exa 1.15 Addition and Subtraction
1 2 3 4 5
/ / E x am pl e 1 . 1 5 / / A d d i t i on and S u b t r a c t i o n / / P ag e n o . 1 3 clc ; clear ; close ; function [ x 1 ] = a d d ( x , y )
// fun ctio n
f o r a dd it io n o f b i n a ri e s 6 7 8 9 10 11 12 13 14 15 16 17 18
19 20 21 22 23 24 25 26
c=0; for i = 1 : 1 0 x1(1,i)=0 end for i = 1 0 : - 1 : 1 x=x/10; x d = x - fix ( x ) x=x-xd; y=y/10; y d = y - fix ( y ) y=y-yd; if c = = 1 then i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=1;c=1; e l se i f f l oo r( ( x d * 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 i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then
22
27 28
x1(1,i)=0;c=1; e l se i f f l oo r( ( x d * 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 end 33 34 end 35 disp ( x 1 , ’ A d d it i o n o f 1 73 and 141= ’ ) 36 e n d f u n c t i o n 37 function [ x 1 ] = s u b ( x , y ) // f u n c t i o n f o r
s u bt r ac t io n o f b i n a r i e s 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
c=0; for i = 1 : 1 0 x1(1,i)=0 end for i = 1 0 : - 1 : 1 x=x/10; x d = x - fix ( x ) x=x-xd; y=y/10; y d = y - fix ( y ) y=y-yd; if c = = 1 then i f f lo or ( ( x d * 10 ) + 0 . 1) = = 0 & floor ((yd * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=0;c=1; e l se i f f l oo r( ( x d * 10 ) + 0 . 1) = = 0 & floor ((yd*10)+0.1)==0 x1(1,i)=1;c=0; e l se i f f l oo r( ( x d * 10 ) + 0 . 1) = = 1 & floor ((yd*10)+0.1)==1 x1(1,i)=1;c=1; e l se i f f l oo r( ( x d * 10 ) + 0 . 1) = = 1 & floor ((yd*10)+0.1)==0 x1(1,i)=0;c=0; end
23
59 60
else
61 62
63 64
65 66
i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ((yd * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=1;c=1; e l se i f f l oo r( ( x d * 10 ) + 0 . 1) = = 0 & floor ((yd*10)+0.1)==0 x1(1,i)=0;c=0; e l se i f f l oo r( ( x d * 10 ) + 0 . 1) = = 1 & floor ((yd*10)+0.1)==0 x1(1,i)=1;c=0; e l se i f f l oo r( ( x d * 10 ) + 0 . 1) = = 0 & floor ((yd*10)+0.1)==1 x1(1,i)=1;c=1; end
67 68 end 69 70 end disp ( x 1 , ’ S u b t ra c t i o n o f 45 fro m 228= ’ ) 71 72 e n d f u n c t i o n 73 a d d ( 1 0 1 0 1 1 0 1 , 1 0 0 0 1 1 0 1 ) 74 s u b ( 1 1 1 0 0 1 0 0 , 0 0 1 0 1 1 0 1 )
Scilab code Exa 1.16 Addition
1 2 3 4 5 6
/ / E x am pl e 1 . 1 6 // Additi on / / P ag e n o . 1 4 clc ; close ; clear ;
// fun ctio n
function [ x 1 ] = a d d ( x , y )
f o r a dd it io n o f b i n a ri e s 7 8 9
c=0; printf ( ’ A d d i t i on 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; x d = x - fix ( x ) x=x-xd; y=y/10; y d = y - fix ( y ) y=y-yd; if c = = 1 then i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=1;c=1; e l se i f f l oo r( ( x d * 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 i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=0;c=1; e l se i f f l oo r( ( x d * 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 ( x 1 ) endfunction add(0010,0101); add(1110,1011); add(1110,0101); add(0010,1011);
25
44 a d d ( 1 1 1 0 , 0 0 1 0 ) ; 45 a d d ( 0 0 0 0 , 0 0 0 0 ) ;
Scilab code Exa 1.17 Addition
1 2 3 4 5 6
/ / E x am pl e 1 . 1 7 // Additi on / / P ag e n o . 1 4 clc ; close ; clear ;
// fun ctio n
function [ x 1 ] = a d d ( x , y )
f o r a dd it io n o f b i n a ri e s 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
c=0; printf ( ’ A d d i t i on 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; x d = x - fix ( x ) x=x-xd; y=y/10; y d = y - fix ( y ) y=y-yd; if c = = 1 then i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=1;c=1; e l se i f f l oo r( ( x d * 10 ) + 0 . 1) = = 0 & floor ((yd *10)+0.1)==0 x1(1,i)=1;c=0; else x1(1,i)=0;c=1;
26
end
26 27 28
else
29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=0;c=1; e l se i f f l oo r( ( x d * 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 ( x 1 ) endfunction
add(0010,0101); add(1101,1010); add(1101,0101); add(0010,1010); add(1101,0010); add(1111,0000);
Scilab code Exa 1.18 Addition
1 2 3 4 5 6
/ / E x am pl e 1 . 1 8 // Additi on / / P ag e n o . 1 5 clc ; close ; clear ;
// fun ctio n
function [ x 1 ] = a d d ( x , y )
f o r a dd it io n o f b i n a ri e s 27
7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
c=0; printf ( ’ A d d i t i on 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; x d = x - fix ( x ) x=x-xd; y=y/10; y d = y - fix ( y ) y=y-yd; if c = = 1 then i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=1;c=1; e l se i f f l oo r( ( x d * 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 i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=0;c=1; e l se i f f l oo r( ( x d * 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
31 32 33 34 35 36 37 38 disp ( x 1 ) 39 e n d f u n c t i o n 40
28
41 a d d ( 0 1 0 0 , 0 1 0 1 ) ; 42 a d d ( 1 1 0 0 , 1 0 1 1 ) ; 43 a d d ( 1 0 0 0 , 1 0 0 0 ) ;
Scilab code Exa 1.19 Addition
1 2 3 4 5 6
/ / E x am pl e 1 . 1 9 // Additi on / / P ag e n o . 1 5 clc ; close ; clear ;
// fun ctio n
function [ x 1 ] = a d d ( x , y )
f o r a dd it io n o f b i n a ri e s 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
c=0; printf ( ’ A d d i t i on 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; x d = x - fix ( x ) x=x-xd; y=y/10; y d = y - fix ( y ) y=y-yd; if c = = 1 then i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=1;c=1; e l se i f f l oo r( ( x d * 10 ) + 0 . 1) = = 0 & floor ((yd *10)+0.1)==0 x1(1,i)=1;c=0; else
29
25 26 27 28
x1(1,i)=0;c=1; end
else
29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=0;c=1; e l se i f f l oo r( ( x d * 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 ( x 1 ) endfunction
add(0010,0101); add(11110,11011); add(1000,0101); add(00010,11011); add(11110,00010); add(11111,0000);
Scilab code Exa 1.20 Subtraction
1 2 3 4 5
/ / E x am pl e 1 . 2 0 // Subtra ction / / P ag e n o . 1 6 clc ; close ; clear ; function [ x 1 ] = a d d ( x , y )
// fun ctio n
f o r a dd it io n o f b i n a ri e s 30
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; x d = x - fix ( x ) x=x-xd; y=y/10; y d = y - fix ( y ) y=y-yd; if c = = 1 then i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=1;c=1; e l se i f f l oo r( ( x d * 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 i f f lo or ( ( x d * 10 ) + 0 . 1) = = 1 & floor ( ( y d * 1 0 ) + 0 . 1 ) = = 1 then x1(1,i)=0;c=1; e l se i f f l oo r( ( x d * 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 ( x 1 , ’ A d d it i o n o f 1 73 and 141= ’ ) endfunction
29 30 31 32 33 34 35 36 37 38 a d d ( 0 1 0 0 , 1 0 1 1 ) ; 39 a d d ( 1 1 0 0 , 0 1 0 1 ) ;
31
40 a d d ( 1 0 0 0 , 1 0 0 0 ) ;
Scilab code Exa 1.23 Multiplication
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
/ / E x am pl e 1 . 2 3 // M u l t i p l i c a t i o n / / P ag e n o . 1 8 clc ; clear ; close ; function [ x 1 ] = m u l ( x , y ) for i = 1 : 8 x1(1,i)=0 end printf ( ’ M u l t i p l i c a t i o n o f %. 4 i and %. 4 i = ’ , x , y ) x=x*y; c=0; for i = 8 : - 1 : 1 x=x/10; xd = floor ( ( x - fix ( x ) ) * 1 0 + 0 . 1 ) if c = = 1 then if x d = = 0 then x1(1,i)=1;c=0 elseif x d = = 1 x1(1,i)=0; c=1; elseif x d = = 2 x1(1,i)=1;c=1; end else if x d == 0 | xd = =1 then x1(1,i)=xd;c=0 elseif x d = = 2 x1(1,i)=0;
32
30 i=i-1;c=1; end 31 32 end end 33 34 disp ( x 1 ) 35 e n d f u n c t i o n 36 m u l ( 1 1 1 0 , 1 0 1 1 ) ;
Scilab code Exa 1.24 Multiplication
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/ / E x am pl e 1 . 2 4 // M u l t i p l i c a t i o n / / P ag e n o . 1 8 clc ; clear ; close ; function [ x 1 ] = m u l ( x , y ) for i = 1 : 8 x1(1,i)=0 end printf ( ’ M u l t i p l i c a t i o n o f %. 4 i and %. 4 i = ’ , x , y ) x=x*y; c=0; for i = 8 : - 1 : 1 x=x/10; xd = floor ( ( x - fix ( x ) ) * 1 0 + 0 . 1 ) if c = = 1 then if x d = = 0 then x1(1,i)=1;c=0 elseif x d = = 1 x1(1,i)=0; c=1; elseif x d = = 2 x1(1,i)=1;c=1;
33
24 end else 25 26 if x d == 0 | xd = =1 then x1(1,i)=xd;c=0 27 28 elseif x d = = 2 x1(1,i)=0; 29 30 i=i-1;c=1; end 31 32 end 33 end 34 disp ( x 1 ) 35 e n d f u n c t i o n 36 m u l ( 1 1 1 0 , 1 0 1 1 ) ;
Scilab code Exa 1.25 Division
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/ / E x am pl e 1 . 2 5 // Div isi on / / P ag e n o . 1 9 clc ; close ; clear ; function [ c o ] = c o m ( x , y ) co=1; for i = 1 : length ( x ) if x ( i ) > y ( i ) then break elseif x ( i ) = = y ( i ) continue else c o = 0 ; break end end endfunction function [ x 1 ] = s u b ( x , y )
// f u n c t i o n f o r 34
s u bt r ac t io n o f b i n a r i e s 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
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 ( x 1 , ’ R e ma i nd e r = ’ ) endfunction d1=11011001;d2=01011;d22=[0,0,0,0,0] for i = 8 : - 1 : 1 d3=d1/10; d i v ( 1 , i ) =int ( 1 0 * ( d 3 - int ( d 3 ) ) ) 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
d 2 1 ( 1 , i ) =int ( 1 0 * ( d 3 - int ( d 3 ) ) + 0 . 5 ) d2=d2/10 end div1(1,1)=0 for j = 1 : 4 div1(1,j+1)=div(1,j) end for i 1 = 1 : 5 printf ( ’ A f te r S te p %i : \n ’ , i 1 ) if c o m ( d i v 1 , d 2 1 ) = = 1 then dis(1,i1)=1 n=sub(div1,d21) else dis(1,i1)=0 n=sub(div1,d22) end disp ( d i s , ’ D i v i s o r = ’ ) if i 1 = = 5 then break end for j = 1 : 5 if j <5 then div1(1,j)=n(j+1) else div1(1,j)=div(1,i1+4) end end printf ( ’ \n\n\n\n ’ ) end
Scilab code Exa 1.26 Multiplication
36
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 37 38
/ / E x am pl e 1 . 2 6 // M u l t i p l i c a t i o n / / P ag e n o . 1 9 clc ; clear ; close ;
function [ x 1 ] = m u l ( x , y ) for i = 1 : 8 x1(1,i)=0 end printf ( ’ M u l t i p l i c a t i o n o f %. 4 i and %. 4 i = ’ , x , y ) x=x*y; c=0; for i = 1 0 : - 1 : 1 x=x/10; xd = floor ( ( x - fix ( x ) ) * 1 0 + 0 . 1 ) if c = = 1 then if x d = = 0 then x1(1,i)=1;c=0 elseif x d = = 1 x1(1,i)=0; c=1; elseif x d = = 2 x1(1,i)=1;c=1; end else if x d == 0 | xd = =1 then x1(1,i)=xd;c=0 elseif x d = = 2 x1(1,i)=0; i=i-1;c=1; end end end for i = 1 : 1 0 if x 1 ( 1 , i ) = = 1 then x1(1,i-1)=1; break
37
39 end end 40 41 disp ( x 1 ) 42 e n d f u n c t i o n 43 m u l ( 1 1 1 0 , 1 0 1 1 ) ;
Scilab code Exa 1.29 Normalized Floating Point Representation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E x am pl e 1 . 2 9 // N o r ma l iz ed F l o a t i n g P o in t R e p r e s e n t a t i o n / / P ag e n o . 2 3 clc ; clear ; close ; function [ ] = f p ( x ) x1=x; if x >0 then for i = 1 : 1 0 x=x/10 if i nt ( x ) = = 0 then break end end printf ( ’ \n
%i \ n No rm al iz ed F l o a t i n g P o in t R e p r e s e n t a t i o n o f %g = %. 4 f x 10 ’ , i , x 1 , x ) 16 17 18 19 20 21
else
for i = 1 : 1 0 x=x*10 i f c ei l( x ) ~ = 0 then break end
38
22 23 24
end x=x/10;i=i-1; printf ( ’ \n
−%i \ n No rm a li ze d F l o a t i n g P o in t R e p r e s e n t a t i o n o f %g = %. 4 f x 10 ’ , i , x 1 , x ) 25 26 27 28 29 30 31 32
end endfunction x=[25.12,-0.00287,87000]; for i = 1 : 3 fp(x(i)) end
Scilab code Exa 1.30 Add
1 2 3 4 5 6 7
/ / E x am pl e 1 . 3 0 //Add / / P ag e n o . 2 6 clc ; clear ; close ; a=0.4532e7;b=0.5427e7; c=a+b printf ( ’ A d d i t i o n o f %. 6 g a nd %. 6 g = %. 6 g ’ , a , b , c )
Scilab code Exa 1.31 Add
1 / / E x am pl e 1 . 3 1
39
2 3 4 5 6 7
//Add / / P ag e n o . 2 6 clc ; clear ; close ; a=0.4532e5;b=0.5427e7; c=a+b printf ( ’ A d d i t i o n o f %. 4 g a nd %. 6 g = %. 6 g ’ , a , b , c )
Scilab code Exa 1.32 Add
1 2 3 4 5 6 7
/ / E x am pl e 1 . 3 2 //Add / / P ag e n o . 2 6 clc ; clear ; close ; a=0.4532e3;b=0.5427e7; c=a+b printf ( ’ A d d i t i o n o f %. 2 g a nd %. 6 g = %. 4 g ’ , a , b , c )
Scilab code Exa 1.33 Add
1 2 3 4 5 6 7 8 9
/ / E x am pl e 1 . 3 3 //Add / / P ag e n o . 2 7 clc ; clear ; close ; a=[0.4632e3,0.4632e99];b=[0.5427e3,0.5427e99]; for i = 1 : 2 c(i)=a(i)+b(i) printf ( ’ \ n A d d i t i o n o f %. 2 g a nd %. 2 g = %. 5 g\n ’ ,a ( i),b(i),c(i))
40
10 end
Scilab code Exa 1.34 Subtraction
1 2 3 4 5 6 7 8 9
/ / E x am pl e 1 . 3 4 // Subtra ction / / P ag e n o . 2 7
clc ; clear ; close ; a=[0.5427e-3,0.9627e4,0.9627e-99];b=[0.9632e -4,0.9622e4,0.9622e-99]; for i = 1 : 3 c(i)=a(i)-b(i) printf ( ’ \ n S u b t r a c t i o n o f %. 2 g f ro m %. 3 g = %. 6 g \n ’ ,a(i),b(i),c(i)) end
Scilab code Exa 1.35 Multiplication
1 2 3 4 5 6 7 8 9
/ / E x am pl e 1 . 3 5 // M u l t i p l i c a t i o n / / P ag e n o . 2 8 clc ; clear ; close ; a=[ 0.9632 e12 ,0.1132 e12 ,0.1132 e52 ,0.1132e -52];b =[0.5427e-15,0.1027e15 ,0.1027e50 ,0.1027e-50]; for i = 1 : 4 c(i)=a(i)*b(i) printf ( ’ \ n M u l t i p l i c a t i o n o f %. 3 g and %. 2 g = %. 6 g \n ’ , a ( i ) , b ( i ) , c ( i ) ) end
41
Scilab code Exa 1.36 Division
1 2 3 4 5 6 7 8 9
/ / E x am pl e 1 . 3 6 // Div isi on / / P ag e n o . 2 8
clc ; clear ; close ; a = [ 0 . 1 1 3 2 e 1 , 0 . 1 1 3 2 e - 6 , 0 . 1 1 3 2 e 6 ] ; b = [ 0 . 1 0 0 0 e - 9 9 , 0 . 1 00 0 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
Scilab code Exa 2.4 Solving Simultaneous Linear Equation
1 2 3 4 5 6 7 8 9 10
/ / E xa mp le 2 . 4 / / S o l v i n g S i m ul t a ne o u s L i n e a r E qu a ti o n / / P a ge 3 6 clc ; close ; clear ;
/ / e q 1 = 5 x −331y=3.5 / / e q 2 = 6 x −397y=5.2 A=[5,-331;6,-397]; B=[3.5;5.2]; // f i n d i n g C = inv ( A ) * B ;
i n v e r s e w it h v a l ue s 11 disp ( C ( 1 , 1 ) , ’ V al ue o f x= ’ ) ; 12 disp ( C ( 2 , 1 ) , ’ V al ue o f y= ’ ) ;
43
v a l u e by m u l t i p l y i n g
Scilab code Exa 2.6 Integration
1 2 3 4 5
/ / E xa mp le 2 . 6 // Int egra tio n / / P ag e n o . 3 6 clc ; clear ; close ; disp ( i n t e g r a t e( ’ 1 / x ’ , ’ x ’ , exp ( - 4 ) , 1 ) , ’ I n t e g r a t i o n Value=’ ); // p e rf o rm i ng i n t e g r a t i o n w it h
r e s p e c t t o dx
44
Chapter 3 Errors and Their Propagation
Scilab code Exa 3.1 Limiting Error
1 2 3 4 5 6 7
/ / E xa mp le 3 . 1 // Limiting Error / / P ag e n o . 4 5 clc ; clear ; close ; R=1000; // l i m i t i n g e r r o r c a l c u l a t i o n e=0.1*1000; printf ( ’ Mag ni tude o f t he R e s i s t o r r e s i s t e n c e (R) =\ n% i <= R <= %i ’ , R - e , R + e )
Scilab code Exa 3.2 Known Error
1 2 3 4
/ / E xa mp le 3 . 2 //Known Er ro r / / P ag e n o . 4 6 clc ; clear ; close ;
45
5 6 7 8 9
l=28;d=5; v=%pi*l*(d/2)^2; printf ( ’ \ n Vol ume o f C y l i n d e r = %f c u . cm ’ , v ) ; re_d=0.1;re_l=-0.5; // r e l a t i v e e r r o r re_v=2*re_d+re_l;
computation 10 printf ( ’ \n\ n R e l a t i v e e r r o r i n v ol um e= %f %% ’ , r e _ v ) ;
Scilab code Exa 3.3 Absolute Relative and Percentage Errors
1 2 3 4 5 6 7 8 9
/ / E xa mp le 3 . 3 // A b so lu te , R e l a t i v e and P e rc e ta g e E r r o rs / / P ag e n o . 4 8
clc ; clear ; close ; x=0.00006;x1=0.00005; // a b s o l u t e e r r o r ex=x-x1; Ex=ex/x1; // r e l a t i v e e r r o r //percentage erro r px=100*Ex; printf ( ’ \ n A b s ol u t e E r r o r= %f \ n R e l a t i v e E r ro r= %f \ n P e r c e n t a g e E r r o r = %f %% ’ , e x , E x , p x ) ;
Scilab code Exa 3.4 Absolute Relative and Percentage Errors
1 2 3 4 5 6
/ / E xa mp le 3 . 4 // A b so lu te , R e l a t i v e and P e rc e ta g e E r r o rs / / P ag e n o . 4 8 clc ; clear ; close ; x=100500;x1=100000; // a b s o l u t e e r r o r ex=x-x1;
46
7 Ex=ex/x1; // r e l a t i v e e r r o r 8 px=100*Ex; //percentage erro r 9 printf ( ’ \ n A b s ol u t e E r r o r= %f \ n R e l a t i v e E r ro r= %f \ n P e r c e n t a g e E r r o r = %f %% ’ , e x , E x , p x ) ;
Scilab code Exa 3.5 Absolute Relative and Percentage Errors
/ / E xa mp le 3 . 5 / / A b so l ut e , R e l a t i v e and P e r ce n t ag e E r r o r s / / P ag e n o . 5 2
1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17
18 19 20 21
clc ; clear ; close ; x=9.12345;y=7.654321; x1=9.1234;y1=7.6543; / / on a 5 d e c im a l c om pu te r // a b s o l u t e e r r o r o f x ex=x-x1; 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 ol u t e E r r o r i n x= %f ’ , e x ) ; printf ( ’ \ n A b s ol u t e E r r o r i n y= %f ’ , e y ) ; printf ( ’ \ n Ad di ti on on a 5 d ec i ma l co mp ut er y i e l d s = % . 5 g ’ ,z1); z2=16.777; printf ( ’ \ n A b s ol u t e T o t a l E r r o r= % f ’ , x + y - z 2 ) ; printf ( ’ \ n A b s o l u t e P r o p a g at e d E r r o r = %f ’ , x + y - z 1 ) ; printf ( ’ \ n A b s o l u t e Round− o f f E r r o r= %. 4 g ’ , z 1 - z 2 ) ; printf ( ’ \ n R e a l t i v e T o t a l E r r o r= %. 4 g ’ , ( x + y - z 2 ) / ( x + y ) ); printf ( ’ \ n R e l a t i v e P r op a g at e d E r r o r= %. 2 g ’ , ( x + y - z 1 ) /(x+y)); printf ( ’ \ n R e l a t i v e Round− o f f E r r o r= %. 3 g ’ , ( z1 - z 2 ) / ( x +y)); printf ( ’ \nBound on t he p ro pa ga te d r e l a t i v e e r r o r = %f ’ ,2*10^-4); printf ( ’ \nBound on t he 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 ca n s e e t h at b ot h t he p ro pa ga te d and
t o t a l r e l a t i v e e r r o r a re l e s s th a n t h e i r bound values ’)
48
Chapter 4 Programming Tools and Techniques
Scilab code Exa 4.1 Quadratic Equation
1 2 3 4 5 6 7 8 9 10 11
/ / E xa mp le 4 . 1 / / Q u a d r a t i c E q u a t io n / / P ag e n o . 9 6
clc ; clear ; close ; a = input ( ” E nt e r v a l u e o f a= ” ) ; b = input ( ” E nt er v l au e o f b= ” ) ; c = input ( ” E nt e r v a l u e o f c= ” ) ; x 1 = ( - 1 * b + sqrt ( ( b ^ 2 ) - 4 * a * c ) ) / ( 2 * a ) ; x 2 = ( - 1 * b - sqrt ( ( b ^ 2 ) - 4 * a * c ) ) / ( 2 * a ) ; printf ( ’ \ n 1 s t R oo t= %f ’ , x1 ); printf ( ’ \ n2nd Root= %f ’ , x2 );
Scilab code Exa 4.2 Database Management
49
//1st root / / 2nd r o o t
1 2 3 4 5
/ / E xa mp le 4 . 2 / / D a t a b a s e M an ag em en t / / P ag e n o . 1 1 2 clc ; clear ; close ; M = [ 1 2 , 2 5 , 2 1 , 3 5 ; 2 5 , 7 , 2 3 , 2 9 ; 1 0 , 2 7 , 7 , 3 6 ; 2 6 , 2 6 , 2 6 , 3 5 ; 2 9 ,0 , 2 3 , 3 0 ] ;
//marks 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
// c a l c u l a t i o n o f c om po si te s c o r e for i = 1 : 5 , j=1;k=0; max1=M(i,j); if ( m a x 1 < M ( i , j + 1 ) ) max1=M(i,j+1) else k=1; end , if ( M ( i , j + 2 ) > M ( i , j + k ) ) max2=M(i,j+2);
else
max2=M(i,j);
end , CS(i,1)=max1+max2+M(i,4); end I =[ ’ R eg . No . ’ , ’ Name o f S t u d e n t s ’ , ’ T e s t 1 ’ , ’ T e s t 2 ’ , ’ T e st 3 ’ , ’ F i n a l ’ ; ’CS/01 ’ , ’C.V. Rajan ’ , ’ 1 2 ’ , ’ 2 5 ’ , ’ 2 1 ’ , ’ 3 5 ’ ; ’CS/02 ’ , ’B. X. Roy ’ , ’ 2 5 ’ , ’ 0 7 ’ , ’ 2 3 ’ , ’ 2 9 ’ ; ’CS/03 ’ , ’ P . C . S a s i k u m a r ’ , ’ 1 0 ’ , ’ 2 7 ’ , ’ 0 7 ’ , ’ 3 6 ’ ; ’CS/04 ’ , ’B .D . Box ’ , ’ 2 6 ’ , ’ 2 6 ’ , ’ 2 6 ’ , ’ 3 5 ’ ; ’CS/05 ’ , ’K.K. Mukher jee ’ , ’ 2 9 ’ , ’ 0 ’ , ’ 2 3 ’ , ’ 3 0 ’ ;] 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
’ ,I(i,j)); printf ( ’ %s if ( i ~ = 1 ) if ( j > 2 ) printf ( ’ \ t ’ ) end printf ( ’ ’) end if ( i == 1 & j = =6 ) printf ( ’ C o mp o s it e S c o r e \n ’ ) end
end
end
if ( i ~ = 1 ) printf ( ’ %i \n ’ , C S ( i - 1 , 1 ) ) ;
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 ( m a x 1 < C S ( i , 1 ) ) max1=CS(i,1);j=i; end , end printf ( ’ \n\ n To pp er i s : \ n%s \ t%s \ t%s ’ , I ( 1 , 1 ) , I ( 1 , 2 ) , ’ C o mp o s it e S c o r e ’ ) 65 printf ( ’ \nCS/0%i\ t \ t% s \ t \ t \ t%i ’ , j , I ( j + 1 , 2 ) , C S ( j , 1 ) )
51
Chapter 5 Solutions of Algebraic and Transcendental Equations
Scilab code Exa 5.1 Bisection Method
1 2 3 4 5 6 7 8
/ / E xa mp le 5 . 1 / / B i s e c t i o n Metho d / / P ag e n o . 1 4 5
clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=2ˆx−3∗x ’ ) x1=0;x2=2;e=0.001;i=0; printf ( ’ I t e r a t i o n \ t x 1 \ t \ t x 2 \ t \ t z \ t \ t f ( z ) \n ’ ) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 w h i le a bs ( x 1 - x 2 ) > e 10 z=(x1+x2)/2 11 %i \ t \ t%f \ t%f \ t%f \ t%f \n ’ , i , x 1 , x 2 , z , f printf ( ’ (z)) 12 if f ( z ) * f ( x 1 ) > 0 x1=z 13 14 else
52
15 x2=z end 16 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 qu a t io n i s %g a f t e r %i I t e r a t i o n s ’ , z , i - 1 )
Scilab code Exa 5.2 Bisection Method
1 2 3 4 5 6 7 8
/ / E xa mp le 5 . 2 / / B i s e c t i o n Metho d / / P ag e n o . 1 4 7
clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=xˆ x−2∗x+2 ’ ) x1=0;x2=2;e=0.001;i=0; printf ( ’ I t e r a t i o n \ t x 1 \ t \ t x 2 \ t \ t z \ t \ t f ( z ) \n ’ ) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 w h i le a bs ( x 1 - x 2 ) > e 10 z=(x1+x2)/2 printf ( ’ 11 %i \ t \ t%f \ t%f \ t%f \ t%f \n ’ , i , x 1 , x 2 , z , f (z)) if f ( z ) * f ( x 1 ) > 0 12 13 x1=z else 14 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 qu a t io 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 printf ( ’ \n\n\ nNote : Th ere a r e c o mp u ta t io n al
erro rs i n t he a n sw e r g i ve n by t he book f o r t h i s ex a m pl e ’
)
Scilab code Exa 5.3 Regula Falsi Method
1 2 3 4 5 6 7
/ / E xa mp le 5 . 3 / / R e gu l a F a l s i Method / / P ag e n o . 1 4 9
8
clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=xˆ3 −3∗x−5 ’ ) x1=2;x2=3;e=0.00001 printf ( ’ n\ t x 1 \ t \ t f ( x 1 ) \ t \ t x 2 \ t \ t f ( x 2 ) \ t \ t x 3 \ t \ t f ( x 3 ) ’) printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 for i = 0 : 1 9 10 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 , x 1 , f ( x 1 11 ),x2,f(x2),x3,f(x3)) if f ( x 1 ) * f ( x 3 ) > 0 then 12 13 x1=x3 else 14 15 x2=x3 end 16 17 if a bs ( f ( x 3 ) ) < e then 18 break 19 end 20 end 21 printf ( ’ \n\ n T he r ef o re t he s o l u t i o n i s %. 1 0 g ’ , x 3 )
54
Scilab code Exa 5.4 Ridders Method
1 2 3 4 5 6 7
/ / E xa mp le 5 . 4 / / R i d d e r s M et ho d / / P ag e n o . 1 5 3
8
clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=xˆ3 −3∗x−5 ’ ) x1=2;x2=3;e=0.00001 printf ( ’ n\ t x 1 \ t \ t f ( x 1 ) \ t \ t x 2 \ t \ t f ( x 2 ) \ t \ t x 3 \ t \ t f ( x 3 ) s i g n \ t x4 ’ ) \t printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 for i = 0 : 8 x3=(x1+x2)/2 10 11 a=f(x1)-f(x2); s = a * abs ( 1 / a ) 12 13 x 4 = x 3 + ( x 3 - x 2 ) * ( s * f ( x 3 ) ) /sqrt ( f ( x 3 ) - f ( x 1 ) * f ( x 2 ) ) printf ( ’ %i \ t%f \ t%f \ t%f \ t%f \ t%f \ t%f 14 %i \ t%f \n ’ ,i,x1,f(x1),x2,f(x2),x3,f(x3),s,x4) if f ( x 1 ) * f ( x 4 ) > 0 then 15 16 x1=x4 17 else 18 x2=x4 19 end if a bs ( f ( x 4 ) ) < e then 20 21 break end 22 23 end 24 printf ( ’ \n\nThe s o l u t i o n o f t h i s e qu a t io n i s %g a f t e r %i I t e r a t i o n s ’ , x 4 , i ) 25 printf ( ’ \n\n\ nThere a r e c om pu ta ti on e r r o r i n t he
55
a ns we rs g i ve n by t he book i n t h i s ex am ple \n\n ( v al ue o f x1 i s u s e d i n s t e a d o f x2 ) ’ )
Scilab code Exa 5.5 General Iterative Method
1 2 3 4 5 6 7 8 9 10 11 12 13
/ / E xa mp le 5 . 5 / / G e ne r al I t e r a t i v e Method / / P ag e n o . 1 5 4
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 qu a t io n a f t e r %i I t e r a t i o n s i s %. 1 0 f ’ , i , x )
Scilab code Exa 5.6 Linear Iterative Method
1 2 3 4 5 6 7
/ / E xa mp le 5 . 6 / / L i n e a r I t e r a t i v e Method / / P ag e n o . 1 5 9 clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=1+s i n (x ) /10 ’ ) printf ( ’ n\ t x \ t \ t f ( x ) \n ’ ) printf ( ’−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n ’ )
56
8 9 10 11 12 13
x=0; for i = 1 : 7 . 1 0 f \n ’ ,i,x,f(x)) printf ( ’ %i \t% . 1 0 f \t% x=f(x); end printf ( ’ \n\nThe s o l u t i o n o f t h i s e qu a t io n a f t e r %i I t e r a t i o n s i s %. 1 0 f ’ , i , x )
Scilab code Exa 5.7 Aitkens Method
1 2 3 4 5 6 7
/ / E xa mp le 5 . 7 / / A i t k e n s M et ho d / / P ag e n o . 1 6 1 clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=exp(−x ) ’ ) printf ( ’ n\ t x 0 \ t \ t x 1 \ t \ t x 2 \ t \ t x 3 \ t \ t y \ t \ t d x 0 \n ’ ) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 8 x0=0.5;e=0.0001 9 for i = 1 : 3 x1=f(x0);x2=f(x1);x3=f(x2); 10 11 y=x3-((x3-x2)^2)/(x3-2*x2+x1) dx0=y-x0; 12 13 printf ( ’ %i \t% . 1 0 f \t% 14 . 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) 15 x0=y; 16 if a bs ( x 0 ) < e then 17 break ; end 18 19 end 20 printf ( ’ \n\nThe s o l u t i o n o f t h i s e qu a t io n a f t e r %i
57
Iterations
i s %. 1 0 f ’ , i , y )
Scilab code Exa 5.8 Newton Raphson Method
1 2 3 4 5 6 7
/ / E xa mp le 5 . 8 //Newton Raphson Method / / P ag e n o . 1 6 3
8
clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=x−exp(−x ) ’ ) deff ( ’ x= f 1 ( x ) ’ , ’ x=1+exp (−x ) ’ ) printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 x0=0.5;e=0.00001 10 for i = 1 : 4 11 x1=x0-f(x0)/f1(x0) 12 e1 = abs ( x 0 - x 1 ) printf ( ’ %i \t% . 1 0 f \t% 13 . 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; 14 15 if a bs ( x 0 ) < e then break ; 16 17 end 18 end 19 printf ( ’ \n\nThe s o l u t i o n o f t h i s e qu a t io n a f t e r %i I t e r a t i o n s i s %. 1 0 f ’ , i , x 1 )
58
Scilab code Exa 5.9 Modified Newton Raphson Method
1 2 3 4 5 6 7
/ / E xa mp le 5 . 9 / / M o d i f i e d N ew to n R a ph s on M et ho d / / P ag e n o . 1 6 5
8
clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=exp ( x ) −3∗x−s i n ( x ) ’ ) deff ( ’ x= f 1 ( x ) ’ , ’ x=exp ( x )−3−c o s ( x ) ’ ) printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 x0=0;e=0.00001 10 for i = 1 : 4 11 x1=x0-f(x0)/f1(x0) e1 = abs ( x 0 - x 1 ) 12 13 . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n printf ( ’ %i \t% . 1 0 f \t% ’ ,i-1,x0,f(x0),f1(x0),x1,e1) 14 x0=x1; 15 if a bs ( x 0 ) < e then 16 break ; 17 end 18 end 19 printf ( ’ \n\ n Th er ef or e , t he r o o t i s %. 1 0 f ’ , x 1 )
Scilab code Exa 5.10 Newton Raphson Method
1 2 3 4 5
/ / E x am pl e 5 . 1 0 //Newton Raphson Method / / P ag e n o . 1 6 7 clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=x ∗ exp(−x ) ’ )
59
6 deff ( ’ x= f 1 ( x ) ’ , ’ x=exp(−x )−x ∗ exp(−x ) ’ ) 7 printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) 8 printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 x0=2;e=0.00001 10 for i = 1 : 1 1 11 x1=x0-f(x0)/f1(x0) 12 e1 = abs ( x 0 - x 1 ) 13 . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n printf ( ’ %i \t% . 1 0 f \t% ’ ,i-1,x0,f(x0),f1(x0),x1,e1) x0=x1; 14 15 if a bs ( x 0 ) < e then break ; 16 17 end 18 end 19 printf ( ’ \n\ n Th er ef or e , t h i s i s n ot c o nv e rg e nt ( i . e . ) divergent ’)
Scilab code Exa 5.11 Newton Raphson Method
1 2 3 4 5 6 7
/ / E x am pl e 5 . 1 1 //Newton Raphson Method / / P ag e n o . 1 6 7
8
clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=xˆ3−x−3 ’ ) deff ( ’ x= f 1 ( x ) ’ , ’ x=3∗xˆ2 −1 ’ ) printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 60
9 x0=0;e=0.00001 10 for i = 1 : 1 1 11 x1=x0-f(x0)/f1(x0) e1 = abs ( x 0 - x 1 ) 12 13 . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n printf ( ’ %i \t% . 1 0 f \t% ’ ,i-1,x0,f(x0),f1(x0),x1,e1) 14 x0=x1; if a bs ( x 0 ) < e then 15 16 break ; 17 end 18 end 19 printf ( ’ \n\ n Th er ef o re , i t i s c y c l i c i n n at ur e ’ )
Scilab code Exa 5.12 Newton Raphson Method
1 2 3 4 5 6 7
/ / E x am pl e 5 . 1 2 //Newton Raphson Method / / P ag e n o . 1 6 8
8
clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=atan ( x ) ’ ) deff ( ’ x= f 1 ( x ) ’ , ’x=1/(1+x ˆ2 ) ’ ) printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 x0=1.45;e=0.00001 10 for i = 1 : 1 2 11 x1=x0-f(x0)/f1(x0) 12 e1 = abs ( x 0 - x 1 ) printf ( ’ %i \t% .5 g 13 \ t \t% . 5 g \ t \t% . 5 g \ t \t% . 5 g \ t \t% . 5 g \n ’ , i - 1 , x 0 , f ( x 0 ) , f 1 ( x 0 ) , x 1 , e 1 ) x0=x1; 14
61
15 if a bs ( x 0 ) < e then break ; 16 17 end 18 end 19 printf ( ’ \n\ n Th er ef or e ,
it
i s d i v e r g e nt ’ )
Scilab code Exa 5.13 Secant Method
1 2 3 4 5 6 7
/ / E x am pl e 5 . 1 3 / / S e c a n t M et ho d / / P ag e n o . 1 7 0
8
clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=exp ( x ) −3∗x−s i n ( x ) ’ ) deff ( ’ x= f 1 ( x ) ’ , ’ x=exp ( x )−3−c o s ( x ) ’ ) printf ( ’ n\ t x n \ t \ t f ( x n ) \ t \ tXn+1\ t \ t f (Xn+1)\ t \tXn+2\ t \ t E r r o r \n ’ ) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 x0=0.567123008;x1=1;e=0.00001 10 for i = 1 : 9 x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0)) 11 12 e1 = abs ( x 0 - x 2 ) printf ( ’ %i \t% . 1 0 f \t% 13 . 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; 14 15 x1=x2 16 if a bs ( x 0 ) < e then 17 break ; 18 end 19 end 20 printf ( ’ \n\ n Th er ef or e , t he r o o t i s %. 1 0 f ’ , x 2 )
62
Scilab code Exa 5.14 Kizner Method
1 2 3 4 5 6 7
/ / E x am pl e 5 . 1 4 / / K i z n e r M et ho d / / P ag e n o . 1 7 2 clc ; clear ; close ; h2=0.00001 deff ( ’x=f ( x ) ’ , ’ x=2∗x−3−c o s ( x ) ’ ) deff ( ’ y=f1 ( x , y ) ’ , ’ y=h2 /(−x+y) ’ )
// f u n c t i o n f o r
differentiation 8 printf ( ’ n\ th \ t c \ t xn \ t \ t f ( x n ) \ t \ t F ( x n ) \ t \ t k 1 \ t \ t v \ t \ tXn+1\n ’ ) 9 printf ( ’ −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 10 x 0 = 2 ; e = 0 . 0 0 0 0 1 ; h = 0 . 5 ; c = 0 . 5 ; 11 for i = 1 : 1 1 12 h1=-f(x0); F=f1(f(x0),f(x0+h2)) 13 14 k1=h1*F/2; v=h*f(x0)/(c*(f(x0+c+h)-f(x0+c)))-k1/c; 15 16 a=0; 17 for j = 0 : 3 18 a=a+(v^j)/factorial(j+1) 19 end x1=x0+k1*a 20 21 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 , x 0 , f ( x 0 ) , F , k 1 , v , x 1 ) 22 x0=x1; if a bs ( x 0 ) < e then 23 24 break ; end 25
63
26 end 27 printf ( ’ \n\ n Th er ef or e , t he s o l u t i o n
i s %. 1 0 f ’ , x 1 )
Scilab code Exa 5.15 Brent Method
1 2 3 4 5 6 7 8 9 10
/ / E x am pl e 5 . 1 5 / / B r e n t M et ho d / / P ag e n o . 1 7 3
clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=xˆ2+x−2 ’ ) 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 ( ’ R oot i s : %. 1 0 g ’ , x 2 + ( p / q ) )
Scilab code Exa 5.19 Horner Method
1 2 3 4 5 6 7 8 9 10
/ / E x am pl e 5 . 1 9 / / H o r n e r M et ho d / / P ag e n o . 1 7 7 clc ; clear ; close ; deff ( ’ y=f ( x , a1 , a2 , a3 , a4 ) ’ , ’ y=a1 ∗ xˆ3+a2 ∗ xˆ2+a3 ∗x+a4 ’ ) k=1;m=2; a=[4;-13;-31;-275]; for i = 1 : 1 0 s=1;
64
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
s i = f ( s , a ( 1 ) , a ( 2 ) , a ( 3 ) , a ( 4 ) ) *abs ( 1 / f ( s , a ( 1 ) , a ( 2 ) , a(3),a(4))) while 1 a 1 = f ( s , a ( 1 ) , a ( 2 ) , a ( 3 ) , a ( 4 ) ) *abs ( 1 / f ( s , a ( 1 ) , a (2),a(3),a(4))) if s i ~ = a 1 then d(i)=s-1 break end si=a1; s=s+1; end b(1)=a(1) for j = 1 : 3 for k = 1 : 4 - j b(k+1)=a(k+1)+b(k)*d(i) a(k+1)=b(k+1) end end for j = 1 : 3 a(j+1)=10^j*a(j+1) end
end printf ( ’ The p o s i t i v e r o ot for i = 2 : 1 0 printf ( ’ %i ’ , d ( i ) ) end
i s %i . ’ , d ( 1 ) )
Scilab code Exa 5.20 Laguerre Method
1 / / E x am pl e 5 . 2 0 2 / / L a g u e r r e M et ho d 3 / / P ag e n o . 1 8 0
65
4 5 6 7 8 9
10
clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’y=xˆ3+xˆ2+10∗x−20 ’ ) deff ( ’ y= f 1 ( x ) ’ , ’ y=3∗xˆ2+2∗x+10 ’ ) deff ( ’ y= f 2 ( x ) ’ , ’ y=6∗x+2 ’ ) n=3; printf ( ’ i \ tn \ t x i \ t \ t P ( x ) \ t \ t P 1 ( x ) \ t \ t P 2 ( x ) \ t \ t P r o o t \ t \ t N r o o t \n ’ ) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 11 x i = 1 12 for i = 0 : 9 P r o o t = x i - ( n * f ( x i ) ) / ( f 1 ( x i ) +sqrt ( ( n - 1 ) * f 1 ( x i ) ^ 2 - n 13 *f(xi)*f2(xi))) N r o o t = x i - ( n * f ( x i ) ) / ( f 1 ( x i ) -sqrt ( ( n - 1 ) * f 1 ( x i ) ^ 2 - n 14 *f(xi)*f2(xi))) printf ( ’ %i \ t% i \ t%f \ t%f \ t%f \ t%f \ t%f \ t%f \n ’ , i , n , 15 xi,f(xi),f1(xi),f2(xi),Proot,Nroot) xi=Proot 16 17 end 18 printf ( ’ \n\ n P r o o t = % f \ n\ n N r o o t = %f ’ , P r o o t , N r o o t )
Scilab code Exa 5.21 Mullers Method
1 2 3 4 5 6 7 8
/ / E x am pl e 5 . 2 1 / / M u l l e r s Me th od / / P ag e n o . 1 8 2 clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=xˆ3−x−4 ’ ) zi=[1;2;3]; s =[ ” i ” , ” z 2 ” , ” z 0 ” , ” z 1 ” , ” f2 ” , ” f0 ” , ” f1 ” , ” a 0 ” , ” a 1 ” , ” a 2 ” , ” z r + ” , ” z r −” ]
66
9 li(1)=(zi(3,1)-zi(2,1))/(zi(2,1)-zi(1,1)) 10 h i ( 1 ) = z i ( 3 , 1 ) - z i ( 2 , 1 ) ; 11 for i = 2 : 6 for j = 1 : 3 12 13 fz(j,i-1)=f(zi(j,i-1)) end 14 15 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+ 16 fz(3,i-1)*(li(i-1)+di(i-1)) 17 D 1 ( i - 1 ) = g i ( i - 1 ) +sqrt ( g i ( i - 1 ) ^ 2 - 4 * f z ( 3 , i - 1 ) * d i ( i -1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i -1)+fz(3,i-1))) D 2 ( i - 1 ) = g i ( i - 1 ) -sqrt ( g i ( i - 1 ) ^ 2 - 4 * f z ( 3 , i - 1 ) * d i ( i 18 -1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i -1)+fz(3,i-1))) 19 if a bs ( D 1 ( i - 1 ) ) >abs ( D 2 ( i - 1 ) ) then li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1) 20 21 else li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1) 22 23 end 24 hi(i)=li(i)*hi(i-1); 25 z(i-1)=zi(3,i-1)+hi(i) 26 for j = 1 : 2 zi(j,i)=zi(j+1,i-1) 27 28 end zi(3,i)=z(i-1) 29 30 end 31 for i = 1 : 1 2 32 if i = = 1 then printf ( s ( i ) ) 33 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 printf ( ’ \ t \t% . 1 0 f ’ , z i ( i - 1 , j ) ) 40 41 end
67
42 elseif i < = 7 printf ( ’ \n %s ’ , s ( i ) ) 43 44 for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f ’ , f z ( i - 4 , j ) ) 45 46 end elseif i < = 8 47 48 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 49 50 printf ( ’ \ t \t% . 1 0 f ’ , l i ( j ) ) 51 end 52 elseif i < = 9 53 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 54 55 printf ( ’ \ t \t% . 1 0 f ’ , d i ( j ) ) end 56 57 elseif i < = 1 0 printf ( ’ \n %s ’ , s ( i ) ) 58 59 for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f ’ , g i ( j ) ) 60 61 end 62 elseif i < = 1 1 63 printf ( ’ \n %s ’ , s ( i ) ) 64 for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f ’ , z ( j ) ) 65 66 end elseif i < = 1 2 67 68 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 69 70 printf ( ’ \ t \t% . 1 0 f ’ , z i ( j ) ) end 71 72 end 73 end 74 printf ( ’ \n\nAt t he end o f t h e % i i t e r a t i o n , t h e r o o t o f t he e q u a t i on i s %. 1 0 f ’ , j - 2 , z ( j ) )
68
Scilab code Exa 5.22 Mullers Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18
19 20 21 22 23 24
/ / E x am pl e 5 . 2 2 / / M u l l e r s Me th od / / P ag e n o . 1 8 3 clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=xˆ3−x−4 ’ ) zi=[1;2;3]; s =[ ” i ” , ” z 0 ” , ” z 1 ” , ” z 2 ” , ” f0 ” , ” f1 ” , ” f2 ” , ” l i ” , ” d i ” , ” gi ” , ” 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)) D 1 ( i - 1 ) = g i ( i - 1 ) +sqrt ( g i ( i - 1 ) ^ 2 - 4 * f z ( 3 , i - 1 ) * d i ( i -1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i -1)+fz(3,i-1))) D 2 ( i - 1 ) = g i ( i - 1 ) -sqrt ( g i ( i - 1 ) ^ 2 - 4 * f z ( 3 , i - 1 ) * d i ( i -1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i -1)+fz(3,i-1))) if a bs ( D 1 ( i - 1 ) ) >abs ( D 2 ( 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) for j = 1 : 2 26 27 zi(j,i)=zi(j+1,i-1) end 28 29 zi(3,i)=z(i-1) 30 end 31 for i = 1 : 1 6 if i = = 1 then 32 33 printf ( s ( i ) ) 34 for j = 1 : 5 35 printf ( ’ \ t \ t \ t%i ’ , j - 1 ) 36 end elseif i < = 4 37 38 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 39 40 printf ( ’ \ t \t% . 1 0 f ’ , z i ( i - 1 , j ) ) end 41 42 elseif i < = 7 printf ( ’ \n %s ’ , s ( i ) ) 43 44 for j = 1 : 5 45 printf ( ’ \ t \t% . 1 0 f ’ , f z ( i - 4 , j ) ) 46 end 47 elseif i < = 8 printf ( ’ \n %s ’ , s ( i ) ) 48 49 for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f ’ , l i ( j ) ) 50 51 end elseif i < = 9 52 53 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 54 55 printf ( ’ \ t \t% . 1 0 f ’ , d i ( j ) ) 56 end 57 elseif i < = 1 0 58 printf ( ’ \n %s ’ , s ( i ) ) 59 for j = 1 : 5 60 printf ( ’ \ t \t% . 1 0 f ’ , g i ( j ) ) end 61 62 elseif i < = 1 1
70
63 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 64 65 printf ( ’ \ t \t% . 1 0 f end 66 67 elseif i < = 1 2 printf ( ’ \n %s ’ , s ( i ) ) 68 69 for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f 70 71 end 72 elseif i < = 1 3 73 printf ( ’ \n %s ’ , s ( i ) ) 74 for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f 75 76 end elseif i < = 1 4 77 78 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 79 80 printf ( ’ \ t \t% . 1 0 f end 81 82 elseif i < = 1 5 83 printf ( ’ \n %s ’ , s ( i ) ) 84 for j = 1 : 5 85 printf ( ’ \ t \t% . 1 0 f end 86 87 elseif i < = 1 6 printf ( ’ \n %s ’ , s ( i ) ) 88 89 for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f 90 91 end end 92 93 end 94 printf ( ’ \n\nAt t he end o f t he
’ ,li(j+1))
’ ,hi(j))
’ ,hi(j+1))
’ ,z(j))
’ ,D1(j))
’ ,D2(j))
%i th i t e r a t i o n , t he r o o t o f t he e qu a ti o n i s %. 1 0 f ’ , j - 1 , z ( j ) )
71
Scilab code Exa 5.23 Bairstow Hitchcock Method
1 2 3 4 5 6 7 8 9 10 11 12
/ / E x am pl e 5 . 2 3 / / B a i r s t o w H i t c h c o c k Method / / P ag e n o . 1 8 7
clc ; clear ; close ; deff ( ’ y=f (x , p , q ) ’ , ’ y=xˆ2+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
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 13 p ( 1 , 1 ) = - 1 . 2 ; q ( 1 , 1 ) = 0 . 9 5 ; 14 s =[ ” b 1 ” , ” b 2 ” , ” b 3 ” , ” b 4 ” , ” c 1 ” , ” c 2 ” , ” c 3 ” , ” c 4 ” , ” c ” , ”dp” , ”dq”,”p”,”q”] 15 // s1 =[ b1 ; b2 ; b3 ; b4 ; c1 ; c2 ; c3 ; c 4 ; c ; dp ; dq ; p ; q ] 16 for i = 1 : 5 17 b(1,i)=0;b(2,i)=a(1,i);c(1,i)=0;c(2,i)=a(1,i); 18 for k = 1 : 4 b(k+2,i)=a(k+1,i)-p(1,i)*b(k+1,i)-q(1,i)* 19 b(k,i) c(k+2,i)=b(k+2,i)-p(1,i)*c(k+1,i)-q(1,i)* 20 c(k,i) end 21 22 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) 23 ^2-cb(1,i)*c(3,i)) 24 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)) 25 p(1,i+1)=p(1,i)+dp(1,i);q(1,i+1)=q(1,i)+dq(1,i) ; 26 end 27 for j = 1 : 1 3 28 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 < 1 0 for i = 1 : 5 printf ( ’%. 9 f \ t ’ , c b ( 1 , i ) ) end elseif j < 1 1 for i = 1 : 5 printf ( ’%. 9 f \ t ’ , d p ( 1 , i ) ) end elseif j < 1 2 for i = 1 : 5 printf ( ’%. 9 f \ t ’ , d q ( 1 , i ) ) end elseif j < 1 3 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\ nRo ot s f o r Q u ad ra ti c E qu at io n Q = ’ ) disp ( a ) a = roots ( a ) printf ( ’ \n\ t a r e \n ’ ) disp ( a ( 1 ) ) disp ( a ( 2 ) )
73
Scilab code Exa 5.24 Bernoulli Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/ / E x am pl e 5 . 2 4 / / B e r n o u l l i Metho d / / P ag e n o . 1 8 9 clc ; clear ; close ; a=[1,-8,-15,10]; for i = 1 : 2 c(i)=0; end c(3)=1; for k = 4 : 1 3 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 fo r e t h e e xa ct r o o t i s = ’ )
Scilab code Exa 5.25 Graeffe Method
1 2 3 4 5
/ / E x am pl e 5 . 2 5 / / G r a e f f e Me th od / / P ag e n o . 1 9 1 clc ; clear ; close ;
74
6 a=[1,-6,11,-6] 7 k=0; 8 for k = 2 : 6 for i = 1 : 4 9 10 a(k,i)=(-1)^(i-1)*(a(k-1,i))^2 j=1; 11 12 while i +j < 5 & i +j > 2 a(k,i)=a(k,i)+(-1)^(i-j-1)*2*(a(k-1,i-j) 13 )*a(k-1,i+j) 14 break 15 j=j+1; 16 end end 17 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
\ 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 ’ ) 20 printf ( ’ \n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 21 for i = 1 : 4 22 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)))) 23 end 24 for i = 5 : 6 25 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)))) 26 end 27 printf ( ’ \n\nThe A bs o lu te V al ue s o f t he 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
Scilab code Exa 5.26 QD Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/ / E x am pl e 5 . 2 6 / /QD Meth od / / P ag e n o . 1 9 4 clc ; clear ; close ; a=[32,-48,18,-1] 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 : 1 6 for j = 1 : 3 q(i,j)=e(i-1,j+1)+q(i-1,j)-e(i-1,j) end for j = 1 : 2 e(i,j+1)=e(i-1,j+1)*q(i,j+1)/q(i,j) end end printf ( ’ 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 ’ ) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 25 for i = 1 : 1 4 26 for j = 1 : 3 printf ( ’ \ t \t% . 1 0 f \ 27 t ’ ,q(i,j)) 28 end
76
29 printf ( ’ \n ’ ) for j = 1 : 4 30 31 printf ( ’%.1 0 f \ t \ t \ t ’ , e ( i , j ) ) end 32 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 (15,2),q(15,3)) 36 printf ( ’ \nThe e x ac t r o o t s a r e \t% . 1 0 f and %. 1 0 f ’ ,q(15,1),q(15,3))
Scilab code Exa 5.27 Linear Iteration Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E x am pl e 5 . 2 7 / / L i n e a r I t e r a t i o n Method / / P ag e n o . 1 9 8 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 : 1 9 printf ( ’ %i \t% . 1 0 f \t% . 1 0 f \n ’ ,i,x,f(x)) x1=x; x=f(x); end printf ( ’ \n\ n x = % . 1 0 f ’ , x 1 )
77
Scilab code Exa 5.28 Aitkens Method
1 2 3 4 5 6 7 8
/ / E x am pl e 5 . 2 8 / / A i t k e n s M et ho d / / P ag e n o . 1 9 9 clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’x =20/(xˆ2+2∗ x+10) ’ ) printf ( ’ n\ t x 0 \ t \ t x 1 \ t \ t x 2 \ t \ t x 3 \ t \ t y \ t \ t d x 0 \n ’ ) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 x0=1;e=0.0001 10 for i = 1 : 3 x1=f(x0);x2=f(x1);x3=f(x2); 11 12 y=x3-((x3-x2)^2)/(x3-2*x2+x1) dx0=y-x0; 13 14 printf ( ’ %i \t% . 1 0 f \t% 15 . 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) 16 x0=y; 17 if a bs ( x 0 ) < e then 18 break ; end 19 20 end 21 printf ( ’ \n\nThe s o l u t i o n o f t h i s e qu a t io n a f t e r %i I t e r a t i o n s i s %. 1 0 f ’ , i , y )
Scilab code Exa 5.29 Newton Raphson Method
1 / / E x am pl e 5 . 2 9 2 //Newton Raphson Method 3 / / P ag e n o . 1 9 9
78
4 5 6 7
8
clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=xˆ3+2 ∗ xˆ2+10∗x−20 ’ ) deff ( ’ x= f 1 ( x ) ’ , ’ x=3∗xˆ2+4∗x+10 ’ ) printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 x0=01;e=0.00001 10 for i = 1 : 4 11 x1=x0-f(x0)/f1(x0) 12 e1 = abs ( x 0 - x 1 ) printf ( ’ %i \t% . 1 0 f \t% 13 . 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; 14 15 if a bs ( x 0 ) < e then break ; 16 17 end 18 end 19 printf ( ’ \n\nThe s o l u t i o n o f t h i s e qu a t io n a f t e r %i I t e r a t i o n s i s %. 1 0 f ’ , i , x 1 )
Scilab code Exa 5.31 Secant Method
1 2 3 4 5
/ / E x am pl e 5 . 3 1 / / S e c a n t M et ho d / / P ag e n o . 2 0 0
clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=(x − 0 . 6 ) ∗ ( x − 1.3 ) ˆ2 ∗ ( x −2 ) ˆ 3 + 0 . 0 1 2 3 4∗ log (x) ’ ) 6 printf ( ’ n\ t x n \ t \ t f ( x n ) \ t \ tXn+1\ t \ t f (Xn+1)\ t \tXn+2\ t \ t E r r o r \n ’ ) 7 printf ( ’
79
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 8 x0=0.1;x1=1.2;e=0.00001 9 for i = 1 : 7 10 x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0)) e1 = abs ( x 0 - x 2 ) 11 12 . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \ printf ( ’ %i \t% . 1 0 f \t% t% . 1 0 f \n ’ ,i-1,x0,f(x0),x1,f(x1),x2,e1) 13 x0=x1; 14 x1=x2 15 if a bs ( x 0 ) < e then 16 break ; end 17 18 end 19 printf ( ’ \n\ n Th er ef or e , t he r o o t i s %. 1 0 f ’ , x 2 )
Scilab code Exa 5.32 Regula Falsi Newton Raphson and Mullers Method
1 2 3 4 5
/ / E x am pl e 5 . 3 2 / / R e gu l a F a l s i , Newton R ap hs on a nd M u l l e r s Method / / P ag e n o . 2 0 1
clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=x ˆ5 −3.7∗ x ˆ 4 + 7 . 4∗ x ˆ 3 −1 0 . 8∗ x ˆ 2 + 1 0 . 8∗ x −6.8 ’ ) 6 deff ( ’ x= f 1 ( x ) ’ , ’ x=5∗x ˆ 4 −4∗3 .7∗ x ˆ 3 + 3∗7 . 4 ∗ x ˆ 2 −21.6 ∗ x +10.8 ’ ) 7 / / n ew to n r a p h s o n 8 printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) 9 printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 10 x 0 = 1 . 5 ; e = 0 . 0 0 0 0 1
80
11 for i = 1 : 4 x1=x0-f(x0)/f1(x0) 12 13 e1 = abs ( x 0 - x 1 ) printf ( ’ %i \t% . 1 0 f \t% 14 . 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; 15 16 if a bs ( x 0 ) < e then break ; 17 18 end 19 end 20 printf ( ’ \n\nThe s o l u t i o n o f t h i s e q u a ti 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 x 1 = 1 ; x 2 = 2 ; e = 0 . 0 0 0 0 1 24 printf ( ’ n\ t x 1 \ t \ t f ( x 1 ) \ t \ t x 2 \ t \ t f ( x 2 ) \ t \ t x 3 \ t \ t f ( x 3 ) ’) 25 printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 26 for i = 0 : 7 27 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 , x 1 , f ( x 1 28 ),x2,f(x2),x3,f(x3)) if f ( x 1 ) * f ( x 3 ) > 0 then 29 30 x1=x3 else 31 32 x2=x3 end 33 34 if a bs ( f ( x 3 ) ) < e then 35 break 36 end 37 end 38 printf ( ’ \n\ n T he r ef o re t he 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 , x 3 ) 39 40 / / m u l l e r s m et ho d
81
41 z i = [ 1 ; 2 ; 3 ] ; 42 s =[ ” i ” , ” z 0 ” , ” z 1 ” , ” z 2 ” , ” f0 ” , ” f1 ” , ” f2 ” , ” l i ” , ” d i ” , ” gi ” , ” l i +1” , ” h i ” , ” h i + 1 ” , ” z i +1” , ”D+” , ” D ” ] 43 l i ( 1 ) = ( z i ( 3 , 1 ) - z i ( 2 , 1 ) ) / ( z i ( 2 , 1 ) - z i ( 1 , 1 ) ) 44 h i ( 1 ) = z i ( 3 , 1 ) - z i ( 2 , 1 ) ; 45 for i = 2 : 6 46 for j = 1 : 3 fz(j,i-1)=f(zi(j,i-1)) 47 48 end 49 di(i-1)=1+li(i-1) 50 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)) D 1 ( i - 1 ) = g i ( i - 1 ) +sqrt ( g i ( i - 1 ) ^ 2 - 4 * f z ( 3 , i - 1 ) * d i ( i 51 -1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i -1)+fz(3,i-1))) 52 D 2 ( i - 1 ) = g i ( i - 1 ) -sqrt ( g i ( i - 1 ) ^ 2 - 4 * f z ( 3 , i - 1 ) * d i ( i -1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i -1)+fz(3,i-1))) if a bs ( D 1 ( i - 1 ) ) >abs ( D 2 ( i - 1 ) ) then 53 54 li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1) 55 else 56 li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1) 57 end hi(i)=li(i)*hi(i-1); 58 59 z(i-1)=zi(3,i-1)+hi(i) for j = 1 : 2 60 61 zi(j,i)=zi(j+1,i-1) end 62 63 zi(3,i)=z(i-1) 64 end 65 printf ( ’ \n\n ’ ) 66 for i = 1 : 1 6 67 if i = = 1 then 68 printf ( s ( i ) ) 69 for j = 1 : 5 70 printf ( ’ \ t \ t \ t %i ’ ,j-1) end 71 72 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 end elseif i < = 7 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f end elseif i < = 8 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f end elseif i < = 9 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f end elseif i < = 1 0 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f end elseif i < = 1 1 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f end elseif i < = 1 2 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f end elseif i < = 1 3
83
’ ,zi(i-1,j))
’ ,fz(i-4,j))
’ ,li(j))
’ ,di(j))
’ ,gi(j))
’ ,li(j+1))
’ ,hi(j))
109 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 110 111 printf ( ’ \ t \t% . 1 0 f end 112 113 elseif i < = 1 4 printf ( ’ \n %s ’ , s ( i ) ) 114 115 for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f 116 117 end 118 elseif i < = 1 5 119 printf ( ’ \n %s ’ , s ( i ) ) 120 for j = 1 : 5 printf ( ’ \ t \t% . 1 0 f 121 122 end elseif i < = 1 6 123 124 printf ( ’ \n %s ’ , s ( i ) ) for j = 1 : 5 125 126 printf ( ’ \ t \t% . 1 0 f end 127 128 end 129 end 130 printf ( ’ \n\nAt t he end o f t h e
’ ,hi(j+1))
’ ,z(j))
’ ,D1(j))
’ ,D2(j))
%i th i t e r a t i o n by m u l l e r s method , t he r o o t o f t he e q ua t io n i s %. 1 0 f ’ ,j-1,z(j))
Scilab code Exa 5.33 Newton Raphson and Mullers Method
1 2 3 4 5 6
/ / E x am pl e 5 . 3 3 / / N e wt on R ap hs on a nd M u l l e r s M et ho d / / P ag e n o . 2 0 2 clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=xˆ4 −8∗xˆ3+18∗ x ˆ 2 + 0 . 1 2∗ x − 24.24 ’ ) deff ( ’ x= f 1 ( x ) ’ , ’ x=4∗x ˆ3 −24∗ xˆ2+36∗ x + 0 . 1 2 ’ )
84
7 8 9 10 11 12 13 14 15 16 17 18 19 20
/ / n ew to n 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 : x 9 ( 2 , h ) x1=x0-f(x0)/f1(x0) e1 = abs ( x 0 - x 1 ) x0=x1; if a bs ( x 0 ) < 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 ti 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 m et ho d 24 z x = [ 1 , 2 , 2 . 7 , 3 . 1 ; 2 , 3 , 3 . 7 , 4 . 1 ; 3 , 4 , 4 . 7 , 5 . 1 ] 25 z i = [ 1 ; 2 ; 3 ] ; 26 s =[ ” i ” , ” z 0 ” , ” z 1 ” , ” z 2 ” , ” f0 ” , ” f1 ” , ” f2 ” , ” l i ” , ” d i ” , ” gi ” , ” l i +1” , ” h i ” , ” h i + 1 ” , ” z i +1” , ”D+” , ” D ” ] 27 l i ( 1 ) = ( z i ( 3 , 1 ) - z i ( 2 , 1 ) ) / ( z i ( 2 , 1 ) - z i ( 1 , 1 ) ) 28 h i ( 1 ) = z i ( 3 , 1 ) - z i ( 2 , 1 ) ; 29 for i = 2 : 4 for j = 1 : 3 30 31 fz(j,i-1)=f(zi(j,i-1)) end 32 33 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+ 34 fz(3,i-1)*(li(i-1)+di(i-1)) 35 D 1 ( i - 1 ) = g i ( i - 1 ) +sqrt ( g i ( i - 1 ) ^ 2 - 4 * f z ( 3 , i - 1 ) * d i ( i -1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i -1)+fz(3,i-1))) 36 D 2 ( i - 1 ) = g i ( i - 1 ) -sqrt ( g i ( i - 1 ) ^ 2 - 4 * f z ( 3 , i - 1 ) * d i ( i -1)*li(i-1)*(fz(1,i-1)*li(i-1)-fz(2,i-1)*di(i -1)+fz(3,i-1))) 37 if a bs ( D 1 ( i - 1 ) ) >abs ( D 2 ( i - 1 ) ) then
85
38 li(i)=-2*fz(3,i-1)*di(i-1)/D1(i-1) else 39 40 li(i)=-2*fz(3,i-1)*di(i-1)/D2(i-1) end 41 42 hi(i)=li(i)*hi(i-1); z(i-1)=zi(3,i-1)+hi(i) 43 44 for j = 1 : 2 zi(j,i)=zi(j+1,i-1) 45 46 end 47 zi(3,i)=z(i-1) 48 end 49 printf ( ’ \n\nAt t he end o f t h e %i th i t e r a t i o n
by m u l l e r s method , t he r o o t o f t he e q ua t io n i s %. 1 0 f ’ ,j+2,z(j))
Scilab code Exa 5.34 QD Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/ / E x am pl e 5 . 3 4 / /QD Meth od / / P ag e n o . 2 0 2
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 for j = 1 : 2 18 19 e(i,j+1)=e(i-1,j+1)*q(i,j+1)/q(i,j) end 20 21 end 22 printf ( ’ 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 ’ ) 23 printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 24 for i = 1 : 7 25 for j = 1 : 3 26 t ’ ,q(i,j)) printf ( ’ \ t \t% . 1 0 f \ end 27 28 printf ( ’ \n ’ ) for j = 1 : 4 29 30 printf ( ’%.1 0 f \ t \ t \ t ’ , e ( i , j ) ) end 31 32 printf ( ’ \n ’ ) 33 end 34 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)) 35 printf ( ’ \nThe e x ac t r o o t s a r e \t% . 1 0 f and %. 1 0 f ’ ,q(7,1),q(7,3))
Scilab code Exa 5.35 Newton Raphson Method
1 2 3 4 5 6 7
/ / E x am pl e 5 . 3 5 //Newton Raphson Method / / P ag e n o . 2 0 3 clc ; clear ; close ; deff ( ’x=f ( x ) ’ , ’ x=xˆ3 −30∗ xˆ2+2552 ’ ) deff ( ’ x= f 1 ( x ) ’ , ’ x=3∗x ˆ2 −60∗ x ’ )
/ / n ew to n r a p h s o n 87
8 printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) 9 printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 10 x 0 = 1 0 ; e = 0 . 0 0 0 0 1 11 for i = 1 : 4 x1=x0-f(x0)/f1(x0) 12 13 e1 = abs ( x 0 - x 1 ) 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) 15 x0=x1; if a bs ( x 0 ) < e then 16 17 break ; end 18 19 end 20 printf ( ’ \n\ nThus t he b a l l i s su bmerg ed u pt o h e i g h t o f %. 1 0 f cm\n\n\n ’ , x 1 )
Scilab code Exa 5.36 Secant Method
1 2 3 4 5 6
/ / E x am pl e 5 . 3 6 / / S e c a n t M et ho d / / P ag e n o . 2 0 4 clc ; clear ; close ; a=8670;c=10^-8;t2=1.4*10^-4; deff ( ’x=f ( x ) ’ , ’ x=−t2+lo g ((1 −2∗ x / a ) / ( 2 − x / a ) ) ∗ ( a ∗ x ∗ c ) /( a+x ) ’ )
7 8 printf ( ’ n\ t x n \ t \ t f ( x n ) \ t \ tXn+1\ t \ t f (Xn+1)\ t \tXn+2\ t \ t E r r o r \n ’ ) 9 printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 88
n ’) 10 x 0 = 2 0 0 0 0 ; x 1 = 2 5 0 0 0 ; e = 0 . 0 0 0 0 1 11 for i = 1 : 8 x2=x1-f(x1)*(x1-x0)/(f(x1)-f(x0)) 12 13 e1 = abs ( x 0 - x 2 ) printf ( ’ %i \ t%f \t% . 1 0 f \ 14 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; 15 16 x1=x2 17 if a bs ( x 0 ) < e then 18 break ; 19 end 20 end 21 printf ( ’ \n\ n T h e r e f o r e , Rb = %. 1 0 f ohm ’ , x 2 )
Scilab code Exa 5.37 Newton Raphson Method
1 2 3 4 5 6 7 8
/ / E x am pl e 5 . 3 7 //Newton Raphson Method / / P ag e n o . 2 0 4
9
clc ; clear ; close ; p=1.1;T=250;R=0.082;a=3.6;b=0.043; deff ( ’y=f ( v ) ’ , ’ y=p∗ vˆ3 −(b ∗p+R∗T) ∗ vˆ2+a ∗v−a ∗b ’ ) deff ( ’ y= f 1 ( v ) ’ , ’ y=3∗p∗ v ˆ2 −2∗( b ∗p+R∗T) ∗ v ’ ) printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 10 x 0 = 0 . 1 ; e = 0 . 0 0 0 0 1 11 for i = 1 : 1 0 12 x1=x0-f(x0)/f1(x0) e1 = abs ( x 0 - x 1 ) 13
89
14
. 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n printf ( ’ %i \t% . 1 0 f \t% ’ ,i-1,x0,f(x0),f1(x0),x1,e1) x0=x1; if a bs ( x 0 ) < e then break ; end
15 16 17 18 19 end 20 printf ( ’ \n\ n T h e re f o r e , Volume v = %. 1 0 f
l t r ’ ,x1)
Scilab code Exa 5.38 Newton Raphson Method
1 2 3 4 5 6 7
/ / E x am pl e 5 . 3 8 //Newton Raphson Method / / P ag e n o . 2 0 5
8
clc ; clear ; close ; deff ( ’ y=f ( p) ’ , ’ y=pˆ3 −9∗pˆ2+33∗p−65 ’ ) deff ( ’y=f1 (p ) ’ , ’ y=3∗pˆ2 −18∗p+33 ’ ) printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 x0=6;e=0.00001 10 for i = 1 : 1 0 11 x1=x0-f(x0)/f1(x0) e1 = abs ( x 0 - x 1 ) 12 13 . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n printf ( ’ %i \t% . 1 0 f \t% ’ ,i-1,x0,f(x0),f1(x0),x1,e1) 14 x0=x1; 15 if a bs ( x 0 ) < e then break ; 16 17 end 18 end
90
19 printf ( ’ \n\ n Th er ef or e , Market P r i c e a t e q u i l i b r i u m = Rs . %. f ’ , x 1 )
Scilab code Exa 5.39 Newton Raphson Method
1 2 3 4 5 6 7
/ / E x am pl e 5 . 3 9 //Newton Raphson Method / / P ag e n o . 2 0 5
8
clc ; clear ; close ; deff ( ’y=f ( v ) ’ , ’ y=vˆ3 −20∗ v+30 ’ ) deff ( ’ y= f 1 ( v ) ’ , ’ y=3∗vˆ2 −20 ’ ) printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 x0=10;e=0.00001 10 for i = 1 : 1 0 11 x1=x0-f(x0)/f1(x0) e1 = abs ( x 0 - x 1 ) 12 13 . 1 0 f \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n printf ( ’ %i \t% . 1 0 f \t% ’ ,i-1,x0,f(x0),f1(x0),x1,e1) 14 x0=x1; if a bs ( x 0 ) < e then 15 16 break ; end 17 18 end 19 printf ( ’ \n\ n Th er ef or e , s i d e s a r e = %. 5 f m x %. 5 f m x %. 5 f m ’ , x 1 , x 1 , 2 0 / x 1 ^ 2 )
91
Scilab code Exa 5.40 Newton Raphson Method
1 2 3 4 5 6 7
/ / E x am pl e 5 . 4 0 //Newton Raphson Method / / P ag e n o . 2 0 6
8
clc ; clear ; close ; deff ( ’ y=f (F) ’ , ’ y=−10∗Fˆ 3 −21 ∗F+10 ’ ) deff ( ’ y=f1 (F) ’ , ’ y=−21−30∗Fˆ2 ’ ) printf ( ’ n\ t x n \ t \ t \ f ( xn ) \ t \ t f 1 ( x n ) \ t \tXn+1\ t \ t E r r o r \n ’) printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 x0=1;e=0.00001 10 for i = 1 : 1 0 11 x1=x0-f(x0)/f1(x0) e1 = abs ( x 0 - x 1 ) 12 13 . 6 f \t% . 5 f \t% . 1 0 f \t% . 1 0 f \n ’, printf ( ’ %i \t% . 1 0 f \t% i-1,x0,f(x0),f1(x0),x1,e1) 14 x0=x1; if a bs ( x 0 ) < e then 15 16 break ; 17 end 18 end 19 printf ( ’ \n\ t \ t \ t \ t \ t 2 \n T h er e fo r e , M ag ne ti c F lu x = % . 5 f Wb m’ , x 1 )
92
Chapter 6 Numerical Methods of Linear Equations Direct Methods
Scilab code Exa 6.1 Gaussian Elimination Method
1 2 3 4 5 6
/ / E xa mp le 6 . 1 / / G a u s s i an E l i m i n a t i o n Method / / P ag e n o . 2 2 0 clc ; clear ; close ; A=[5,10,1,28;1,1,1,6;4,8,3,29];
a u gm e nt ed m a t r i x 7 8 9 10 11 12 13 14 15 16
// t r i a n g u l a r i z a t i o n for i = 1 : 4 B(1,i)=A(1,i) B(2,i)=A(2,i)-(A(2,1)/A(1,1))*A(1,i) B(3,i)=A(3,i)-(A(3,1)/A(1,1))*A(1,i) end disp (A , ’ A ug me nt ed M a t r i x = ’ ) disp (B , ’ T r i a n g u l a t e d M at ri x= ’ )
/ / ba ck s u b s t i t u t i o n 93
//
17 18 19 20 21 22 23 24 25 26
x(3)=B(3,4)/B(3,3); printf ( ’ \ nx ( 3 )=%f \ n ’ , x ( 3 ) ) for i = 2 : - 1 : 1 k =0 for j = i + 1 : 3 k=k+B(i,j)*x(j) end x(i)=(1/B(i,i))*(B(i,4)-k) printf ( ’ \ nx(%i)=%f \ n ’ , i , x ( i ) ) end
Scilab code Exa 6.2 Gaussian Elimination Method for TriDiagonal Sys-
tem 1 / / E xa mp le 6 . 2 2 / / G a u ss i an E l i m i n a t i o n Method f o r T ri −D i a g o n a l
System 3 / / P ag e n o . 2 2 2 4 5 6 7 8 9 10 11 12 13
clc ; clear ; close ;
/ / e q u a t i o n m a t ri 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 w(i)=(A(i,i+1))/(A(i,i)-A(i,i-1)*w(i-1)) 17
94
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
g(i)=(K(i)-A(i,i-1)*g(i-1))/(A(i,i)-A(i,i-1)*w(i -1)) printf ( ’ \nw(%i)=%f ’ , i , w ( i ) ) printf ( ’ \ ng ( %i)=%f ’ , i , g ( i ) )
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)) x(i)=g(i) printf ( ’ \ ng ( %i)=%f ’ , i , g ( i ) ) printf ( ’ \n\ nx ( %i)=%f ’ , i , x ( i ) )
// sol uti on for i = 3 : - 1 : 1 x(i)=g(i)-w(i)*x(i+1) printf ( ’ \n\ nx ( %i)=%f ’ , i , x ( i ) ) end
Scilab code Exa 6.3 Gauss Jordan Method
1 2 3 4 5 6 7
/ / E xa mp le 6 . 3 //Gauss−J o r d a n M et ho d / / P ag e n o . 2 2 4 clc ; clear ; close ; A=[5,10,1,28;4,8,3,29;1,1,1,6];
matrix 8 9 for i = 1 : 3 10 j=i while ( A (i , i ) == 0 & j < = 3) 11
95
//augmented
12 for k = 1 : 4 B(1,k)=A(j+1,k) 13 14 A(j+1,k)=A(i,k) A(i,k)=B(1,k) 15 16 end disp ( A ) 17 18 j=j+1 end 19 20 disp ( A ) 21 for k = 4 : - 1 : i 22 A(i,k)=A(i,k)/A(i,i) 23 end disp ( A ) 24 25 for k = 1 : 3 if ( k ~ = i ) then 26 27 l=A(k,i)/A(i,i) for m = i : 4 28 29 A(k,m)=A(k,m)-l*A(i,m) end 30 31 end 32 33 end 34 disp ( A ) 35 end 36 37 for i = 1 : 3 38 printf ( ’ \ n x ( % i ) = % g\n ’ , i , A ( i , 4 ) ) 39 end
Scilab code Exa 6.4 Gaussian Elimination Method without Pivoting
1 / / E xa mp le 6 . 4 2 / / G a u ss i an E l i m i n a t i o n Method w i th o ut P i v o t i n g
96
3 / / P ag e n o . 2 2 7 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 , ’ A ug me nt ed M a t r i x = ’ ) disp (B , ’ T r i a n g u l a t e d M at ri x= ’ )
/ / ba ck 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
Scilab code Exa 6.5 Dolittle Factorization Method
1 2 3 4 5
/ / E xa mp le 6 . 5 // D o l i t t l e F a c t o r i z a t i o n Method / / P ag e n o . 2 3 3 clc ; clear ; close ;
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
A=[2,1,1;1,3,1;1,1,4]; 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 for j = 1 : 3 printf ( ’%. 2 f \ t ’ , L ( i , j ) ) end
if ( i = = 2 ) printf ( ’ ∗ else printf ( ’ \ t ’ ) end
’)
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 ) ) end 45 46 printf ( ’ \n ’ ) 47 end
Scilab code Exa 6.6 Trangularization Method
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
/ / E xa mp le 6 . 6 / / T r a n g u l a r i z a t i o n Method / / P ag e n o . 2 3 6 clc ; clear ; close ;
A=[2,1,1;1,3,1;1,1,4]; B=[7;10;15]; printf ( ’A c an 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 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
for j = 1 : 3 printf ( ’%. 2 f \ t ’ , L ( i , j ) ) end
if ( i = = 2 ) printf ( ’ ∗ else printf ( ’ \ t ’ ) end
’)
for j = 1 : 3 printf ( ’%. 2 f \ t ’ , U ( i , j ) ) end if ( i = = 2 ) printf ( ’ = ’) else printf ( ’ \ t ’ ) end for j = 1 : 3 printf ( ’%. 2 f \ t ’ , A ( i , j ) ) end printf ( ’ \n ’ )
end printf ( ’ \nY=U∗X ’ ) Y = inv ( L ) * B X = inv ( U ) * Y printf ( ’ \n\nX= ’ ) for i = 1 : 3 %i ’ , X ( i , 1 ) ) printf ( ’ \n end
Scilab code Exa 6.7 Wilkinson Method
100
1 2 3 4 5 6
/ / E xa mp le 6 . 7 / / W i l k i n s o n M et ho d / / P ag e n o . 2 4 0 clc ; clear ; close ; A = [ 0 . 3 * 1 0 ^ 5 , 0 . 2 1 2 , 0 . 3 3 2 ; 0 . 2 1 6 , 0 . 3 7 6 , 0 . 4 7 7 ; 0 . 1 7 3 , 0 . 6 63 , 0 . 6 2 6 ] ;
7 B=[0.235;0.128;0.285]; 8 X = inv ( A ) 9 disp ( X * B , ’ F i na l S o l u ti o n = ’ )
Scilab code Exa 6.8 Choleskys Factorization
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/ / E xa mp le 6 . 8 / / Ch ol es ky ’ s F a c t o r i z a t i o n / / P ag e n o . 2 4 3 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); end 21 22 U(i,j)=(A(i,j)-k)/U(i,i) end 23 24 end 25 end 26 disp (U , ’ R e q u i r e d M a t ri x (U)= ’ )
Scilab code Exa 6.9 Complex System of Linear Equation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E xa mp le 6 . 9 / / C omplex S ys te m o f L i n e a r E q ua t io n / / P ag e n o . 2 4 4 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 ti o n = ’ )
Scilab code Exa 6.10 Solving Matrices
1 / / E x am pl e 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 ce s / / P ag e n o . 2 4 4 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 ( 3 6 0 3 6 0 * X , ’ The S o l u t i o n by 3 60 36 0∗X= ’ ) disp (X , ’ F i na l S o l u ti o n = ’ )
103
Chapter 7 Numerical Solutions for Matrix Inversion
Scilab code Exa 7.1 Gauss Jordan Two Array Method
1 2 3 4 5 6 7 8
/ / E xa mp le 7 . 1 //Gauss−J o r d a n Two A r r a y M et ho d / / P ag e n o . 2 5 4 clc ; clear ; close ; A=[2,6,1;3,9,2;0,-1,3]; C = eye ( 3 , 3 ) ; for i = 1 : 3
// matr ix / / U n it M a tr i x // i n t e r ch a n g e o f row 1
a nd 2 9 B(1,i)=A(1,i); 10 A(1,i)=A(2,i); 11 A(2,i)=B(1,i); 12 B(2,i)=C(1,i); C(1,i)=C(2,i); 13 14 C(2,i)=B(2,i); 15 end 16 printf ( ’ \n ’ )
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 ’ ) ;
// i n t e r ch a n g e o f row 2 and 3 for i = 1 : 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 at ri x o f A ’ )
Scilab code Exa 7.2 Inverse in Place without Pivoting
1 2 3 4 5 6 7 8
/ / E xa mp le 7 . 2 / / I n v e r s e i n P l ac e w it ho ut P i v o ti n g / / P ag e n o . 2 5 6 clc ; clear ; close ;
// matr ix / / c o p i e d m a t ri x
A=[3,-6,7;9,0,-5;5,-8,6]; B=[3,-6,7;9,0,-5;5,-8,6]; for i = 1 : 3
108
9 printf ( ’ \n\ n S t a g e %i ’ , i ) ; for j = 1 : 3 10 11 if ( i = = j ) B(i,j)=1/B(i,j); 12 13 else B(i,j)=A(i,j)/A(i,i); 14 15 end , end 16 17 disp ( B ) 18 for j = 1 : 3 19 for k = 1 : 3 20 if ( i ~ = j ) B(j,k)=A(j,k)-A(j,i)*B(i,k); 21 22 end , end 23 24 end disp ( B ) 25 26 for j = 1 : 3 if ( i ~ = j ) 27 28 B(j,i)=-1*A(j,i)*B(i,i); 29 end , 30 31 end disp ( B ) 32 33 A=B; 34 end 35 disp (B , ’ I n v e r s e o f M at ri x A= ’ )
Scilab code Exa 7.3 Inverse in Place with Pivoting
1 / / E xa mp le 7 . 3 2 / / I n v e r s e i n P l ac e w it h P i v ot i n g 3 / / P ag e n o . 2 5 8
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
clc ; clear ; close ;
// matr ix / / c o p i e d m a t ri x
A=[3,-6,7;9,0,-5;5,-8,6]; B=[3,-6,7;9,0,-5;5,-8,6];
for i = 1 : 3 printf ( ’ \n\ n S t a g e %i ’ ,i ) if ( i < 3 ) / / i n t e r c h a n g e o f r ow s for j = 1 : 3 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 c olu mn 2 and 3 C(j,1)=A(j,2); A(j,2)=A(j,3); A(j,3)=C(j,1); end / / i n t e r c h a n g e o f c olu mn 2 and 1 for j = 1 : 3 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 at ri x A= ’ )
Scilab code Exa 7.4 Inverse of Triangular Matrices
1 2 3 4 5 6
/ / E xa mp le 7 . 4 / / I n v e r s e o f T r i an g u l ar M a tr i ce s / / P ag e n o . 2 6 0 clc ; clear ; close ; R=[2,4,-4,0;0,3,-3,-3;0,0,4,2;0,0,0,3];
R 7 for i = 4 : - 1 : 1 for j = 4 : - 1 : 1 8 9 if ( i > j ) 10 Y(i,j)=0; 11 end 12 if ( i = = j ) Y(i,j)=1/R(i,j); 13 14 end if ( i < j ) 15
111
// matr ix
16 l=0; for k = i + 1 : j 17 18 l=l-R(i,k)*Y(k,j); end 19 20 Y(i,j)=l/R(i,i); end 21 22 end 23 end 24 disp (Y , ’ I n v e r s e o f M at ri x R= ’ )
Scilab code Exa 7.5 Inverse of Complex Matrices
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/ / E xa mp le 7 . 5 / / I n v e r s e o f Complex M a t r i c e s / / P ag e n o . 2 6 2 clc ; clear ; close ;
17 18 19
A=[1,-1,0;2,3,4;0,1,2]; B=[1,1,3;1,3,-3;-2,-4,-4]; P=A+%i*B; disp (P , ’ Matri x P= ’ ) disp (A , ’ Matri x A= ’ ) ; disp (B , ’ Matri x B= ’ ) ; A1 = inv ( A ) ; B 1 =inv ( B ) ; disp ( A 1 , ’ I n v e r s e o f M at ri x A= ’ ) ; disp ( B 1 , ’ I n v e r s e o f M at ri x B= ’ ) ; B 1 A = B 1 * A ; disp ( B 1 A , ’ I n v e r s e ( B) ∗A= ’ ) ; A B 1 A _ B = A * B 1 A + B ;disp (AB1A_B , ’ A∗ I n v e r s e ( B) ∗A+B= ’ ) ; A B 1 A _ B 1 = inv ( A B 1 A _ B ) ;disp (AB1 A_B1 , ’ I n v e r s e ( A∗ I n v e r s e ( B) ∗A+B)= ’ ) ; X = B 1 A * A B 1 A _ B 1 ;disp (X , ’X= ’ ) ; Y = - 1 * A B 1 A _ B 1 ;disp (Y , ’Y= ’ ) ; Q = X + % i * Y ; disp (Q , ’ I n v e r s e o f M at ri x P= ’ )
112
Scilab code Exa 7.6 Iterative Procedure
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
/ / E xa mp le 7 . 6 // I t e r a t i v e P ro ce du re / / P ag e n o . 2 6 5 clc ; clear ; close ;
A=[3,1,3/2;-5/4,-1/4,-3/4;-1/4,-1/4,-1/4]; disp (A , ’ Matri x A= ’ ) ; B=[1,1,3.5;1,3,-3;-2,-3,-4]; disp (B , ’ A ss um ed 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 ( E 1 > = 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 , ’ Matri x C= ’ ) ; E = A * C - eye ( 3 , 3 ) ; disp (E , ’ Matri x E= ’ ) ; B = C ; printf ( ’ \ n I n v e rs e o f M a tr 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 ( E 1 ) ; k=k+1; end
113
Chapter 8 Numerical Solutions of Linear Systems of Equations Iterative Methods
Scilab code Exa 8.1 Jacobi Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E xa mp le 8 . 1 / / J a c o b i M et ho d / / P ag e n o . 2 7 3 clc ; clear ; close ; A=[8,-3,2;4,11,-1;6,3,12]; B=[20;33;36] for i = 0 : 1 9 X(i+1,1)=i; end for i = 2 : 4 X(1,i)=0; end for r = 1 : 1 9 for i = 1 : 3
114
/ / e q u a t i o n m a t ri x / / s o l u t i o n m a tr i x
16 17 18 19 20 21 22 23 24 25 26 27 28
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);
end
end printf ( ’ r\t x ( r ) \ t \ ty ( r ) \ t z ( r ) ’ ); printf ( ’ \n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’ ) disp ( X ) printf ( ’ \n\ n A ft e r 18 i t e r a t i o n s e x ac t s o l u t i o n i s : \ nx=%i\ ty=%i\ t z=%i ’ , X ( 1 9 , 2 ) , X ( 1 9 , 3 ) , X ( 1 9 , 4 ) )
Scilab code Exa 8.2 Gauss Seidel Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/ / E xa mp le 8 . 2 //Gauss−S e i d e l Metho d / / P ag e n o . 2 7 4 clc ; clear ; close ; A=[8,-3,2;4,11,-1;6,3,12]; B=[20;33;36] for i = 0 : 1 0 X(i+1,1)=i; end for i = 2 : 4 X(1,i)=0; end for r = 1 : 1 0 for i = 1 : 3 k1=0;
115
/ / e q u a t i o n m a t ri x / / s o l u t i o n m a tr i x
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
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);
end
end printf ( ’ r\t x ( r ) \ t \ ty ( r ) \ t z ( r ) ’ ); printf ( ’ \n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’ ) disp ( X ) printf ( ’ \n\ n A ft e r 9 i t e r a t i o n s e xa ct s o l u t i o n i s : \ nx =%i \ ty=%i\ tz=%i ’ , X ( 1 0 , 2 ) , X ( 1 0 , 3 ) , X ( 1 0 , 4 ) )
Scilab code Exa 8.3 SOR Method
1 2 3 4 5 6 7 8 9 10 11
/ / E xa mp le 8 . 3 //SOR Method / / P ag e n o . 2 7 5 clc ; clear ; close ; A=[5,2,1;-1,4,2;2,-3,10]; B=[-12;20;3]; w=0.9; for i = 0 : 1 3 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 : 1 3 for i = 1 : 3 k1=0; for j = 1 : i - 1
end k2=0; for j = i + 1 : 3
k1=k1-A(i,j)*X(r+1,j+1);
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 ft e r 12 i t e r a t i o n s e x ac t s o l u t i o n i s : \ nx=%i\ ty=%i\ t z=%i ’ , X ( 1 3 , 2 ) , X ( 1 3 , 3 ) , X ( 1 3 , 4 ) ) ;
Scilab code Exa 8.4 Gauss Seidel Point Iterative Method
1 / / E xa mp le 8 . 4 2 //Gauss−S e i d e l P oi nt 3 / / P ag e n o . 2 7 8
I t e r a t i v e Method
117
4 clc ; clear ; close ; 5 6 7 A = [ 1 0 , 1 , 0 , 0 , 0 , - 1 ; 1 , 1 0 , 1 , 0 , 0 , 0 ; 2 , 0 , 2 0 , 1 , 0 , 0 ; 0 , 0 , 1 , 1 0 ,- 1 , 0 ; 0 , 3 , 0 , 0 , 3
/ / e q u a t i o n m a t ri x // sol uti on
8 B=[5;10;10;0;0;5]
matrix 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
for i = 1 : 6 for j = 1 : 6 if ( A ( j , j ) = = 0 ) for k = 1 : 6 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 : 7 X(i+1,1)=i; end for i = 2 : 7 X(1,i)=0; end for r = 1 : 7 for i = 1 : 6 k1=0; for j = 1 : i - 1
k1=k1-A(i,j)*X(r+1,j+1); end k2=0; for j = i + 1 : 6
k2=k2-A(i,j)*X(r,j+1);
118
39 40 41 42 43 44 45 46 47
end X(r+1,i+1)=(k1+k2+B(i,1))/A(i,i);
end
end r printf ( ’ for i = 1 : 6 printf ( ’ x%i end printf ( ’ \n
’ ); ’ ,i);
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 48 disp ( X ) 49 printf ( ’ \n\ n A ft e r 6 i t e r a t i o n s e xa ct s o l u t i o n ); 50 for i = 1 : 6 51 ’ ,i,X(7,i+1)); printf ( ’ x%i=%f 52 end
is :\n’
Scilab code Exa 8.5 Gauss Seidel Point Iterative Method
1 2 3 4 5 6
/ / E xa mp le 8 . 5 //Gauss−S e i d e l P oi nt I t e r a t i v e Method / / P ag e n o . 2 7 9 clc ; clear ; close ; A=[2,3,-4,1;1,-2,-5,1;5,-3,1,-4;10,2,-1,2];
e q u a t i on m a tr i x 7 B=[3;2;1;-4]; / / s o l u t i o n m a tr i x 8 9 / / t r a n s f o rm a t i o n o f t he e q u a t io n s 10 for i = 1 : 4 11 A1(1,i)=A(4,i); B1(1,1)=B(4,1); 12
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 an s fo r me d e q u a t io n s printf ( ’ \ n T ra n sf or m ed E q u a t io n s a r e =\n\n ’ ) for i = 1 : 4 for j = 1 : 4 printf ( ’ ( %ix( %i) ) ’ , A 1 ( i , j ) , j ) ; if ( j < 4 ) printf ( ’ + ’ ) end end printf ( ’ = % i\n ’ , B 1 ( 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 : 1 2
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
X(i+1,1)=i; end for i = 2 : 5 X(1,i)=0; end for r = 1 : 1 2 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 end printf ( ’ \n
’ ); ’ ,i);
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 78 disp ( X ) 79 printf ( ’ \n\ n A ft e r 11 i t e r a t i o n s e x ac t s o l u t i o n i s : \ n ’ ); 80 for i = 1 : 4 81 printf ( ’ x%i=%f ’ ,i,X(12,i+1)); 82 end
121
Scilab code Exa 8.6 Block Jacobi Method
1 2 3 4 5 6
/ / E xa mp le 8 . 6 / / B l o c k J a c o b i Metho d / / P ag e n o . 2 8 1 clc ; clear ; close ; A = [ 1 0 , 1 , 0 , 0 , 0 , - 1 ; 1 , 1 0 , 1 , 0 , 0 , 0 ; 2 , 0 , 2 0 , 1 , 0 , 0 ; 0 , 0 , 1 , 1 0 ,- 1 , 0 ; 0 , 3 , 0 , 0 , 3
/ / e q u a t i o n m a t ri x // sol uti on
7 B=[5;10;10;0;0;5]
matrix 8 disp (B , ’B= ’ ,A , ’A= ’ ) 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
for i = 1 : 3 for j = 1 : 3 A11(i,j)=A(i,j); end B1(i,1)=B(i,1); end for i = 1 : 3 for j = 1 : 3 A12(i,j)=A(i,j+3); end end for i = 1 : 3 for j = 1 : 3 A21(i,j)=A(i+3,j); end end for i = 1 : 3 for j = 1 : 3 A22(i,j)=A(i+3,j+3);
122
28 end B2(i,1)=B(i+3,1); 29 30 end 31 disp ( B 2 , ’ B2= ’ , B 1 , ’ B1= ’ , A 2 2 , ’ A22= ’ , A 2 1 , ’ A21= ’ , A 1 2 , ’ A12=’ , A 1 1 , ’ A11= ’ ) ; 32 A 1 1 _ 1 = inv ( A 1 1 ) ; A 2 2 _ 1 = inv ( A 2 2 ) ; 33 disp ( A 2 2 _ 1 , ’ I n v e r s e o f A22= ’ , A 1 1 _ 1 , ’ I n v e r s e o f A11= ’ ) 34 for i = 1 : 3 35 X1(i,1)=0; 36 X2(i,1)=0; 37 end 38 for r = 1 : 2 39 X11=A11_1*(-1*A12*X2+B1); X22=A22_1*(-1*A21*X1+B2); 40 41 X1=X11; X2=X22; 42 43 disp ( X 1 , ’ X1= ’ ) disp ( X 2 , ’ X2= ’ ) 44 45 end 46 for i = 1 : 6 47 if ( i < 4 ) 48 X(i,1)=X1(i,1); else 49 50 X(i,1)=X2(i-3,1); end 51 52 end 53 disp (X , ’X= ’ ) 54 printf ( ’ \n\n\ nNote : Th ere i s a c o mp ut at io n e r r o r i n c a l c u l a t i o n o f X1 ( 2 ) ’ )
Scilab code Exa 8.7 Block Gauss Seidel Method
123
1 2 3 4 5 6
/ / E xa mp le 8 . 7 / / B l o c k G au ss−S e i d e l Method / / P ag e n o . 2 8 3 clc ; clear ; close ; A = [ 1 0 , 1 , 0 , 0 , 0 , - 1 ; 1 , 1 0 , 1 , 0 , 0 , 0 ; 2 , 0 , 2 0 , 1 , 0 , 0 ; 0 , 0 , 1 , 1 0 ,- 1 , 0 ; 0 , 3 , 0 , 0 , 3
/ / e q u a t i o n m a t ri x // sol uti on
7 B=[5;10;10;0;0;5]
matrix 8 disp (B , ’B= ’ ,A , ’A= ’ ) 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
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
end
59 60 61 62 63 64 65 66 67 68 69 70
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 ( B 3 , ’ B3= ’ , B 2 , ’ B2= ’ , B 1 , ’ B1= ’ , A 3 3 , ’ A33= ’ , A 3 2 , ’A32= ’ , A 3 1 , ’ A31= ’ , A 2 3 , ’ A23= ’ , A 2 2 , ’ A22= ’ , A 2 1 , ’ A21= ’ ,A13 , ’ A13= ’ , A 1 2 , ’ A12= ’ , A 1 1 , ’ A11= ’ ) ; A 1 1 _ 1 = inv ( A 1 1 ) ; A 2 2 _ 1 = inv ( A 2 2 ) ; A 3 3 _ 1 = inv ( A 3 3 ) ; disp ( A 3 3 _ 1 , ’ I n v e r s e o f M a tr i x A33= ’ , A 2 2 _ 1 , ’ I n v e r s e o f M a t ri x A22= ’ , A 1 1 _ 1 , ’ I n v e r s e o f M at r ix 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
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 ( X 3 , ’ X3= ’ , X 2 , ’ X2= ’ , X 1 , ’ 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
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 97 disp ( X ) 98 printf ( ’ \n\ n A ft e r 4 i t e r a t i o n s e xa ct s o l u t i o n ); 99 for i = 1 : 6 100 ’ ,i,X(5,i+1)); printf ( ’ x%i=%f 101 end
126
is :\n’
Scilab code Exa 8.8 Block SOR Method
1 2 3 4 5 6 7
/ / E xa mp le 8 . 8 // Blo ck SOR Method / / P ag e n o . 2 8 4 clc ; clear ; close ;
A = [ 1 0 , 1 , 0 , 0 , 0 , - 1 ; 1 , 1 0 , 1 , 0 , 0 , 0 ; 2 , 0 , 2 0 , 1 , 0 , 0 ; 0 , 0 , 1 , 1 0 ,- 1 , 0 ; 0 , 3 , 0 , 0 , 3
/ / e q u a t i o n m a t ri x // sol uti on
8 B=[5;10;10;0;0;5]
matrix 9 disp (B , ’B= ’ ,A , ’A= ’ ) 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
w=0.8 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);
127
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
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 ( B 3 , ’ B3= ’ , B 2 , ’ B2= ’ , B 1 , ’ B1= ’ , A 3 3 , ’ A33= ’ , A 3 2 , ’A32= ’ , A 3 1 , ’ A31= ’ , A 2 3 , ’ A23= ’ , A 2 2 , ’ A22= ’ , A 2 1 , ’ A21= ’ ,A13 , ’ A13= ’ , A 1 2 , ’ A12= ’ , A 1 1 , ’ A11= ’ ) ; A 1 1 _ 1 = inv ( A 1 1 ) ; A 2 2 _ 1 = inv ( A 2 2 ) ; A 3 3 _ 1 = inv ( A 3 3 ) ; disp ( A 3 3 _ 1 , ’ I n v e r s e o f M a tr i x A33= ’ , A 2 2 _ 1 , ’ I n v e r s e o f M a t ri x A22= ’ , A 1 1 _ 1 , ’ I n v e r s e o f M at r ix A11= ’ ) ; 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 ( X 3 , ’ X3= ’ , X 2 , ’ X2= ’ , X 1 , ’ 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 x%i ’ ,i); printf ( ’ end printf ( ’ \n
129
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 98 disp ( X ) 99 printf ( ’ \n\ n A ft e r 5 i t e r a t i o n s e xa ct s o l u t i o n ); 100 for i = 1 : 6 101 ’ ,i,X(6,i+1)); printf ( ’ x%i=%f 102 end
130
is :\n’
Chapter 9 Linear Least Squares Problem
Scilab code Exa 9.1 Moore Penrose Generalized Inverse
1 2 3 4 5 6 7 8 9
/ / E xa mp le 9 . 1 //Moore−P en ro se G e n e ra l i z ed I n v e r s e / / P ag e n o . 2 9 2 clc ; clear ; close ;
AT=[3,0,3;0,3,3]; // trans pose A=AT’; I = inv ( A T * A ) ; // inv ers e disp (I , ’ I n v e r s e o f AT∗A= ’ , A T * A , ’AT∗A= ’ ,A , ’A= ’ , A T , ’AT = ’ ); 10 A # = I * A T ; 11 disp ( A # , ’ Moore−P en ro se G e n er a l i z ed I n v e r s e o f A= ’ )
Scilab code Exa 9.2 Curve Fitting
131
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
/ / E xa mp le 9 . 2 / / Cu rve F i t t i n g / / P ag e n o . 2 9 3 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 . 4 2 ; y ( 1 , 6 ) = 0 . 2 6 ; // y v a l u e s
// c o n s t r u c t i o n o f n orm al e q u a ti o n s
32
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 \ t E Y ( k ) \ t \ t E x 2 ( k ) \ t \ t E x ( k ) ∗Y ( k ) ’ ) 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 printf ( ’ \n% f \ t%f \ t%f \ t%f \ t%f ’ , E x , 0 , E Y , E x 2 , E x Y ) 38 printf ( ’ \n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n\n ’ ) 39 A = [ 6 , E x ; E x , E x 2 ] ; // s y s te m o f n or ma l e q u a t i o n s 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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
printf ( ’ \ ta%i ’ , i ) ; if ( i = = 1 ) printf ( ’ = ’ ) end
printf ( ’ \ t%f \n ’ , B ( i , 1 ) ) end 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 ur ve i s : \ n %fx \ ny=%f e ’ ,b,a)
133
Scilab code Exa 9.3 Gram Schmidt Orthogonalization or Orthonormal-
ization Process 1 / / E xa mp le 9 . 3 2 //Gram−S ch mi dt 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 3 / / P ag e n o . 2 9 4 4 clc ; clear ; close ; 5 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 ) ’ ) ; 6 deff ( ’ y=f1 ( 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) ,0,1,0,0;1/ sqrt ( 3 ) , 1 / sqrt (7) ,0,0,1 ,0;-1/sqrt (3) ,-1/ sqrt (7) ,0,0,0,1]; 9 for i = 1 : 4 10 V(i,1)=U(i,1); 11 end 12 for i = 1 : 4 13 if ( f ( V , 1 ) ~ = 0 ) 14 W(i,1)=V(i,1)/f(V,1); 15 else W(i,1)=0; 16 17 end 18 end 19 for j = 2 : 6 for i = 1 : 4 20 21 for l = 1 : 4 k(l,1)=0; 22 23 end 24 for l = 1 : j - 1 25 for m = 1 : 4 26 w(m,1)=W(m,l); 27 end 28 k=k-(f1(U,j,W,l))*w; end 29 30 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
Scilab code Exa 9.4 QR Decomposition
135
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
/ / E xa mp le 9 . 4 //QR Decomposition / / P ag e n o . 2 9 6 clc ; clear ; close ; A=[2,1,1;1,3,1;1,1,4]; B=A*A’; disp (B , ’AT∗A= ’ )
/ / c ho le sk y f a c t o r i z a t i o n to f i nd 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 , ’ U pp er T r i a n g u l a r M a tr 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
Scilab code Exa 9.5 Vector Computation
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
/ / E xa mp le 9 . 5 / / V e c t o r C o m p u t a ti o n / / P ag e n o . 2 9 9 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 Valu 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 s i g m a = X ( 1 ) * abs ( 1 / X ( 1 ) ) * sqrt ( k ) ; printf ( ’ \ nsigma=%f \ n ’ , s i g m a ) ; X(1)=X(1)+sigma; printf ( ’ \ n M o d i fi 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 printf ( ’ \n p = %f \ n\n s ig m a = %f ’ , p , s i g m a ) ; 32 printf ( ’ \n\n\ nNote : Th ere i s a c o mp ut at io n e r r o r i n c a l c u l a t i o n o f U1 ’ )
Scilab code Exa 9.6 House Holder Transformation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/ / E xa mp le 9 . 6 / / H ou se H o l d er T r a n s f o r ma t i o n / / P ag e n o . 3 0 0 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 ( U 1 , ’UT∗U= ’ ) U2=U*U’; disp ( U 2 , ’U∗UT= ’ ) 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 om pu ta ti on e r r o r
i n t he a ns we rs g i ve n by t he book i n t h i s ex am ple \n\n ( a22 v al ue e r r o r i n U∗UT) ’ )
138
Scilab code Exa 9.7 Givens QR Method
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
/ / E xa mp le 9 . 7 //Givens QR Method / / P ag e n o . 3 0 3 clc ; clear ; close ; A=[4,2,1;2,5,-2;1,-2,7] deff ( ’ y=c ( i , j ) ’ , ’ y=A( j , j ) / s q r t ( (A( i , j )ˆ2+A( j , j ) ˆ2 ) ) ’ ) 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 / /C m a tr i x e v a l u a t i o n for k = 1 : 3 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 if ( k < l ) 30 31 if ( k == j & l == i ) C(k,l)=s(i,j) 32 33 else C(k,l)=0 34 35 end end 36 37 end 38 end 39 printf ( ’ \n\n I t e r a t i o n %i ’ ,m ) 40 m=m+1 disp (C , ’C=’ ) ; 41 42 R=C*R; Q=Q*C’; 43 44 disp (Q , ’Q= ’ ,R , ’R=’ ) end 45 46 end 47 disp ( Q * R , ’Q∗R=A= ’ ) // v e r i f i c a t i o n
Scilab code Exa 9.8 Recursive Least Square Method
1 2 3 4 5 6 7 8 9 10 11
/ / E xa mp le 9 . 8 / / R e c u r s i v e L e as t −S q u a r e M et ho d / / P ag e n o . 3 0 8 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 ( G 0 , ’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
G 0 _ 1 = inv ( G 0 ) ; disp ( G 0 _ 1 , ’ I n v e r s e X0=G0_1*A0T*B0; disp ( X 0 , ’ X0= ’ )
o f G0= ’ )
/ /by r e c u r s i v e l e a s t s qu ar e a l g o r it h m
G1=G0+A1’*A1; disp ( G 1 , ’G1= ’ ) ; G 1 _ 1 = inv ( G 1 ) ; disp ( G 1 _ 1 , ’ I n v e r s e o f G1 ’ ) X1=X0+G1_1*A1’*(B1-A1*X0); disp ( X 1 , ’ 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 ( ’ T hu s X a nd X1 a r e Same ’ )
141
Chapter 10 Numerical Solutions of System of Non Linear Equations
Scilab code Exa 10.1 System of Non Linear Equations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/ / E x am pl e 1 0 . 1 / / S ys te m o f Non L i n e a r E q u a t io n s / / P ag e n o . 3 1 1 clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=xˆ2− exp (2 ∗ x ) −4 ’ ) deff ( ’ y= f 1 ( x ) ’ , ’ y=2∗x−2∗ exp (2 ∗ x ) ’ ) x0=0;e=0.00001 for i = 1 : 1 0 x1=x0-f(x0)/f1(x0) e1 = abs ( x 0 - x 1 ) x0=x1; if a bs ( x 0 ) < e then break ; end end printf ( ’ \n\nThe s o l u t i o n o f t h i s e qu a t io n a f t e r %i
142
I t e r a t i o n s by newto n r ap hs ho n method i s %. 1 0 f ’ ,i , x1 )
Scilab code Exa 10.2 Contraction Method and Seidel Method
1 2 3 4 5 6
/ / E x am pl e 1 0 . 2 / / C o n t r a c t i o n Method a nd S e i d e l Metho d / / P ag e n o . 3 1 5 clc ; clear ; close ; x(1)=0;y(1)=0 printf ( ’ ( a ) C o n t r a c t i o n Mapping \n\nn \ t x n \ t \ ty n \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n 0\ t%f \ t%f \n ’ ,x 7 8 9 10 11 12
(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 \ ty n \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n 0\ t%f \ t%f \n ’ ,x 13 14 15 16 17
(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
Scilab code Exa 10.3 Non Linear System of Equation
143
1 2 3 4 5 6 7
/ / E x am pl e 1 0 . 3 / / Non L i n e a r S ys te m o f E q ua t i on / / P ag e n o . 3 1 5 clc ; clear ; close ; x(1,1)=1;x(1,2)=0;x(1,3)=0; y(1,1)=0;y(1,2)=2;y(1,3)=2; printf ( ’ Case −−>\t \ t I \ t \ t \ t \ t \ I I \ t \ t \ t \ t I I I \ n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 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 ) , 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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\ nNo te : T he re a r e c o m pu t a t io n a l e r r o r s i n t h i s e xa m p l e g i v en by t he book ’ )
144
Scilab code Exa 10.4 Newton Method
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
/ / E x am pl e 1 0 . 4 // Newton Method / / P ag e n o . 3 1 7 clc ; clear ; close ;
deff ( ’ y=f1 ( x1 , x2 ) ’ , ’ y=x1+3∗ l o g 1 0 ( x 1 )−x2ˆ2 ’ ) deff ( ’ y=f2 ( x1 , x2 ) ’ , ’ y=2∗x1ˆ2−x1 ∗ x2 −5∗x1 +1 ’ ) deff ( ’ y=f1 1 ( x1 , x2 ) ’ , ’ y = 1+ 3/ ( l o g ( 1 0 ) ∗ x 1 ) ’ ) deff ( ’ y=f1 2 ( x1 , x2 ) ’ , ’ y=−2∗x2 ’ ) deff ( ’ y=f2 1 ( x1 , x2 ) ’ , ’ y=4∗x1−x2 −5 ’ ) deff ( ’ y=f2 2 ( x1 , x2 ) ’ , ’ y=−x1 ’ ) x=[3.4;2.2]; disp (x , ’ x (0 ) = ’ ) for i = 1 : 3 fx=[f1(x(1),x(2));f2(x(1),x(2))] printf ( ’ \n f x ( %i ) = \n ’ ,i ) disp ( f x ) A=[f11(x(1),x(2)),f12(x(1),x(2));f21(x(1),x(2)), f22(x(1),x(2)),] disp (A , ’A = ’ ) A _ 1 = inv ( A ) disp ( A _ 1 , ’ I n v e r s e o f A = ’ ) x=x-A_1*fx printf ( ’ \n x ( % i ) = \n ’ ,i ) disp ( x ) end
Scilab code Exa 10.5 Newton Raphshon Method
1 / / E x am pl e 1 0 . 5 2 //Newton Raphshon Method
145
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
/ / P ag e n o . 3 2 0 clc ; clear ; close ;
deff ( ’ y=f1 ( x , y ) ’ , ’ y=xˆ3 −3∗x ∗ yˆ2+1 ’ ) deff ( ’ y=f2 ( x , y ) ’ , ’ y=3∗x ˆ2∗ y−yˆ3 ’ ) deff ( ’ y=f1 1 (x , y ) ’ , ’ y=3∗xˆ2 −6∗yˆ2 ’ ) deff ( ’ y=f1 2 (x , y ) ’ , ’ y=−6∗x ∗y ’ ) deff ( ’ y=f2 1 (x , y ) ’ , ’ y=6∗x ∗ y ’ ) deff ( ’ y=f2 2 (x , y ) ’ , ’ y=3∗xˆ2 −3∗yˆ2 ’ ) x=[0;1]; printf ( ’ \ n x ( 0 ) = %g\ n y ( 0 ) = %g\n ’ , x ( 1 ) , x ( 2 ) ) for i = 1 : 3 fx=[f1(x(1),x(2));f2(x(1),x(2))] printf ( ’ \n f x ( %i ) = \n ’ ,i ) disp ( f x ) J=[f11(x(1),x(2)),f12(x(1),x(2));f21(x(1),x(2)), f22(x(1),x(2)),] disp (J , ’ J = ’ ) d = det ( J ) ; if d = = 0 then dx1=0;dx2=0; else dx1=(fx(1)*J(2,2)-fx(2)*J(1,2))/d; dx2=(fx(2)*J(1,1)-fx(1)*J(2,1))/d; end x(1)=x(1)+dx1; x(2)=x(2)+dx2; printf ( ’ \ n x ( % i ) = % g\ n y ( % i ) = % g\n ’ , i , x ( 1 ) , i , x (2)) end
Scilab code Exa 10.6 Newton Method
146
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/ / E x am pl e 1 0 . 6 // Newton Method / / P ag e n o . 3 2 2 clc ; clear ; close ;
deff ( ’ y=f 1 ( x , y , z ) ’ , ’ y=x −0.1∗ y ˆ 2 + 0 . 0 5∗ z ˆ2 −0.7 ’ ) deff ( ’ y=f 2 ( x , y , z ) ’ , ’ y=y+ 0.3 ∗ x ˆ2 −0.1∗ x ∗ z −0.5 ’ ) deff ( ’ y=f 3 ( x , y , z ) ’ , ’ y=z +0.4 ∗ y ˆ 2 + 0 . 1∗ x ∗y −1.2 ’ ) deff ( ’ y=f 1 1 ( x , y , z ) ’ , ’ y=1 ’ ) deff ( ’ y=f 1 2 ( x , y , z ) ’ , ’ y = −0. 2∗y ’ ) deff ( ’ y=f 1 3 ( x , y , z ) ’ , ’ y = 0 . 1∗ z ’ ) deff ( ’ y=f 2 1 ( x , y , z ) ’ , ’ y = 0 . 6∗ x −0.1∗ z ’ ) deff ( ’ y=f 2 2 ( x , y , z ) ’ , ’ y=1 ’ ) deff ( ’ y=f 2 3 ( x , y , z ) ’ , ’ y = −0. 1∗x ’ ) deff ( ’ y=f 3 1 ( x , y , z ) ’ , ’ y = 0 . 1∗ y ’ ) deff ( ’ y=f 3 2 ( x , y , z ) ’ , ’ y = 0 . 8∗ y + 0 . 1∗ x ’ ) deff ( ’ y=f 3 3 ( x , y , z ) ’ , ’ y=1 ’ ) x=[0;0;0]; printf ( ’ n\ tx n \ t \ t yn \ t \ t z n \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ n ’) 20 for i = 1 : 6 21 fx=[f1(x(1),x(2),x(3));f2(x(1),x(2),x(3));f3(x (1),x(2),x(3))] 22 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))] 23 J _ 1 = inv ( J ) printf ( ’ %i \ t%f \ t%f \ t%f \n ’ , i - 1 , x ( 1 ) , x ( 2 ) , x ( 3 ) ) 24 25 x=x-J_1*fx 26 end 27 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 ) ) 28 29 printf ( ’ \n\n\ nNote : Th ere a r e c om pu ta ti on e r r o r s i n c a l c u l a t i o n g i v en by t he book ’ )
147
Scilab code Exa 10.7 Iterative Method
1 2 3 4 5 6 7
/ / E x am pl e 1 0 . 7 / / I t e r a t i v e Method / / P ag e n o . 3 2 6 clc ; clear ; close ; x=[0;0;0]; printf ( ’ n\ t x n \ t \ t yn \ t \ t z n \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ n ’) 8 for i = 1 : 7 9 . 1 0 f \t% . 1 0 f \n ’ ,i-1,x(1),x printf ( ’ %i \t% . 1 0 f \t% (2),x(3)) x(1)=0.7+0.1*x(2)^2-0.05*x(3)^2 10 11 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) 12 13 end 14 printf ( ’ \n\nThe s o l u t i o n i s x = %. 1 0 f , y = %. 1 0 f and z = %.10 f ’ ,x(1),x(2),x(3))
Scilab code Exa 10.8 Steepest Descent
1 2 3 4 5
/ / E x am pl e 1 0 . 8 / / S t e e p e s t D e s ce n t / / P ag e n o . 3 2 8 clc ; clear ; close ;
148
6 deff ( ’ y=f (x1 , x2 ) ’ , ’ y=(x1 −2)ˆ4+3∗(x2 +3)ˆ2 ’ ) 7 x=[1;-2]; 8 printf ( ’ n\ t x1 \ t \ t x2 \ t \ t F ( x1 , x2 ) \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ n ’) 9 for i = 1 : 1 1 10 Fx=[f(x(1),x(2))]; J=[4*(x(1)-2)^3,6*(x(2)+3)]; 11 12 u=(Fx*J*J’*Fx)/(J*J’*Fx*J*J’*Fx) 13 printf ( ’ %i \t% . 1 0 f \t% . 1 0 f \t% . 1 0 f \n ’ ,i-1,x(1),x (2),Fx) 14 x=x-u*J’*Fx 15 end 16 printf ( ’ \n\ n T h i s s hows t ha t t he s o l u t i o n t en ds t o x 1 = %i and x2 = %i ’ , ceil ( x ( 1 ) ) ,floor ( x ( 2 ) ) )
149
Chapter 11 Eigenvalues and Eigenvectors
Scilab code Exa 11.1 Eigenvalues and Eigenvectors
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E x am pl e 1 1 . 1 / / E i g e n v a l u e s and E i g e n v e c t o r s / / P ag e n o . 3 3 3 clc ; clear ; close ; A1=[0.6;0.2];A2=[-0.2;0.6];A3=[-0.6;-0.2];A4 =[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 ( B 4 , B 3 , B 2 , B 1 , ’ The t r an s fo r me d v e c t o r s a r e : ’ ) disp ( ’ T h es e p o i n t s l i e o n t h e e l l i p s e : ’ ) printf ( ’ 2 2\ n ( x−3y)+(3x+y) \n−−−−−− −−−−−−\ n 16 4\ n\n ’ ) A 5 = [ 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 : ’ ) 2\ nx + y = 4 \ n 10\ n\n printf ( ’ 2 −\n ’) B5=T*A5;
150
16 disp ( ’ A l s o l i e s o n t h e s am e e l l i p s e ’ , B 5 ) 17 printf ( ’ \n\nWe can s e e t ha 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 etw ee n t he 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 t ra n s fo rm en d v e c t o r s t hr ou gh t he 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 respecti vely ’)
Scilab code Exa 11.2 Leverriers Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/ / E x am pl e 1 1 . 2 / / L e v e r r i e r ’ s M et ho d / / P ag e n o . 3 3 7 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 ( A 1 , ’ A1= ’ ) printf ( ’ \nC1=’ ) disp ( C ( 1 ) ) ; for i = 2 : 3 A 2 = A * ( A 1 - C ( i - 1 ) *eye ( 3 , 3 ) ) ; printf ( ’ \n\n\nA%i=’ ,i ) disp ( A 2 ) ; C(i)=0;
151
25 for j = 1 : 3 for k = 1 : 3 26 27 if ( j = = k ) C(i)=C(i)+A2(j,k)/i 28 29 end end 30 31 end printf ( ’ \nC%i=’ ,i ) 32 33 disp ( C ( i ) ) 34 A1=A2; 35 end 36 printf ( ’ \n\n\ n Th er ef or e t h e c h a r a c t e r i s t i c
p o l y no m i a l i s : \ n 3
2\ nx − %ix − % ix %i = 0 ’ ,C
(1),C(2),C(3)) 37 38 39 40 41 42 43
// v e r i f i c a t i o n printf ( ’ \n\ n V e r i f i c a t i o n : ’ ) s = poly (0 , ” s ” ) ; p = poly (A , ’ x ’ ) ; A = A - eye ( 3 , 3 ) * % s ; disp (p , ’= ’ ,A )
Scilab code Exa 11.3 Danilevsky Method
1 2 3 4 5 6 7 8 9
/ / E x am pl e 1 1 . 3 / / D a n i l e v s k y Me th od / / P ag e n o . 3 4 1 clc ; close ; clear ; A=[-1,0,0;1,-2,3;0,2,-3]; G = [ A ; eye ( 3 , 3 ) ] ; disp ( G ) ;
/ / t r a n s f o rm a t i o n t o f r o b e n i u s m at ri x 152
10 for k = 3 : - 1 : 2 g(k)=0; 11 12 for j = 1 : k - 1 if ( g ( k ) < G ( k , j ) ) 13 14 g(k)=G(k,j) p=j; 15 16 end end 17 18 if ( g ( k ) ~ = 0 ) 19 for j = 1 : 3 20 r(1,j)=G(k,j) 21 end for i = 1 : 6 22 23 G(i,k-1)=G(i,k-1)/g(k) end 24 25 disp ( G ) for j = 1 : 3 26 27 if ( j ~ = k - 1 ) l=G(k,j) 28 29 for i = 1 : 6 30 G(i,j)=G(i,j)-l*G(i,k-1) 31 end 32 end end 33 34 disp ( G ) end 35 36 for j = 1 : 3 for i = 1 : 3 37 38 c(i,1)=G(i,j) end 39 40 G(k-1,j)=0 41 for i = 1 : 3 42 G(k-1,j)=G(k-1,j)+r(1,i)*c(i,1) 43 end 44 end 45 disp ( G ) 46 end 47
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 pu t a ti o n printf ( ’ \n\n\ n C h a r a t e r i s t i c p o ly n om i al : ’ ) 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 om pu t at io 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 pu t a ti 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 en v e ct o r s : \ n ’ ) 87 for i = 1 : 3 88 printf ( ’ | %.1 f | \ t \ t | %.1 f | \ t \ t | %.1 f | ’ , x 1 ( i , 1 ) , x 2 ( i ,1),x3(i,1)) 89 printf ( ’ \n ’ ) 90 end
Scilab code Exa 11.4 Power Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E x am pl e 1 1 . 4 //Power Method / / P ag e n o . 3 4 5 clc ; close ; clear ; A=[1,2;3,4]; e=0.001; q0=[1;1]; for i = 1 : 5 q1=A*q0; a = max ( q 1 ) 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 ’ ,
i,q1(1),a,i,q2(1),q1(2),q2(2)) q1=q2; q0=q1;
16 17 18 end 19 printf ( ’ H ence t he l a r g e s t
e i g e n v a l u e i s %. 4 f w i th t he c o r r e sp o n di n g e i g e n v e c t o r a s %. 4 f \n
155
%i ’ , a , q 0 ( 1 ) , q 0 ( 2 ) )
Scilab code Exa 11.5 Inverse Power Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/ / E x am pl e 1 1 . 5 / / I n v e r s e P ow er Me th od / / P ag e n o . 3 4 7 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
printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 20 21 22 23 24 25
for j = 1 : 1 0 printf ( ’ \ n%i %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
%f
26 m=0; for k = 1 : 3 27 28 m=m+(Y(k)-X(k))^2; end 29 30 e2 = sqrt ( m ) ; er = max ( e 1 , e 2 ) ; 31 32 if ( e r < e ) break 33 34 end 35 l=B; 36 37 end a=-3; 38 39 printf ( ’ \n\n\n\n ’ ) 40 end 41 printf ( ’ \n\n\ nNo te : C om pu ta ti on o f Y i s i n t h e b oo k ’ )
wrong g i v e n
Scilab code Exa 11.6 Rayleigh Quotient
1 2 3 4 5 6 7 8 9 10 11 12 13
/ / E x am pl e 1 1 . 6 // Rayleigh Quotient / / P ag e n o . 3 4 8 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\ n Do mi na nt E i g e n v a l u e = % f\ n\n\n ’ ,l )
157
14 15 16 17 18 19 20 21 22 23 24 25 26
e=0.001; for i = 1 : 5 q1=A*q0; a = max ( q 1 ) for j = 1 : 4 q2(j)=q1(j)/a; end q1=q2; q0=q1; end disp ( q 2 , ’ C o r re s p on d i ng E i g e n v e c t o r = ’ )
Scilab code Exa 11.7 Jacobi Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E x am pl e 1 1 . 7 // Jac obi ’ s Method / / P ag e n o . 3 5 5 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 40 41 42 43 44 45 46 47 48 49
end end
end V I = sqrt ( V I ) ; // f i n a l t h r es h o l d VF=VI*10^-7; 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 ) > V 1 ) a=-A(p,q); b=(A(p,p)-A(q,q))/2 if (b~=0) w = b * a b s ( 1 / b ) * ( a / sqrt ( a ^ 2 + b ^ 2 ) ) ; else w = ( a / sqrt ( a ^ 2 + b ^ 2 ) ) ; end s i n 0 = w / sqrt (2*(1+ sqrt ( 1 - w ^ 2 ) ) ) ; c o s 0 = sqrt ( 1 - s i n 0 ^ 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 end
end end disp ( B , ”B =” ) disp ( S , ” S = ” ) printf ( ’ \ n \ n \ n C o mp mp u t a t i o n e r r o r p r o v i d e d b y b oo oo k ’ )
159
i n t he he s o l u t i o n
Scilab code Exa 11.8 Recursive Formula
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/ / E x am am pl pl e 1 1 . 8 / / R e c u r s i v e F or o r mu m u la la / / P ag ag e n o . 3 5 7 c l c ; 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 \ n p % i ( l ) = ’ , i ) ; disp ( p 2 ) end
Scilab code Exa 11.9 QR Method
1 2 3 4 5 6
/ / E x am am pl pl e 1 1 . 9 //QR //Q R Method / / P ag ag e n o . 3 6 0 c l c ; close ; clear ; A=[2,-1,0;-1,2,-1;0,-1,2];
160
7 deff ( ’ y=c ( i , j ) ’ , ’ y=A( j , j ) / s q r t ( (A( i , j )ˆ2+A( )ˆ2+A( j , j ) ˆ2 ) ) ’ ) 8 deff ( ’ y=s2 ( i , j ) ’ , ’ y=A( i , j ) / s q r t ( (A( i , j ) ˆ2+ ˆ2+A( j , j ) ˆ2 ) ) ’) 9 disp ( A , ’A= ’A= ’ ) 10 l 0 = 0 ; f = 1 ; m = 0 ; s = 0 ; w = 0 ; 11 f o r n = 1 : 5 for j=1:2 12 13 for k=1:2 14 V(j,k)=A(j,k) 15 end 16 end disp ( V , ’V= 17 ’V= ’ ) 18 p = poly ( V , ’ x ’ ) ; disp ( ’ =0 ’ , p ) ; 19 20 a = roots ( p ) ; for j=1:2 21 22 printf ( ’ \ n a ( % i ) = % f ’ , j , a ( j ) ) end 23 24 if ( abs ( a ( 1 ) - V ( 1 , 1 ) ) < = abs ( a ( 2 ) - V ( 1 , 1 ) ) ) 25 a=a(1) 26 else 27 a=a(2) end 28 29 n ’ ,a) printf ( ’ \ n a = % f \ s=s+a; 30 31 A = A - a * ey e () R=A;Q= eye (3,3); 32 33 for j=1:2 34 35 for i=j+1:3 36 for k=1:3 / /C /C m a t r i x
evaluation 37 38 39 40 41
for l=1:3 if(k==l) i f ( k == = = i | k == == j ) C(k,l)=c(i,j) else
161
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 ) i f ( k == = = i & l == == j ) C(k,l)=-1*s2(i,j) else C(k,l)=0 end end if( k < l ) i f ( 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= ’Q= ’ , R , ’R=’ ) ’ Q∗R= ’ ) disp ( Q * R , ’Q A=R*Q; ’A= ’ ) 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= ’V= ’ ) p = poly ( V , ’ x ’ ) ;
162
80 disp ( ’ =0 ’ , p ) ; a = roots ( p ) ; 81 82 for j=1:2 printf ( ’ \ n a ( % i ) = % f ’ , j , a ( j ) ) 83 84 end l2=l1+a(1) 85 86 l3=l1+a(2) disp ( l 3 , ’ l 3 = ’ , l 2 , ’ l 2 = ’ , l 1 , ’ l 1 = ’ ) 87 88 printf ( ’ \ n \ n \ n N o t e : V a lu l u es es 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 ’ )
Scilab code Exa 11.10 LU Method
1 2 3 4 5 6
/ / E x am am pl pl e 1 1 . 1 0 //LU //L U Meth Method od / / P ag ag e n o . 3 6 3 c l c ; close ; clear ; A = [ 1 2 0 , 8 0 , 4 0 , - 1 6 ; 8 0 , 1 2 0 , 1 6 , - 4 0 ; 4 0 , 1 6 , 1 2 0 , - 8 0 ; - 1 6 , - 4 0, - 8 0 , 1 2 0 ] ;
7 8 9 10 11 12 13 14 15 16 17
disp ( A , ”A =” ) L= eye (4,4); for l=1:20 for j=1:4 for i = 1 : j k =0 for p=1:i-1 k=k-A(i,p)*A(p,j) end A(i,j)=A(i,j)+k 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 Af te r %i i t e r a t i o n s , m at ri 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 er e f o r e t he e i g e n v a l u e s a r e t he
d i a go n a l e le m e nt s f t he t ra n sf or m e d t r i a n g u l a r m a t ri x a r e : \ n\n ’ ) 47 for i = 1 : 4 48 printf ( ’ % . 2 f , ’ , A ( i , i ) ) 49 end
164
Scilab code Exa 11.11 Generalized Eigenvalue Problem
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
/ / E x am pl e 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 P ro bl em / / P ag e n o . 3 6 5 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 rm a t i o n t o f r o b e n i u s m at ri 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 pu t a ti 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 at ri 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 95 96 97 98 99 100 101 102 103 104 105
end disp ( B )
/ / e i g e n v e c t o r s c om pu t at io 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 re : \ n\n ’ ) for i = 1 : 3 printf ( ’ | %.5 f | \ t \ t | %.5 f | \ t \ t | %.5 f | ’ , x 3 ( i , 1 ) , x 2 ( i ,1),x1(i,1)) printf ( ’ \n ’ ) end x=[x3,x2,x1]
B=[2,2,2;2,5,5;2,5,11] G =0 for i = 1 : 3
167
106 107 108 109 110 111 112 113 114 115 116 117 118
for j = 1 : 3 if i = = j then G ( i , j ) = sqrt ( B ( i , j ) ) else G(i,j)=0; end end end B = inv ( G ) * x ’ * A * x * inv ( G ) disp (B , ” E i g e n v e c t o r s o f A =” ) printf ( ’ \n\n\ nNo te : C om pu ta ti on E r r or i n bo ok i n c ac u la ti o n o f e i ge n ve ct or o f B thus f o r A ’ )
168
Chapter 12 Interpolation and Extrapolation
Scilab code Exa 12.1 Linear Interpolation Technique
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/ / E x am pl e 1 2 . 1 // L i n ea r I n t e r p o l a t i o n T ec hn iq ue / / P ag e n o . 3 7 2 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; x 1 = 2 ; x 2 = 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; x 1 = 2 ; x 2 = 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; x 1 = 1 ; x 2 = 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\ n E xa ct v a l u e = %. 2 f ’ ,2.5^2)
Scilab code Exa 12.2 Lagarangian Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/ / E x am pl e 1 2 . 2 / / L a g a r a n g i a n M et ho d / / P ag e n o . 3 7 3 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=’ )
Scilab code Exa 12.3 Aitken Nevilles Method
1 2 3 4 5 6
/ / E x am pl e 1 2 . 3 / / A i t k e n −N e v i l l e ’ s M et ho d / / P ag e n o . 3 7 8 clc ; close ; clear ; function [ x , y , z ] = t r a n ( a , b )
// f u n c t i o n f o r
e x ch a n gi n g v a l u e s 7 z=a;y=b;x=z; 8 endfunction 9 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 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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 x−x i y i \n ’ )
171
25 printf ( ’−−−−−−−−−−−−−−−−−−−−−−−−\n ’ ) 26 for i = 1 : 5 27 %. 2 f \ t%f \n ’ , x i ( i ) , x _ x i ( i ) , y i ( i ) printf ( ’%. 1 f ) 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 ( x i , y i , x _ x i , j , i ) ) 34 yi(j)=P(xi,yi,x_xi,j,i) 35 end printf ( ’ \n\n\n ’ ) 36 37 end
Scilab code Exa 12.4 Newtons Divided Difference Interpolation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E x am pl e 1 2 . 4 / /Newton ’ s D i vi d ed D i f f e r e n c e I n t e r p o l a t i o n / / P ag e n o . 3 8 1 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 ly n om i al 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
y f ( x0 , x1 ) f ( x0 , x1 , x3 ) f ( x0 , x1 , x2 , x3 ) \n ’ ) 18 printf ( ’ −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ n ’) 19 20 21 22 23 24
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 )
Scilab code Exa 12.5 Interpolation Methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/ / E x am pl e 1 2 . 5 / / I n t e r p o l a t i o n Metho ds / / P ag e n o . 4 0 3 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 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
end end P=P+p;
end disp (P , ’ I n v e r s e L ag ra ng e i n t e r p o l a t i o n x= ’ )
// 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 x1=x; deff ( ’ xi=P( a , b , d , y ) ’ , ’ x i =( b ( d + 1 )−b ( d ) ) / ( a ( d+y )−a ( d ) ) ’ ) // f u n c t i o n f o r f i n d i n g p o ly n om i al 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 ) , x 1 ( 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 = x 1 ; 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 ] = t r a n ( a , b )
// f u n c t i o n f o r
e x ch a n gi n g v a l u e s 46 z=a;y=b;x=z; 47 e n d f u n c t i o n 48 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 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
y1=20
[y(4),y(1),a]=tran(y(1),y(4)) [y(3),y(2),a]=tran(y(2),y(3)) [x(4),x(1),a]=tran(x(1),x(4)) [x(3),x(2),a]=tran(x(2),x(3)) for i = 1 : 5 y1_y(i)=y1-y(i); end printf ( ’ y \ ty1 −y \ t x \n ’ ) printf ( ’−−−−−−−−−−−−−−−−−−\n ’ ) for i = 1 : 5 printf ( ’%. 1 f \ t%i \ t% i \n ’ , y ( i ) , y 1 _ y ( i ) , x ( i ) ) end printf ( ’ \n\ n P o l y n o m i a l s \n ’ ) printf ( ’−−−−−−−−−−−\n ’ ) for i = 1 : 4 for j = 1 : 5 - i printf ( ’ %f \n ’ , P ( y , x , y 1 _ y , j , i ) ) x(j)=P(y,x,y1_y,j,i) end printf ( ’ \n\n ’ ) end printf ( ’ 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 x ( 2 0 ) = %f ’ ,x ( j))
x=[0,1,2,3,4]; y=[0,1,8,27,64]; y1=y;
// S u gg e st ed I n t e r p o l a t i o n for i = 1 : 4 for j = 1 : 5 - i z(j,i)=y(j+1)-y(j); y(j)=z(j,i) end
175
84 e n d 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 ’ ) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n 87 f o r i = 1 : 5 printf ( ’ %i \ t % i \ t % i \ t % i \ t % i \ t % i \ n ’ , x ( i ) , y 1 ( i ) , z ( 88 i,1),z(i,2),z(i,3),z(i,4)) 89 e n d 90 s = poly ( 0 , ’ s ’ ) 91 p = y 1 ( 4 ) ; k = 3 ; 92 f o r i = 1 : 3 93 r=1; for j = 1 : i 94 95 r=r*(s+(j-1)) end 96 97 r=r*z(k,i)/factorial(j); k=k-1; 98 99 p=p+r; printf ( ’ \ n \ n St 100 S t a ge ge %i : ’ , i ) 101 disp ( p ) 102 e n d 103 s 0 = - 7 / 1 9 ; 104 disp ( s 0 , ’ s0= s0= ’ ) ; 105 s 1 = ( - 7 - s 0 * ( s 0 + 1 ) * 6 ) / 1 9 106 disp ( s 1 , ’ s1= s1= ’ ) 107 disp ( 3 + s 1 , ’ x 1= 1= ’ ) 108 s 2 = ( - 7 - s 1 * ( s 1 + 1 ) * 6 - s 1 * ( s 1 + 1 ) * ( s 1 + 2 ) ) / 1 9 109 disp ( s 2 , ’ s2= s2= ’ ) 110 x 2 = 3 + s 2 ; 111 disp ( x 2 , ’ S u gg g g e st s t ed e d I n t e r p o l a t i o n x ( 2 0 )= )= ’ ) ;
Scilab code Exa 12.6 Chebyshev Interpolating Polynomial
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
/ / E x am am pl pl e 1 2 . 6 / / Ch C h eb e b ys ys he h e v I n t e r p o l a t i n g P o ly l y n om o m i al al / / P ag ag e n o . 4 0 7 c l c ; close ; clear ; deff ( ’y=f ( x ) ’ , ’y =1/(1 =1/(1+ex +ex p(− p( − 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 ) *c *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 P3 ( x ) = ’ ) disp ( P , ’ P3 printf ( ’ \ n \ n \ n N o t e : B o o k h a s C a l c u l a t i o n ca l c u la t i o n of c o e f f i c i e n t s ’ )
177
errors in
Scilab code Exa 12.7 Double Interpolation
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
/ / E x am am pl pl e 1 2 . 7 / / Do D o ub ub l e I n t e r p o l a t i o n / / P ag ag e n o . 4 0 9 c l c ; close ; clear ; x=[0,1,2,3,4]; y=[0,1,2,3,4]; z = [ 0 , 1 , 8 , 2 7 , 6 4 ; 1 , 3 , 1 1 , 3 1 , 6 9 ; 4 , 7 , 1 6 , 3 7 , 7 6 ; 9 , 1 3 , 2 3 , 4 5 ,8 5 ; 1 6 , 2 1 , 3 2 , 5 5 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 al al u uee s o f z a t x = 2 . 55:: \ 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 ’ , z 1 ( i ) ) end y=1.5; y1=1;y2=2; z2=z1(y1+1)+(z1(y2+1)-z1(y1+1))*(y-y1)/(y2-y1) = 2 . 5 a n d y =1 = 1 . 5 : %g ’ , z 2 ) printf ( ’ \ n \ n V a l u e o f z a t x =2
Scilab code Exa 12.8 Spline Interpolation
1 2 3 4 5 6 7
/ / E x am am pl pl e 1 2 . 8 // S p l i ne I n t e r p o l a t i o n / / P ag ag e n o . 4 1 4 c l c ; close ; clear ; xi=[0.10,0.11,0.12,0.13,0.14,0.15,0.16,0.17]; yi = [ 0 . 1 1 1 0 , 0 . 1 2 3 4 , 0 . 1 3 6 1 , 0 . 1 4 9 1 , 0 . 1 6 2 3 , 0 . 1 7 5 9 , 0 . 1 8 9 7 ,0 . 2 0 3 8 ] ;
8 h=0.01; 9 10 p i ( 1 ) = 0 ; q i ( 1 ) = 0 ; p i ( 8 ) = 0 ; q i ( 8 ) = 0 ; 11 f o r i = 2 : 7 pi(i)=-1/(4+pi(i-1)) 12 13 qi(i)=((6/h^2)*(yi(i+1)-2*yi(i)+yi(i-1))-qi(i-1) )/(4+pi(i-1)) 14 e n d 15 s i 2 ( 8 ) = 0 ; 16 s i 2 ( 1 ) = 0 ; s i 1 ( 8 ) = 0 ; 17 s i 1 ( 1 ) = 0 ; 18 f o r i = 7 : - 1 : 2
179
si2(i)=pi(i)*si2(i+1)+qi(i) end for i=2:8 si1(i)=si1(i-1)+h*(si2(i)+si2(i-1))/2 end printf ( ’ \ n i \ t x i \ t f i \ i \ t pi \t\t q i \ t \ t si1 ’) 25 printf ( ’ \ n 19 20 21 22 23 24
s i 2 \ t \ t
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 26 f o r i = 1 : 8 27 t% g \ t%g t% g \ t % f \ f \ t % f \ f \ t % f \ f \ t%f ’ , i , x i ( i ) , printf ( ’ \ n %i \ t%g yi(i),pi(i),qi(i),si2(i),si1(i)) 28 e n d 29 x = 0 . 1 3 2 5 ; 30 i = 4 ; 31 s = y i ( i ) + ( x - x i ( i ) ) * s i 1 ( i ) + ( s i 2 ( i ) * ( x - x i ( i ) ) ^ 2 ) / 2 + ( ( si2(i+1)-si2(i))/(xi(i+1)-xi(i)))*((x-xi(i))^3)/6 32 printf ( ’ \ n \ n S pl pl i ne I n t e r p o l a te d Value o f s ( 0 . 1 3 2 5 ) i s : %f ’ , s )
180
Chapter 13 Numerical Differentiation
Scilab code Exa 13.1 Differentiation
1 2 3 4 5 6 7 8 9 10 11 12
/ / E x am pl e 1 3 . 1 // D i f f e r e n t i a t i o n / / P ag e n o . 4 2 0 clc ; close ; clear ;
deff ( ’y=f ( x ) ’ , ’y=xˆ2+5 ’ ) deff ( ’ y=f1 ( 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 d x ’ ,d ) printf ( ’ \n\n\n d 2 y \n −−− = %g\n d x2 ’ , d 1 )
Scilab code Exa 13.2 Calculation of x coordinate of Minimum Point
181
1 2 3 4 5 6 7 8 9 10 11 12
/ / E x am pl e 1 3 . 2 // C a l c ul a t i o n o f x−c o o r d i n a t e o f Minimum P o i n t / / P ag e n o . 4 2 2 clc ; close ; clear ; for i = 1 : 7 for j = 1 : 6 z(i,j)=0 end end h=0.2 x printf ( ’
d3
y d4 \n ’ )
d
d2
13 printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
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 )
s = poly (0 , ’ s ’ ) p=z(5,2);k=4; for i = 3 : 5 r=1; for j = 1 : i - 2
182
36 37 38 39 40 41 42 43 44 45 46 47
r=r*(s+(j-1)) end r=r*z(k,i)/factorial(j); k=k-1; p=p+r;
end disp ( p ) s=(-z(4,3)+z(3,4)/2)/z(3,4) disp (s , ’ s= ’ ) x=z(5,1)+s*h disp (x , ’ x= ’ )
Scilab code Exa 13.3 Newton Forward Difference Formula
1 2 3 4 5
/ / E x am pl e 1 3 . 3 / / Newton ’ s F or wa rd D i f f e r e n c e F or mu la / / P ag e n o . 4 2 3
clc ; close ; clear ; printf ( ’ x\ t \ t d4 \n ’ ) \t 6 printf ( ’
y\ t \ t
d\ t \ t
d2 \ t \ t
d3 \ t
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 7 h=0.05; 8 z = [ 1 . 0 0 , 1 . 0 0 0 0 0 ; 1 . 0 5 , 1 . 0 2 4 7 0 ; 1 . 1 0 , 1 . 0 4 8 8 1 ; 1 . 1 5 , 1 . 0 7 23 8 ; 1 . 2 0 , 1 . 0 9 5 4 9 deff ( ’ y=f 1 (x , s ) ’ , ’ y=(z (x , 3 ) +(s −1/2) ∗ z (x ,4 )+z (x ,5 ) ∗ ( 3 ∗ s ˆ2 −6∗ s + 2) / 6 ) / h ’ ) 10 deff ( ’ y=f 2 (x , s ) ’ , ’ y=(z (x ,4 )+z (x ,5 ) ∗ ( s −1) ) / h ˆ 2 ’ ) 11 deff ( ’ y=f 3 (x , s ) ’ , ’ y=z ( x , 5 ) /hˆ 3 ’ ) 12 for i = 3 : 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 ’ , f 1 ( 1 , 0 ) ) printf ( ’ \n\ ny2 (1 ) = %g ’ , f 2 ( 1 , 0 ) ) printf ( ’ \n\ ny3 (1 ) = %g ’ , f 3 ( 1 , 0 ) ) printf ( ’ \n\ n y1 ( 1 . 0 2 5 ) = %g ’ , f 1 ( 1 , 0 . 5 ) )
184
Scilab code Exa 13.4 Newton Backward Difference Formula
1 2 3 4 5
/ / E x am pl e 1 3 . 4 / / Newto n ’ s B ac kw ar d D i f f e r e n c e F or m ul a / / P ag e n o . 4 2 5
clc ; close ; clear ; x\ t \ t printf ( ’ d4 \n ’ ) \t 6 printf ( ’
y\ t \ t
d\ t \ t
d2 \ t \ t
d3 \ t
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 7 h=0.02; 8 z = [ 0 . 9 6 , 1 . 8 0 2 5 ; 0 . 9 8 , 1 . 7 9 3 9 ; 1 . 0 0 , 1 . 7 8 5 1 ; 1 . 0 2 , 1 . 7 7 6 3 ; 1. 0 4 , 1 . 7 6 7 3 ] ; 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
deff ( ’ y=f 1 (x , s ) ’ , ’ y=(z (x ,3 ) +(s +1/2) ∗ z (x ,4 ) )/h ’ ) for i = 3 : 6 for j = 1 : 7 - i z(j,i)=z(j+1,i-1)-z(j,i-1) end end printf ( ’ \n ’ ) for i = 1 : 5 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 printf ( ’ \n\ ny1 (1 ) = %g ’ , f 1 ( 2 , 0 ) ) printf ( ’ \n\ n y1 ( 1 . 0 3 ) = %g ’ , f 1 ( 4 , 0 . 5 ) )
185
Scilab code Exa 13.5 Stirlings Central Difference Derivatives
1 2 3 4 5
/ / E x am pl e 1 3 . 5 // S t i r l i n g s C en tr al D i f f e r e n c e D e r i v a t i v e s / / P ag e n o . 4 2 6
clc ; close ; clear ; x\ t \ t printf ( ’ ’) 6 printf ( ’
y\ t \ t
d\ t \ t
d2 \ t \ t
d3 \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 7 h=0.01;s=0.5; 8 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 ’ ) 9 deff ( ’ y=f 2 (x , s ) ’ , ’ y=(z (x −1 , 4 ) ) / h ˆ 2 ’ ) 10 deff ( ’ y=f 3 (x , s ) ’ , ’ y=(z (x −1 ,5)+z ( x −2 ,5) ) /(2 ∗ h ˆ 3 ) ’ ) 11 z = [ 1 . 0 0 , 1 . 0 0 0 0 0 ; 1 . 0 1 , 1 . 0 0 4 9 9 ; 1 . 0 2 , 1 . 0 0 9 9 5 ; 1 . 0 3 , 1 . 0 1 48 9 ; 1 . 0 4 , 1 . 0 1 9 8 12 13 14 15 16 17 18 19 20 21 22 23 24
for i = 3 : 5 for j = 1 : 1 9 - i z(j,i)=z(j+1,i-1)-z(j,i-1) end end printf ( ’ \n ’ ) for i = 1 : 1 7 for j = 1 : 5 if z ( i , j ) = = 0 then printf ( ’ \ t ’ ) else printf ( ’%. 7 f \ t ’ , z ( i , j ) ) end
186
25 end printf ( ’ \n ’ ) 26 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.20951) ’ ,f2(13,0.5)) 30 printf ( ’ \n\ ny3 ( 1 . 1 2 5 ) = %g ’ ,f3(13,0.5))
( e xa c t v a l u e = ( e xa c t v a l u e = ( e xa c t v a l u e = 0 . 2 7 9 3 5 )
Scilab code Exa 13.6 Extrapolation
1 2 3 4 5 6
/ / E x am pl e 1 3 . 6 // Extrap olati on / / P ag e n o . 4 3 0 clc ; close ; clear ; x=[-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8]; y = [ 0 . 2 0 1 9 , 0 . 3 0 1 1 9 , 0 . 4 4 9 3 3 , 0 . 6 7 0 3 2 , 1 , 1 . 4 9 1 8 2 , 2 . 2 2 5 5 4 ,3 . 3 2 0 1 2 , 4 . 9 5 3 0
7 for i = 1 : 4 8 printf ( ’ \ nh = %g\n ’ , x ( 1 0 - i ) ) 9 y1=(y(10-i)-y(i))/(2*x(10-i)) 10 printf ( ’ f 1 ( 0 ) = %g\n\n ’ , y 1 ) 11 end
Scilab code Exa 13.7 Richardson Extrapolation
1 / / E x am pl e 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 ar d s o n E x t r a p o l a t i o n / / P ag e n o . 4 3 1 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
o f the f un ct io n y = f ( x ) = e %g ’ , D ( i , j ) )
Scilab code Exa 13.8 Application
1 2 3 4 5 6 7
/ / E x am pl e 1 3 . 8 // Appli cation / / P ag e n o . 4 3 3 clc ; close ; clear ; deff ( ’y=f ( x ) ’ , ’ y=2/xˆ2 ’ ) a=1;b=2;a1=1;b1=0;
188
a t x=0 i s D (3 , 3 ) =
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 a bs ( 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
Scilab code Exa 14.2 Simpsons 1 3rd Rule
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17
/ / E x am pl e 1 4 . 2 / / S i m ps o ns 1 /3 r d R ul e / / Pa ge no 4 42 clc ; clear ; close ; x(1,1)=0 for i = 2 : 9 x(1,i)=x(1,1)+(i-1)*10 end y = [ 3 0 , 3 1 . 6 3 , 3 3 . 4 4 , 3 5 . 4 7 , 3 7 . 7 5 , 4 0 . 3 3 , 4 3 . 2 9 , 4 6 . 6 9 , 5 0 . 67 ]
// t r a p e z o i d a l r u l e S=0; h=(x(9)-x(1))/8 for j = 1 : 9 S=0; 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
end
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
y1(1)=0;
/ / S i m ps o ns 1 /3 r d R ul e 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 so n s 1 / 3 r d R u l e Sum = %g km ’ ,S )
Scilab code Exa 14.3 Trapezoidal Rule and Simpsons Rule
1 / / E x am pl e 1 4 . 3 2 / / T r a p e z o i d a l R ul e and S i mp s on s R ul e 3 / / P ag e n o . 4 4 2
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
clc ; close ; clear ; n=2;a=0;b=1; h=(b-a)/n deff ( ’y=f ( x ) ’ , ’y =1/(1+x ) ’ ) for i = 0 : 2 x(i+1)=i/2; y(i+1)=f(x(i+1)) end printf ( ’ x i \ t ’ ) for i = 1 : 3 printf ( ’ %g\ t ’ , x ( i ) ) end printf ( ’ \n y i \ t ’ ) for i = 1 : 3 printf ( ’ 1/%g\ t ’ ,1+(i-1) /2) end
// t r a p e z o i d a l r u l e S=0; for i = 1 : 3 if ( i == 1 | i = =3 ) S=S+y(i) else S=S+2*y(i) end end S=S*h/2 printf ( ’ \n\ n T r a p e z o i d a l R u le Sum = %g ’ ,S )
/ / S i m ps o ns 1 /3 r d R ul e S=0; for i = 1 : 3 if ( i == 1 | i = =3 ) S=S+y(i) elseif ( ( ( i ) / 2 ) - fix ( ( i ) / 2 ) = = 0 ) S=S+4*y(i) else S=S+2*y(i)
192
42 end 43 end 44 S = S * h / 3 45 printf ( ’ \n\ n S i m p so n s 1 / 3 r d R u l e Sum = %g ’ ,S )
Scilab code Exa 14.5 Romberg Method
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
/ / E x am pl e 1 4 . 5 / /Romberg Method / / P ag e n o . 4 5 7 clc ; close ; clear ; deff ( ’y=f ( x ) ’ , ’y =1/(1+x ) ’ ) h=[0.5,0.25,0.125] for k = 1 : 3 for i = 0 : h ( k ) : 1 x(i/h(k)+1)=i; y(i/h(k)+1)=f(x(i/h(k)+1)) end n=1+(1/h(k))
// t r a p e z o i d a l r u l e
S=0; for i = 1 : n if ( i == 1 | i == n ) S=S+y(i) else S=S+2*y(i) end end S=S*h(k)/2 printf ( ’ \n\ nI (%g) = %g ’ , h ( k ) , S ) z(2*k-1,1)=S
193
27 end 28 for i = 2 : 3 29 for k = 1 : 4 - i z(k*2+i-2,i)=z(2*k-1+i,i-1)+(z(2*k-1+i,i-1)-z(2* 30 k-3+i,i-1))/3 31 end 32 end 33 34 printf ( ’ \n\n ’ ) 35 disp (z , ’ The T ab le o f v a l u e s : ’ )
Scilab code Exa 14.7 Gaussian Quadrature Formula
1 2 3 4 5 6 7 8 9
/ / E x am pl e 1 4 . 7 / / G a u s s i a n Q u a d ra t u r e F or mu la / / P ag e n o . 4 6 3 clc ; close ; clear ;
deff ( ’y=f ( x ) ’ , ’ y= c o s ( x ) ∗ l o g ( x ) ’ ) s=0; for i = 0 : 2 : 2 0 0 0 s = s + i n t e g r a t e( ’ ( ( −1) ˆ( i /2 ) ) ∗ ( x ˆ i ) / f a c t o r i a l ( i ) ∗ l o g ( x ) ’ , ’ x ’ ,0,1) 10 end 11 disp (s , ’ T i l l 1 0 0 0 t e rm s . . . . I = ’ )
Scilab code Exa 14.8 Gauss Legendre Two Point Rule
1 / / E x am pl e 1 4 . 8
194
2 3 4 5 6 7 8 9 10
/ / G a us s L e g e n d re Two P o i n t R u le / / P ag e n o . 4 7 2 clc ; close ; clear ; deff ( ’y=f ( x ) ’ , ’ y=1/(x+3) ’ ) s = i n t e g r a t e( ’ f ( x ) ’ , ’ x ’ ,-1,1) printf ( ’ By D i r e c t Method , I = %g ’ ,s ) s = f ( - 1 / sqrt ( 3 ) ) + f ( 1 / sqrt ( 3 ) ) printf ( ’ \n\n By G au ss−L eg en dr e 2 p o i n t r u l e , I = %g ’ ,s )
Scilab code Exa 14.9 Gauss Legendre Three Point Rule
1 2 3 4 5 6 7 8 9 10
/ / E x am pl e 1 4 . 9 / / G au ss L e g en d r e T hr ee P o i n t R ul e / / P ag e n o . 4 7 3 clc ; close ; clear ; deff ( ’y=f ( x ) ’ , ’ y=1/(x+3) ’ ) s = i n t e g r a t e( ’ f ( x ) ’ , ’ x ’ ,-1,1) printf ( ’ By D i r e c t Method , I = %g ’ ,s ) s = 5 / 9 * f ( - sqrt ( 3 / 5 ) ) + 8 / 9 * f ( 0 ) + 5 / 9 * f ( sqrt ( 3 / 5 ) ) printf ( ’ \n\n By G au ss−L eg en dr e 3 p o i n t r u l e , I = %g ’ ,s )
Scilab code Exa 14.10 Spline Integration Method
1 / / E x am pl e 1 4 . 1 0 2 / / S p l i n e I n t e g r a t i o n Method
195
3 4 5 6 7 8 9 10
/ / P ag e n o . 4 7 8 clc ; close ; clear ;
deff ( ’y=f ( x ) ’ , ’ y= s i n d ( % p i∗x ) ’ ) deff ( ’ y=f1 ( x , h ) ’ , ’ y=(f ( x+h )− f ( x ) ) /h ’ ) h=0.01; n=2;h=0.5;a=0;b=1; disp ( i n t e g r a t e( ’ f ( x ) ’ , ’ x ’ ,0,1), ’ I = ’ )
Scilab code Exa 14.11 Trapezoidal Rule
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/ / E x am pl e 1 4 . 1 / / T r a p e z o i d a l R ul e / / Pa ge no 4 40 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
Scilab code Exa 14.14 Trapezoidal and Simpsons Rule
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
/ / E x am pl e 1 4 . 1 4 / / T r a p e z o i d a l and S i mp s on s R ul e / / P ag e n o . 4 8 6 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 R ul e Sum = ’ ) printf ( ’ \n\n ’ )
/ / s i m p s on 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 R u l e Sum = ’ )
Scilab code Exa 14.15 Trapezoidal and Simpsons Rule
1 2 3 4 5 6 7 8
/ / E x am pl e 1 4 . 1 5 / / T r a p e z o i d a l and S i mp s on s R ul e / / P ag e n o . 4 8 7 clc ; close ; clear ; x(1)=0;y(1)=0;h=0.25 for i = 2 : 5 x(i)=x(i-1)+h
198
9 y(i)=y(i-1)+h 10 e n d 11 printf ( ’ y /x / x \ t | \ t%g t% g \ t%g t% g \ t%g t% g \ t%g t% g \ t%g ’ , x ( 1 ) , x ( 2 ) , x ( 3 ) , x(4),x(5)) 12 printf ( ’ \ n
−−−−−−−−|−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 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 i=1:5 n%g \ t | \ t ’ , y ( i ) ) printf ( ’ \ n%g\ for j=1:5 z(i,j)=x(j)*y(i) %g\ t ’ , z ( i , j ) ) printf ( ’ %g\ 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 i f i == = = 1 & j == = = 1 then s=s+z(i,j) elseif i = =5 =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 ’ ) u l e Sum = ’ ) disp ( s , ’ T r a p e z o i d a l R ul printf ( ’ \ n \ n ’ )
/ / s i m p s on on s r u l e s=0; for i=1:5 for j=1:5 i f i / 2 - i n t ( i /2 / 2 ) == = = 0 & j /2 / 2 -i - i n t ( j / 2 ) = = 0 then
199
44 i f i = = j then s=s+16*z(i,j) 45 46 else s=s+4*z(i,j) 47 48 end 49 50 elseif i / 2 - i n t ( i /2 / 2 ) ~= ~ = 0 & j /2 / 2 -i -int (j/2)~=0 s=s+z(i,j) 51 52 else 53 s=s+4*z(i,j) 54 end 55 end 56 e n d 57 s = ( s * ( h ^ 2 ) ) / 9 58 disp ( s , ’ S i m p s o n s R u l e Sum = ’ )
Scilab code Exa 14.16 Multiple Integration with Variable Limits
1 2 3 4 5 6 7 8 9 10 11
/ / E x am am pl pl e 1 4 . 1 6 / / M u l ti t i p l e I n t e g r a t i o n w it i t h V a r i ab a b l e L i m it it s / / P ag ag e n o . 4 9 1 c l c ; close ; clear ;
deff ( ’ z=f (x ) ’ , ’ z=x+1 ’ ) y+3)ˆ2 ’ ) deff ( ’ z=f1 ( 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 * f 1 ( - sqrt ( 3 / 5 ) ) + 8 / 9 * f 1 ( 0 ) + 5 / 9 * f 1 ( sqrt ( 3 / 5 ) ) s=s/256; disp ( s , ’ I = ’ )
200
Scilab code Exa 14.18 Integration
1 2 3 4 5 6 7
/ / E x am am pl pl e 1 4 . 1 8 // Int egra tio n / / P ag ag e n o . 4 9 4 c l c ; close ; clear ; s = i n t e g r a t e( e ( ’ x ˆ 2∗ 2 ∗ s i n ( x ˆ 2 ) ’ , ’ x ’ ,0,1) disp ( s , ’ I = ’ )
Scilab code Exa 14.19 Integration
1 2 3 4 5 6 7
/ / E x am am pl pl e 1 4 . 1 9 // Int egra tio n / / P ag ag e n o . 4 9 4 c l c ; close ; clear ; s = i n t e g r a t e( e ( ’ si n ( t ) / t ’ , ’ t ’ ,1,999) disp ( s , ’ I = ’ )
201
Chapter 15 Numerical Solutions of Ordinary Differential Equations Initial Value Problem
Scilab code Exa 15.1 Ordinary Differential Equation
1 2 3 4 5 6
/ / E x am am pl pl e 1 5 . 1 // / / O r di d i na n a ry r y D i f f e r e n t i a l E qu q u at a t io io n / / P ag ag e n o . 5 0 3 c l c ; clear ; close ; s = l o g ( 2 ) / l o g (1.02) disp ( s , ’ T im im e T ak ak en en = ’ )
Scilab code Exa 15.6 Taylor Method
1 / / E x am am pl pl e 1 5 . 6
202
2 3 4 5 6 7 8 9 10 11 12 13
// Tay lor Me Meth thod od / / P ag ag e n o . 5 1 0 c l c ; clear ; close ;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
deff ( ’ y=f1 ( x , y ) ’ , ’ y=xˆ2+yˆ 2 ’ ) deff ( ’ y=f2 ( x , y ) ’ , ’ y=2 y=2∗ x +2∗ +2 ∗ y ∗ f1 ( x , y ) ’ ) y=2+2∗∗ f1 ( x , y ) ˆ2+2 ˆ2+2 ∗ y ∗ f2 ( x , y ) ’ ) deff ( ’ y=f3 ( x , y ) ’ , ’ y=2+2 deff ( ’ y=f4 ( x , y ) ’ , ’ y=6 y=6∗ f1 ( x , y ) ∗ f2 ( x , y ) +2∗ +2∗ y ∗ f3 ( x , y ) ’ ) h=0.2; for l=1:2 a=0;y=0;x=0; n h = % g\ g\ n printf ( ’ \ n−−−−−−−−−−−−−−−\ −−−−−−−−−−−−−−−\n n ’ ,h) −−−−−−−−−−−−−−−\n −−−−−−−−−−−−−−−\ for i=1:4 x=a+(i-1)*h k=0; for j=1:4 i f 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; g \ n \ ny(%g) ny(%g) = %g %g \ n \ n ’ , x , x + 0 . 2 , y ) printf ( ’ \ n x = % g\ end h=h+0.2; end
203
Scilab code Exa 15.7 Picard Method
1 2 3 4 5 6 7 8 9 10
/ / E x am pl e 1 5 . 7 / / P i c a r d M et ho d / / P ag e n o . 5 1 1 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=xˆ2+y ˆ2 ’ ) y(1)=0; for i = 1 : 2 y ( i + 1 ) = y ( 1 ) + i n t e g r a t e( ’ f ( x , y ( i ) ) ’ , ’ x ’ ,0,i /10) printf ( ’ \n y ( %g ) = %g\n ’ , i / 1 0 , y ( i + 1 ) ) end
Scilab code Exa 15.8 Euler Method
1 2 3 4 5 6 7 8 9 10 11 12
/ / E x am pl e 1 5 . 8 / / E u l e r M et ho d / / P ag e n o . 5 1 3 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 ) / 1 0 , y ( i ) ) y(i+1)=y(i)+h*f((i-1)/10,y(i)) end
Scilab code Exa 15.9 Trapezium Method
204
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/ / E x am pl e 1 5 . 9 / / T r a p e z i u m M et ho d / / P ag e n o . 5 1 6 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x∗yˆ2 ’ ) y=1; h=0.2; y2 = poly (0 , ’ y 2 ’ ) for i = 1 : 2 x=(i-1)*h; x1=x+h y1 = roots ( - y 2 + y + h * ( f ( x , y ) + f ( x 1 , y 2 ) ) / 2 ) printf ( ’ \n Y( %i ) = %g o r %g\n ’ , i , y 1 ( 1 ) , y 1 ( 2 ) ) end
Scilab code Exa 15.10 Heun Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/ / E x am pl e 1 5 . 1 0 / /Heun Method / / P ag e n o . 5 1 7 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 ’ , x 1 , y ) end
205
Scilab code Exa 15.11 Midpoint Method
1 2 3 4 5 6 7 8
/ / E x am pl e 1 5 . 1 1 / / M i d p o i n t M et ho d / / P ag e n o . 5 1 8
clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=y+x ’ ) y=1; h=0.2; printf ( ’ i \ t x i \ t y i \ t s l o p e 1 \ t s l o p e 2 \ ty ( i +1) \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n ’) 9 for i = 1 : 3 10 x=(i-1)*h s1=f(x,y); 11 12 s2=f(x+h/2,y+s1*h/2); printf ( ’ %i \t%g\t%g\t%g\t%g ’ , i - 1 , x , y , s 1 , s 2 ) 13 14 y=y+s2*h; printf ( ’ \t%g\n ’ ,y ) 15 16 end
Scilab code Exa 15.12 Modified Midpoint Method
1 2 3 4 5
/ / E x am pl e 1 5 . 1 2 / / M o d i f i e d M i d p o i n t Met ho d / / P ag e n o . 5 1 9 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=y+x ’ )
206
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 y 4 = %g ’ , ( 4 * Y ( 4 ) - Y ( 2 ) ) / 3 ) 6 7 8 9 10 11 12 13 14 15
Scilab code Exa 15.13 Single Step Method
1 2 3 4 5 6 7 8 9
/ / E x am pl e 1 5 . 1 3 / / S i n g l e S t e p Method / / P ag e n o . 5 2 1 clc ; clear ; close ;
deff ( ’y=f ( x ) ’ , ’ y=xˆ 2 ’ ) deff ( ’ y= f 1 ( x ) ’ , ’ y=1/(1−x ) ’ ) y=1;h=0.2; printf ( ’ n\ tXn \tYn ( by s i n g l e −s t e p m et ho d ) \ tYn (
computed) \n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 10 for i = 1 : 6 11 x=(i-1)*h if i <6 then 12 13 n ’ ,i-1,x printf ( ’ %i \t% . 2 f \t% . 5 f \ t \ t \ t \t% . 5 f \ ,y,f1(x))
207
14 else printf ( ’ %i \t% . 2 f \t% . 5 f \ t \ t \ t \ t \n ’ , i - 1 , x , y ) 15 16 end y=y+h*f(y); 17 18 end
Scilab code Exa 15.14 Second Order Runge Kutta Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/ / E x am pl e 1 5 . 1 4 / / S e c o n d O r d er Ru ng e K u tt a M et ho d / / P ag e n o . 5 2 5 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x−y ’ ) y=1;x=1;h=0.1;
/ / s i m p l e r u ng 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 un g e k u tt a method = %g\n \n ’ , y 1 )
/ / e u l e r c a uc h y m et ho d
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 au ch y method = %g\n\n ’ , y 1 )
/ / o p t i m a l m et ho d
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 ’ , y 1 )
208
Scilab code Exa 15.15 Third Order Runge Kutta Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/ / E x am pl e 1 5 . 1 5 / / T h i r d O r d er R un ge K u tt a Me th od / / P ag e n o . 5 2 6 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x−y ’ ) y=1;x=1;h=0.1;
/ / s c h em e 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 s ch em e 1 = %g\n\n ’ , y 1 )
/ / s c h em e 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 ’ ,y1) printf ( ’ \ ny ( 1 . 1 ) by s ch em e 2 = %. 7 f \n\n
Scilab code Exa 15.16 Fourth Order Runge Kutta Method
1 / / E x am pl e 1 5 . 1 6 2 / / F o u r t h O r d er Ru ng e K u tt a Met ho d 3 / / P ag e n o . 5 2 8
209
4 5 6 7 8 9 10 11 12 13
clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x−y ’ ) y=1;x=1;h=0.1; K1=h*f(x,y); K2=h*f(x+h/2,y+K1/2); K3=h*f(x+h/2,y+K2/2); K4=h*f(x+h,y+K3); disp ( K 4 , ’ K4 = ’ , K 3 , ’ K3 = ’ , K 2 , ’ K2 = ’ , K 1 , ’ K1 = ’ ) y1=y+(K1+2*K2+2*K3+K4)/6 printf ( ’ \ ny ( 1 . 1 ) = %. 8 f \n\n ’ ,y1)
Scilab code Exa 15.17 New Variant of Runge Kutta Method
1 2 3 4 5 6 7 8 9 10 11 12
/ / E x am pl e 1 5 . 1 7 / / New V a r i a n t o f R un ge K u tt a M et ho d / / P ag e n o . 5 3 0
13 14
clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x−y ’ ) y=1;x=1;h=0.1; K1=h*f(x,y); K2=h*f(x+h/2,y+K1/2); K3=h*f(x+h/2,y+K2/2); K4=h*f(x+h,y+K3); K5=h*f(x+3*h/4,y+(5*K1+7*K2+13*K3-K4)/32) disp ( K 5 , ’ K5 = ’ , K 4 , ’ K4 = ’ , K 3 , ’ K3 = ’ , K 2 , ’ K2 = ’ , K 1 , ’ K1 =’) y1=y+(K1+2*K2+2*K3+K5)/6 printf ( ’ \ ny ( 1 . 1 ) = %. 8 f \n\n ’ ,y1)
210
Scilab code Exa 15.18 Runge Kutta Merson Method
1 2 3 4 5 6 7
/ / E x am pl e 1 5 . 1 8 / / R un ge K u t t a M e rs o n M et ho d / / P ag e n o . 5 3 2 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x+y ’ ) y=1;x=0;h=0.1; printf ( ’ n\ t Xn\ t Yn\ t K1\ t K2\ t K3\ t K4\ t K5\tY(n+1)
\n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 8 9 10 11 12 13 14 15
16 17 18
for i = 0 : 1 4 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 . 3 f \t% printf ( ’ \n %i \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f ’ , i , x , y , K 1 , K 2 , K 3 , K 4 , K 5 , y 1 ) y=y1; x=x+h; end
Scilab code Exa 15.19 Runge Kutta Fehlberg Method
1 2 3 4 5 6
/ / E x am pl e 1 5 . 1 9 / / R un ge K u tt a F e h l b e r g Me th od / / P ag e n o . 5 3 5 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x−y ’ ) y=1;x=1;h=0.1;
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); K 4 = h * f ( x + 1 2 * h / 1 3 , y + 1 9 3 2 * K 1 / 2 1 9 7 - 7 2 0 0 * K 2 / 2 1 9 7 + 7 2 9 6 *K 3 /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 ( K 6 , ’ K6 = ’ , K 5 , ’ K5 = ’ , K 4 , ’ K4 = ’ , K 3 , ’ K3 = ’ , K 2 , ’ K2 = ’ , K 1 , ’ 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) ’ ,y11) printf ( ’ \ ny ˜ ( 1 . 1 ) = % . 9 f \n\n
Scilab code Exa 15.20 Carp Karp Runge Kutta Method
1 2 3 4 5 6 7 8
/ / E x am pl e 1 5 . 2 0 / / C a rp K ar p R un ge K u t ta M et ho d / / P ag e n o . 5 3 7 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x−y ’ ) 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/5
9 a=[37/378,0,250/621,125/594,0,512/1771]; 10 a1 = [ 2 8 2 5 / 2 7 6 4 8 , 0 , 1 8 5 7 5 / 4 8 3 8 4 , 1 3 5 2 5 / 5 5 2 9 6 , 2 7 7 / 1 4 3 3 6 , 1 /4 ] ;
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 ( ’ \ n y ( 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
Scilab code Exa 15.21 Implicit Runge Kutta Method
1 / / E x am pl e 1 5 . 2 1 2 / / I m p l i c i t Runge K ut ta Meth od 3 / / P ag e n o . 5 3 9
213
4 5 6 7 8 9 10 11 12 13
clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x−y ’ ) y = 1 ; x = 1 ; h = 0 . 1 ; printf ( ’ \n ’ ) U=[0,1/5]; v=[0,0;1/2,1/2]; a2=1; K(1)=h*f(x,y); K(2)=(x+h/2-y-K(1)/2)/(1/h-1/2) y1=y+(K(1)+a2*K(2)) printf ( ’ \ ny ( 1 . 1 ) = %. 9 f \n\n ’ ,y1)
Scilab code Exa 15.22 Linear Multi Step Method
1 2 3 4 5 6 7
/ / E x am pl e 1 5 . 2 2 / / L i n e a r M u l t i S t e p Metho d / / P ag e n o . 5 4 0 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x+y ’ ) y(1)=1;y(2)=1;x(1)=0;h=0.1; printf ( ’ n\ tXn \ t \tYn \ t \ t f n \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n 0\ t%g\ t \t% . 3 f \ t \t% . 3 f \n ’ , x ( 1 ) , y ( 1 ) , f ( x ( 1 ) , y ( 1 ) ) ) ; 8 for i = 2 : 1 1 9 x(i)=(i-1)*h; y(i+1)=(-y(i)-y(i-1)+h*(f(x(i),y(i))+f(x(i-1),y( 10 i-1))))/2; 11 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))) 12 end
214
Scilab code Exa 15.23 Milne Simpson Predictor Corrector Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/ / E x am pl e 1 5 . 2 3 / / M i ln e S im ps on P r e d i c t o r C o r r e c t o r Method / / P ag e n o . 5 4 4
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 , f 1 ( 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 ’ ,y41) printf ( ’ \n\n\ n P r e d i c t o r = %. 9 f \n\n 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 ’ , y 4 )
Scilab code Exa 15.24 Improved Milne Simpson Predictor Corrector Method
1 2 3 4 5
/ / E x am pl e 1 5 . 2 4 / / I mp ro ve d M il n e S im ps on P r e d i c t o r C o r r e c t o r Method / / P ag e n o . 5 4 6 clc ; clear ; close ;
215
6 deff ( ’ y=f (x , y ) ’ , ’ y=y−xˆ2 ’ ) 7 y(1)=1;h=0.25;x=0; 8 printf ( ’ n \ tXn tX n \ tYn tY n \ t f n \tY tY n \ tY ‘ n+1\ n+1\ tm(n+1) \ tv (n \ tY ‘ n \ tYn
+1 ) \ n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 f1(1)=f(x,y(1)); 10 f o r i = 1 : 3 11 K1=h*f(x,y(i)); 12 K2=h*f(x+2*h/3,y(i)+2*K1/3); 13 y(i+1)=y(i)+(K1+3*K2)/4 14 n ’ ,i-1,x,y(i),f1(i printf ( ’ %i \ t% . 3 f \t \ t% . 3 f \t \ t% . 3 f \ )) 15 x=x+h f1(i+1)=f(x,y(i+1)) 16 17 e n d 18 Y 3 1 = 0 19 f o r i = 3 : 1 0 Y41=y(i-2)+4*h*(2*f1(4)-f1(3)+2*f1(2))/3 20 //
predictor 21 22 23 24
25 26 27 28 29 30 31 e n d
m4=Y41+28*(y(i+1)-Y31)/29 // mod ifie r // eval uato r v4=f(x+h,m4) Y4=y(i)+h*(v4+4*f1(4)+f1(3))/3 // cor rec tor printf ( ’ %i \ t% . 3 f \t t% . 3 f \t \ t% . 3 f \t \ t% . 3 f \t \ \ t% . 3 f \t \ t% . 3 f \ \ t% . 3 f \t \ t% . 3 f \ \ n ’ , i , x , y ( i + 1 ) , f 1 ( 4 ) , Y 3 1 , 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
216
Scilab code Exa 15.25 Hamming Predictor Corrector Method
1 2 3 4 5 6 7 8
/ / E x am am pl pl e 1 5 . 2 5 / / Ha Ha m m i n g P r e d i c t o r C o r r e c t o r M e t h o d / / P ag ag e n o . 5 4 8 c l c ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=y−xˆ2 ’ ) y(1)=1;h=0.25;x=0; printf ( ’ n \ tXn tX n \ tYn tY n \ t f n \tY n+1 \tm(n+1) \ t v ( \ tY ‘ n \ t Y c ( n ) \ tY ‘ n+1\
n+1) \ t Y c ( n + 1 )\ )\n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 9 f1(1)=f(x,y(1)); 10 f o r i = 1 : 3 11 K1=h*f(x,y(i)); K2=h*f(x+2*h/3,y(i)+2*K1/3); 12 13 y(i+1)=y(i)+(K1+3*K2)/4 printf ( ’ %i \ t% . 3 f \t 14 n ’ ,i-1,x,y(i),f1(i \ t% . 3 f \t \ t% . 3 f \ )) 15 x=x+h 16 f1(i+1)=f(x,y(i+1)) 17 e n d 18 Y 3 1 = y ( 4 ) ; Y c = 0 19 f o r i = 3 : 1 0 Y41=y(i-2)+4*h*(2*f1(4)-f1(3)+2*f1(2))/3 // 20
predictor 21 22 23
m4=Y41+112*(Y31-Yc)/121 // mod ifie r // eval uato r v4=f(x+h,m4) Y4c=(9*y(i+1)-y(i-1))/8+3*h*(v4+2*f1(4)-f1(3))/8
// cor rec tor 24 25
26 27 28
Y4=Y4c+9*(Y41-Y4c)/121 // f i n a l v al ue t% . 3 f \t printf ( ’ %i \ t% . 3 f \t \ t% . 3 f \t \ t% . 3 f \t \ \ t% . 3 f \t \ t% . 3 f \ \ \ t% . 3 f \t \ t% . 3 f \ t% . 3 f \t 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 e n d
f1(3)=f1(4); f1(4)=f(x+h,y(i+2)) Yc=Y4c x=x+h
Scilab code Exa 15.26 Multi Valued Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/ / E x am am pl pl e 1 5 . 2 6 / / M u l t i V a l u e d M et et ho ho d / / P ag ag e n o . 5 5 3 c l c ; clear ; close ;
y=2∗ xˆ2 xˆ 2 − y ’ ) deff ( ’ y=f1 ( x , y ) ’ , ’ y=2 h=0.1;x=0;y=-1; y=4∗ x− f 1 (x , y ) ’ ) deff ( ’ y=f2 ( x , y ) ’ , ’ y=4 deff ( ’ y=f3 ( x , y ) ’ , ’ y=4 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
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n x = 0 \ t \ t \ t x = 0 . 1 \ t \ t \ t \ t x = 0 . 2 \ n \ t ’ ) 17 f o r i = 1 : 2 18 y11=B*y01 19 s(i)=h*(f1(x+h,y11(1)))-y11(2) 20 y1=y11+s(i)*r i f i = = 2 then 21 22 break end 23
218
24 y2=y1; y22=y11; 25 26 y01=y1 27 e n d 28 printf ( ’ \ t ( s = %. 5 g ) \ t \ t
( s = %. 9 f ) \ n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n Y0\ Y0 \ t \ t Y ‘ i \ t \ t Y1\ Y1 \ t \ t Y ‘ 2 \ t \ t Y2\ Y2 \ n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’ ,s(1),s(2))
29 f o r i = 1 : 4 30 ’%. 5 f \ t% . 5 f \ t% . 5 f \ t% . 5 f \ t% . 5 f \ printf ( ’%. \ n ’ , y 0 ( i ),y22(i),y2(i),y11(i),y1(i)) 31 e n d 32 y 0 = [ y ; h * f 1 ( x , y ) ; h ^ 2 * f 2 ( x , y ) / 2 ; h ^ 3 * f 3 ( x , y ) / 6 ] 33 y 0 1 = y 0 ; 34 r = [ 5 / 1 2 ; 1 ; 3 / 4 ; 1 / 6 ] 35 disp ( r , ’ I f r = ’ ) 36 printf ( ’ \ n \ n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n x = 0 \ t \ t \ t x = 0 . 1 \ t \ t \ t \ t x = 0 . 2 \ n \ t ’ ) 37 f o r i = 1 : 2 38 y11=B*y01 39 s(i)=h*(f1(x+h,y11(1)))-y11(2) y1=y11+s(i)*r 40 41 i f i = = 2 then break 42 43 end y2=y1; 44 45 y22=y11; y01=y1 46 47 e n d 48 printf ( ’ \ t ( s = %. 5 g ) \ t \ t ( s = %. 9 f ) \ n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n Y0\ Y0 \ t \ t Y ‘ i \ t \ t Y1\ Y1 \ t \ t Y ‘ 2 \ t \ t Y2\ Y2 \ n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’ ,s(1),s(2)) 49 f o r i = 1 : 4 50 ’%. 5 f \ t% . 5 f \ t% . 5 f \ t% . 5 f \ t% . 5 f \ printf ( ’%. \ n ’ , y 0 ( i
219
),y22(i),y2(i),y11(i),y1(i)) 51 e n d
Scilab code Exa 15.27 First order ODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/ / E x am am pl pl e 1 5 . 2 7 / / F i r s t o r d e r ODE / / P ag ag e n o . 5 5 8 c l c ; clear ; close ; deff ( ’ y=f 1 (x , y1 , y2 ) ’ , ’ y=y1 ∗ y2+x ’ ) =y1−x ’ ) deff ( ’ y=f 2 (x , y1 , y2 ) ’ , ’ y=y1− h=0.2;x=0;y1=0;y2=1;
/ / h e un u n m et et ho ho d printf ( ’ Heun Heun Method Method : \ n \ n x \ t y 1 \ t y 2 \ n n’) −−−−−−−−−−−−−−−−−−−−−−−−−\n −−−−−−−−−−−−−−−−−−−−−−−−−\ Y=[y1;y2] for i=1:8
F=[f1(x,Y(1),Y(2));f2(x,Y(1),Y(2))] Y1=Y+h*F x=x+h; F1=[f1(x,Y1(1),Y1(2));f2(x,Y1(1),Y1(2))] Y=Y+(h/2)*(F+F1) %g\ t% . 3 f \t printf ( ’ %g\ \ t% . 3 f \ \ n ’ , x - h , Y ( 1 ) , Y ( 2 ) )
end
// c l a s s i c a l run ge ku tt a met method hod printf ( ’ \ n \ n \ n C l a s s i c a l R u n g e K ut u t ta ta Met hod : \ n \ n n \ t x tY n \ tK 1 \ tK 2 \ tK 3 \ tK 4 \ tY(n+1) \ n \ tYn −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 220
25 Y = [ y 1 ; y 2 ] ; x = 0 ; 26 for i = 1 : 6 27 K1=h*[f1(x,Y(1),Y(2));f2(x,Y(1),Y(2))] K2=h*[f1(x+h/2,Y(1)+K1(1)/2,Y(2)+K1(2)/2);f2(x+h 28 /2,Y(1)+K1(1)/2,Y(2)+K1(2)/2)] K3=h*[f1(x+h/2,Y(1)+K2(1)/2,Y(2)+K2(2)/2);f2(x+h 29 /2,Y(1)+K2(1)/2,Y(2)+K2(2)/2)] K4=h*[f1(x+h,Y(1)+K3(1),Y(2)+K3(2));f2(x+h,Y(1)+ 30 K3(1),Y(2)+K3(2))] 31 Y1=Y+(K1+2*K2+2*K3+K4)/6 32 . 3 f \t% . 3 f \t% . 3 f \ printf ( ’ %i \t% . 2 f \t% . 3 f \t% . 3 f \t%
t% . 3 f \n\ t \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \t% . 3 f \n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’ ,i-1,x,Y(1),K1(1),K2(1),K3(1),K4(1),Y1(1),Y 33 34 35 end
(2),K1(2),K2(2),K3(2),K4(2),Y1(2)) Y=Y1; x=x+h
Scilab code Exa 15.28 Differential Equation
1 2 3 4 5 6 7 8 9 10 11
/ / E x am pl e 1 5 . 2 8 // D i f f e r e n t i a l E qu at io n / / P ag e n o . 5 6 2 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=2∗y ˆ 2 / ( 1 + x ) ’ ) h=0.1;z(1)=-1; for i = 1 : 1 1 printf ( ’ \nZ(%g) = %g\n ’ , (i - 1 ) / 1 0 , 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
Scilab code Exa 16.1 Outline of Linear Shooting Method
1 2 3 4 5 6 7 8 9 10 11 12 13
/ / E x am pl e 1 6 . 1 / / O u t l i ne o f L i n e a r S h o o ti n g Method / / P ag e n o . 5 7 2 clc ; close ; clear ;
deff ( ’y=f ( x ) ’ , ’ y=xˆ 2 ’ ) ; h=0.5;X0=0;Y0=1;Z1=[-1,-1.5,-1.1771];i=1;Y1=Y0; for j = 1 : 3 Z0=Z1(i); i=i+1 Y0=1; for n = 1 : 2 printf ( ’ \ n Fo r n = %i \n −−−−−−−−−−−−−−−−−−−−−−−−−−−\n ’ ,n-1) 14 K1(1)=h*Z0;
223
15 printf ( ’ \n K11 = %g ’ , K 1 ( 1 ) ) ; K1(2)=h*f(Y0); 16 17 printf ( ’ \n K12 = %g ’ , K 1 ( 2 ) ) ; K2=h*f(Y0+K1(2)) 18 19 printf ( ’ \n K22 = %g ’ , K 2 ) ; Z0=Z0+(K1(2)+K2)/2 20 21 printf ( ’ \n Z%i = %g ’ , n , Z 0 ) ; K2=h*Z0; 22 23 printf ( ’ \n K21 = %g ’ , K 2 ) ; 24 Y0=Y0+(K1(1)+K2)/2 25 printf ( ’ \n Y%i = %g ’ , n , Y 0 ) ; 26 printf ( ’ \n\n\n ’ ) if n = = 1 then 27 28 Y2=Y0 end 29 30 end printf ( ’ \n\n\n ’ ) 31 32 end 33 printf ( ’ H ence t he s o l u t i o n i s y (%g ) = %i , y (%g) = % .4 f and y (%g ) = %. 1 f ’ , X 0 , Y 1 , X 0 + h , Y 2 , X 0 + 2 * h , Y 0 )
Scilab code Exa 16.2 Linear Shooting Method
1 2 3 4 5 6 7 8 9 10
/ / E x am pl e 1 6 . 2 / / L i n e a r S h o o t i n g Meth od / / P ag e n o . 5 7 6 clc ; close ; clear ;
deff ( ’ y=f 1 (x , y , y1 ) ’ , ’ y=−x ∗ y1+xˆ2∗ y+2∗xˆ3 ’ ) deff ( ’ y=F1( x , y , y1 ) ’ , ’ y=−x ∗ y1+xˆ2∗ y+2∗xˆ3 ’ ) deff ( ’ y=F2( x , y , y1 ) ’ , ’ y=−x ∗ y1+xˆ2∗ y ’ ) a=0;b=1; y0=1;y1=-1;n=5;
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); K2=h*(y+R1/2); 50 51 R2=h*f1(x+h/2,y+K1/2,y01+R1/2) K3=h*(y01+R2/2) 52 53 R3=h*f1(x+h/2,y+K2/2,y01+R2/2) K4=h*(y+R3) 54 55 R4=h*f1(x+h,y+K3,y01+R3) y=y+(K1+2*K2+2*K3+K4)/6 56 57 y01=y01+(R1+2*R2+2*R3+R4)/6 58 x=x+h 59 end 60 x = a ; 61 printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n\ t x ’ ) 62 63 64 65 66 67 68 69 70 71 72 73 74
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 ’ , y i ( i ) ) end printf ( ’ \n by RK ’ ) for i = 1 : 6 printf ( ’ \t% . 4 f \ t ’ , y i r ( i ) ) end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 75 printf ( ’ \n\n\ nNote : Co mpu tatio n 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 fo r m e d i n b oo k ’ )
226
Scilab code Exa 16.3 Multiple Shooting Method
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
/ / E x am pl e 1 6 . 3 / / M u l t i p l e S h o o t i n g Metho d / / P ag e n o . 5 7 7 clc ; close ; clear ; h=0.25;x=0;y1=0; deff ( ’y=f ( x ) ’ , ’ y=−(4∗h ˆ 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 ’ , y 1 ) ; for i = 1 : 4 printf ( ’ \t% . 4 f ’ , y ( i ) ) end
227
Scilab code Exa 16.4 Finite Difference Method
1 2 3 4 5 6 7
/ / E x am pl e 1 6 . 4 / / F i n i t e D i f f e r e n c e Method / / P ag e n o . 5 8 2 clc ; close ; clear ; x=0;h=0.25;q=-1;Y(1)=-2;Y(5)=1; printf ( ’ \n i \ t x i \ t Y i \ t p i \ t q i \ t r i \ n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n ’) 8 for i = 1 : 5 r(i)=-x^2 9 10 if i >1 & i <5 then printf ( ’ %i \t%g\ t% s \t%g\ t%i \t%g\n ’ , i - 1 , x , ” ? ” 11 ,x,q,r(i)) 12 else 13 printf ( ’ %i \t%g\t%g\t%g\ t%i \t%g\n ’ , i - 1 , x , Y ( i ),x,q,r(i)) end 14 15 x=x+h 16 end 17 x = 0 ; 18 printf ( ’
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\n ’) 19 for i = 1 : 3 20 x=x+h 21 for j = 1 : 3 22 if i = = j then A(i,j)=2+h^2*q 23 24 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 ti 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
Scilab code Exa 16.5 Non Linear Problem
1 / / E x am pl e 1 6 . 5 2 / / Non L i n e a r P ro bl em
229
3 4 5 6 7 8 9 10
/ / P ag e n o . 5 8 4 clc ; close ; clear ;
deff ( ’y=f ( x ) ’ , ’y =2/(1+x ) ’ ) Y=[1,0.75,0.75,0.75,0.5];h=0.25 A = [ - 2 , 1 , 0 ; 1 , - 2 , 1 ; 0 , 1 , - 2 ] ; A _ 1 =inv ( A ) disp ( A _ 1 , ” I n v e r s e o f A =” ,A , ”A =” ) printf ( ’ \nThe S o l u t i o n o f t he s ys te m 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 −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 11 for i = 0 : 6 printf ( ’ \n 12 %i ’ ,i ) 13 for j = 1 : 5 if j <4 & i ~=0 then 14 15 Y(j+1)=y(j) end 16 17 printf ( ’ \ t \t% . 4 f ’ , Y ( j ) ) end 18 19 x=0; 20 for j = 1 : 3 21 x=x+h 22 if j ~ = 2 then B(j)=h^2*f(x)*Y(j+1)^2-Y(1+2*(j-1)) 23 24 else B(j)=h^2*f(x)*Y(j+1)^2 25 26 end end 27 28 y=A_1*B 29 end
Scilab code Exa 16.6 Collocation Method
230
1 2 3 4 5 6 7 8 9 10 11 12
/ / E x am pl e 1 6 . 6 / / C o l l o c a t i o n Method / / P ag e n o . 5 8 9 clc ; close ; clear ;
h1=0.000001;h=0.25;x=0; Y(1)=0;Y(5)=0; deff ( ’y=p ( x ) ’ , ’ y=1 ’ ) deff ( ’y=q ( x ) ’ , ’ y=−2/(1+x)ˆ2 ’ ) deff ( ’y=f ( x ) ’ , ’ y = (2∗x −4 ) / (1 + x ) ˆ 4 ’ ) deff ( ’ y= f i ( x , j ) ’ , ’ y=(1−x ) ∗xˆ j ’ ) // f u n c t i o n f o r deff ( ’ y=f1 ( x , y ) ’ , ’ y=(−x+y ) /h1 ’ )
differentiation 13 for i = 1 : 4 x=x+h 14 15 for j = 1 : 4 A(i,j)=p(x)*f1(f1(fi(x,j),fi(x+h1,j)),f1(fi( 16 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) 17 end 18 end 19 x = 0 ; 20 for i = 1 : 4 x=x+h 21 22 B(i)=f(x) 23 end 24 disp (B , ’B = ’ ,A , ”A =” ) 25 C = inv ( A ) * B 26 x = 0 ; 27 for i = 2 : 4 28 x=x+h; 29 for j = 1 : 4 30 Y(i)=Y(i)+C(j)*fi(x,j) 31 end 32 end 33 disp (Y , ” S o l u t i o n M at ri x Y = ” )
231
Chapter 18 Numerical Solutions of Parabolic Partial Differential Equations
Scilab code Exa 18.4 Forward Difference Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E x am pl e 1 8 . 4 / / F or wa rd D i f f e r e n c e Method / / P ag e n o . 6 2 4 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 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n | \ t | \ t | \ t x −−>\t ’ ) for i = 0 : 5 printf ( ’%. 3 f \ t ’ ,(i)/5) end
232
16 printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 17 for j = 1 : 6 18 printf ( ’ \n %i \t% . 3 f \ t | \ t \ t ’ , j - 1 , ( j - 1 ) / 5 0 ) for i = 1 : 6 19 20 if i ==1 | i ==6 then u(j,i)=0; 21 22 elseif j = = 1 then 23 u ( j , i ) = sin ( % p i * ( i - 1 ) / 5 ) 24 else 25 u(j,i)=(u(j-1,i-1)+u(j-1,i+1))/2 end 26 27 printf ( ’%. 3 f \ t ’ , u ( j , i ) ) end 28 29 end
Scilab code Exa 18.5 Bender Schmidt Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/ / E x am pl e 1 8 . 5 / / B e n d e r S c h mi d t Met ho d / / P ag e n o . 6 2 5 clc ; clear ; close ; h=0.1;k=0.005; r=k/h^2; printf ( ’ \n j |\ t i for i = 0 : 1 0 %i \ t printf ( ’ end printf ( ’ \n | | \ t x for i = 0 : 1 0 printf ( ’%. 3 f \ t
−−>\t ’ ) ’ ,i ) −−>\t ’ ) ’ ,(i) /10) 233
15 end 16 printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 17 for j = 1 : 9 printf ( ’ \n %i 18 | \ t \ t ’ ,j-1) 19 for i = 1 : 1 1 if i == 1 | i = =1 1 then 20 21 u(j,i)=0; 22 elseif j = = 1 then 23 u ( j , i ) = sin ( % p i * ( i - 1 ) / 1 0 ) 24 else u(j,i)=u(j-1,i)/2+(u(j-1,i-1)+u(j-1,i+1) 25 ) /4 end 26 27 printf ( ’%. 3 f \ t ’ , u ( j , i ) ) end 28 29 end
Scilab code Exa 18.6 Crank Nicolson Method
1 2 3 4 5 6 7 8 9 10 11 12
/ / E x am pl e 1 8 . 6 / / C ra nk N i c o l s o n Metho d / / P ag e n o . 6 3 1 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 ’) 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
for i = 1 : 9 printf ( ’ \n %i ’ ,i ) for j = 1 : 3 printf ( ’ \ t %. 9 f ’ , u ( i , j ) ) end end
h=1/4;k=1/8; r=k/h^2; for i = 1 : 4 : 5 for j = 1 : 9 if i ==1 | j ==1 then u(i,j)=0; end if i = = 5 then u(i,j)=(j-1)/8
235
48 49 50 51 52 53 54
55 56 57 58 59 60 61 62 63 64
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 ’) 65 for i = 1 : 9 66 printf ( ’ \n %i ’ ,i ) for j = 1 : 5 67 68 %. 9 f ’ , u ( i , j ) ) printf ( ’ \ t end 69 70 end
Scilab code Exa 18.7 Gauss Seidel Method
1 / / E x am pl e 1 8 . 7 2 / / G au ss S e i d e l Method 3 / / P ag e n o . 6 3 7
236
4 5 6 7 8 9 10 11 12 13 14 15 16
clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=4∗x−4∗xˆ2 ’ ) h=0.2;k=0.04; r=k/h^2; printf ( ’ \n k \ t | \ t i −−>\t ’ ) for i = 0 : 5 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n | \ t | \ t x −−>\t ’ ) for i = 0 : 5 printf ( ’%. 2 f \ t ’ ,(i)/5) end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 17 for k = 1 : 7 printf ( ’ \n %i \ t | \ t \ t ’ ,k-1) 18 19 for i = 1 : 6 if i ==1 | i ==6 then 20 21 u(k,i)=0; 22 elseif k = = 1 then 23 u(k,i)=f((i-1)/5) 24 else u(k,i)=(u(k-1,i-1)+u(k-1,i+1))/2 25 26 end printf ( ’%. 2 f \ t ’ , u ( k , i ) ) 27 28 end 29 end
Scilab code Exa 18.8 ADI Method
1 / / E x am pl e 1 8 . 8 2 / /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
/ / P ag e n o . 6 4 2 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 = [ 2 5 , 3 0 , 3 5 , 5 0 , 6 0 ; 3 5 , 0 , 0 , 0 , 7 0 ; 4 5 , 0 , 0 , 0 , 8 0 ; 6 0 , 7 0 , 8 0 , 10 0 , 9 0 ] 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 ’ , P 1 ( 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 e l s ei f a b s( 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 e l s ei f a b s( 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; B h = [ r * 3 0 + ( 1 - 2 * r ) * v 1 ( 1 ) + r * v 1 ( 4 ) + r * 3 5 ; r * 3 5 + ( 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 v1 = inv ( A v ) * B v 73 74 u1 = inv ( A h ) * B h k=1; 75 76 for i = 2 : 3 for j = 2 : 4 77 78 P1h(i,j)=u1(i+j+k-4) end 79 80 k=k+2 81 end 82 k=0; 83 for j = 2 : 4 for i = 2 : 3 84 85 P1v(i,j)=v1(i+j+k-3) end 86 87 k=k+1 end 88 89 end 90 printf ( ’ \n\n\n\ n R e s u l ts f o r V e r t i c a l T r a n s v e rs e i n C el s iu s : \ n ’ ) 91 for i = 1 : 7 92 printf ( ’ \n ’ ) 93 if i = = 1 then printf ( ’ I t r −−> ’ ) 94 95 for j = 1 : n printf ( ’ \ t %i ’ , j - 1 ) 96 97 end printf ( ’ \n 98
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 99 else 100 printf ( ’ v %i ’ , i - 1 ) 101 for j = 1 : n 102 printf ( ’ \t% . 2 f ’ , v ( i - 1 , j ) ) 103 end 104 end 105 end 106 printf ( ’ \n\n\n\ n R e s u l ts f o r H o r i z o n ta l T ra n sv er s e i n
240
C el s iu s : \ n ’ ) 107 for i = 1 : 7 108 printf ( ’ \n ’ ) if i = = 1 then 109 110 printf ( ’ I t r −−> ’ ) for j = 1 : n 111 112 printf ( ’ \ t %i ’ , j - 1 ) end 113 114 printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 115 else printf ( ’ u %i ’ , i - 1 ) 116 117 for j = 1 : n printf ( ’ \t% . 2 f ’ , u ( i - 1 , j ) ) 118 119 end end 120 121 end
241
Chapter 19 Numerical Solutions of Hyperbolic Partial Differential Equations
Scilab code Exa 19.3 Simple Explicit Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E x am pl e 1 9 . 3 / / S i mp l e E x p l i c i t Method / / P ag e n o . 6 5 8 clc ; clear ; close ; c=-2;dt=0.07;dx=0.2; r = abs ( c ) * d t / d x ; printf ( ’ \n x \ t i \ t | \ t j −−>\t ’ ) for i = 0 : 6 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n | \ t | \ t | \ t t −−>\t ’ ) for i = 0 : 6 printf ( ’%. 3 f \ t ’ , i * d t ) end
242
16 printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 17 for j = 1 : 6 18 t%i \ t | \ t \ t ’ , ( j - 1 ) * d x , j - 1 ) printf ( ’ \n % . 1 f \ for i = 1 : 7 19 20 if i = = 1 then u(j,i)=0; 21 22 elseif j = = 1 then 23 u(j,i)=1 24 else 25 u(j,i)=(1-r)*u(j,i-1)+r*u(j-1,i-1) end 26 27 printf ( ’%. 3 f \ t ’ , u ( j , i ) ) end 28 29 end
Scilab code Exa 19.4 Simple Implicit Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/ / E x am pl e 1 9 . 4 / / S i mp l e I m p l i c i t Method / / P ag e n o . 6 5 9 clc ; clear ; close ; c=-2;dt=0.07;dx=0.2; r = abs ( c ) * d t / d x ; printf ( ’ \n x \ t i \ t | \ t j −−>\t ’ ) for i = 0 : 6 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n | \ t | \ t | \ t t −−>\t ’ ) for i = 0 : 6 printf ( ’%. 3 f \ t ’ , i * d t )
243
15 end 16 printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 17 for j = 1 : 6 printf ( ’ \n % . 1 f \ 18 t%i \ t | \ t \ t ’ , ( j - 1 ) * d x , j - 1 ) 19 for i = 1 : 7 if i = = 1 then 20 21 u(j,i)=0; 22 elseif j = = 1 then 23 u(j,i)=1 24 else u(j,i)=(1/(1+r))*u(j,i-1)+r*u(j-1,i)/(1+ 25 r) end 26 27 printf ( ’%. 3 f \ t ’ , u ( j , i ) ) end 28 29 end
Scilab code Exa 19.5 Lax Wendroff Method
1 2 3 4 5 6 7 8 9 10 11 12
/ / E x am pl e 1 9 . 5 / / L a x W e n d r o f f M et ho d / / P ag e n o . 6 6 0 clc ; clear ; close ; c=-2;dt=0.07;dx=0.2; r = abs ( c ) * d t / d x ; 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 14 15 16 17
for i = 0 : 6 printf ( ’%. 3 f \ t ’ , i * d t ) end i=1; printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 18 for j = 1 : 7 19 for i = 1 : 6 20 if j = = 1 then 21 u(i,j)=0; 22 u(i+1,j)=0; elseif i = = 1 then 23 24 u(i,j)=1 else 25 26 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 27 end end 28 29 end 30 for i = 1 : 6 31 t%i \ t | \ t \ t ’ , ( i - 1 ) * d x , i - 1 ) printf ( ’ \n % . 1 f \ 32 for j = 1 : 7 printf ( ’%. 3 f \ t ’ , u ( i , j ) ) 33 34 end 35 end
Scilab code Exa 19.6 Wendroff Method
1 2 3 4
/ / E x am pl e 1 9 . 6 / / W e n d r o f f M et ho d / / P ag e n o . 6 6 1 clc ; clear ; close ;
245
5 6 7 8 9 10 11 12 13 14 15 16
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
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 17 for i = 1 : 6 18 t%i \ t | \ t \ t ’ , ( i - 1 ) * h , i - 1 ) printf ( ’ \n % . 1 f \ for j = 1 : 7 19 20 if j = = 1 then u(i,j)=0; 21 22 elseif i = = 1 then 23 u(i,j)=1 24 else 25 u(i,j)=u(i-1,j-1)+(u(i,j-1)-u(i-1,j))/a end 26 27 printf ( ’%. 3 f \ t ’ , u ( i , j ) ) end 28 29 end
Scilab code Exa 19.7 Leapfrog Method
1 / / E x am pl e 1 9 . 7 2 / / L e a p f r o g M et ho d 3 / / P ag e n o . 6 6 2
246
4 5 6 7 8 9 10 11 12 13 14 15 16
clc ; clear ; close ; c=2;k=0.07;h=0.2; r=c*k/h printf ( ’ \n x \ t i \ t | \ t j −−>\t ’ ) for i = 0 : 6 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n | \ t | \ t | \ t t −−>\t ’ ) for i = 0 : 6 printf ( ’%. 3 f \ t ’ , i * k ) end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 17 18 for j = 1 : 7 19 for i = 1 : 6 if j ==1 | j ==2 & i ~=1 then 20 21 u(i,j)=0; 22 u(i+1,j)=0; 23 elseif i = = 1 then 24 u(i,j)=1 else 25 26 u(i,j)=u(i,j-2)-r*(u(i+1,j-1)-u(i-1,j-1) ) 27 end end 28 29 end 30 for i = 1 : 6 31 t%i \ t | \ t \ t ’ , ( i - 1 ) * h , i - 1 ) printf ( ’ \n % . 1 f \ 32 for j = 1 : 7 33 printf ( ’%. 3 f \ t ’ , u ( i , j ) ) 34 end 35 end
247
Scilab code Exa 19.8 Variable Coefficients
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/ / E x am pl e 1 9 . 8 // V a ri a b l e C o e f f i c i e n t s / / P ag e n o . 6 6 3 clc ; clear ; close ;
// s i mp l e e x p l i c i t method printf ( ’ \n\nBy S i m pl 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 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n \ t \ t \ t | \ t t −−>\t ’ ) for i = 0 : 6 printf ( ’%. 3 f \ t ’ , i * d t ) end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 19 for j = 1 : 6 20 r = sqrt ( 1 + 2 * x ) * d t / d x ; 21 printf ( ’ \n %i \t% . 3 f \t% . 3 f \ t | \ t \ t ’ , ( j - 1 ) , x , r ) for i = 1 : 7 22 23 if i = = 1 then u(j,i)=0; 24 25 elseif j = = 1 then u(j,i)=1 26 27 else u(j,i)=(1-r)*u(j,i-1)+r*u(j-1,i-1) 28
248
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
end
end printf ( ’%. 3 f \ t ’ , u ( j , i ) ) end x=x+dx
/ / s i m p l e i m p l i c i t method printf ( ’ \n\n\nBy S i mp l e I m p l i c i t Method : \ n ’ ) c=-2;dt=0.05;dx=0.2;x=0 r \ t | \ t j −−>\t ’ ) printf ( ’ \n i \ t x \ t for i = 0 : 6 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n \ t \ t \ t | \ t t −−>\t ’ ) for i = 0 : 6 printf ( ’%. 3 f \ t ’ , i * d t ) end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 49 for j = 1 : 6 r = sqrt ( 1 + 2 * x ) * d t / d x ; 50 51 printf ( ’ \n %i \t% . 3 f \t% . 3 f \ t | \ t \ t ’ , ( j - 1 ) , x , r ) for i = 1 : 7 52 53 if i = = 1 then u(j,i)=0; 54 55 elseif j = = 1 then u(j,i)=1 56 57 else 58 u(j,i)=(1/(1+r))*u(j,i-1)+r*u(j-1,i)/(1+ r) 59 end 60 printf ( ’%. 3 f \ t ’ , u ( j , i ) ) 61 end x=x+dx 62 63 end
249
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/ / w e n d r o f f m et ho d printf ( ’ \n\n\nBy Wendro ff Method : \ n ’ ) k=0.05;h=0.2; x=0.1; c \ t a \ t | \ t j −−>\t ’ ) printf ( ’ \n i \ t x \ t for i = 0 : 6 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n \ t \ t \ t \ t | \ t t −−>\t ’ ) for i = 0 : 6 printf ( ’%. 3 f \ t ’ , i * k ) end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 79 for i = 1 : 6 c = sqrt ( 1 + 2 * x ) ; 80 81 a=(h+k*c)/(h-k*c) 82 printf ( ’ \n %i \t% . 3 f \t% . 3 f \t% . 3 f \ t | \ t \ t ’ , ( i - 1 ) , x h/2,c,a) 83 for j = 1 : 7 if j = = 1 then 84 85 u(i,j)=0; u(i+1,j)=0; 86 87 elseif i = = 1 then u(i,j)=1 88 89 else u(i,j)=u(i-1,j-1)+(u(i,j-1)-u(i-1,j))/a 90 91 end 92 printf ( ’%. 3 f \ t ’ , u ( i , j ) ) 93 end 94 x=x+h 95 end
250
Scilab code Exa 19.9 Inhomogeneous 1st Order Hyperboolic Differential
Equation 1 / / E x am pl e 1 9 . 9 2 // I nh om og en eo us 1 s t O rder H y pe r bo o l ic
Di ff er en ti al
Equation 3 / / P ag e n o . 6 6 5 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
clc ; clear ; close ;
// s i mp l e e x p l i c i t method printf ( ’ \n\nBy S i m pl e E x p l i c i t Method : \ n ’ ) c=-2;dt=0.07;dx=0.2; r = abs ( c ) * d t / d x ; i \ t x \ t | \ t j −−>\t ’ ) printf ( ’ \n for i = 0 : 6 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n | \ t | \ t | \ t t −−>\t ’ ) for i = 0 : 6 printf ( ’%. 3 f \ t ’ , i * d t ) end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 19 x = 0 ; 20 for j = 1 : 6 21 printf ( ’ \n %i \t% . 1 f \ t | \ t \ t ’ , j - 1 , x ) for i = 1 : 7 22 23 if i = = 1 then u ( j , i ) = exp ( - x ) ; 24 25 elseif j = = 1 then u(j,i)=1 26
251
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
else
end
u(j,i)=(1-r)*u(j,i-1)+r*u(j-1,i-1)+dt*2* x end printf ( ’%. 3 f \ t ’ , u ( j , i ) )
end x=x+dx
/ / s i m p l e i m p l i c i t method printf ( ’ \n\n\nBy S i mp l e I m p l i c i t Method : \ n ’ ) c=-2;dt=0.07;dx=0.2; r = abs ( c ) * d t / d x ; 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 * d t ) end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 49 x = 0 ; 50 for j = 1 : 6 printf ( ’ \n %i \t% . 1 f \ t | \ t \ t ’ , j - 1 , x ) 51 52 for i = 1 : 7 if i = = 1 then 53 54 u ( j , i ) = exp ( - x ) ; 55 elseif j = = 1 then 56 u(j,i)=1 57 else 58 u(j,i)=(1/(1+r))*u(j,i-1)+r*u(j-1,i)/(1+ r)+dt*2*x end 59 60 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
end
end x=x+dx
/ / w e n d r o f f m et ho d printf ( ’ \n\n\nBy Wendro ff 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 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n | \ t | \ t | \ t t −−>\t ’ ) for i = 0 : 6 printf ( ’%. 3 f \ t ’ , i * k ) end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 79 x = 0 ; 80 for i = 1 : 6 81 printf ( ’ \n % . 1 f \ t%i \ t | \ t \ t ’ , x , i - 1 ) for j = 1 : 7 82 83 if j = = 1 then u ( i , j ) = exp ( - x ) ; 84 85 elseif i = = 1 then u(i,j)=1 86 87 else u(i,j)=u(i-1,j-1)+(u(i,j-1)-u(i-1,j))/a 88 +(2*h*k)*(x+h/2)/(a*(h+c*k)) 89 end 90 printf ( ’%. 3 f \ t ’ , u ( i , j ) ) 91 end 92 x=x+h 93 end
253
Scilab code Exa 19.10 Non Linear 1st Order Hyperboolic Differential Equa-
tion 1 / / E x am pl e 1 9 . 1 0 2 / /Non L i n ea r 1 s t O rder H y pe r bo o l ic
Di ff ere nt ia l
Equation 3 / / P ag e n o . 6 6 7 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
clc ; clear ; close ; c=-2;k=0.05;h=0.2; r = abs ( c ) * k / h ; printf ( ’ \n i \ t x \ t | \ t j −−>\t ’ ) for i = 0 : 6 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n | \ t | \ t | \ t t −−>\t ’ ) for i = 0 : 6 printf ( ’%. 3 f \ t ’ , i * k ) end i=1; x=0; printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 19 for j = 1 : 7 for i = 1 : 6 20 21 if j = = 1 then u ( i , j ) = exp ( - x ) ; 22 23 u ( i + 1 , j ) = exp ( - ( x + h ) ) ; elseif i = = 1 then 24 25 u(i,j)=1 else 26
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
Scilab code Exa 19.11 Finite Difference Method
1 2 3 4 5 6 7 8 9 10 11 12 13
/ / E x am pl e 1 9 . 1 1 / / F i n i t e D i f f e r e n c e Method / / P ag e n o . 6 7 0
clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y= s i n ( % p i∗ 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 printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ nfi ’) 15 16 17 18 19 20 21 22 23 24 25 26 27 28
for i = 1 : n f1(i)=f(h*(i-1)) printf ( ’ \t% . 3 f ’ , f 1 ( i ) ) end printf ( ’ \ n g i ’ ) for i = 1 : n g1(i)=g(h*(i-1)) printf ( ’ \ t %g ’ , g 1 ( i ) ) end printf ( ’ \n\n\n i / j −−> ’ ) for i = 1 : m printf ( ’ \ t %i ’ ,i ) end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 29 for j = 1 : m 30 for i = 1 : n 31 if i ==1 | i == n then 32 u(i,j)=0; elseif j = = 1 33 34 u(i,j)=f1(i) elseif j = = 2 35 36 u(i,j)=s1*f1(i)+k*g1(i)+r2*(f1(i+1)+f1(i -1)) 37 else u(i,j)=s2*u(i,j-1)+r1*u(i-1,j-1)+u(i+1,j 38 -1)-u(i,j-2) 39 end 40 41 end 42 end 43 for i = 1 : n printf ( ’ \n %i \ t ’ ,i ) 44 45 for j = 1 : m
256
46 printf ( ’ \t% . 3 f ’ , u ( i , j ) ) end 47 48 end
Scilab code Exa 19.12 Hyperbolic Partial Differential Equations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/ / E x am pl e 1 9 . 1 2 / / H y p e r b o l ic P a r t i a l D i f f e r e n t i a l E qu at io ns / / P ag e n o . 6 7 3
clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=12∗x ’ ) Ua(1)=0.25; Ua(2)=0.75 A=[1,-2;1,2]; x1 = inv ( A ) * U a ; and Tb = %g ’ , x 1 ( 1 ) , x 1 ( 2 ) ) printf ( ’Xb = %g A=[2,-1;2,1]; B=[-7.5;-8.5]; x2 = inv ( A ) * B ; and Qb = %g ’ , x 2 ( 1 ) , x 2 ( 2 ) ) printf ( ’ \n\n Pb = %g x1(1)=x1(1)-Ua(1) du=x1’*x2 printf ( ’ \n\n dU = %g ’ , d u ) Ub=f(Ua(1))+du; printf ( ’ \n\n M o d i f i e d Ub = %g ’ , U b )
Scilab code Exa 19.13 Hyperbolic Differential Equations in 2D or 3D
1 / / E x am pl e 1 9 . 1 3
257
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// H y p e r b o l ic D i f f e r e n t i a l E qu at io ns i n 2D o r 3D / / P ag e n o . 6 7 5 clc ; clear ; close ; deff ( ’ y=f (x , y ) ’ , ’ y=x∗(2 − x ) ∗ y∗(2 − y ) ’ ) c2=3;k=0.4;h=0.4;c2=3;s2=0.5 for l = 0 : 1 1 if l = = 0 then printf ( ’ \n t = %i \n\n i \ t x \ t | \ t j −−>\t ’ ,l ) for i = 0 : 5 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n | \ t | \ t | \ t y −−>\t ’ ) for i = 0 : 5 printf ( ’%. 3 f \ t ’ , i * k ) end x=0; printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 20 for i = 1 : 6 21 y=0; 22 printf ( ’ \n %i \t% . 3 f \ t | \ t \ t ’ , i - 1 , x ) for j = 1 : 6 23 24 if i ==1 | i ==6 then u(i,j)=0; 25 26 elseif j = =1 | j ==6 then u(i,j)=0 27 28 else u(i,j)=f(x,y) 29 30 end 31 printf ( ’%. 3 f \ t ’ , u ( i , j ) ) 32 y=y+k; 33 end 34 x=x+h 35 end 36 u 2 = u ; 37 else
258
38 39 40 41 42 43 44 45 46 47
printf ( ’ \n\n\n t = %i \n\n i \ t x \ t | \ t j −−>\t ’ ,l ) for i = 0 : 5 %i \ t ’ ,i ) printf ( ’ end printf ( ’ \n | \ t | \ t | \ t y −−>\t ’ ) for i = 0 : 5 printf ( ’%. 3 f \ t ’ , i * k ) end x=0; printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 48 49 50 51 52 53 54 55 56 57
58 59
60 61 62 63 64 65 66 67 68 69
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 end if l >1 then u2=u1 end
259
70 u 1 = u ; 71 end
260
Chapter 20 Numerical Solutions of Elliptical Partial Differential Equations
Scilab code Exa 20.1 Direct Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E x am pl e 2 0 . 1 / / D i r e c t M et ho d / / P ag e n o . 6 8 2 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 * % p i ) ^ 2 end end
261
16 17 18 19
disp (A , ’A = ’ ) disp (B , ’B = ’ ) U = inv ( A ) * B disp (U , ’U = ’ )
Scilab code Exa 20.2 Five Point Formula
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
/ / E x am pl e 2 0 . 2 / / F i v e P o i n t F or mu la / / P ag e n o . 6 8 3 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 , u 1 ( 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
elseif i = =1 | i ==4 u(i,j)=u(i,j-1)*2 else u(i,j)=u1((j-1)*2-i+2) end printf ( ’ \t%g\ t ’ , u ( i , j ) )
Scilab code Exa 20.3 Finite Difference Method
1 2 3 4 5 6
/ / E x am pl e 2 0 . 3 / / F i n i t e D i f f e r e n c e Method / / P ag e n o . 6 8 5 clc ; clear ; close ; printf ( ’ I t r \ t \ t U11\ t \ t U21\ t \ t U12\ t \ t U22\n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 7 for i = 1 : 4 8 for j = 1 : 4 if j = = 1 then 9 10 u(i,j)=0 elseif j = = 4 11 12 u(i,j)=100 elseif ( i ==1 | i == 4) & j ==2 13 14 u(i,j)=25 15 elseif i = =1 | i ==4 16 u(i,j)=u(i,j-1)*2 17 else u(i,j)=0 18 19 end end 20
263
21 end 22 for k = 0 : 1 7 23 %i \ t \t% . 3 f \ t \t% . 3 f \ t \t% . 3 f \ t \t% . 3 f \n ’, printf ( ’ k,u(3,2),u(3,3),u(2,2),u(2,3)) 24 for i = 3 : - 1 : 2 for j = 2 : 3 25 26 u1(i,j)=(u(i,j+1)+u(i,j-1)+u(i-1,j)+u(i +1,j))/4 27 end 28 end 29 for i = 3 : - 1 : 2 30 for j = 2 : 3 u(i,j)=u1(i,j) 31 32 end end 33 34 end 35 disp (u , ’U = ’ )
Scilab code Exa 20.4 Seven Point Formula
1 2 3 4 5
/ / E x am pl e 2 0 . 4 / / S e v en P o i n t F or mu la / / P ag e n o . 6 8 6 clc ; clear ; close ; printf ( ’ I t r \ t \ t U1 11 \ t \ t U 21 1\ t \ t U1 21 \ t \ t U 22 1\n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 6 for i = 1 : 4 7 for j = 1 : 4 8 for k = 3 : - 1 : 1 if k = = 3 then 9 10 u(i,j,k)=100 elseif ( i ==1 | i ==4 | j ==1 | j ==4) & k 11
264
12 13 14 15
==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
16 17 18 19 20 end 21 end 22 end 23 k =2 24 for l = 0 : 1 4 25 %i \ t \t% . 3 f \ t \t% . 3 f \ t \t% . 3 f \ t \t% . 3 f \n ’, printf ( ’ l,u(3,2,2),u(3,3,2),u(2,2,2),u(2,3,2)) 26 for i = 3 : - 1 : 2 for j = 2 : 3 27 28 u1(i,j)=(u(i,j+1,k)+u(i,j-1,k)+u(i-1,j,k )+u(i+1,j,k)+u(i,j,k+1)+u(i,j,k-1))/6 29 end 30 end for i = 3 : - 1 : 2 31 32 for j = 2 : 3 u(i,j,2)=u1(i,j) 33 34 end end 35 36 end
Scilab code Exa 20.5 Nine Point Formula
1 / / E x am pl e 2 0 . 5
265
2 3 4 5 6
/ / N i n e P o i n t F or mu la / / P ag e n o . 6 8 8 clc ; clear ; close ; printf ( ’ I t r \ t \ t U11\ t \ t U12\ t \ t U21\ t \ t U22\n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 7 for i = 1 : 4 8 for j = 1 : 4 9 if j = = 1 then 10 u(i,j)=0 11 elseif j = = 4 u(i,j)=100 12 13 elseif ( i ==1 | i == 4) & j ==2 u(i,j)=25 14 15 elseif i = =1 | i ==4 u(i,j)=u(i,j-1)*2 16 17 else u(i,j)=0 18 19 end 20 end 21 end 22 for k = 0 : 1 7 printf ( ’ 23 %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 24 25 for j = 2 : 3 u1(i,j)=(u(i+1,j-1)+u(i-1,j-1)+u(i+1,j 26 +1)+u(i-1,j+1)+4*(u(i,j+1)+u(i,j-1)+u (i-1,j)+u(i+1,j)))/20 27 end 28 end 29 for i = 3 : - 1 : 2 30 for j = 2 : 3 31 u(i,j)=u1(i,j) 32 end end 33 34 end
266
35 disp (u , ’ The S o l u ti o n o f t he System i s = ’ )
Scilab code Exa 20.6 Five Point Formula
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
/ / E x am pl e 2 0 . 6 / / F i v e P o i n t F or mu la / / P ag e n o . 6 8 9 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 ; for j = 1 : 5 27
267
28 %g\ t ’ ,x ) printf ( ’ \ t x=x+k 29 30 end 31 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 : 2 0 34 y=0; 35 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 ’ , l , u ( 4 , 2 ) , u ( 4 , 3 ) , u ( 4 , 4 ) ,
36 37 38 39 40 41 42 43 44 45 46 47 end
u(3,2),u(3,3),u(3,4),u(2,2),u(2,3),u(2,4)) for i = 4 : - 1 : 2 y=y+k for j = 2 : 4 u1(i,j)=(u(i,j+1)+u(i,j-1)+u(i-1,j)+u(i +1,j)-h^2*y)/4 end end for i = 4 : - 1 : 2 for j = 2 : 4 u(i,j)=u1(i,j) end end
Scilab code Exa 20.7 Laplace Distribution
1 2 3 4
/ / E x am pl e 2 0 . 7 / / L a p la c e D i s t r i b u t i o n / / P ag e n o . 6 9 4 clc ; clear ; close ;
268
5 6 dr=3;r0=4;dth=%pi/4; 7 deff ( ’ y=f ( u1 , u2 , u3 , u4 ) ’ , ’y=(u1+u3+(dr ∗ ( u3−u1 ) ) /(2 ∗ r 0
)+(u2+u4) ∗ ( dr / ( r0 ∗ dth ) ) ˆ2 ) /(2 ∗ (1 +( dr /( r0 ∗ dth ) ) ˆ2 ) ) ’) // l a p l a c e d i s t r i b u t i o n 8 for i = 1 : 8 9 U(i)=0; 10 end 11 printf ( ’ I t r \ t
U1\ t
U2\ t
U3\ t
U4\ t
U5\ t
U6\ t
U7\ t U8\n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 12 for l = 0 : 1 5 13 printf ( ’ \n %i ’ ,l ) for i = 1 : 8 14 15 if i = = 1 then u1(i)=f(100,U(8),40,U(i+1)) 16 17 elseif i = = 8 u1(i)=f(100,U(i-1),40,U(1)) 18 19 else 20 u1(i)=f(100,U(i-1),40,U(i+1)) 21 end 22 end 23 24 for i = 1 : 8 U(i)=u1(i) 25 26 printf ( ’ \t% . 3 f ’ , U ( i ) ) end 27 28 end
Scilab code Exa 20.8 Spherical Coordinate System
1 / / E x am pl e 2 0 . 8
269
2 3 4 5 6
/ / S p h e r i c a l C o o r d i n a te S ys te m / / P ag e n o . 6 9 7
clc ; clear ; close ; deff ( ’ y= c o t ( x ) ’ , ’y=1/t an ( x ) ’ ) dr=5;r0=50;dth=%pi/4;dfi=%pi/4;N=-10;Z=60;Nb=0;Zt =70; 7 deff ( ’ y=f ( u1 , u2 , u3 , u4 , u5 , u6 , t h0 ) ’ , ’ y = ( ( u 1+u 3 ) / d r ˆ 2+ (
u3−u1 ) /( r0 ∗ dr )+(u2+u4 ) /( r0 ∗ dth )ˆ2+(u2 ∗ co t ( th 0 ) /( r0 ˆ2∗ dth )+(u5+u6 ) /( r0 ∗ s i n ( th0 ) ∗ d f i ) ˆ2 ) ) /(2 / dr ˆ2+2/( r0 ∗ dth )ˆ2+ co t ( th 0 ) /( r0 ˆ2 ∗ d t h ) + 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 s p h e r i c a l c o o r d in a t e 8 T1=40;T2=20;H1=35;H2=10;B1=5;B2=0;t1=240;t2=180;b1 =100;b2=80;h1=210;h2=150 9 printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 10 s =[ ”T” , ”H” , ”B” , ” t ” , ” h” , ” b ” ]; 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
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
if i = = 1 then printf ( ’ T em pe ra tu re D i s t r i b u t i o n i n O ut er S p h e r e \n ’ ) printf ( ’
31 32 33
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n’) end if i = = 4 then printf ( ’ \ n Te mp er at ur e D i s t r i b u t i o n S p h e r e \n ’ ) printf ( ’
34 35 36 37
i n I n n er
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n’) 38 39 40 41 42 43 44 45 46 47 48 49 50 51
end printf ( ’ \ n P o i n t : ’ ) for j = 1 : 8 printf ( ’ \ t%s%i ’ , s ( i ) , j ) end printf ( ’ \ n Te mp er at ur e : ’ ) for j = 1 : 8 if ( j ==1 | j == 2) & i ==5 then printf ( ’ \ t%s ’ , ” ? ” ) else printf ( ’ \ t%i ’ , A l ( i , j ) ) end end printf ( ’ \n
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− ’) 52 end 53 t h 0 = 1 0 ^ - 3 0 54 U h 1 = f ( 1 0 0 0 , A l ( 5 , 8 ) , A l ( 2 , 1 ) , A l ( 5 , 2 ) , A l ( 6 , 1 ) , A l ( 4 , 1 ), th0) 55 disp ( U h 1 , ’ Uh1 = ’ ) 56 t h 0 = % p i / 4 ; 57 U h 2 = f ( 1 0 0 0 , U h 1 , A l ( 2 , 2 ) , A l ( 5 , 3 ) , A l ( 6 , 2 ) , A l ( 4 , 2 ) , t h 0) 58 disp ( U h 2 , ’ Uh2 = ’ )
271
Chapter 21 Advances in Numerical Methods Using Parallel Computing Paradigm
Scilab code Exa 21.1 Parallel Bisection Method
1 2 3 4 5 6 7 8
/ / E x am pl e 2 1 . 1 // P a r a l l e l B i s e c t i o n Method / / P ag e n o . 7 2 1 clc ; close ; clear ; deff ( ’y=f ( x ) ’ , ’ y=xˆ2− c o s ( x ) ’ ) a=0;b=1;e=0.0001;i=1; printf ( ’ I t r \ t a \ tb \ th \ t \ t x 0 \ t \ t x 1 \ t \ t x 2 \ t \ t x 3 \ t \ t x 4 \ t
\ 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;
Scilab code Exa 21.2 Lagrange Interpolation in Parallel Computing
1 2 3 4 5 6 7 8
/ / E x am pl e 2 1 . 2 // L a gr an ge I n t e r p o l a t i o n i n P a r a l l e l Computing / / P ag e n o . 7 2 3 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) ”] 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
for i = 1 : 4 printf ( ’ \ t P r o c e s s o r \ t ’ ) end printf ( ’ \n ’ ) for i = 1 : 4 N%i\ t \ t ’ ,i ) printf ( ’ \ t end printf ( ’ \n ’ ) for i = 1 : 7 for j = 1 : 4 %s\ t \ t ’ , s ( i ) ) printf ( ’ end printf ( ’ \n ’ ) end x=1;T=0; for k = 0 : 3 p=yi(k+1) for j = 0 : 3 if ( j ~ = k ) p=p*((x-xi(j+1))/(xi(k+1)-xi(j+1))) end end T=T+p; printf ( ’ \nT( %i) = %g ’ , k + 1 , p ) end printf ( ’ \n\nT = %g ’ ,T )
Scilab code Exa 21.3 Trapezoidal Rule and Simpsons Rule in Parallel Com-
puting 1 / / E x am pl e 2 1 . 3
274
2 // T r a pe z o id a l Ru le and S im ps on s Ru le i n P a r a l l e l
Computing 3 / / P ag e n o . 7 2 6 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
clc ; close ; clear ; n=8;a=0;b=8; h=(b-a)/n deff ( ’y=f ( x ) ’ , ’y =1/(1+x ) ’ ) for i = 0 : 8 x(i+1)=i; y(i+1)=f(x(i+1)) end printf ( ’ x i \ t ’ ) for i = 1 : 9 printf ( ’ %i \ t ’ , x ( i ) ) end printf ( ’ \n y i \ t ’ ) for i = 1 : 9 printf ( ’ 1 / %i\ t ’ ,i ) end
// t r a p e z o i d a l r u l e S=0; for i = 1 : 9 if ( i == 1 | i = =9 ) S=S+y(i) else S=S+2*y(i) end end S=S*h/2 printf ( ’ \n\ n T r a p e z o i d a l R u le Sum = %g ’ ,S )
/ / S i m ps o ns 1 /3 r d R ul e S=0; for i = 1 : 9 if ( i == 1 | i = =9 ) S=S+y(i) elseif ( ( ( i ) / 2 ) - fix ( ( i ) / 2 ) = = 0 )
275
39 S=S+4*y(i) else 40 41 S=S+2*y(i) end 42 43 end 44 S = S * h / 3 45 printf ( ’ \n\ n S i m p so n s 1 / 3 r d R u l e Sum = %g ’ ,S )
Scilab code Exa 21.4 Parallel Gauss Seidel Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/ / E x am pl e 2 1 . 4 / / P a r a l l e l Gauss−S e i d e l Method / / P ag e n o . 7 3 0 clc ; close ; clear ;
16 17 18 19 20 21
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 oc 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 ’ ) %i \ t \t%g\t%g\ t \t%g\t%g\n ’ ,0 , o l d ( 1 ) , n e w ( 1 ) , printf ( ’ 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 ’ , o l d ( j ) , n e w ( j ) )
end printf ( ’ \n ’ ) old(1)=new(1) old(2)=new(2)
Scilab code Exa 21.5 Poissons Partial Differential Equation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/ / E x am pl e 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 qu at io n / / P ag e n o . 7 3 3 clc ; clear ; close ; s =[ ” s t ” , ”nd” , ” r d ” ] for i = 4 : 2 0 s(i)=” t h ” end h = 0 . 2 5 ; 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 ’ ) 29 x=0; for j = 1 : 5 30 31 %g\ t \ t ’ ,x ) printf ( ’ x=x+h 32 33 end end 34 35 printf ( ’ \n ’ ) 36 end 37 38 printf ( ’ \n\n\n ’ ) 39 40 for l = 0 : 1 7 y=1; 41 42 if l ~ = 0 then printf ( ’ A f te r t he %i%s I t e r a t i o n 43
: \n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ n %i ’ , l , s ( l ) , l )
44 45 46 47 48 49 50 51
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 ; for i = 4 : - 1 : 2 64 65 y=y+h for j = 2 : 4 66 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 P(i,j)=P1(i,j) 72 73 end end 74 75 end
279
Chapter 22 Numerical Methods Using Neural Networks
Scilab code Exa 22.1 MLP Algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/ / E x am pl e 2 2 . 1 //MLP A lg or it hm / / P ag e n o . 7 4 8
clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’y =1/(1+ex p(−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 Af te r I t e r a t i o n %i : \ n\n ’ ,k ) disp ( W i h , ’ Wih = ’ ) disp ( W h o , ’Who = ’ ) a1=i*Wih; disp ( a 1 , ’ 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 ( b 1 , ’ 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 ( d W h o , ’dWho =’ ) Who=Who+dWho; dWih=a*i’*e; disp ( d W i h , ’dWih = ’ ) Wih=Wih+dWih; end
Scilab code Exa 22.2 MLP
1 2 3 4 5 6 7
/ / E x am pl e 2 2 . 2 //MLP / / P ag e n o . 7 5 8 clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=xˆ3−xˆ2+x−1 ’ ) printf ( ’ I n p u t \ t \ t D e s i r e d \ t \ t N e t w o r k \ t \ t E r r o r \n\ t \ t
Output \ t \ t O ut pu t\n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ n ’) 8 in
= [ 0 . 7 5 7 2 , 0 . 7 6 0 1 , 0 . 7 6 2 0 , 1 . 4 8 3 1 , 1 . 4 8 7 4 , 1 . 4 9 0 0 , 2 . 0 9 1 3 ,2 . 0 9 3 4 , 2 . 1 0 0 6 ,
281
9 n
= [ - 0 . 3 9 41 , - 0 . 38 9 6 , - 0 .3 8 6 7 , 1 . 60 5 4 , 1 . 6 25 9 , 1 . 6 3 91 , 5 . 87 6 2 , 5 . 8 96 9 , 5 . 9 6 10 for i = 1 : 1 8 printf ( ’ %. 4 f \ 11 t \t% . 4 f \ t \t% . 4 f \ t \t% . 4 f \ n ’ , i n ( i ) , f (in(i)),n(i),n(i)-f(in(i))) 12 end
Scilab code Exa 22.3 Bisection Method
1 2 3 4 5 6 7
/ / E x am pl e 2 2 . 3 / / B i s e c t i o n Metho d / / P ag e n o . 7 6 4 clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=xˆ3−xˆ2+x−1 ’ ) printf ( ’ N01\ tN02 \ tN11 \ tN12 \ tN21 \ t N e t 3 1 \ tO31 \ tN41 \
tN42 \n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− n ’) 8 N01 = [ 0 , 1 , 0 . 5 , 0 . 7 5 , 0 . 8 7 5 , 0 . 9 3 8 , 0 . 9 6 9 , 0 . 9 8 4 , 0 . 9 9 2 , 0 . 9 9 6 ,0 . 9 9 8 , 0 . 9 9 9 , 1 , 9 10 11 12 13 14 15 16 17
N02(1)=2 for i = 2 : 1 3 N02(i)=1; end for i = 1 : 1 3 net31(i)=f(N01(i+1))*f(N01(i)) if n e t 3 1 ( 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 , N 0 2 ( i ) , f ( N 0 1 ( i ) ) , N01(i+1),f(N01(i+1)),net31(i),O31(i),N41( i),N42(i)) else . 3 f \t% . 3 f \t% . 3 f \t% . 3 f printf ( ’%. 3 f \t% . 3 f \t% . 3 f \t% \t% . 3 f \t% . 3 f \n ’ , N 0 1 ( i ) , N 0 2 ( i ) , f ( N 0 1 ( i ) ) , N 0 1 ( 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 er e f o r e t he s o l u t i o n
i s %. 3 f ’ , N 4 2 ( 1 3 ) )
Scilab code Exa 22.4 Hopfield Neural Network
1 2 3 4 5 6 7 8 9 10 11 12
/ / E x am pl e 2 2 . 4 / / H o p f i e l d N e u r a l N et wo rk / / P ag e n o . 7 6 6 clc ; clear ; close ; A=[1,2,1;-1,1,1;1,0,-1]; disp ( inv ( A ) , ’ I n v e rs e o f A = ’ ,A , ’A = ’ ) for i = 1 : 3 for j = 1 : 3 k=0; for l = 1 : 3 k=k+A(i,l)*A(j,l)
283
13 end T(i,j)=k; 14 15 end 16 end 17 disp (T , ’T = ’ )
Scilab code Exa 22.5 RBF Network
1 2 3 4 5 6 7
/ / E x am pl e 2 2 . 5 //RBF Netwo rk / / P ag e n o . 7 7 3 clc ; clear ; close ; deff ( ’y=f ( x ) ’ , ’ y=10∗ s i n ( x ) ’ ) printf ( ’ I n p u t \ t \ t D e s i r e d \ t \ t N e t w o r k \ t \ t E r r o r \n\ t \ t
Output \ t \ t O ut pu t\n −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−\ n ’) 8 in
= [ 0 . 7 0 5 3 , 0 . 7 0 6 0 , 0 . 7 0 9 7 , 1 . 5 0 5 6 , 1 . 5 1 0 3 , 1 . 5 3 7 7 , 2 . 2 4 8 1 ,2 . 2 5 1 4 , 2 . 2 5 9 9 , 9 n
= [ 6 . 4 8 2 8 , 6 . 4 8 8 3 , 6 . 5 1 6 4 , 9 . 9 7 8 6 , 9 . 9 8 1 6 , 9 . 9 9 4 4 , 7 . 7 9 2 6 ,7 . 7 7 1 8 , 7 . 7 1 8 0 , 10 for i = 1 : 1 8 printf ( ’ %. 4 f \ 11 t \t% . 4 f \ t \t% . 4 f \ t \t% . 4 f \ n ’ , i n ( i ) , f (in(i)),n(i),f(in(i))-n(i)) 12 end
284