Numerical Methods for Computer Sciences, Essays (university) of Computer Science

• Exposition and Program Design for; - Methods of Interpolation - Halving the Square Method - Newton Raphson’s Method - Regular Falsi Mathod - Gaussian Elimination Method - Trapezoid Rule, Simpson’s Rule

Typology: Essays (university)

2016/2017

Uploaded on 10/25/2017

Samik_1
Samik_1 🇳🇬

5

(1)

1 document

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page | 1
INTRO
DUCTI
ON TO
NUME
Computer
Math’s
BY MR.
IORLAHA
S.K.
MCOEG.
Computer
Math’s
Exp
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Numerical Methods for Computer Sciences and more Essays (university) Computer Science in PDF only on Docsity!

Page | 1

INTRO

DUCTI

ON TO

NUME

Computer Math’s IORLAHA^ BY MR. MCOEG. S.K. ComputerMath’s

Exp

  • Page |

L 2 = (x-x (^) o)(x-x (^) 1)/(x2-x (^) o)(x (^) 2-x (^) 1) = (x-1) (x-1.5)/(3-1)(3-1.5) = (x 2 -2.5x+1.5)/ Therefore: P2(x)= 5 (x 2 -4.5x+4.5) + ( -3 )[-4/3(x^2 -4x+3)] + 0 [(x 2 -2.5x+1.5)/3]

5 (x 2 -4.5x+4.5) – 3 [-4/3(x 2 -4x+3)] + 0 Example 3 Interpolate for f(2.3) from the table below. X 1.1 1.7 3. F(x) 10.6 15.2 20. Solution: P2(x) = ni=0 Li(x).f(x (^) i) = L (^) o(x).f(x (^) o) + L1(x).f(x (^) 1) +L2(x).f (x (^) 2) where f(2.3)=x=2.3; xo = 1.1, x (^) 1=1.7, x2=3.0; y (^) o=10.6, y1=15.2, y (^) 2=20. Lo = (x-x (^) 1)(x-x (^) 2)/(xo-x (^) 1)(x (^) o-x (^) 2)= (2.3-1.7)(2.3-3.0)/ (1.1-1.7)(1.1-3.0) = -0. L 1 = (x-x (^) o)(x-x (^) 2)/(x1-x (^) o)(x (^) 1-x (^) 2) = (2.3-1.1)(2.3- O3.0)/(1.7-1.1)(1.7-3.0) = 1. L 2 = (x-x (^) o)(x-x (^) 1)/(x2-x (^) o)(x (^) 2-x (^) 1) = (2.3-1.1) (2.3-1.7)/(3.0-1.1)(3.0-1.7) = 0. P2(2.3) = 10.6(-0.37) + 15.2(1.08) + 20.3(0.29) =

EXERCISE

a. Interpolate for f(0.65) from the table below using Lagrange polynomial. x 0.40 0.50 0.70 0. y -0.916 -0.693 -0.357 -0. b. (^) Given the following points: (0,0), (1, -1), (8, 2); write down the interpolating polynomial. NEWTON’S DIVIDED DIFFERENCE INTERPOLATION FORMULA (NDIF) Let y=f(x) take the values of f(xo), f(x (^) 1), …, f(x (^) n) where the arguments x (^) o, x1, … , xn corresponds to them. By definition, f(x, x (^) o) = f(x)-f(x (^) o)/( x- xo) f(x)=f(x)+(x- x (^) o)f(x, x (^) o) -----------(1) Similarly, f(x, x (^) o, x1 ) = f(x, x (^) o, x1)= f(x, x (^) o)-f(x (^) o, x1)/ x- x (^1) Therefore; f(x, x (^) o) =f(x (^) o, x1)+ (x-x1)F(x, x (^) o,x1) Using the value of f(x, x (^) o) in eqn(1) above; F(x)= f(x (^) o)+( x-xo)f(x (^) o, x1)+(x-x (^) 1)f(x, x (^) o, x1) ---------------- (2) Again, f(x, x (^) o, x1, x2) =[ f(x, xo, x1)-f(x (^) o, x1, x1)]/ (x-x 2 )

Then f(x, x (^) o, x1) = f(xo, x1, x2)+(x-x (^) 2)f(x, x (^) o, x1, x2) Using the value in eqn. (2); we have F(x)=f(xo)+(x-x (^) o)f(x,x (^) o)+(x-x (^) 1)[f(x (^) o, x1, x2)+(x- x (^) 2)f(x, x (^) o, x1, x2)] F(x)=f(xo)+(x-x (^) o)f(x (^) o,x1)+ (x-x (^) o)(x-x (^) 1) f(x (^) o, x1, x2)

  • (x-x (^) o)(x-x (^) 1) (x-x (^) 2) f(x, x (^) o, x1, x2) ------ (3) Continuing in the like manner, we have; F(x)=f(xo)+(x-x (^) o)f(x (^) o,x1)+ (x-x (^) o)(x-x (^) 1) f(x (^) o, x1, x2)
  • (x-x (^) o)(x-x (^) 1) (x-x (^) 2) f(x (^) o, x1, x2, x3)+…+ (x-xo)(x- x1) (x-x (^) 2)…(x-x (^) n)f(x, x (^) o, x1, x2,…, xn) --------------- (4). If f(x) is a polynomial of degree n, then the eqn. becomes thus: F(x)=f(xo)+(x-x (^) o)f(x (^) o,x1)+ (x-x (^) o)(x-x (^) 1) f(x (^) o, x1, x2) +…+ (x-x (^) o)(x-x (^) 1)…(x-x (^) n-1)f(x (^) o, x1, x2,…, xn) ……………..(5). Example

Using Newton’s divided difference formula, find the values of f(2), f(8), and f(15) given the table below.

Given the table below, form the forward difference table xi 0 1 2 3 4 yi -2 1 4 13 34 Solution:

xi yi yo 2 yo 3 yo 4 yo 0 -2 Leading term 3 1 1 0 3 6 2 4 6 0 9 6 3 13 12 21 4 34 Coefficients of forward difference. Thus we have in the above table; yi = -2, y (^) o =3; 2 yo =0; 3 yo = 6 and 4 yo = 0. Exercise Obtain the forward difference table for the following relations: a)

x 1 2 3 4 5 6 y 4 15 40 85 156 259 b)

x 0 1 2 3 4 5 6 y -1 3 19 53 111 199 323

To Find any Term in Terms of y (^) o, 2 yo, e.t.c. we have the relation yk = E k^ yo; where E k^ is known as Inverse Operator. = (1+ )y (^) ok = (1+ kC 1 yo + kC 2 2 + … k^ )y (^) o. = y (^) o + kC 1 yo + kC 2 2 yo + …+ k^ yo; Where k is the nth term (position) to be found. Example 2 From the forward difference table in example 1, find the value of y5. Solution 3 y5^ = y(x=5) = -2+^5 C^1 yo^ +^5 C^2 2 yo^ +^5 C^3 yo

  • 5 C 4 4 yo + 5 C 5 5 yo = -2 + 5X3 + 5X4^ / 2 X 0 + 5X4X3/ (^) 3X2 X6 + 0 = -2 + 15 + 0 + 60 = 73. Assignment II
  1. Discuss the inverse operator (E -1^ ).
  2. Find the first term of the series whose 2 nd and subsequent terms are 8,3,0,-1,0,…
  3. Find the 7 th^ and 8th^ terms from the table below: x 0 1 2 3 4 5 6 7 8 y -1 3 19 53 111 199 323

SOLUTIONS OF ALGEBRAIC AND TRANSCENDENTAL EQUATIONS (NON- LINEAR EQUATIONS) The equations with the form: f(x) = x 3 -5x+4=0 are said to be Algebraic; while those with the form: f (x) = cos x + 3sin2x + e x^ are said to be transcendental. The following methods can be used to solve Algebraic and Transcendental equations:

  • Bisection method(halving the square)
  • False position method (Regular Falsi)
  • Fixed point iteration method
  • Newton Raphson method METHOD OF BISECTION Given f(x), continuous function;
  • Initialization : Find a, b, such that f(a). f(b) < 0. This means there is a root r∈ (a, b) s.t. f(r) = 0.
  • Let c = a+b/2, Mid-point.
  • If f(c) = 0; done (Lucky!).
  • Else: check if f(c).f(a)<0 or f(c).f(b)<0.
  • Pick that interval [a,c] or [c, b], and repeat the procedure until stop criteria satisfied. Convergence Analysis Consider [ao, b (^) o], c (^) o= ao+b (^) o/2, let r∈(a (^) o, bo) be a root. The error: eo=r - co bo-ao/ if the error tolerance is E, we require enE, then -b (^) o-ao/2n+1^ E. Example 1. Find the root of the algebraic equation x^3 – x – 1 = 0, x∈[1, 2]. Solution f(a) = f(1) = 1^3 – 1 – 1 = -1 <0. f(b) = f(2) = 2^3 – 2 – 1 = 5 > therefore; this shows that there is at least a root of f(x) = x^3 – x – 1 in [a, b].

thus: x1 = 1+2^ / 2 = 1. Now test for f(x1) … if the result equals 0, satisfied(as according to the rule); i.e. f(1.5) = (1.5) 3 – 1.5 – 1 = 0.875 …… >0; a=1, b=1.5. since f(x1)>0: x2∈[1, 1.5]. thus: x (^) 2= 1+1.5/ 2 = 2.5/ 2 = 1.25. Repeat from the action test , until f(x) = 0, then stop. i.e. F(1.25) = (1.25) 3 – 1.25 – 1 = -0.296875 ……… <0. x3∈[1.25, 1.5] Thus; x 3 = 1.25+1.5^ / 2 = 1.375.

f(1.375) = (1.375) 3 – 1.375 – 1 = 0.224……

x 4 ∈ [1.25, 1.375] x 4 = 1.25+1.375/ 2 =1.

f(1.3125) = 1.3125 3 -1.3125-1 = -0.0515…..< Thus a=1.3125; b=1. x5∈[1.3125, 1.375] ξ 5 = 1.3175+1.375^ / 2 = 1.

φ

(1.34375)=1.34375 3 −1.34375−1= 0.08261 ……………> Τηυσ α=3.3125; β=3. ξ 6 ∈[1.3125, 1.34375] Χοντινυε υντιλ E is met or the required no of iterations is reached. ALGORITHM FOR BISECTION METHOD Purpose: Algorithm for finding the solution of f (x)=0 given that f(x) is continuous in [a, b] and f (a) and f(b) are of opposite signs. Input: End points a, b; tolerance, tol; max number of iterations. Output: Approximate soln. P or a message of failure. Steps; 1: Set i=1; fA=f(a);

  1. While iNo. do steps 3 – 6
  2. Set P = a+(b-a)/ If P<tol. Then fP = f(P)
  3. If fP=0 or (b-a)/2 < tol. Then output P. {Procedure completed}
  4. Set i = i+
  5. End.

Solution: f(0) = 0^3 -4(0) + 1 = 1 and f(1) = 1^3 – 4(1) + 1 = 1-4+1 = - This clearly shows that x∈[0, 1] hence a (^) o = 0, bo=1. xo = bof(ao)-aof (b (^) o) f(ao)

  • f(b (^) o) = 1(1)-0(-2)^ / (^) 1-(-2) = 1 / 3 = 0. f(x (^) o) = 0.3333^3 – 4(0.3333) + 1 = -0.2963 ……… <0. a 1 =0, b 1 =0. Continue until E =0.004 ( Home Work ) ALGORITHM FOR REGULAR FALSI METHOD Purpose: The algorithm for the soln. of the equation f(x) = 0, x∈[a, b] and f(a).f(b)<0 by R.F. method. Input: End points of interval a, b; Tolerance h; n- iterations N. Output: Approximate soln. of f(x) or a failure message. Step I: Initialization: set i=1, P (^) o=f(a), P (^) 1=f(b) Step II: While iN Do steps III to VII

Step III: x = (bPo – aP (^) 1)/P (^) o-P (^1) Step IV: If x-b <h THEN Output x. {Procedure completed} Step V: Set I = i+1, P = f(x) Step VI: If P * P (^) 1<0 THEN set a=x, Po=P (^1) Step VII: b=x, P1=P Step VIII: Output message of failure after N iterations. Step IX: End. Assignment 3 Draw the flowchart for the R.F. method and state four features of R-F method. NEWTON RAPHSON METHOD The Newton Raphson method for the numerical solution of an equation f(x)=0 is of great importance. Suppose f(x) is continuous in the interval [a, b], let x (^) o∈[a, b] be an approximate in P such that f’(x)0 and x (^) o-P0. Then the Taylor’s Polynomial expansion about x in f(x) = f (x (^) o) + (x-xo)f’(x (^) o) + (x-x (^) o) 2 f’’(x (^) o) + (x-x (^) o) 3 f’’’(x (^) o) +… 2! 3!

  • (x-x (^) o) nf n(x (^) o) n! f(x (^) o) = (P-x (^) o)f’(x) + (P-xo) 2 f’’ 0 1 04 1 9(P) Where 0 1 04 1 9(P) is between x and x (^) o.

Since P – x (^) o is small, P – xo^2 and higher order is smaller. Hence we may write: f(x (^) o) + f’(xo)(P-x (^) o) = 0 f(x (^) o) +f’(xo)P – f’(x (^) o)x (^) o =0 (Open brackets) f’(xo)P = f’(x (^) o)x (^) o – f(x (^) o) (Make P the subject…) f’(xo) f’(x (^) o) f’(x (^) o) P = x (^) o – [f(x (^) o)/f’(x (^) o)] ----------------------- (1.0) Thus the Newton’s method begins with an initial guess Po to generate a sequence of {P (^) n} defined by: Pn = P (^) n-1 – [f(Pn-1 )/f’(Pn-1 )] ------------------(1.1) Substitute Pn with xn and x (^) n≃x (^) n+1, eqn. (1.1) becomes: Xn+1 = x (^) n+1-1 – f(x (^) n+1-1) f’(xn+1-1 ) Xn+1 = x (^) n – f(x (^) n) ----------------------(1.2) f’(xn) Now, equation (1.2) becomes the N-R scheme for solving equation f(x) = 0. Example Solve by the N-R method the equation: f (x)=x 4 -x-10. Let our P (^) o (guess value) be 1.5 (or x (^) o in this case)

f(x) = x^4 – x – 10 f’(x) = 4x^3 – 1. Therefore test the value of Xo in f(x) and f’(x) f(1.5) = 1.5^4 – 1.5 – 10 = -6. f’(1.5) = 4(1.5)^3 – 1 = 12.

x 1 = 1.5 –(-6.4375)/12 = 2.

f(x (^) 1) = (2.015)^4 -2.015 – 10 = 4. f’(2.015) = 4(2.015)^3 – 1 = 31.

x 2 = 2.015 – (4.4704)/31.725 = 1.

… Continue for eight iterations. Excersice: Solve the following equations using N-R method.

  1. f(x) = x^3 -x-1=
  2. f(x) = cosx-xex^ = 0
  3. find the value of 1 / 31 to 4d.p using N-R method.
  4. Find the cube root of 24 to 3d.p
  5. X^3 + 2x 2 + 50x + 7 = 0

ALGORITHM FOR NEWTON RAPHSON

METHOD

Input: initial approximate Po, tolerance tol., n- iterations N Output: approximate solution P or message of failure. Step 1: set i=1 {initial counter} Step 2: WHILE iN Do steps 3-

  1. P=Po-f(P (^) o)/f’(P (^) o)
  2. If P-P (^) o < tol. THEN output P{procedure completed}
  3. Set i=i+
  4. Set P (^) o = P {update P}
  5. Output message of failure after N no of iterations
  6. End. GAUSSIAN ELIMINATION METHOD OF SOLVING SYSTEMS OF LINEAR EQUATIONS Consider the system of linear equations of the form:

a 11 x1 + a12x2 + a12x3 + … + a (^) 1nxn = b (^1)

a21x1 + a22x2 + a23x3 + … + a (^) 2nxn =b (^2) .

. ----(1) . an1x1 + an2x2 + an3x3 + … + a (^) nnxn = b (^) n This can be written in a matrix form as: a 11 a 12 a 13 … a1n x1 b a 21 a 21 a 23 … a2n x2 b .. =. ---------- (2) ... ... an1 a (^) n2 an3 … ann xn bn This is equivalent to the matrix AX = B Where

a 11 a 12 … a (^) 1n x1 b 1 A = a 21 a 21 … a (^) 2n x2 b 2

. X =. and B=.

c. 8x – 3y + 2z = 20 4x + 11y – z = 33 6x + 3y + 12z = 35 d. 2x + 3y – z = 12 6x – 3z = 6 3x – 3y + 2z = 8

x f(x) f(x) 2 f(x) 3 f(x) 4 48 52 5 100 15 97 1 7 294 21 202 1 10 900 27 310 1 11 1210 33 409 13 2028

By NDIF: F(x) = f(x (^) o)+(x-xo)f(x (^) o,x1)+ (x-x (^) o)(x-x (^) 1) f(x (^) o, x1, x (^) 2)+ (x-x (^) o)(x-x (^) 1) (x-x (^) 2) f(x (^) o, x1, x2, x3)+… Given that x (^) o=4, x1=5, x (^) 2=7, x (^) 3=10, x (^) 4=11, x5=13; and f(xo)= 48, f(xo ,x1)=52, f(x (^) o, x1, x2)=15, f(x (^) o, x1, x2, x3)=1 … F(x)= 48+(x-4)(52)+(x-4)(x-5)(15)+(x-4)(x-5) (x-7)(1)+ F(x)= 48+52x-208+(x 2 -9x+20)(15)+[x 3 -16x 2 +83-140] F(2)= 48+52(2)-208+(4-18+20)(15)+8-16(4)+ (2)-