computational mathematics - jaipur national universityjnujprdistance.com/assets/lms/lms...

184
Computational Mathematics

Upload: others

Post on 20-Apr-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

Page 2: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Board of Studies

Prof. H. N. Verma Prof. M. K. GhadoliyaVice- Chancellor Director, Jaipur National University, Jaipur School of Distance Education and Learning Jaipur National University, JaipurDr. Rajendra Takale Prof. and Head AcademicsSBPIM, Pune

___________________________________________________________________________________________

Subject Expert Panel

Prof. Milind M. Kulkarni Ashwini PanditProfessor, Sinhgad College of Engineering Subject Matter ExpertPune

___________________________________________________________________________________________

Content Review Panel

Tejaswini MulaySubject Matter Expert

___________________________________________________________________________________________Copyright ©

This book contains the course content for Computational Mathematics.

First Edition 2013

Printed byUniversal Training Solutions Private Limited

Address05th Floor, I-Space, Bavdhan, Pune 411021.

All rights reserved. This book or any portion thereof may not, in any form or by any means including electronic or mechanical or photocopying or recording, be reproduced or distributed or transmitted or stored in a retrieval system or be broadcasted or transmitted.

___________________________________________________________________________________________

Page 3: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

I

Index

ContentI. ...................................................................... II

List of FiguresII. ........................................................... V

AbbreviationsIII. .........................................................VI

ApplicationIV. ........................................................... 165

BibliographyV. .......................................................... 172

Self Assessment AnswersVI. ..................................... 175

Book at a Glance

Page 4: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

II

Contents

Chapter I ....................................................................................................................................................... 1Introduction to Errors in Numerical Calculations ................................................................................... 1Aim ................................................................................................................................................................ 1Objectives ...................................................................................................................................................... 1Learning outcome .......................................................................................................................................... 11.1 Errors and their Computation ................................................................................................................... 21.2 Absolute, Relative and Percentage Errors ................................................................................................ 31.3 General Error Formulae ........................................................................................................................... 61.4 Errors in a Series Approximation ............................................................................................................. 7Summary ..................................................................................................................................................... 12References ................................................................................................................................................... 12Recommended Reading ............................................................................................................................. 12Self Assessment .......................................................................................................................................... 13

Chapter II ................................................................................................................................................... 15Solution to Algebraic and Transcendental Equations ............................................................................ 15Aim .............................................................................................................................................................. 15Objectives .................................................................................................................................................... 15Learning outcome ........................................................................................................................................ 152.1 Bisection Method ................................................................................................................................... 162.2 Method of False Position ....................................................................................................................... 182.3 Newton -Raphson Method ..................................................................................................................... 27 2.3.1 Newton-Raphson Iteration ..................................................................................................... 27Summary ..................................................................................................................................................... 30References ................................................................................................................................................... 30Recommended Reading ............................................................................................................................. 30Self Assessment ........................................................................................................................................... 31

Chapter III .................................................................................................................................................. 33Interpolation ............................................................................................................................................... 33Aim .............................................................................................................................................................. 33Objectives .................................................................................................................................................... 33Learning outcome ........................................................................................................................................ 333.1 Introduction to Interpolation .................................................................................................................. 343.2 Forward Difference ................................................................................................................................ 343.3 Backward Difference ............................................................................................................................. 373.4 Newton’s Forward Difference Interpolation .......................................................................................... 493.5 Newton’s Backward Difference Interpolation ....................................................................................... 533.6 Lagrange’s Interpolation ........................................................................................................................ 56Summary ..................................................................................................................................................... 72References ................................................................................................................................................... 72Recommended Reading ............................................................................................................................. 72Self Assessment .......................................................................................................................................... 73

Chapter IV .................................................................................................................................................. 75Least Square Curve Fitting ....................................................................................................................... 75Aim .............................................................................................................................................................. 75Objectives .................................................................................................................................................... 75Learning outcome ........................................................................................................................................ 754.1 Curve Fitting .......................................................................................................................................... 764.2 Method of Least Square ......................................................................................................................... 764.3 Polynomials Least-Squares Fitting ........................................................................................................ 76

Page 5: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

III

4.3.1 The Least-Squares Line ......................................................................................................... 76 4.3.2 The Least-Squares Parabola ................................................................................................... 77 4.3.3 The Least-Squares mth Degree Polynomials ......................................................................... 78 4.3.4 Multiple Regression ............................................................................................................... 79Summary .................................................................................................................................................... 81References .................................................................................................................................................. 81Recommended Reading ............................................................................................................................. 81Self Assessment ........................................................................................................................................... 82

Chapter V .................................................................................................................................................... 84Solution of Simultaneous Algebraic Equation ......................................................................................... 84Aim .............................................................................................................................................................. 84Objectives .................................................................................................................................................... 84Learning outcome ........................................................................................................................................ 845.1 Cramer’s Rule ........................................................................................................................................ 855.2 Gauss Elimination Method .................................................................................................................... 905.3 Gauss Elimination with Partial Pivoting ................................................................................................ 935.4 Gauss-Jordan Method ........................................................................................................................... 995.5 Gauss Siedel Method ........................................................................................................................... 105Summary ................................................................................................................................................... 108References ................................................................................................................................................. 108Recommended Reading ........................................................................................................................... 108Self Assessment ......................................................................................................................................... 109

Chapter VI .................................................................................................................................................111Numerical Solution of 1st and 2nd Order Differential Equations ...........................................................111Aim .............................................................................................................................................................111Objectives ...................................................................................................................................................111Learning outcome .......................................................................................................................................1116.1 Taylor Series .........................................................................................................................................1126.2 Euler’s Method ......................................................................................................................................1176.3 Modified Euler’s Method ..................................................................................................................... 1206.4 Runge-Kutta Method for 1st and 2nd Order Differential Equation ........................................................ 1236.5 Picards Method .................................................................................................................................... 126Summary .................................................................................................................................................. 129References ................................................................................................................................................. 129Recommended Reading ........................................................................................................................... 129Self Assessment ........................................................................................................................................ 130

Chapter VII .............................................................................................................................................. 132Numerical Integration ............................................................................................................................. 132Aim ............................................................................................................................................................ 132Objectives .................................................................................................................................................. 132Learning outcome ...................................................................................................................................... 1327.1 Trapezoidal Rule .................................................................................................................................. 1337.2 Simpson’s 1/3 Rule .............................................................................................................................. 1367.3 Simpson’s 3/8 Rule .............................................................................................................................. 147Summary ................................................................................................................................................... 151References ................................................................................................................................................. 151Recommended Reading ........................................................................................................................... 151Self Assessment ........................................................................................................................................ 152

Page 6: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

IV

Chapter VIII ............................................................................................................................................. 154Linear Programming ............................................................................................................................... 154Aim ............................................................................................................................................................ 154Objectives .................................................................................................................................................. 154Learning outcome ...................................................................................................................................... 1548.1 Linear Programming Model and their Graphical Solutions ................................................................. 1558.2 Transportation Problems ...................................................................................................................... 1568.3 PERT/CPM .......................................................................................................................................... 159 8.3.1 Critical Path (CPM) Computations ......................................................................161Summary ................................................................................................................................................... 162Reference................................................................................................................................................... 162Recommended Reading ........................................................................................................................... 162Self Assessment ......................................................................................................................................... 163

Page 7: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

V

List of Figures

Fig. 6.1 Solution by the Taylor series method ............................................................................................116Fig. 6.2 Euler’s method ...............................................................................................................................117Fig. 6.3 Graphical depiction of Heun’s method (a) Predictor and (b) corrector ........................................ 121Fig. 6.4 Graphical depiction of midpoint method ...................................................................................... 122Fig. 7.1 Integration of a function ............................................................................................................... 136Fig. 8.1 Phases for project planning with CPM-PERT .............................................................................. 160Fig. 8.2 Use of dummy activity to produce unique representation of concurrent activities ...................... 160

Page 8: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

VI

Abbreviations

ODE - Ordinary Differential EquationCPM - Critical Path MethodPERT - Program Evaluation and Review Technique

Page 9: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

1

Chapter I

Introduction to Errors in Numerical Calculations

Aim

The aim of this chapter is to:

define approximate numbers•

explain errors and their computation•

describe the process of rounding off•

Objectives

The objectives of this chapter are to:

elucidate percentage errors•

derive a general error formulae•

describe absolute errors•

Learning outcome

At the end of this chapter, you will be able to:

identify relative errors•

understand errors in a series approximation•

enlist • different types of numbers

Page 10: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

2

1.1 Errors and their ComputationThere are two types of numbers, exact and approximate numbers. Examples of exact numbers are 1, 2, 3,…,1/2. 3/2…, , π,e, etc., written in this manner. Approximate numbers are those that represent the numbers to a certain degree of accuracy. Thus, an approximate value of π is 3.1416, or if we desire approximation, it is 3.14159265. But we cannot write the exact value of π.

The digits that are used to express a number are called significant digits or significant figures. Thus, the numbers 3.1416, 0.66667 and 4.0687 contain five significant digits each. The numbers 0.00023 has, however, only two significant digits, viz., 2 and 3, since the zeros serve only to fix the position of the decimal point. Similarly, the numbers 0.00145, 0.000145 and 0.0000145 all have three significant digits. In case of ambiguity, the scientific notation should be used. For instance, in the number 25.600, the number of significant figures is uncertain, whereas the numbers

, and have three, four and five significant digits, respectively.

In numerical computations, we come across numbers which have large number of digits and it will be necessary to cut them to a usable number of figures. This process is called rounding off. It is usual to round-off numbers according to the following rule:

To round-off a number to n significant digits, discard all digits to the right of the nth digit, and if this discarded number is:

Less than half a unit in the nth place, leave the nth digit unaltered;• Greater than half a unit in the nth place, increase the nth digit by unity;• exactly half a unit in the nth place, , increase the nth digit by unity if it is odd; otherwise, leave it unchanged•

The number thus rounded-off is said to be correct to n significant figures

Example 1.1The numbers given below are rounded-off to four significant figures:1.6583 to 1.65830.0567 to 30.060.859378 to 0.8594314159 to 3.142

In hand computations, the round-off error can be reduced by carrying out the computations to more significant figures at each step of the computation. A useful rule is: at each step of the computations, retain at least one more significant figure than that given in the data, perform the last operation and then round-off. However, most computers allow number if significant figures than are usually required in engineering computations. Thus, there are computers which allow a precision of seven significant figures in the range of about to . Arithmetic carried out with this precision is called single precision arithmetic, and several computers implement double precision arithmetic, which could be used in problems requiring greater accuracy. Usually, the double precision arithmetic is carried out to 15 decimals with a range of about to . In MATLAB, there is a provision to use double precision arithmetic.

In addition to the round-off error discussed above, there is another type of error which can be caused by using approximate formulae in computations, such as the one that arises when a truncated infinite series is used. This type of error is called truncation error and its study is naturally associated with the problem of convergence. Truncation error in a problem can be evaluated and we are often required to make it as small as possible.

Page 11: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

3

1.2 Absolute, Relative and Percentage ErrorsAbsolute error is the numerical difference between the true value of a quantity and its approximate value. Thus, if X is the true value of a quantity and X1 is its approximate value, then the absolute error EA is given by

The relative error is defined by

and the percentage error (Ep) by

Let ∆X be a number such that

∆X

Then ∆X is an upper limit on the magnitude of the absolute error and is said to measure absolute accuracy. Similarly, the quantity

measures the relative accuracy.

It is easy to deduce that if two numbers are added or subtracted, then the magnitude of the absolute error in the result is the sum of the magnitudes of the absolute errors in the two numbers. More generally, if , , … are the absolute errors in n numbers, then the magnitude of the absolute error in their sun is given by

To find the absolute error, EA, in a product of two numbers a and b, we write

EA = (a+ ) (b+ )−ab, where are the absolute errors in a and b respectively. Thus,

EA = a +b +

= b + a , approximately

Similarly, the absolute error in the quotient a/b is given by

Page 12: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

4

assuming that is small in comparison with 1

Example 1.2 If the number X is rounded to N decimal places, then

If X=0.51 and is correct to 2 decimal places, then =0.005, and the relative accuracy is given by 0.005/0.51≃0.98%.

Example 1.3 An approximate value of π is given by X1=22/7= 3.1428571 and its true value is X=3.1415926. Find the absolute and relative errors.

We have

Example 1.4 Three approximate value of the number 1/3 are given as 0.30, 0.33 and 0.34. Which of these three is the best approximation? We have

It follows that 0.33 is the best approximation for 1/3.

Example 1.5 Find the relative error of the number 8.6 if both of its digits are correct.Here

EA=0.05

Page 13: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

5

Hence,

Example 1.6Evaluate the sum to 4 significant digits and find its absolute and relative errors.

We have: , and

Hence, S=6.614. Then

The total absolute error shows that the sum is correct to 3 significant figures only. Hence we take S=6.61 and then,

Example 1.7 Sum the following numbers:0.1532, 15.45, 0.000354, 305.1, 8.12, 143.3, 0.0212, 0.643 and 0.1734

Where in each of the given digits are correct

Here we have two numbers which have the greatest absolute error. These are 305.1 and 143.3 and the absolute error in both these is 0.05. Hence, we round-off all the other number to two decimal digits. These are:0.15, 15.45, 0.00, 8.12, 0.02, 0.64 and 0.17The sum S is given byS=305.1+143.4+0.15+15.45+0.00+8.12+0.02+0.64+0.17= 472.59=472.6

To determine the absolute error, we note that the first two numbers have each an absolute error of 0.05 and the remaining seven numbers have an absolute error of 0.005 each. Thus, the absolute error in all the 9 numbers is:

=0.1+0.035

=0.135

=0.14

In addition to the above absolute error, we have to take into account the rounding error in the above and this is 0.01. Hence the total absolute error is S=0.14+0.01=0.15. Thus,

S=472.6±0.15Example 1.8 Two numbers are given as 2.5 and 48.289, both of which being correct to the significant figures given. Find their product.

Page 14: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

6

Here, the number with the greatest absolute error is 2.5. Hence, we round-off the second number to three significant digits, i.e., 48.8. Their product is given by:

P=48.3× 2.5=120.751.2×102

Where we have retained only two significant digits since one of the given numbers, viz., 2.5 contained only two significant digits.

1.3 General Error FormulaeHere, in this section, we will derive a general formula for the error committed in using a certain formula or functional relation. Let

be a function of several variables xi(i=1, 2, …, xn), and let the error in each xi be ∆ xi. Then the error ∆u in u is given by:

Expanding the right-hand side by Taylor’s series, we obtain,

Assuming that the errors in xi are small and that (∆xi)/ xi<<1, so that the squares and higher powers of ∆xi, can be neglected, the above relation yields:

We observe that this formula has the same form as that for the total differential of u. The formula for the relative error follows immediately:

The following example illustrates the use of this formula.

Example 1.9

Let

Then,

, ,

And

Page 15: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

7

In general, the error ∆x, ∆y and ∆z may be positive or negative, and hence, we take the absolute values of the terms on the right side. This gives:

Now, let ∆x=∆y=∆z=0.001 and x=y=z=1. Then, the relative maximum error (ER)max is given by:

1.4 Errors in a Series ApproximationThe truncation error committed in a series approximation can be evaluated by using Taylor’s series stated in the above theorem. If xi and x i+1 are two successive values of x then we have:

Where:

Where

In the above equation the term , is called the remainder term which, for a convergent series, tends to zero as n→∞. Thus, if f(x i+1) is approximated by the first-n terms of the series, then maximum error committed by using thus approximation is given by the remainder term . Conversely, if the accuracy required is specified in advance, then it would be possible to find n, the number of terms, such that the finite series yields the required accuracy.

Defining the interval length:

Where means that the truncation error is of the order of hn+1 , i.e., it is proportional to hn+1. The meaning of this statement will be made clearer now.

Let the series be truncated after the first term. This gives the zero-order approximation:

Page 16: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

8

which means that halving the interval length h will also halve the error in the approximate solution. Similarly, the first-order Taylor series approximation is given by:

which means that halving the interval length, h will quarter the error in the approximation. In such cases we say that approximation has second-order of convergence.

Example 1.10 Evaluate f (1) using Taylor’s series for f(x), where

It is easily seen that f(1)=−7 but it will be instructive to see how the Taylor series approximations of orders 0 to 3 improve the accuracy of f(1) gradually.

Let h=1, xi=0 and xi+1=1. We then require f(xi+1). The derivatives of f(x) are given by:

f iv(x) and higher derivatives being all zero. Hence,

Also,

Hence, Taylor’s series gives:

(1)

From (1), the zero-order approximation is given by:

(2)

And therefore,

the error in which is −7+10, i.e., 3 units.

Page 17: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

9

For the first approximation, we have:

(3)

and therefore,

the error which is −7+5, i.e., −2 units.

Again, the second-order Taylor approximation is given by:

and therefore,

In which the error is −7+8, i.e., 1 unit.

Finally, the third-order Taylor series approximation is given by:

(5)

and therefore,

=−7which is the exact value of f (1)

Example 1.11Given f(x)= sin x, construct the Taylor series approximations of orders 0 to 7 at x=π/3 and state their absolute errors.

Let xi+1 =π/3 and xi=π/6 so that h=π/3-π/6=π/6. We then have:

(1)

Page 18: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

10

Since f(x) = sin x, eq (1) becomes:

The different orders of approximation can be evaluated successively. Thus, the zero-order approximation is 0.5; the first -order approximation is , i.e., 0.953449841; and the second-order approximation is:

which simplifies to 0.884910921. Similarly, the successive approximations are evaluated and the respective absolute errors can be calculated since the exact value of sin (π/3) is 0.866025403. Table given below gives the approximate values of sin (π/3) for the orders 0 to 7 as also the absolute errors in these approximations. The results show that the errors decreases with an increases in the order of approximation.

Order of approximations Computed value of sin (π/3 Absolute error0 0.5 0.3660254031 0.953449841 0.0874244382 0.884910921 0.0188855183 0.864191613 0.001833794 0.865757474 0.0002679295 0.86604149 0.0000160876 0.86602718 0.0000017777 0.866025326 0.000000077

We next demonstrate the effect of halving the interval length on any approximate value. For this, we consider the first-order approximation in the form:

(2)

Where E(h) is the absolute error of the first-order approximation with interval h, Taking f(x)= sin x and x=π/6, we obtain:

(3)

Putting h=π/6 in the above equation, we get:

Since sin(π/3)= 0.866025403, the above equation gives:E(h)=−0.087424438

Page 19: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

11

Now, let the interval be halved so that we now take h=π/12. Then , eq (3) gives:

(4)

Where E (h/2) is the absolute error with interval length h/2. Since

Equation (4) gives

and then,

In similar way, we obtain the values

and

The h2-order of convergence is quite revealing in the above results.

Page 20: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

12

SummaryThere are two types of numbers, exact and approximate numbers.• The digits that are used to express a number are called significant digits or significant figures.• Approximate numbers are those that represent the numbers to a certain degree of accuracy.• The digits that are used to express a number are called significant digits or significant figures. • In hand computations, the round-off error can be reduced by carrying out the computations to more significant • figures at each step of the computation. Absolute error is the numerical difference between the true value of a quantity and its approximate value.• The truncation error committed in a series approximation can be evaluated by using Taylor’s series.•

ReferencesSastry, S. S., 2005. • Introductory methods of numerical analysis, PHI Learning Pvt. Ltd.Babu, R., 2010. • Engineering Mathematics-I, Pearson Education India.Bramer, B., • Errors in numerical computation [Online] Available at: <http://www.cse.dmu.ac.uk/~bb/Teaching/ComputerSystems/ErrorsInComputation/ErrorsInComputation.htm> [Accessed 17 November 2011].Numerical Computation• [pdf] Available at: <http://www.newagepublishers.com/samplechapter/001997.pdf> [Accessed 17 November 2011].USF, 2009. • Truncation Error: Definition [Video Online] Available at: <http://www.youtube.com/watch?v=pfV9kqh4Mpg> [Accessed 17 November 2011].Leaf, B., • How to Calculate Relative Error [Video Online] Available at: <http://www.ehow.com/video_4951553_calculate-relative-error.html> [Accessed 17 November 2011].

Recommended ReadingSing, R. R., 2010. • Engineering Mathematics, Tata McGraw-Hill Education.Sastry, S. S., 2004. • Engineering Mathematics, 3rd ed., PHI Learning Pvt. Ltd.Islam, N., 2003. • Numerical Analysis, Academic Publishers.

Page 21: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

13

Self Assessment There are two types of numbers, _____________ and approximate numbers.1.

exacta. single precisionb. double precisionc. absolute d.

Which of the following represent numbers to a certain degree of accuracy?2. Exacta. Approximateb. Double precisionc. Absoluted.

Which of the following statements is true?3. The digits that are used to express a number are called absolute digits.a. The digits that are used to express a number are called trivial digits.b. The digits that are used to express a number are called significant digits.c. The digits that are used to express a number are called insignificant digits.d.

In hand computations, the _________ error can be reduced by carrying out the computations to more significant 4. figures at each step of the computation.

absolutea. truncationb. normalc. round-offd.

_________ error is the numerical difference between the true value of a quantity and its approximate value.5. Absolutea. Truncationb. Normalc. Round-offd.

The _________ error committed in a series approximation can be evaluated by using Taylor’s series6. absolutea. truncationb. normalc. round-offd.

In MATLAB, there is a provision to use ____________precision arithmetic.7. singlea. finiteb. doublec. infinited.

Page 22: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

14

_____________numbers are those that represent the numbers to a certain degree of accuracy.8. Exact a. Even b. Odd c. Approximated.

The digits that are used to express a number are called____________. 9. exact number a. even number b. significant digitsc. approximate number d.

_________________ are 1, 2, 3,10. …,1/2. 3/2…, , π,e, etc.Exact number a. Even number b. Significant digitsc. Approximate number d.

Page 23: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

15

Chapter II

Solution to Algebraic and Transcendental Equations

Aim

The aim of this chapter is to:

define bisection method•

explain the method of false position•

discuss the use of linear approximations to solve equations•

Objectives

The objectives of this chapter are to:

explicate the Newton-Raphson iteration•

explain bisection method•

describe the method of false position•

Learning outcome

At the end of this chapter, you will be able to:

elucidate the Newton-Raphson method•

use C program to solve equation by using c Newton-Raphson method•

solve equ• ations by using linear approximations

Page 24: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

16

2.1 Bisection MethodA binary search procedure applied to an x interval known to contain root of f(x).

Example: Polynomial f(x) = x5 + x + 1 has exactly five roots, at least one real root.

Step 1: Determine an x interval containing a real root.

We can do this by simple analysis of f(x):compute (or estimate) f(x) for convenient values of x, such as 0, ± 1• estimate position of local extrema with first derivative of f(x)• make rough sketch of f(x).•

Step 2: Bisect interval repeatedly until root is determined to desired accuracy

d(x)=x5 + x + 1 1

-1

0.5

-0.5-1 -0.8 -0.6 -0.4 -0.2

f(xmid)=0.46875

xup=0xlow=

xmid =xlow + sup

2

If f(xlow) and f(xmid) have opposite signs ( f(xlow) . f(xmid ) < 0, root is in left half of interval.• If f(xlow) and f(xmid) have same signs ( f(xlow) . f(xmid ) > 0), root is in right half of interval.•

Continue subdividing until interval width has been reduced to a size ≤ εwhere

ε = selected x tolerance.

Using a y tolerance can result in poor estimate of root

Page 25: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

17

Example

60

40

20

10 20 30 40 50

false root

Oscillating function

false root

Asymptotically converging function

5

4

3

2

1

-2 -1 1 2 3 4 5-1

Pseudocode Algorithm: Bisection method

Input xLower, xUpper, xTolyLower = f(xLower) (* invokes fcn definition *)xMid = (xLower + xUpper)/2.0yMid = f(xMid)iters = 0 (* count number of iterations *)While ( (xUpper - xLower)/2.0 < xTol )iters = iters + 1if( yLower * yMid > 0.0) Then xLower = xMidElse xUpper = xMidEndofifxMid = (xLower + xUpper)/2.0yMid = f(xMid)EndofwhileReturn xMid, yMid, iters (* xMid = approx to root *)

(Do not need to recalculate yLower in loop, since it can never change sign.)

For a given x tolerance (epsilon), we can calculate the number of iterations directly. The number of divisions of the original interval is the smallest value of n that satisfies:

Page 26: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

18

Thus,

In our previous example xlower = -1, xupper = 0

Choosing ε = 10-4, we have n > log2 10-4 = 13.29

2.2 Method of False PositionThe false position method retains the main feature of the bisection method; that a root is trapped in a sequence of intervals of decreasing size. Rather than selecting the midpoint of each interval, this method uses the point where the secant lines intersect the x-axis.

ab

(b,f(b))

(a,f(a))

cr

In the above figure, the secant line over the interval [a, b] is the chord between (a, f (a)) and (b, f (b)). The two right triangles in the figures are similar, which means that

It is easy to show that

We then compute f(c) and proceed to the next step with the interval [a, c] if f (a) f(c) <0 or to the interval [c,b] if f(c) f(b)<0.

In general case, the false position method starts with the interval [a0, b0] containing a root: f(a0) and f(b0) are of opposite signs. The false position method uses intervals [ak.bk] that contain roots in almost the same way that the bisection method does. However, instead of finding the midpoint of the interval, it finds where the secant line joining (ak, f (bk)) crosses the x-axis and then selects it to be the new endpoint.

A C++ Program to read a Non-Linear equation in one variable, then evaluate it using False-Position Method and display its kD accurate root.

//-------------------------- Header Files -----------------------------// # include <iostream.h> # include <string.h> # include <stdlib.h> # include <conio.h>

Page 27: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

19

# include <math.h>

//------------------------- Global Variables --------------------------//

const int max_size=30; int top=-1; int accuracy=0; int iterations=0; long double a=0; long double b=0; long double x=0; char Non_linear_equation[100]={NULL}; char Stack[max_size][max_size]={NULL}; char Postfix_expression[max_size][max_size]={NULL};//----------------------- Function Prototypes -------------------------// void push(const char *); void convert_infix_expression_to_postfix_expression(const char *);

const char* pop( ); const long double evaluate_postfix_expression(const long double); void show_screen( ); void clear_screen( ); void get_input_non_linear_equation( ); void apply_false_position_method( ); void show_result( );

//------------------------------ main( ) ------------------------------//

int main( ) { clrscr( ); textmode(C4350); show_screen( ); get_input_non_linear_equation( ); apply_false_position_method( ); show_result( ); getch( ); return 0; }//---------------------- Function Definitions -------------------------////-------------------------- push(const char*) ------------------------// void push(const char* Operand) { if(top==(max_size-1)) { cout<<”Error : Stack is full.”<<endl; cout<<”\n Press any key to exit.”; getch( ); exit(0); } else { top++;

Page 28: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

20

strcpy(Stack[top],Operand); } }//------------------------------ pop( ) -------------------------------//

const char* pop( ) { char Operand[40]={NULL}; if(top==-1) { cout<<”Error : Stack is empty.”<<endl; cout<<”\n Press any key to exit.”; getch( ); exit(0); } else { strcpy(Operand,Stack[top]); strset(Stack[top],NULL); top--; } return Operand; }//---- convert_infix_expression_to_postfix_expression(const char*) ----// void convert_infix_expression_to_postfix_expression(const char* Expression) { char Infix_expression[100]={NULL}; char Symbol_scanned[30]={NULL}; push(“(“); strcpy(Infix_expression,Expression); strcat(Infix_expression,”+0)”); int flag=0; int count_1=0; int count_2=0; int equation_length=strlen(Infix_expression); if(Infix_expression[0]==’(‘) flag=1; do { strset(Symbol_scanned,NULL); if(flag==0) { int count_3=0; do { Symbol_scanned[count_3]=Infix_expression[count_1]; count_1++; count_3++; } while(count_1<=equation_length && Infix_expression[count_1]!=’(‘ && Infix_expression[count_1]!=’+’ && Infix_expression[count_1]!=’-’ &&

Page 29: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

21

Infix_expression[count_1]!=’*’ && Infix_expression[count_1]!=’/’ && Infix_expression[count_1]!=’^’ && Infix_expression[count_1]!=’)’); flag=1; } else if(flag==1) { Symbol_scanned[0]=Infix_expression[count_1]; count_1++; if(Infix_expression[count_1]!=’(‘ && Infix_expression[count_1]!=’^’ && Infix_expression[count_1]!=’*’ && Infix_expression[count_1]!=’/’ && Infix_expression[count_1]!=’+’ && Infix_expression[count_1]!=’-’ && Infix_expression[count_1]!=’)’) flag=0; if(Infix_expression[count_1-1]==’(‘ && (Infix_expression[count_1]==’-’ || Infix_expression[count_1]==’+’)) flag=0; } if(strcmp(Symbol_scanned,”(“)==0) push(“(“); else if(strcmp(Symbol_scanned,”)”)==0) { while(strcmp(Stack[top],”(“)!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } pop( ); } else if(strcmp(Symbol_scanned,”^”)==0 || strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0 || strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0) { if(strcmp(Symbol_scanned,”^”)==0) { } else if(strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0) { while(strcmp(Stack[top],”^”)==0 || strcmp(Stack[top],”*”)==0 || strcmp(Stack[top],”/”)==0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } }

Page 30: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

22

else if(strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0) { while(strcmp(Stack[top],”(“)!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } push(Symbol_scanned); } else { strcat(Postfix_expression[count_2],Symbol_scanned); count_2++; } } while(strcmp(Stack[top],NULL)!=0); strcat(Postfix_expression[count_2],”=”); count_2++; }//---------- evaluate_postfix_expression(const long double) -----------// const long double evaluate_postfix_expression(const long double x) { long double function_value=0; int count_1=-1; char Symbol_scanned[30]={NULL}; do { count_1++; strcpy(Symbol_scanned,Postfix_expression[count_1]); if(strcmp(Symbol_scanned,”^”)==0 || strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0 || strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0) { char Result[30]={NULL}; char Operand[2][30]={NULL}; strcpy(Operand[0],pop( )); strcpy(Operand[1],pop( )); long double operand[2]={0}; long double result=0; char *endptr; for(int count_2=0;count_2<2;count_2++) { int flag=0; if(Operand[count_2][0]==’-’) { int length=strlen(Operand[count_2]); for(int count_3=0;count_3<(length-1);count_3++) Operand[count_2][count_3]=Operand[count_2][(count_3+1)]; Operand[count_2][count_3]=NULL;

Page 31: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

23

flag=1; } if(strcmp(Operand[count_2],”x”)==0) operand[count_2]=x; else if(strcmp(Operand[count_2],”e”)==0) operand[count_2]=2.718282; else if(strcmp(Operand[count_2],”sinx”)==0) operand[count_2]=sinl(x); else if(strcmp(Operand[count_2],”cosx”)==0) operand[count_2]=cosl(x); else if(strcmp(Operand[count_2],”tanx”)==0) operand[count_2]=tanl(x); else if(strcmp(Operand[count_2],”lnx”)==0) operand[count_2]=logl(x); else if(strcmp(Operand[count_2],”logx”)==0) operand[count_2]=log10l(x); else operand[count_2]=strtod(Operand[count_2],&endptr); if(flag) operand[count_2]*=-1; } switch(Symbol_scanned[0]) { case ‘^’ : result=powl(operand[1],operand[0]); break; case ‘*’ : result=operand[1]*operand[0]; break; case ‘/’ : result=operand[1]/operand[0]; break; case ‘+’ : result=operand[1]+operand[0]; break; case ‘-’ : result=operand[1]-operand[0]; break; } gcvt(result,25,Result);

push(Result); } else if(strcmp(Symbol_scanned,”=”)!=0) push(Symbol_scanned); } while(strcmp(Symbol_scanned,”=”)!=0); char Function_value[30]={NULL}; char *endptr; strcpy(Function_value,pop( )); function_value=strtod(Function_value,&endptr); return function_value; }//-------------------------- show_screen( ) ---------------------------// void show_screen( ) { cprintf(“\n********************************************************************************”);

Page 32: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

24

cprintf(“**************************- -***************************”); cprintf(“*-------------------------- “); textbackground(1); cprintf(“ False-Position Method “); textbackground(8); cprintf(“ ---------------------------*”); cprintf(“**************************- -***************************”); cprintf(“********************************************************************************”); for(int count=0;count<42;count++) cprintf(“* *”); gotoxy(1,46); cprintf(“********************************************************************************”); cprintf(“*------------------------------------------------------------------------------*”); cprintf(“********************************************************************************”); gotoxy(1,2); }

//------------------------- clear_screen( ) ---------------------------// void clear_screen( ) { for(int count=0;count<37;count++) { gotoxy(3,8+count); cout<<” “; } gotoxy(1,2); }

//------------------- get_input_non_linear_equation( ) -----------------// void get_input_non_linear_equation( ) { gotoxy(4,11); cout<<”Non-Linear Equation with One Variable:”; gotoxy(4,12); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(6,37); cout<<”Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3”; gotoxy(6,40); cout<<”Available Operators : ^ (raised to power) , * , / , + , -”; gotoxy(6,42); cout<<”Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,”; gotoxy(6,44); cout<<” n = any number”; gotoxy(4,14); cout<<”Enter the Function with variabel x = f(x) = “; cin>>Non_linear_equation; gotoxy(4,19); cout<<”Interval [a,b] where the root x lies :”; gotoxy(4,20); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”;

Page 33: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

25

gotoxy(4,22); cout<<”Enter the value of a = “; cin>>a; gotoxy(4,24); cout<<”Enter the value of b = “; cin>>b; gotoxy(4,28); cout<<”Enter the number of accuracy required = k = “; cin>>accuracy; gotoxy(1,2); }

//------------------ apply_false_position_method( ) -------------------// void apply_false_position_method( ) { clear_screen( ); gotoxy(4,10); cout<<”Solution :”; gotoxy(4,11); cout<<”ÍÍÍÍÍÍÍÍÍÍ”; convert_infix_expression_to_postfix_expression(Non_linear_equation); long double k=0.5; for(int count_1=1;count_1<=accuracy;count_1++) k*=0.1; gotoxy(4,13); cout<<”ÚÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄ¿”; gotoxy(4,14); cout<<”³n ³ a ³ Xp ³ b ³ f(Xp) ³ Accuracy ³”; gotoxy(4,15); cout<<”ÃÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄ´”; gotoxy(4,16); cout<<”³ ³ ³ ³ ³ ³ ³”; long double a=::a; long double b=::b; long double old_x=0; long double new_x=x; long double fa=0; long double fb=0; long double fx=0; int count_2=1; int x_cord=4; int y_cord=17; do { gotoxy(x_cord,y_cord); cout<<”³ ³ ³ ³ ³ ³ ³”; gotoxy(x_cord,(y_cord+1)); cout<<”³ ³ ³ ³ ³ ³ ³”; old_x=new_x; fa=evaluate_postfix_expression(a); fb=evaluate_postfix_expression(b);

Page 34: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

26

new_x=(((a*fb)-(b*fa))/(fb-fa)); gotoxy((x_cord+1),y_cord); cout<<count_2; gotoxy((x_cord+4),y_cord); cout<<a; gotoxy((x_cord+18),y_cord); cout<<new_x; gotoxy((x_cord+32),y_cord); cout<<b; fx=evaluate_postfix_expression(new_x); gotoxy((x_cord+46),y_cord); cout<<fx; gotoxy((x_cord+60),y_cord); cout<<fabs(new_x-old_x); if(fx==0) break; else if((fx*fa)<0) b=new_x; else a=new_x; y_cord+=2; if((count_2%12)==0 && fabs(new_x-old_x)>k) { y_cord=17; gotoxy(30,44); cout<<”Press any key to continue...”; getch( ); for(int count_3=1;count_3<25;count_3++) { gotoxy(3,(16+count_3)); cout<<” “; } } count_2++; iterations++; } while(fabs(new_x-old_x)>k); gotoxy(x_cord,y_cord); cout<<”ÀÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÙ”; x=new_x; gotoxy(30,44); cout<<”Press any key to continue...”; getch( ); }//--------------------------- show_result( ) --------------------------// void show_result( ) { clear_screen( ); gotoxy(6,10); cout<<”Function of Non-Linear Equation :”; gotoxy(6,11);

Page 35: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

27

cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(10,13); cout<<”f(x) = “<<Non_linear_equation; gotoxy(6,17); cout<<”Interval [a,b] = [ “<<a<<” , “<<b<<” ]”; gotoxy(6,19); cout<<”Accuracy Required = kD = “<<accuracy<<”D”; gotoxy(6,25); cout<<”Required Root :”; gotoxy(6,26); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(10,28); cout<<”x = “<<x; gotoxy(10,30); cout<<”Number of Iterations = “<<iterations;

gotoxy(1,2); }

2.3 Newton -Raphson MethodThe Newton-Raphson method, or Newton Method, is a powerful technique for solving equations numerically. Like so much of the differential calculus, it is based on the simple idea of linear approximation.

Using linear approximations to solve equations:Let f(x) be a well-behaved function, and let r be a root of the equation f(x) = 0. We start with an estimate x0 of r. From x0, we produce an improved we hope estimate x1. From x1, we produce a new estimate x2. From x2, we produce a new estimate x3. We go on until we are `close enough’ to r or until it becomes clear that we are getting nowhere.

The above general style of proceeding is called iterative. Of the many iterative root-finding procedures, the Newton-Raphson method, with its combination of simplicity and power, is the most widely used.

2.3.1 Newton-Raphson IterationLet x0 be a good estimate of r and let r = x0 + h. Since the true root is r, and h = r −x0, the number h measures how far the estimate x0 is from the truth. Since h is `small,’ we can use the linear (tangent line) approximation to conclude that

0 = f(r) = f(x0 + h) ≈ f(x0) + hf’(x0);

and therefore, unless f’(x0) is close to 0,

It follows that

Our new improved (?) estimate x1 of r is therefore given by

Page 36: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

28

The next estimate x2 is obtained from x1 in exactly the same way as x1 was obtained from x0:

Continue in this way. If xn is the current estimate, then the next estimate xn+1 is given by

A sample calculation We use the Newton Method to find a non-zero solution of x = 2sinx. Let f(x) = x − 2 sin x. Then f’’ (x) = 1 − 2 cos x, and the Newton-Raphson iteration is

Let x0 = 1:1. The next six estimates, to 3 decimal places, are:x1= 8:453 x3 = 203:384 x5 = − 87:471x2= 5:256 x4 = 118:019 x6 = −203:637:

Things don’t look good, and they get worse. It turns out that x35 < −64000000. We could be stubborn and soldier on. To get an idea of what’s going wrong, use a graphing program to graph y = x−2 sinx, and recall that xn+1 is where the tangent line at xn meets the x-axis. The bumps on y = x − 2 sin x confuse the Newton Method terribly.

Note that choosing x0 = =3 1:0472 leads to immediate disaster, since then 1 − 2 cos x0 = 0 and therefore x1 does not exist. Thus with x0 = 1:1 we are starting on a (nearly) flat part of the curve. Riding the tangent line takes us to an x1 quite far from x0. And x1 is also on a flat part of the curve, so x2 is far from x1. And x2 is on a flat part of the curve: the chaotic ride continues.

The trouble was caused by the choice of x0. Let’s see whether we can do bettter. Draw the curves y = x and y = 2sinx. A quick sketch shows that they meet a bit past =2. But we will be sloppy and take x0 = 1:5. Here are the next six estimates, to 19 places|the computations were done to 50.

x1 = 2:0765582006304348291 x4 = 1:8954942764727706570x2 = 1:9105066156590806258 x5 = 1:8954942670339809987x3 = 1:8956220029878460925 x6 = 1:8954942670339809471

The next iterate x7 agrees with x6 in the first 19 places, indeed in the first 32, and the true root is equal to x6 to 32 places.

C program to solve equation by using c Newton-Raphson method

function f(x)f=x-exp(-x)returnendfunction f1(x)f1=1+exp(-x)returnend5 write(*,*)’enter initial guess’

Page 37: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

29

read(*,*) x0write(*,*)’enter tolerrence’read(*,*) eps10 x1=x0-f(x0)/f1(x0)if(abs((x1-x0)/x0).lt.eps) thenwrite(*,1) x11 format(‘solution=’,f10.4)stopelsex0=x1go to 10endifend

Page 38: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

30

SummaryThe false position method retains the main feature of the bisection method; that a root is trapped in a sequence • of intervals of decreasing size.The Newton-Raphson method, or Newton Method, is a powerful technique for solving equations numerically. • Of the many iterative root-finding procedures, the Newton-Raphson method, with its combination of simplicity • and power, is the most widely used.

ReferencesBradie, B., 2006. • A Friendly Introduction to Numerical Analysis, Pearson Education India.Saaty, L. T. and Bram, J., 1964. • Nonlinear mathematics, Courier Dover Publications.MathMedics, LLC, 1999. • The Bisection Method [Online] Available at: <http://www.sosmath.com/calculus/limcon/limcon07/limcon07.html> [Accessed 17 November 2011].Smith, D. M., 1998. • The Method of False Position [Online] Available at: <http://web.mit.edu/10.001/Web/Course_Notes/NLAE/node5.html> [Accessed 17 November 2011].burny1, 2010. • RootfindingBisection/Newton/Secant/FalsePositionandOrderofconvergence [Video Online] Available at: <http://www.youtube.com/watch?v=mztGjg04rV8> [Accessed 17 November 2011].Numericalmethodsguy, 2009. • Newton-Raphson Method: Example [Video Online] Available at: <http://www.youtube.com/watch?v=lFYzdOemDj8> [Accessed 17 November 2011].

Recommended ReadingSaaty, L. T., 1981. • Modern nonlinear equations, Courier Dover Publications.Cheney, W. E. and Kincaid, R. D., 2007. • Numerical mathematics and computing, Cengage Learning.Burden, L. R. and Faires, J. D., 2010. • Numerical Analysis, Cengage Learning.

Page 39: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

31

Self AssessmentThe false position method retains the main feature of the____________.1.

bisection methoda. bilateral methodb. Newton-Raphson methodc. Simpson’s 1/3 method d.

Rather than selecting the midpoint of each interval, which of the following method uses the point where the 2. secant lines intersect the x-axis?

Newton-Raphson methoda. Method of False Positionb. Simpson’s 1/3 methodc. Simpson’s 3/8 methodd.

Which of the following statement is true?3. The Newton-Raphson method, or Newton Method, is a powerful technique for solving quadratic a. equations.The Newton-Raphson method, or Newton Method, is a powerful technique for solving simultaneous b. equations.The Newton-Raphson method, or Newton Method, is a powerful technique for solving equations c. numerically.The Simpson’s method is a powerful technique for solving equations numerically.d.

Newton -Raphson Method is based on the simple idea of _________approximation.4. statica. dynamicb. non-linearc. lineard.

Which of the following method retains the main feature of the bisection method; that a root is trapped in a 5. sequence of intervals of decreasing size?

The false positiona. Newton-Raphson methodb. Newton methodc. Simpson’s methodd.

What does “strset” mean?6. Set string to numerical value a. Compare string b. Copy string c. Concatenate two string d.

What does “strcmp” mean?7. Set string to numerical value a. Compare string b. Copy string c. Concatenate two string d.

Page 40: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

32

What does “strcpy” mean?8. Set string to numerical value a. Compare string b. Copy string c. Concatenate two string d.

What does “strcat” mean?9. Set string to numerical value a. Compare string b. Copy string c. Concatenate two string d.

When is “cout” used in code?10. Print output on screen a. Get input b. Copy screen c. print screen d.

Page 41: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

33

Chapter III

Interpolation

Aim

The aim of this chapter is to:

introduce the concept of interpolation•

explain the concept of forward difference•

describe Newton’s backward difference interpolation•

Objectives

The objectives of this chapter are to:

elucidate backward difference•

illustrate Newton’s forward difference interpolation•

describe the code for Newton’s backward difference interpolation in C programming•

Learning outcome

At the end of this chapter, you will be able to:

understand interpolation•

discuss the code for Newton’s forward difference interpolation in C programming•

explai• n Lagrange’s interpolation

Page 42: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

34

3.1 Introduction to InterpolationThe process of finding the curve passing through the points (x0, y0), (x1, y1), (x2, y2),…, (xn, yn) is called as Interpolation and the curve obtained is called as Interpolating curve.

Interpolating polynomial passing through the given set of points is unique. Let x0, x1, x2, …, xn be given set of observations and y =f(x)be the given function, then the method to find f(xm) ∀ x0 ≤ xm ≤ xn is called as an Interpolation.

If xm is not in the range of x0 and xn, then the method to find (xm)is called as Extrapolation.

x0, x1, x2,......,xn

Equally SpacedArguments

Unequally SpacedArguments

Newton’s & GaussInterpolation

LagrangesInterpolation

The Interpolation depends upon finite difference concept.

If x0, x1, x2,…, xn be given set of observations and let y0=f(x0), y1=f(x1),…,yn = f(xn) be their corresponding values for the curve y1=f(x) , then y1−y0, y2−y1,…, yn−yn-1 is called as finite difference.

When the arguments are equally spaced, then we can use one of the following differences.Forward differences• Backward differences• Central differences•

3.2 Forward DifferenceLet us consider x0, x1, x2,…, xn be given set of observations and let y0, y1, y2,…, yn are corresponding values of the curve y=f(x) , then the Forward difference operator is denoted by ∆ and is defined as ∆y0=y1−y0, ∆y1=y2−y1,…, ∆yn=yn−yn-1

In this case ∆y0, ∆y1, …, ∆yn are called as First Forward differences of y.

The difference of first forward differences will give us Second forward differences and it is denoted by ∆2 and is defined as ∆2y0 = ∆(∆y0) = ∆(y1−y0) = ∆y1−∆y0

= (y2−y1) − (y1−y0) = y2−2y1−y0

Similarly, the difference of second forward differences will give us third forward difference and it is denoted by ∆3

Page 43: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

35

Forward difference table

#include <vector>#include <iterator>#include <algorithm> //calculate first order forward difference// requires:// * InputIterator is an input iterator// * OutputIterator is an output iterator// * The value type of InputIterator is copy-constructible and assignable// * The value type of InputIterator supports operator -// * The result type of operator- is assignable to the value_type of OutputIterator// returns: The iterator following the output sequencetemplate<typename InputIterator, typename OutputIterator> OutputIterator forward_difference(InputIterator first, InputIterator last, OutputIterator dest){// special case: for empty sequence, do nothingif (first == last) return dest;

typedef typename std::iterator_traits<InputIterator>::value_type value_type;

value_type temp = *first++;while (first != last){ value_type temp2 = *first++;*dest++ = temp2 - temp;temp = temp2; } return dest;}// calculate n-th order forward difference.// requires:

Page 44: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

36

// * InputIterator is an input iterator// * OutputIterator is an output iterator// * The value type of InputIterator is copy-constructible and assignable// * The value type of InputIterator supports operator -// * The result type of operator- is assignable to the value_type of InputIterator// * The result type of operator- is assignable to the value_type of OutputIterator// * order >= 0// returns: The iterator following the output sequencetemplate<typename InputIterator, typename OutputIterator> OutputIterator nth_forward_difference(int order, InputIterator first, InputIterator last, OutputIterator dest){ // special case: If order == 0, just copy input to output if (order == 0) return std::copy(first, last, dest); // second special case: If order == 1, just forward to the first-order function if (order == 1) return forward_difference(first, last, dest); // intermediate results are stored in a vector typedef typename std::iterator_traits<InputIterator>::value_type value_type; std::vector<value_type> temp_storage; // fill the vector with the result of the first order forward difference forward_difference(first, last, std::back_inserter(temp_storage)); // the next n-2 iterations work directly on the vector typename std::vector<value_type>::iterator begin = temp_storage.begin(), end = temp_storage.end(); for (int i = 1; i < order-1; ++i) end = forward_difference(begin, end, begin); // the final iteration writes directly to the output iterator return forward_difference(begin, end, dest);}// example usage code#include <iostream> int main(){ double array[10] = { 90.0, 47.0, 58.0, 29.0, 22.0, 32.0, 55.0, 5.0, 55.0, 73.0 }; // this stores the results in the vector dest std::vector<double> dest; nth_forward_difference(1, array, array+10, std::back_inserter(dest)); // outut deststd::copy(dest.begin(), dest.end(), std::ostream_iterator<double>(std::cout, “ “));std::cout << std::endl;// however, the results can also be output as they are calculated nth_forward_difference(2, array, array+10, std::ostream_iterator<double>(std::cout, “ “));std::cout << std::endl;nth_forward_difference(9, array, array+10, std::ostream_iterator<double>(std::cout, “ “));

Page 45: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

37

std::cout << std::endl; nth_forward_difference(10, array, array+10, std::ostream_iterator<double>(std::cout, “ “));std::cout << std::endl; nth_forward_difference(0, array, array+10, std::ostream_iterator<double>(std::cout, “ “));std::cout << std::endl;// finally, the results can also be written into the original array// (which of course destroys the original content)double* end = nth_forward_difference(3, array, array+10, array);for (double* p = array; p < end; ++p)std::cout << *p << “ “;std::cout << std::endl; return 0;}This gives the following output-43 11 -29 -7 10 23 -50 50 18 54 -40 22 17 13 -73 100 -32 -2921 90 47 58 29 22 32 55 5 55 73 -94 62 -5 -4 -86 173 -132

3.3 Backward DifferenceLet us consider x0, x1, x2,…, xn be given set of observations and let y0, y1, y2,…, yn are corresponding values of the curve y=f(x) , then the Backward difference operator is denoted by ∇ and is defined as ∇y1=y1−y0, ∇y2=y2−y1,…, ∇yn=yn−yn-1.

In this case∇y1, ∇y2,…, ∇yn are called as First Backward differences of y.

The difference of first Backward differences will give us Second Backward differences and it is denoted by ∇2 and is defined as∇2y0 = ∇(∇y0)

= ∇(y1−y0) = ∇y1−∆y0

= (y2−y1) − (y1−y0) = y2−2y1−y0

Similarly, the difference of second forward differences will give us third forward difference and it is denoted by ∇3

Page 46: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

38

Backward difference table

Code for Program to estimate value of First Derivative of the function at the given points from the given data using Backward Difference Formula , Forward diff in C++ Programming# include <iostream.h> # include <stdlib.h> # include <string.h> # include <stdio.h> # include <conio.h> # include <ctype.h> # include <math.h> constint max_size=13; int n=0; int top=-1; int choice=0;longdouble h=0;longdouble x0=0;longdouble xn[max_size]={0}; longdouble fx[max_size]={0}; char Fx[100]={NULL}; char Dfx[100]={NULL};char Stack[30][30]={NULL};char Postfix_expression[2][30][30]={NULL}; void push(constchar *); void convert_ie_to_pe(constchar *,constint);constchar* pop( );constlongdouble evaluate_postfix_expression(constlongdouble,constint); void show_screen( ); void clear_screen( ); void get_input( ); void estimate_dfx( ); constint get_index(constlongdouble); int main( ) { clrscr( ); textmode(C4350); show_screen( );

Page 47: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

39

get_input( ); estimate_dfx( ); return 0; }

*************************************************************************///-------------------------- show_screen( ) ---------------------------///*************************************************************************/void show_screen( ) { cprintf(“\n********************************************************************************”); cprintf(“*************************- -************************”); cprintf(“*------------------------- “);

textbackground(1); cprintf(“ Numerical Differentiation “); textbackground(8);

cprintf(“ ------------------------*”); cprintf(“*-***********************- -**********************-*”); cprintf(“*-****************************************************************************-*”);

for(int count=0;count<42;count++) cprintf(“*-* *-*”);

gotoxy(1,46); cprintf(“*-****************************************************************************-*”); cprintf(“*------------------------------------------------------------------------------*”); cprintf(“********************************************************************************”);

gotoxy(1,2); }/*************************************************************************///------------------------- clear_screen( ) ---------------------------///*************************************************************************/void clear_screen( ) { for(int count=0;count<37;count++) { gotoxy(5,8+count); cout<<” “; }

gotoxy(1,2); }

/*************************************************************************///-------------------------- push(const char*) ------------------------///*************************************************************************/void push(constchar* Operand)

Page 48: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

40

{ if(top==(max_size-1)) { cout<<”Error : Stack is full.”<<endl; cout<<”\n Press any key to exit.”; getch( ); exit(0); } else { top++; strcpy(Stack[top],Operand); } }/*************************************************************************///------------------------------ pop( ) -------------------------------///*************************************************************************/constchar* pop( ) { char Operand[40]={NULL}; if(top==-1) { cout<<”Error : Stack is empty.”<<endl; cout<<”\n Press any key to exit.”; getch( ); exit(0); } else { strcpy(Operand,Stack[top]); strset(Stack[top],NULL); top--; } return Operand; }/*************************************************************************///---------------- convert_ie_to_pe(const char*,const int) ------------///*************************************************************************/void convert_ie_to_pe(constchar* Expression,constint index) { char Infix_expression[100]={NULL}; char Symbol_scanned[30]={NULL}; push(“(“); strcpy(Infix_expression,Expression); strcat(Infix_expression,”+0)”); int flag=0; int count_1=0; int count_2=0; int equation_length=strlen(Infix_expression); if(Infix_expression[0]==’(‘) flag=1; do { strset(Symbol_scanned,NULL); if(flag==0)

Page 49: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

41

{ int count_3=0; do { Symbol_scanned[count_3]=Infix_expression[count_1]; count_1++; count_3++; } while(count_1<=equation_length && Infix_expression[count_1]!=’(‘ && Infix_expression[count_1]!=’+’ && Infix_expression[count_1]!=’-’ && Infix_expression[count_1]!=’*’ && Infix_expression[count_1]!=’/’ && Infix_expression[count_1]!=’^’ && Infix_expression[count_1]!=’)’); flag=1; } elseif(flag==1) { Symbol_scanned[0]=Infix_expression[count_1]; count_1++; if(Infix_expression[count_1]!=’(‘ && Infix_expression[count_1]!=’^’ && Infix_expression[count_1]!=’*’ && Infix_expression[count_1]!=’/’ && Infix_expression[count_1]!=’+’ && Infix_expression[count_1]!=’-’ && Infix_expression[count_1]!=’)’) flag=0; if(Infix_expression[count_1-1]==’(‘ &&

(Infix_expression[count_1]==’-’ || Infix_expression[count_1]==’+’)) flag=0; }

if(strcmp(Symbol_scanned,”(“)==0) push(“(“);

elseif(strcmp(Symbol_scanned,”)”)==0) { while(strcmp(Stack[top],”(“)!=0) { strcpy(Postfix_expression[index][count_2],pop( ));

count_2++; }

pop( ); }

Page 50: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

42

elseif(strcmp(Symbol_scanned,”^”)==0 || strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0 || strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0) { if(strcmp(Symbol_scanned,”^”)==0) { } elseif(strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0) { while(strcmp(Stack[top],”^”)==0 || strcmp(Stack[top],”*”)==0 || strcmp(Stack[top],”/”)==0) { strcpy(Postfix_expression[index][count_2],pop( )); count_2++; } } elseif(strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0) { while(strcmp(Stack[top],”(“)!=0) { strcpy(Postfix_expression[index][count_2],pop( )); count_2++; } } push(Symbol_scanned); } else { strcat(Postfix_expression[index][count_2],Symbol_scanned); count_2++; } } while(strcmp(Stack[top],NULL)!=0);

strcat(Postfix_expression[index][count_2],”=”); count_2++; }/*************************************************************************///----- evaluate_postfix_expression(const long double,const int) ------///*************************************************************************/constlongdouble evaluate_postfix_expression( constlongdouble x,constint index) { longdouble function_value=0; int count_1=-1; char Symbol_scanned[30]={NULL}; do { count_1++; strcpy(Symbol_scanned,Postfix_expression[index][count_1]); if(strcmp(Symbol_scanned,”^”)==0 ||

Page 51: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

43

strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0 || strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0) { char Result[30]={NULL}; char Operand[2][30]={NULL}; strcpy(Operand[0],pop( )); strcpy(Operand[1],pop( )); longdouble operand[2]={0}; longdouble result=0; char *endptr; for(int count_2=0;count_2<2;count_2++) { int flag=0; if(Operand[count_2][0]==’-’) { int length=strlen(Operand[count_2]); for(int count_3=0;count_3<(length-1);count_3++) Operand[count_2][count_3]=Operand[count_2][(count_3+1)]; Operand[count_2][count_3]=NULL; flag=1; } if(strcmp(Operand[count_2],”x”)==0) operand[count_2]=x; elseif(strcmp(Operand[count_2],”e”)==0) operand[count_2]=2.718282; elseif(strcmp(Operand[count_2],”sinx”)==0) operand[count_2]=sinl(x); elseif(strcmp(Operand[count_2],”cosx”)==0) operand[count_2]=cosl(x); elseif(strcmp(Operand[count_2],”tanx”)==0) operand[count_2]=tanl(x); elseif(strcmp(Operand[count_2],”lnx”)==0) operand[count_2]=logl(x); elseif(strcmp(Operand[count_2],”logx”)==0) operand[count_2]=log10l(x); else operand[count_2]=strtod(Operand[count_2],&endptr); if(flag) operand[count_2]*=-1; } switch(Symbol_scanned[0]) { case’^’ : result=powl(operand[1],operand[0]); break; case’*’ : result=operand[1]*operand[0]; break; case’/’ : result=operand[1]/operand[0]; break; case’+’ : result=operand[1]+operand[0]; break; case’-’ : result=operand[1]-operand[0];

Page 52: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

44

break; } gcvt(result,25,Result); push(Result); } elseif(strcmp(Symbol_scanned,”=”)!=0) push(Symbol_scanned); } while(strcmp(Symbol_scanned,”=”)!=0); char Function_value[30]={NULL}; char *endptr; strcpy(Function_value,pop( )); function_value=strtod(Function_value,&endptr); return function_value; } ************************************************************************///----------------------------- get_input( ) --------------------------///*************************************************************************/void get_input( ) { do { clear_screen( ); gotoxy(6,9); cout<<”Number of Distinct Data Points :”; gotoxy(6,10); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”;

gotoxy(27,13); cout<<”[ min. n = 2 | max. n = 12 ]”; gotoxy(6,12); cout<<”Enter the max. number of distinct data points = n = “; cin>>n; if(n<2 || n>12) { gotoxy(12,25); cout<<”Error : Wrong Input. Press <Esc> to exit or any other key”; gotoxy(12,26); cout<<” to try again.”; n=int(getche( )); if(n==27) exit(0); } } while(n<2 || n>12); gotoxy(6,16); cout<<”Enter the value of x0 = “; cin>>x0; gotoxy(6,18); cout<<”Enter the value of h = “; cin>>h; gotoxy(6,24); cout<<”Input Mode :”; gotoxy(6,25);

Page 53: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

45

cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(8,28); cout<<”Press : “; gotoxy(10,30); cout<<”- ‘Y’ or <Enter> to enter function”; gotoxy(10,32); cout<<”- ‘N’ or <Any other key> to enter values of the function”; gotoxy(8,35); cout<<”Enter your choice : “; char Choice=NULL; Choice=getch( ); if(Choice==’y’ || Choice==’Y’ || int(Choice)==13) { choice=1; gotoxy(28,35); cout<<”Y”; } else { gotoxy(28,35); cout<<”N”; } gotoxy(25,43); cout<<”Press any key to continue...”; getch( ); if(choice) { clear_screen( ); gotoxy(6,11); cout<<”Non-Linear Function :”; gotoxy(6,12); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(6,37); cout<<”Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3”; gotoxy(6,40); cout<<”Available Operators : ^ (raised to power) , * , / , + , -”; gotoxy(6,42); cout<<”Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,”; gotoxy(6,44); cout<<” n = any number”; gotoxy(6,14); cout<<”Enter the Function : f(x) = “; cin>>Fx; gotoxy(6,17); cout<<”Enter the Differential Function : f’(x) = “; cin>>Dfx; convert_ie_to_pe(Fx,0); convert_ie_to_pe(Dfx,1); } clear_screen( ); gotoxy(6,9); cout<<”Data Points & Values of Function :”; gotoxy(6,10);

Page 54: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

46

cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(25,12); cout<<”ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿”; gotoxy(25,13); cout<<”³ x ³ f(x) ³”; gotoxy(25,14); cout<<”ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”; gotoxy(25,15); cout<<”³ ³ ³”; for(int count_1=0;count_1<n;count_1++) { gotoxy(25,(wherey( )+1)); cout<<”³ ³ ³”; gotoxy(25,(wherey( )+1)); cout<<”³ ³ ³”; } gotoxy(25,(wherey( )+1)); cout<<”ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ”; xn[0]=x0; for(int count_2=0;count_2<(n-1);count_2++) xn[(count_2+1)]=(xn[count_2]+h); gotoxy(25,16); for(int count_3=0;count_3<n;count_3++) { gotoxy(27,wherey( )); cout<<xn[count_3]; if(choice) { fx[count_3]=evaluate_postfix_expression(xn[count_3],0); gotoxy(43,wherey( )); cout<<fx[count_3]; } else { gotoxy(43,wherey( )); cin>>fx[count_3]; } if(choice) gotoxy(25,(wherey( )+2)); else gotoxy(25,(wherey( )+1)); } gotoxy(25,43); cout<<”Press any key to continue...”; getch( ); }*************************************************************************///------------------- get_index(const long double) --------------------///*************************************************************************/constint get_index(constlongdouble x) { for(int count=0;count<n;count++) { if(xn[count]==x)

Page 55: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

47

break; } return count; }/*************************************************************************///---------------------------- estimate_dfx( ) ------------------------///*************************************************************************/void estimate_dfx( ) { clear_screen( ); gotoxy(6,9); cout<<”Difference Formulas of Order 1 :”; gotoxy(6,10); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(8,12); cout<<”Backward Difference Formula : f’(x) ÷ [f(x)-f(x-h)]/h”; gotoxy(8,14); cout<<”Forward Difference Formula : f’(x) ÷ [f(x+h)-f(x)]/h”; gotoxy(6,18); cout<<”Estimation of First Derivative - f’(x) :”; gotoxy(6,19); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; char Choice=NULL; longdouble x=0; longdouble dfx=0; longdouble actual_dfx=0; do { Choice=NULL; gotoxy(8,21); cout<<”Press :”; gotoxy(10,23); cout<<”<B> to estimate f’(x) using Backward Difference Formula”; gotoxy(10,25); cout<<”<F> to estimate f’(x) using Forward Difference Formula”; gotoxy(10,27); cout<<”<E> to exit”; do { gotoxy(8,30); cout<<”Enter your choice : “; Choice=getche( ); Choice=toupper(Choice); if(Choice==’B’ || Choice==’F’ || Choice==’E’) break; else { gotoxy(18,35); cout<<”Wrong Choice. Please select a valid Choice.”; gotoxy(25,37); cout<<”Press any Key to continue...”; getch( ); gotoxy(18,35); cout<<” “;

Page 56: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

48

gotoxy(25,37); cout<<” “; gotoxy(8,30); cout<<” “; } } while(1); if(Choice==’E’) exit(0); gotoxy(8,21); cout<<” “; gotoxy(10,23); cout<<” “; gotoxy(10,25); cout<<” “; gotoxy(10,27); cout<<” “; gotoxy(8,30); cout<<” “; Input_x_again: x=0; dfx=0; actual_dfx=0; gotoxy(10,21); cout<<”Enter the value of x = “; cin>>x; if(x>=xn[0] && x<=xn[(n-2)] && Choice==’B’) { int index=0; index=get_index(x); longdouble fxph=::fx[(index+1)]; longdouble fx=::fx[index]; dfx=((fxph-fx)/h); } elseif(x>=xn[1] && x<=xn[(n-1)] && Choice==’F’) { int index=0; index=get_index(x); longdouble fx=::fx[index]; longdouble fxmh=::fx[(index-1)]; dfx=((fx-fxmh)/h); } else { gotoxy(8,25); cout<<”Error: Please enter the valid value of x for the selected formula.”; gotoxy(15,27); cout<<”Press any Key to enter the value of x again...”; getch( ); gotoxy(8,25); cout<<” “; gotoxy(15,27); cout<<” “;

Page 57: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

49

gotoxy(10,21); cout<<” “; goto Input_x_again; } gotoxy(10,25); cout<<”The estimated value of f’(“<<x<<”) ÷ “<<dfx; if(choice) { actual_dfx=evaluate_postfix_expression(x,1); gotoxy(10,27); cout<<”The Actual value of f’(“<<x<<”) = “<<actual_dfx; gotoxy(10,31); cout<<”Absolute Error = E(abs) = “<<fabs((actual_dfx-dfx)); } gotoxy(25,42); cout<<”Press any other key to continue...”; getch( ); gotoxy(10,21); cout<<” “; gotoxy(10,25); cout<<” “; gotoxy(10,27); cout<<” “; gotoxy(10,31); cout<<” “; gotoxy(25,42); cout<<” “; } while(1); }

3.4 Newton’s Forward Difference InterpolationStatement: If x0, x1, x2,…, xn are given set of observations with common difference h and let y0, y1, y2,…, yn are their corresponding values, where y= f(x) be the given function then

Where

Proof: Let us assume nth degree polynomial

(1)

Substitute x=x0 in (1), we get f(x0) = A0 ⇒ y0 =A0

Substitute x=x1 in (1), we get f(x1) = A0+A1 (x1-x0)⇒ y1 = y0+A1h

⇒A1=

Page 58: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

50

Substitute x=x2 in (1), we get f(x2) = A0+A1 (x2−x0) +A2(x2−x0) (x2−x1)

⇒ y2 = y0+A1 (2h) + A2 (2h) (h)

Similarly, we get

Substituting these values in (1) we get

(2)

But given

Similarly, ...

Substituting in the equation (2) we get

Code for Newton’s forward difference interpolation in C Programming#include<stdio.h>#include<conio.h>#define MAX 10

int factorial(int temp);

void main(){ FILE *fp; int number,i,j,k=0;

Page 59: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

51

float xvalue[MAX],yvalue[MAX],search,product; float differ[MAX][MAX],m,uvalue,hvalue,sum; fp=fopen(“nwfdi.dat”,”w”); clrscr(); printf(“\n\n”); fprintf(fp,”\n\n”); printf(“How many numbers you want to enter for x : “); fprintf(fp,”How many numbers you want to enter for x : “); scanf(“%d”,&number); fprintf(fp,”%d”,number); for(i=0;i<number;i++) { printf(“\nEnter value for x(%d) : “,i); fprintf(fp,”\nEnter value for x(%d) : “,i); scanf(“%f”,&xvalue[i]); fprintf(fp,”%f”,xvalue[i]); printf(“\nEnter value for y(%d) : “,i); fprintf(fp,”\nEnter value for y(%d) : “,i); scanf(“%f”,&yvalue[i]); fprintf(fp,”%f”,yvalue[i]); } printf(“\nEnter any value of x for which you want to find y : “); fprintf(fp,”\nEnter any value of x for which you want to find y : “); scanf(“%f”,&search); fprintf(fp,”%f”,search); if(search<xvalue[0] || search>xvalue[number-1]) { printf(“\n\nValue lies outside the given values of x “); fprintf(fp,”\n\nValue lies outside the given values of x “); getch(); exit(1); } else { clrscr(); printf(“\n\nNEWTON’S FORWARD DIFFERENCE INTERPOLATION “); fprintf(fp,”\n\nNEWTON’S FORWARD DIFFERENCE INTERPOLATION “); for(j=0;j<number-1;j++) { for(i=0;i<number-(j+1);i++) { if(j==0) { differ[i][j]=yvalue[i+1]-yvalue[i];

} else { differ[i][j]=differ[i+1][j-1]-differ[i][j-1];

} } }

Page 60: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

52

printf(“\n\n”); fprintf(fp,”\n\n”); printf(“ x y “); fprintf(fp,” x y “); for(i=1;i<number;i++) { printf(“ d^%dy(i) “,i); fprintf(fp,” d^%dy(i) “,i); } printf(“\n\n”); fprintf(fp,”\n\n”); for(i=0;i<number;i++) { printf(“ %.2f %.2f “,xvalue[i],yvalue[i]); fprintf(fp,” %.2f %.2f “,xvalue[i],yvalue[i]); for(j=0;j<number-(i+1);j++) { printf(“ %.4f “,differ[i][j]); fprintf(fp,” %.4f “,differ[i][j]); } printf(“\n”); fprintf(fp,”\n”); }

for(i=0;i<number;i++) { if(search>xvalue[i]) { k=k+1; } } k=k-1; hvalue=xvalue[1]-xvalue[0]; uvalue=(search-xvalue[k])/hvalue; sum=yvalue[k]; for(i=0;i<number-(k+1);i++) { product=1; for(j=0;j<=i;j++) { product=product*(uvalue-j); } m=factorial(i+1); sum=sum+(differ[k][i]*product)/m;

} printf(“\n\n”); fprintf(fp,”\n\n”); printf(“Interpolated value is : %f “,sum); fprintf(fp,”Interpolated value is : %f “,sum); } fclose(fp); getch();

Page 61: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

53

}

int factorial(intvalue){ int i,temp=1; for(i=value;i>=1;i--) { temp=temp*i; } return(temp);}

3.5 Newton’s Backward Difference InterpolationStatement: If x0, x1, x2,…, xn are given set of observations with common difference h and let y0, y1, y2,…, yn are their corresponding values, where y= f(x) be the given function then

Where

Proof: Let us assume nth degree polynomial

(1)

Substitute x=xnin (1), we get f(xn) = A0 ⇒ yn =A0

Substitute x=xn-1 in (1), we get f (xn−1) = A0+A1 (xn−1-xn)⇒ yn−1 = yn+A1h

⇒A1=

Substitute x=xn−2 in (1), we get f(xn−2) = A0+A1 (xn−2−xn) +A2(xn−2−xn) (xn−2−xn−1)

⇒ yn-2 = yn+A1 (−2h) + A2 (−2h) (−h)

Similarly, we get

Substituting these values in (1) we get

(2)

Page 62: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

54

But given

Similarly,

.

Substituting in the equation (2) we get

Code for Newton’s backward difference interpolation in C Programming#include<stdio.h>#include<conio.h>#define MAX 10

int factorial(intvalue);void main(){ FILE *fp; int number,i,j,k=0,m; float xvalue[MAX],yvalue[MAX],search; float differ[MAX][MAX],uvalue,hvalue,product,sum; fp=fopen(“nwbdi.dat”,”w”); clrscr(); printf(“\n\n”); fprintf(fp,”\n\n”); printf(“How many numbers you want to enter for x : “); fprintf(fp,”How many numbers you want to enter for x : “); scanf(“%d”,&number); fprintf(fp,”%d”,number); for(i=0;i<number;i++) { printf(“\nEnter value for x(%d) : “,i); fprintf(fp,”\nEnter value for x(%d) : “,i); scanf(“%f”,&xvalue[i]); fprintf(fp,”%f”,xvalue[i]); printf(“\nEnter value for y(%d) : “,i); fprintf(fp,”\nEnter value for y(%d) : “,i);

Page 63: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

55

scanf(“%f”,&yvalue[i]); fprintf(fp,”%f”,yvalue[i]); } printf(“\nEnter any value of x for which you want to find y : “); fprintf(fp,”\nEnter any value of x for which you want to find y : “); scanf(“%f”,&search); fprintf(fp,”%f”,search); if(search<xvalue[0] || search>xvalue[number-1]) { printf(“\n\nValue lies outside the given values of x “); fprintf(fp,”\n\nValue lies outside the given values of x “); getch(); exit(1); } else { clrscr(); printf(“\n\nNEWTON’S BACKWARD DIFFERENCE INTERPOLATION “); fprintf(fp,”\n\nNEWTON’S BACKWARD DIFFERENCE INTERPOLATION “); for(j=0;j<number-1;j++) { for(i=j+1;i<number;i++) { if(j==0) { differ[i][j]=yvalue[i]-yvalue[i-1]; } else { differ[i][j]=differ[i][j-1]-differ[i-1][j-1]; } } } printf(“\n\n”); fprintf(fp,”\n\n”); printf(“ x y “); fprintf(fp,” x y “); for(i=1;i<number;i++) { printf(“ d^%dy(i) “,i); fprintf(fp,” d^%dy(i) “,i); } printf(“\n\n”); fprintf(fp,”\n\n”); for(i=0;i<number;i++) { printf(“ %.2f %.2f “,xvalue[i],yvalue[i]); fprintf(fp,” %.2f %.2f “,xvalue[i],yvalue[i]); for(j=0;j<i;j++) { printf(“ %.4f “,differ[i][j]); fprintf(fp,” %.4f “,differ[i][j]); }

Page 64: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

56

printf(“\n”); fprintf(fp,”\n”); } for(i=0;i<number;i++) { if(search>xvalue[i]) { k=k+1; } } hvalue=xvalue[1]-xvalue[0]; uvalue=(search-xvalue[k])/hvalue; sum=yvalue[k]; for(i=0;i<number-1 ;i++) { product=1; for(j=0;j<=i;j++) { product=product*(uvalue+j); } m=factorial(i+1); sum=sum+(differ[k][i]*product)/m; } printf(“\n\n”); printf(“Interpolated value is : %f “,sum); fprintf(fp,”Interpolated value is : %f “,sum); } fclose(fp); getch();}int factorial(intvalue){ int i,temp=1; for(i=value;i>=1;i--) { temp=temp*i; } return(temp);}

3.6 Lagrange’s InterpolationGiven that n is a nonnegative integer, let Pn denote the set of all (real-valued) polynomials of degree ≤ n defined over the set R of real numbers.

The simplest interpolation problem can be stated as follows: given x0 and y0 in R, find a polynomial p0 ∈ P0 such that p0(x0) = y0. The solution to this is, trivially, p0(x) ≡ y0. The purpose of this section is to explore the following more general problem.

Let n ≥ 1, and suppose that xi, i = 0, 1,… , n, are distinct real numbers (i.e., xi ≠ xj for i ≠ j) and yi, i = 0, 1,…, n, are real numbers; we wish to find pn ∈ Pn such that pn(xi) = yi, i = 0, 1,… , n.

To prove that this problem has a unique solution, we begin with a useful lemma.

Page 65: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

57

Lemma 3.1 Suppose that n ≥ 1. There exist polynomials Lk ∈ Pn, k = 0, 1,…, n, such that

(3.1)

for all i, k = 0, 1,…, n. Moreover,

satisfies the above interpolation conditions; in other words, pn ∈ Pn and pn(xi) = yi, i = 0, 1,…, , n.

Proof:For each fixed k, 0 ≤ k ≤ n, Lk is required to have n zeros – xi, i = 0, 1,…, n, i ≠ k; thus, Lk(x) is of the form

where Ck ∈ ℝ is a constant to be determined. It is easy to find the value of Ck by recalling that Lk (xk) = 1; using this in (3.3) yields

On inserting this expression for Ck into (6.3) we get

As the function pn defined by (3.2) is a linear combination of the polynomials Lk ∈ Pn, k = 0, 1,…, n, also pn ∈ Pn. Finally, pn(xi) = yi for i = 0, 1,…, n is a trivial consequence of using (3.1) in (3.2).

Remark 3.1 Although the statement of Lemma 3.1 required that n ≥ 1, the trivial case of n = 0 mentioned at the beginning of the section can also be included by defining, for n = 0, L0(x) ≡ 1, and observing that the function p0 defined by

p0(x) = L0(x) y0 (≡ y0)

is the unique polynomial in P0 that satisfies p0(x0) = y0.

We note that, implicitly, the polynomials Lk, k = 0, 1,…, n, depend on the polynomial degree n, n ≥ 0. To highlight this fact, a more accurate but cumbersome notation would have involved writing, for example, instead of Lk(x); this would have made it clear that differs from when the polynomial degrees n and m differ. For the sake of notational simplicity, we have chosen to write Lk(x); the implied value of n will always be clear from the context.

Page 66: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

58

Theorem(Lagrange’s Interpolation Theorem) Assume that n ≥ 0. Let xi, i = 0,…, n, be distinct real numbers and suppose that yi, i = 0,…, n, are real numbers. Then, there exists a unique polynomial pn ∈ Pn such that

Proof:In view of Remark 3.1, for n = 0 the proof is trivial. Let us therefore suppose that n ≥ 1. It follows immediately from Lemma 3.1that the polynomial pn ∈ Pn defined by

satisfies the conditions (3.5), thus showing the existence of the required polynomial. It remains to show that pn is the unique polynomial in Pn satisfying the interpolation property

Suppose, otherwise, that there exists qn ∈ Pn, different from pn, such that qn(xi) = yi, i = 0, 1,…. , n. Then, pn − qn ∈ Pn and pn − qn has n + 1 distinct roots, xi, i = 0, 1,…, n; since a polynomial of degree n cannot have more than n distinct roots, unless it is identically 0, it follows that

which contradicts our assumption that pn and qn are distinct. Hence, there exists only one polynomial pn ∈ Pn which satisfies (3.5).

Definition: Suppose that n ≥ 0. Let xi, i = 0,…, n, be distinct real numbers, and yi, i = 0,…, n, real numbers. The polynomial pn defined by

with Lk(x), k = 0, 1, . . . , n, defined by (3.4) when n ≥ 1, and L0(x) ≡ 1 when n = 0, is called the Lagrange interpolation polynomial of degree n for the set of points {(xi, yi): i = 0, . . . , n}. The numbers xi, i = 0,…, n, are called the interpolation points.

Frequently, the real numbers yi are given as the values of a real-valued function f, defined on a closed real interval [a, b], at the (distinct) interpolation points xi ∈ [a, b], i = 0,…, n.

Definition 3.2: Let n ≥ 0. Given the real-valued function f, defined and continuous on a closed real interval [a, b], and the (distinct) interpolation points xi ∈ [a, b], i = 0,…, n, the polynomial pn defined by

is the Lagrange interpolation polynomial of degree n (with interpolation points xi, i = 0,… , n) for the function f.

Page 67: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

59

Example 3.1We shall construct the Lagrange interpolation polynomial of degree 2 for the function f: x ↦ ex on the interval [−1, 1], with interpolation points x0 = −1, x1 = 0, x2 = 1.

As n = 2, we have that

Similarly, and

Therefore,

Thus, after some simplification

Although the values of the function f and those of its Lagrange interpolation polynomial coincide at the interpolation points, f(x) may be quite different from pn(x) when x is not an interpolation point. Thus, it is natural to ask just how large the difference f(x) − pn(x) is when x ≠ xi, i = 0,…, n. Assuming that the function f is sufficiently smooth, an estimate of the size of the interpolation error f(x)−pn(x) is given in the next theorem.

Theorem 3.2Suppose that n ≥ 0, and that f is a real-valued function, defined and continuous on the closed real interval [a, b], such that the derivative of f of order n + 1 exists and is continuous on [a, b]. Then, given that x ∈ [a, b], there exists ξ = ξ(x) in (a, b) such that

Where,

Moreover

Proof:When x = xi for some i, i = 0, 1,…, n, both sides of (3.8) are zero, and the equality is trivially satisfied. Suppose then that x ∈ [a, b] and x ≠ xi, i = 0, 1,…, n. For such a value of x, let us consider the auxiliary function t ↦ ϕ(t), defined on the interval [a, b] by

Clearly ϕ(xi) = 0, i = 0, 1,…, n, and ϕ(x) = 0. Thus, ϕ vanishes at n + 2 points which are all distinct in [a, b]. Consequently, by Rolle’s Theorem, Theorem A.2, ϕ′ (t), the first derivative of ϕ with respect to t, vanishes at n + 1 points in (a, b), one between each pair of consecutive points at which ϕ vanishes.

Page 68: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

60

In particular, if n = 0, we then deduce the existence of ξ = ξ(x) in the interval (a, b) such that ϕ′(ξ) = 0. Since p0(x) ≡ f(x0) and π1 (t) = t − x0, it follows from (3.11) that

and hence, (3.8) in the case of n=0.

Now suppose that n ≥ 1. As ϕ′(t) vanishes at n+1 points in (a, b), one between each pair of consecutive points at which ϕ vanishes, applying Rolle’s. Theorem again, we see that ϕ′′ vanishes at n distinct points. Our assumptions about f are sufficient to apply Rolle’s Theorem n+1 times in succession, showing that ϕ(n+1) vanishes at some point ξ ∈ (a, b), the exact value of ξ being dependent on the value of x. By differentiating n + 1 times the function ϕ with respect to t, and noting that pn is a polynomial of degree n or less, it follows that

Hence

In order to prove (3.10), we note that as f (n+1) is a continuous function on [a, b] the same is true of |f(n+1)|. Therefore, the function x ↦ |f(n+1)(x)| is bounded on [a, b] and achieves its maximum there; so (3.10) follows from (3.8).

A C++ Program to construct Lagranges’s Interpolation Formula from the given distinct data points and estimate the value of the function at the given points.//--------------------------- Header Files ----------------------------//# include <iostream.h> # include <stdlib.h> # include <string.h> # include <stdio.h> # include <conio.h> # include <math.h>

//------------------------ Global Variables ---------------------------//const int max_size=12;

int n=0; int top=-1; int choice=0;

long double xn[max_size]={0}; long double fx[max_size]={0}; long double px[max_size]={0};

char Non_linear_equation[100]={NULL}; char Stack[30][30]={NULL}; char Postfix_expression[30][30]={NULL};

//------------------------ Funcion Prototypes -------------------------// void push(const char *);

Page 69: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

61

void convert_infix_expression_to_postfix_expression(const char *); const char* pop( ); const long double evaluate_postfix_expression(const long double);

void show_screen( ); void clear_screen( ); void get_input( ); void compute_lagranges_interpolation_polynomial( ); void estimate_pnx( );//------------------------------ main( ) ------------------------------//int main( ) { clrscr( ); textmode(C4350);

show_screen( ); get_input( ); compute_lagranges_interpolation_polynomial( ); estimate_pnx( );

return 0; }//------------------------ Funcion Definitions ------------------------////-------------------------- show_screen( ) ---------------------------// void show_screen( ) { cprintf(“\n********************************************************************************”); cprintf(“*****************************- -***************************”); cprintf(“*----------------------------- “);

textbackground(1); cprintf(“ Lagrange’s Formula “); textbackground(8); cprintf(“ ---------------------------*”); cprintf(“*-***************************- -*************************-*”); cprintf(“*-****************************************************************************-*”); for(int count=0;count<42;count++) cprintf(“*-* *-*”); gotoxy(1,46); cprintf(“*-****************************************************************************-*”); cprintf(“*------------------------------------------------------------------------------*”); cprintf(“********************************************************************************”); gotoxy(1,2); }//------------------------- clear_screen( ) ---------------------------// void clear_screen( ) { for(int count=0;count<37;count++) {

Page 70: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

62

gotoxy(5,8+count); cout<<” “; }

gotoxy(1,2); }

//-------------------------- push(const char*) ------------------------// void push(const char* Operand) { if(top==(max_size-1)) { cout<<”Error : Stack is full.”<<endl; cout<<”\n Press any key to exit.”; getch( ); exit(0); } else { top++; strcpy(Stack[top],Operand); } }//------------------------------ pop( ) -------------------------------// const char* pop( ) { char Operand[40]={NULL}; if(top==-1) { cout<<”Error : Stack is empty.”<<endl; cout<<”\n Press any key to exit.”; getch( ); exit(0); } else { strcpy(Operand,Stack[top]); strset(Stack[top],NULL); top--; } return Operand; }

//---- convert_infix_expression_to_postfix_expression(const char*) ----// void convert_infix_expression_to_postfix_expression(const char* Expression) { char Infix_expression[100]={NULL}; char Symbol_scanned[30]={NULL}; push(“(“); strcpy(Infix_expression,Expression); strcat(Infix_expression,”+0)”); int flag=0; int count_1=0;

Page 71: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

63

int count_2=0; int equation_length=strlen(Infix_expression); if(Infix_expression[0]==’(‘) flag=1; do { strset(Symbol_scanned,NULL); if(flag==0) { int count_3=0; do { Symbol_scanned[count_3]=Infix_expression[count_1]; count_1++; count_3++; } while(count_1<=equation_length && Infix_expression[count_1]!=’(‘ && Infix_expression[count_1]!=’+’ && Infix_expression[count_1]!=’-’ && Infix_expression[count_1]!=’*’ && Infix_expression[count_1]!=’/’ && Infix_expression[count_1]!=’^’ && Infix_expression[count_1]!=’)’); flag=1; } else if(flag==1) { Symbol_scanned[0]=Infix_expression[count_1]; count_1++; if(Infix_expression[count_1]!=’(‘ && Infix_expression[count_1]!=’^’ && Infix_expression[count_1]!=’*’ && Infix_expression[count_1]!=’/’ && Infix_expression[count_1]!=’+’ && Infix_expression[count_1]!=’-’ && Infix_expression[count_1]!=’)’) flag=0; if(Infix_expression[count_1-1]==’(‘ && (Infix_expression[count_1]==’-’ || Infix_expression[count_1]==’+’)) flag=0; } if(strcmp(Symbol_scanned,”(“)==0) push(“(“); else if(strcmp(Symbol_scanned,”)”)==0) { while(strcmp(Stack[top],”(“)!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } pop( );

Page 72: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

64

} else if(strcmp(Symbol_scanned,”^”)==0 || strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0 || strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0) { if(strcmp(Symbol_scanned,”^”)==0) { } else if(strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0) { while(strcmp(Stack[top],”^”)==0 || strcmp(Stack[top],”*”)==0 || strcmp(Stack[top],”/”)==0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } else if(strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0) { while(strcmp(Stack[top],”(“)!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } push(Symbol_scanned); } else { strcat(Postfix_expression[count_2],Symbol_scanned); count_2++; } } while(strcmp(Stack[top],NULL)!=0); strcat(Postfix_expression[count_2],”=”); count_2++; }//---------- evaluate_postfix_expression(const long double) -----------// const long double evaluate_postfix_expression(const long double x) { long double function_value=0; int count_1=-1; char Symbol_scanned[30]={NULL}; do { count_1++; strcpy(Symbol_scanned,Postfix_expression[count_1]); if(strcmp(Symbol_scanned,”^”)==0 || strcmp(Symbol_scanned,”*”)==0 ||

Page 73: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

65

strcmp(Symbol_scanned,”/”)==0 || strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0) { char Result[30]={NULL}; char Operand[2][30]={NULL}; strcpy(Operand[0],pop( )); strcpy(Operand[1],pop( )); long double operand[2]={0}; long double result=0; char *endptr; for(int count_2=0;count_2<2;count_2++) { int flag=0; if(Operand[count_2][0]==’-’) { int length=strlen(Operand[count_2]); for(int count_3=0;count_3<(length-1);count_3++) Operand[count_2][count_3]=Operand[count_2][(count_3+1)]; Operand[count_2][count_3]=NULL; flag=1; } if(strcmp(Operand[count_2],”x”)==0) operand[count_2]=x; else if(strcmp(Operand[count_2],”e”)==0) operand[count_2]=2.718282; else if(strcmp(Operand[count_2],”sinx”)==0) operand[count_2]=sinl(x); else if(strcmp(Operand[count_2],”cosx”)==0) operand[count_2]=cosl(x); else if(strcmp(Operand[count_2],”tanx”)==0) operand[count_2]=tanl(x); else if(strcmp(Operand[count_2],”lnx”)==0) operand[count_2]=logl(x); else if(strcmp(Operand[count_2],”logx”)==0) operand[count_2]=log10l(x); else operand[count_2]=strtod(Operand[count_2],&endptr); if(flag) operand[count_2]*=-1; } switch(Symbol_scanned[0]) { case ‘^’ : result=powl(operand[1],operand[0]); break; case ‘*’ : result=operand[1]*operand[0]; break; case ‘/’ : result=operand[1]/operand[0]; break; case ‘+’ : result=operand[1]+operand[0]; break; case ‘-’ : result=operand[1]-operand[0]; break;

Page 74: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

66

} gcvt(result,25,Result); push(Result);} else if(strcmp(Symbol_scanned,”=”)!=0) push(Symbol_scanned); } while(strcmp(Symbol_scanned,”=”)!=0); char Function_value[30]={NULL}; char *endptr; strcpy(Function_value,pop( )); function_value=strtod(Function_value,&endptr); return function_value; }//----------------------------- get_input( ) --------------------------// void get_input( ) { do { clear_screen( ); gotoxy(6,9); cout<<”Number of Distinct Data Points :”; gotoxy(6,10); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(27,13); cout<<”[ min. n = 2 | max. n = 12 ]”; gotoxy(6,12); cout<<”Enter the max. number of distinct data points = n = “; cin>>n; if(n<2 || n>12) { gotoxy(12,25); cout<<”Error : Wrong Input. Press <Esc> to exit or any other key”; gotoxy(12,26); cout<<” to try again.”; n=int(getche( )); if(n==27) exit(0); } } while(n<2 || n>12); gotoxy(6,19); cout<<”Input Mode :”; gotoxy(6,20); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(8,23); cout<<”Press : “; gotoxy(10,25); cout<<”- ‘Y’ or <Enter> to enter function”; gotoxy(10,27); cout<<”- ‘N’ or <Any other key> to enter values of the function”; gotoxy(8,30); cout<<”Enter your choice : “; char Choice=NULL;

Page 75: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

67

Choice=getch( ); if(Choice==’y’ || Choice==’Y’ || int(Choice)==13) { choice=1; gotoxy(28,30); cout<<”Y”; } else { gotoxy(28,30); cout<<”N”; } gotoxy(25,43); cout<<”Press any key to continue...”; getch( ); if(choice) { clear_screen( ); gotoxy(6,11); cout<<”Non-Linear Function :”; gotoxy(6,12); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(6,37); cout<<”Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3”; gotoxy(6,40); cout<<”Available Operators : ^ (raised to power) , * , / , + , -”; gotoxy(6,42); cout<<”Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,”;gotoxy(6,44); cout<<” n = any number”; gotoxy(6,14); cout<<”Enter the Function : f(x) = “; cin>>Non_linear_equation; convert_infix_expression_to_postfix_expression(Non_linear_equation); } clear_screen( ); gotoxy(6,9); cout<<”Data Points & Values of Function :”; gotoxy(6,10); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(25,12); cout<<”ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿”; gotoxy(25,13); cout<<”³ x ³ f(x) ³”; gotoxy(25,14); cout<<”ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”; gotoxy(25,15); cout<<”³ ³ ³”; for(int count_1=0;count_1<n;count_1++) { gotoxy(25,(wherey( )+1)); cout<<”³ ³ ³”; gotoxy(25,(wherey( )+1));

Page 76: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

68

cout<<”³ ³ ³”; } gotoxy(25,(wherey( )+1)); cout<<”ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ”; gotoxy(25,16); for(int count_2=0;count_2<n;count_2++) { gotoxy(27,wherey( )); cin>>xn[count_2]; if(choice) { fx[count_2]=evaluate_postfix_expression(xn[count_2]); gotoxy(43,(wherey( )-1)); cout<<fx[count_2]; } else { gotoxy(43,(wherey( )-1)); cin>>fx[count_2]; } if(choice) gotoxy(25,(wherey( )+2)); else gotoxy(25,(wherey( )+1)); } gotoxy(25,43); cout<<”Press any key to continue...”; getch( ); }/---------- compute_lagranges_interpolation_polynomial( ) ------------// void compute_lagranges_interpolation_polynomial( ) { for(int count_1=0;count_1<n;count_1++) xn[count_1]=(xn[count_1]*(-1)); long double numerator[max_size][max_size]={0}; long double x[max_size]={0}; for(int count_2=0;count_2<n;count_2++) { for(int count_3=0;count_3<(n-1);count_3++) { if(count_3<count_2) x[count_3]=xn[count_3]; else if(count_3>=count_2) x[count_3]=xn[(count_3+1)]; } numerator[count_2][0]=x[0]; numerator[count_2][1]=1; for(int count_4=0;count_4<(n-2);count_4++) { numerator[count_2][(2+count_4)]=1; for(int count_5=(1+count_4);count_5>0;count_5--) { numerator[count_2][count_5]*=x[(count_4+1)];

Page 77: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

69

numerator[count_2][count_5]+= numerator[count_2][(count_5-1)]; } numerator[count_2][0]*=x[(count_4+1)]; } } for(int count_6=0;count_6<n;count_6++) { for(int count_7=0;count_7<n;count_7++) numerator[count_6][count_7]*=fx[count_6]; } long double denominator[max_size]={0}; for(int count_8=0;count_8<n;count_8++) { denominator[count_8]=1; for(int count_9=0;count_9<n;count_9++) { if(count_8!=count_9) denominator[count_8]*=(-1*(xn[count_8]-xn[count_9])); } } long double equation[max_size][max_size]={0}; for(int count_10=0;count_10<n;count_10++) { for(int count_11=0;count_11<n;count_11++) equation[count_10][count_11]= (numerator[count_10][count_11]/denominator[count_10]); } for(int count_12=0;count_12<n;count_12++) { for(int count_13=0;count_13<n;count_13++) px[count_12]+=equation[count_13][count_12]; } }//---------------------------- estimate_pnx( ) ------------------------//void estimate_pnx( ) { clear_screen( ); gotoxy(6,10); cout<<”Lagrange’s Formula :”; gotoxy(6,11); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(10,13); cout<<”P”<<(n-1)<<”(x) = “; for(int count_1=(n-1);count_1>=0;count_1--) { if(count_1==(n-1) && n>2) cout<<px[count_1]<<” x”<<count_1; else if(count_1>1) cout<<fabs(px[count_1])<<” x”<<count_1; else if(count_1==1) cout<<fabs(px[count_1])<<” x”; else if(count_1==0)

Page 78: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

70

cout<<fabs(px[count_1]); if(wherex( )>=70) gotoxy(30,(wherey( )+2)); if(count_1>0) { if(px[(count_1-1)]>0) cout<<” + “; else cout<<” - “; } if(wherex( )>=70) gotoxy(30,(wherey( )+2)); } gotoxy(6,19); cout<<”Estimation of Pn(x) :”; gotoxy(6,20); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; char Choice=NULL; long double x=0; long double pnx=0; do { Choice=NULL; x=0; pnx=0; gotoxy(10,22); cout<<”Enter the value of x = “; cin>>x; pnx=px[0]; pnx+=(px[1]*x); for(int count_2=2;count_2<n;count_2++) { long double temp=x; for(int count_3=1;count_3<count_2;count_3++) temp*=x; pnx+=(px[count_2]*temp); } gotoxy(10,26); cout<<”The Estimated value of P”<<(n-1)<<”(“<<x<<”) = “<<pnx; if(choice) { long double fx=0; fx=evaluate_postfix_expression(x); gotoxy(10,28); cout<<”The Actual value of f”<<”(“<<x<<”) = “<<fx; gotoxy(10,30); cout<<”Absolute Error = E(abs) = “<<fabs((fx-pnx)); } gotoxy(15,42); cout<<”Press <Esc> to exit or any other key to continue...”; Choice=getch( ); if(int(Choice)!=27) {

Page 79: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

71

gotoxy(10,22); cout<<” “; gotoxy(10,26); cout<<” “; gotoxy(10,28); cout<<” “; gotoxy(10,30); cout<<” “; gotoxy(15,42); cout<<” “; } } while(int(Choice)!=27); gotoxy(1,2); }

Page 80: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

72

SummaryThe process of finding the curve passing through the points (x• 0, y0), (x1, y1), (x2, y2),…, (xn, yn) is called as Interpolation and the curve obtained is called as Interpolating curve.Forward difference operator is denoted by • ∆ and is defined as ∆y0=y1−y0, ∆y1=y2−y1,…, ∆yn=yn−yn-1

The difference of first forward differences will give us Second forward differences and it is denoted by • ∆2 and is defined as ∆2y0 = ∆(∆y0)The difference of second forward differences will give us third forward difference and it is denoted by • ∆3.The difference of first Backward differences will give us Second Backward differences and it is denoted by • ∇2 and is defined as∇2y0 = ∇(∇y0).

ReferencesSzabados, J. and Vértesi, P., 1990. • Interpolation of functions, World Scientific.Dahlquist, G. and Björck, A., 2008. • Numericalmethodsinscientificcomputing, SIAM.Reddy, Y. P., • INTERPOLATION [pdf] Available at: <http://www.sakshieducation.com/(Scfzi10iftouq0lbbjwswlr55))/Engg/EnggAcademia/CommonSubjects/MathMethods-Interpolation.pdf> [Accessed 17 November 2011].Lal, K. A., 2007. • Lagrange’s Interpolation formula [Online] Available at: <http://nptel.iitm.ac.in/courses/Webcourse-contents/IIT-KANPUR/mathematics-2/node113.html> [Accessed 17 November 2011].burny1, 2010. • Interpolation - Newtons forward/difference methods [Video Online] Available at: <http://www.youtube.com/watch?v=Uzc3Tc2ZUXw> [Accessed 17 November 2011].USF, 2010. • Newton’sDividedDifferencePolynomial:LinearInterpolation:Example [Video Online] Available at: <http://www.youtube.com/watch?v=qtraD3e6Ka0> [Accessed 17 November 2011].

Recommended ReadingRao, S., 2008. • NumericalMethodsForScientistsAndEngineers, 3rd, ed., PHI Learning Pvt. Ltd.Sinha, S. and Pradhan, S., • NumericalAnalysis&StatisticalMethods, Academic Publishers.Ahmed, S. Q., 2009. • NUMERICALANDSTATISTICALTECHNIQUES, Ane Books Pvt Ltd.

Page 81: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

73

Self Assessment The process of finding the curve passing through the points (x1. 0, y0), (x1, y1), (x2, y2),…, (xn, yn) is called ____________.

Interpolationa. Intersection b. Immigration c. Imagination d.

The process of finding the curve passing through the points (x2. 0, y0), (x1, y1), (x2, y2),…, (xn, yn ) is called _______________.

Intersection curvea. Immigration curveb. Imagination curvec. Interpolating curved.

The method to find 3. f(xm) ∀ x0 ≤ xm ≤ xn is known as an ________________.Interpolationa. Intersection b. Immigration c. Imagination d.

The ___________difference operator is denoted by 4. ∆.Reversea. Forwardb. Backward c. Square d.

The difference of first forward differences will give us Second forward differences and it is denoted by 5. ______________.

a. ∆2

b. ∆ c. ∆4

d. ∆3

The difference of second forward differences will give us third forward difference and it is denoted by 6. __________.

a. ∆2

b. ∆ c. ∆4

d. ∆3

The Backward difference operator is denoted by _________.7. –a. <<b. c. ∇ d. ∆

Page 82: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

74

What does the syntax “Count_2++” do?8. Increase value by 2 a. Decrease value by 2 b. Replace value by 2 c. Copy value by 2d.

Which of the following data type will be used for storing “abc” value?9. Float a. Arrayb. Char c. Int d.

When is “push” used in program?10. Adds a new element at the top of the stacka. Remove a new element from the top of the stackb. Subtract a new element at the top of the stackc. Add array at the top of the stackd.

Page 83: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

75

Chapter IV

Least Square Curve Fitting

Aim

The aim of this chapter is to:

explain the concept of curve fitting •

describe the method of least square •

introduce polynomials least-squares• fitting

Objectives

The objectives of this chapter are to:

derive expression for the least-squares line•

explain the least-squares parabola•

elaborate the least-squares mth degree polynomials •

Learning outcome

At the end of this chapter, you will be able to:

identify the concept of curve fitting•

understand multiple regression •

recognise• the method of least square

Page 84: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

76

4.1 Curve FittingCurve fitting is a process of introduction mathematical relationship between dependent and independent variables in the form of an equation for a given set of data.

Field data is often accompanied by noise. Even though all control parameters (independent variables) remain constant, the resultant outcomes (dependent variables) vary. A process of quantitatively estimating the trend of the outcomes, also known as regression or curve fitting, therefore becomes necessary.

The curve fitting process fits equations of approximating curves to the raw field data. Nevertheless, for a given set of data, the fitting curves of a given type are generally NOT unique. Thus, a curve with a minimal deviation from all data points is desired. This best-fitting curve can be obtained by the method of least squares.

4.2 Method of Least SquareThe method of least squares assumes that the best-fit curve of a given type is the curve that has the minimal sum of the deviations squared (least square error) from a given set of data.

Suppose that the data points are (x1, y1), (x2,y2) ..., (xn, yn) where x is the independent variable and y is the dependent variable. The fitting curve f(x) has the deviation (error) d from each data point, i.e., d1=y1−f(x1), d2= y2− f(x2), ..., dn=yn−f(xn). According to the method of least squares, the best fitting curve has the property that:

4.3 Polynomials Least-Squares FittingPolynomials are one of the most commonly used types of curves in regression. The applications of the method of least squares curve fitting using polynomials are briefly discussed as follows.

4.3.1 The Least-Squares LineThe least-squares line uses a straight line

Y = a+bx

to approximate the given set of data, (x1, y1), (x2,y2) ..., (xn, yn), where n≥2.The best fitting curve f(x) has the least square error, i.e.,

Please note that and are unknown coefficients while all xi and yi are given. To obtain the least square error, the unknown coefficients a and b must yield zero first derivatives.

Page 85: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

77

Expanding the above equations, we have

The unknown coefficients a and b can therefore be obtained:

Where Σ stands for

4.3.2 The Least-Squares ParabolaThe least-squares parabola uses a second degree curve

to approximate the given set of data, , (x1, y1), (x2,y2) ..., (xn, yn), where n≥3

The best fitting curve f(x) has the least square error, i.e.,

Please note that a, b, and c are unknown coefficients while all xi and yi are given. To obtain the least square error, the unknown coefficients , , and must yield zero first derivatives

Page 86: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

78

Expanding the above equations, we have

The unknown coefficients a, b and c can hence be obtained by solving the above linear equations.

4.3.3 The Least-Squares mth Degree PolynomialsWhen using an mth degree polynomial

to approximate the given set of data, , (x1, y1), (x2,y2) ..., (xn, yn), where n≥ m+1, the best fitting curve f(x) has the least square error, i.e.,

Please note that a0, a1, a2,... and am are unknown coefficients while all xi and yi are given. To obtain the least square error, the unknown coefficients a0, a1, a2,... and am must yield zero first derivatives.

Page 87: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

79

Expanding the above equations, we have

The unknown coefficients a0, a1, a2, ... and am can hence be obtained by solving the above linear equations.

4.3.4 Multiple RegressionMultiple regression estimates the outcomes (dependent variables) which may be affected by more than one control parameter (independent variables) or there may be more than one control parameter being changed at the same time.

An example is the two independent variables x and y and one dependent variable z in the linear relationship case:z=a+bx+cy

Page 88: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

80

For a given data set (x1, y1, z1), (x2, y2, z2) , ..., , (xn, yn, zn) where ,n≥3, the best fitting curve f(x) has the least square error, i.e.,

Please note that a, b, and c are unknown coefficients while all xi, yi, and zi, are given. To obtain the least square error, the unknown coefficients a, b, and c must yield zero first derivatives

Expanding the above equations, we have

The unknown coefficients a, b, and c can hence be obtained by solving the above linear equations.

Page 89: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

81

Summary Curve fitting is a process of introduction mathematical relationship between dependent and independent variables • in the form of an equation for a given set of data.A process of quantitatively estimating the trend of the outcomes, also known as regression or curve fitting, • therefore becomes necessary.The curve fitting process fits equations of approximating curves to the raw field data• The method of least squares assumes that the best-fit curve of a given type is the curve that has the minimal • sum of the deviations squared (least square error) from a given set of data.Polynomials are one of the most commonly used types of curves in regression.• Multiple regression estimates the outcomes (dependent variables) which may be affected by more than one • control parameter (independent variables) or there may be more than one control parameter being changed at the same time.

References Least Square Method, [Online] Available at: <http://www.efunda.com/math/leastsquares/leastsquares.cfm> • [Accessed 6 January 2012].Curve Fitting and Method of Least Squares, [Online] Available at: <http://www.emathzone.com/tutorials/basic-• statistics/curve-fitting-and-method-of-least-squares.html>[Accessed 6 January 2012].Lipschutz, S. and Lipson, M., 2000. • Schaum’soutlineoftheoryandproblemsofprobability, 2nd ed., McGraw-Hill Professional, 2000.Gupta, S. K., 2006. • Numerical Methods for Engineers, New Age International.LearnChemE, 2011. • Least SquaresMethod, [Video Online] Available at: <http://www.youtube.com/watch?v=yVkpq20OtcE>[Accessed 6 January 2012].KaNaungThinkTank, 2007. • LeastSquareMethodParttwo, [Video Online] Available at: <http://www.youtube.com/watch?v=P-d5pUB4yQw>[Accessed 6 January 2012].

Recommended ReadingNixon, M. S., Aguado, A. S., 2008. • Feature extraction and image processing, 2nd ed., Academic Press.Hauser, J. R., 2009. • Numerical Methods for Nonlinear Engineering Models, Springer.Stark, P., 1970. • Introduction to numerical methods. Macmillan.

Page 90: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

82

Self Assessment____________is a process of introducing mathematical relationship between dependent and independent variables 1. in the form of an equation for a given set of data.

Curve fittinga. Integrationb. Parabolic fittingc. Deviation d.

Which of following statements is true?2. A process of alternatively estimating the trend of the outcomes is known as regression or curve fitting. a. A process of quantitatively estimating the trend of the outcomes is known as regression or curve fitting.b. A process of quantitatively estimating the trend of the outcomes is also known integrating.c. A process of quantitatively estimating the trend of the outcomes is known as parabolic fitting.d.

The ___________ process fits equations of approximating curves to the raw field data.3. integrationa. deviationb. curve fittingc. interpolation d.

The method of ___________ assumes that the best-fit curve of a given type is the curve that has the minimal 4. sum of the deviations squared (least square error) from a given set of data.

least parabolaa. least lineb. least polynomialc. least squaresd.

__________ are one of the most commonly used types of curves in regression.5. Polynomialsa. Equationsb. Decimalsc. Integers d.

Which of the following estimates the outcomes (dependent variables) which may be affected by more than one 6. control parameter (independent variables) or there may be more than one control parameter being changed at the same time?

Single regressiona. Multiple regressionb. Multiple analysisc. Single analysisd.

“Y=ax+b” represents which of the following?7. Parabolaa. Curveb. Straight linec. Rectangled.

Page 91: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

83

Which of following statements is true?8. The best-fitting curve cannot be obtained by the method of least squares.a. The best-fitting curve can be obtained by the method of integration.b. The best-fitting curve can be obtained by the method of polynomials.c. The best-fitting curve can be obtained by the method of least squares.d.

Page 92: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

84

Chapter V

Solution of Simultaneous Algebraic Equation

Aim

The aim of this chapter is to:

elaborate Cramer’s rule•

explain Gauss elimination method•

describe Gauss-Jordan method•

Objectives

The objectives of this chapter are to:

elucidate Gauss elimination with partial pivoting•

explain Gauss elimination with complete pivoting•

describe Gauss Siedel method•

Learning outcome

At the end of this chapter, you will be able to:

understand the algorithm for• Gauss-Siedel method

solve a system of n equations in n unknowns using Gaussian elimination•

derive a c• ode for Gauss Seidel method in C Programming

Page 93: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

85

5.1 Cramer’s RuleCramer’s rule is an alternative way of solving a set of simultaneous linear equations by matrix methods. Suppose we have three simultaneous linear equations:

4x + y – 5z = 8-2x + 3y + z = 123x – y + 4z = 5If we define

Then we can write the simultaneous equation as:

Ax=b

Cramer’s rule says that to find the first unknown, x, in the vector x of unknowns, we proceed as follows.

Step 1:Take the matrix A and form a new matrix by replacing the first column of A with the column vector b. We call this new matrix A1. So A1 is given by:

Step 2: Calculate the determinants │A│ and │A1│. Given │A│= 98 (from an example on the matrix algebra help sheet). Using the first row of A1, we can calculate │A1│as│A1│ = 8[(3 x 4) – (-1 x 1)] – 1[(12 x 4) – (5 x 1)] + (-5)[(12 x -1) – (5 x 3)] = 196

Step 3:The first unknown, x, in the vector x of unknowns, is then given by

To find the second unknown, y, in the vector x of unknowns, we follow steps 1 to 3 above, but in step 2 we form a new matrix │A2│, by replacing the second column of A with the column vector b. So A2 is given by

And in step 3 we find that:

Page 94: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

86

Finally, the third variable, z, is found in the same way by forming a new matrix, A3, by replacing the third column of A with the column vector b. The solution for z is then:

This gives us the solution, x = 2, y = 5 and z = 1

A set of simultaneous equations can be solved either by matrix inversion or by applying Cramer’s rule. The choice between the two is a matter of personal choice, though Cramer’s rule has the advantage that less tedious computations are necessary if we are only interested in the solution value of one of the variables.

In practice however computer programs such as Excel, can both evaluate determinants and invert matrices.

Program: cramers rule.cpp#include <iostream.h>#include <string.h>const int MAX_SIZE = 20;// Level 1 Functionsvoid print_message();void initialise(float &, float *, float *, float *);void allocate_floats(int &, float *&, float *&, float *&);void initialise_dynamic_memory(float *, int);void get_matrices(float [][MAX_SIZE], float *, int);void calculate_variable_matrix(float &, float *, float *, float [][MAX_SIZE], float *, int);void print_results(float *, float *, int, float);void return_memory(float *, float *, float *);// Level 2 Functionsvoid calculate_determinant(float &, float [][MAX_SIZE], int);void calculate_modified_determinant(float *, float [][MAX_SIZE], float [], int);void calculate_variables(float *, float *, float, int);void print_determinants(float *, int, float);void print_variables(float *, int, float);// Level 3 Functions void copy_matrix(float[][MAX_SIZE], float [][MAX_SIZE], int);bool swap_rows(float [][MAX_SIZE], int, int);int main(){ int size; float coefficient[MAX_SIZE][MAX_SIZE], *variable, *constant, *determinant_modified, determinant_original; char quit[1]; char yes[] = “y”; print_message(); do { initialise(determinant_original, variable, constant, determinant_modified); allocate_floats(size, variable, constant, determinant_modified); initialise_dynamic_memory(determinant_modified, size); get_matrices(coefficient, constant, size); calculate_variable_matrix(determinant_original, determinant_modified, variable, coefficient, constant, size); print_results(variable, determinant_modified, size, determinant_original); return_memory(variable, constant, determinant_modified);

Page 95: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

87

cout << “Do again (y/n)? “; cin >> quit; } while (strcmp(quit, yes) == 0); return 0;}void print_message(){ cout << “ This program was written completely by Austin Semerad” << endl; cout << “ for Dr. Ansari’s MATH 220 class.” << endl << endl; cout << “This program will solve a square linear system using Cramer’s Rule.” << endl; cout << “Ax = b, where A is a square (n x n) coefficient matrix,” << endl; cout << “x is a variable (n x 1) matrix, and b is a constant (n x 1) matrix.” << endl; return;}void initialise(float &i_determinant_original, float *i_variable, float *i_constant, float *i_determinant_modified){ i_determinant_original = 1; i_variable = NULL; i_constant = NULL; i_determinant_modified = NULL; return;}void allocate_floats(int &af_size, float *&af_variable, float *&af_constant, float *&af_determinant_modified){ cout << “First, enter n (the size of the square matrix): “; cin >> af_size; while ((af_size < 1) || (af_size > 20)) { cout << “That is not a valid size. Numbers must be greater than 0 and less than 20.” << endl; cout << “Enter n: “; cin >> af_size; } af_variable = new float[af_size]; af_constant = new float[af_size]; af_determinant_modified = new float[af_size]; return;}void initialise_dynamic_memory(float *idm_determinant_modified, int idm_size){ int i; for (i = 0; i < idm_size; i ++) idm_determinant_modified[i] = 1; return;}void get_matrices(float gm_coefficient[][MAX_SIZE], float *gm_constant, int gm_size){ int i, j; cout << endl; cout << “Enter the coefficient matrix (A).” << endl; cout << “When entering the rows, put one space between each term in the row.” << endl; for (i = 0; i < gm_size; i ++) { cout << “Enter row “ << i + 1 << “: “;

Page 96: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

88

for (j = 0; j < gm_size; j ++) cin >> gm_coefficient[i][j]; } cout << endl; cout << “Enter the constant matrix (b).” << endl; for (i = 0; i < gm_size; i ++) { cout << “Enter row “ << i + 1 << “: “; cin >> gm_constant[i]; } return;}void calculate_variable_matrix(float &cvm_determinant_original, float *cvm_determinant_modified, float *cvm_variable, float cvm_coefficient[][MAX_SIZE], float *cvm_constant, int cvm_size){ if (cvm_size == 1) { cvm_determinant_original = cvm_coefficient[0][0]; cvm_determinant_modified[0] = cvm_constant[0]; } else { calculate_determinant(cvm_determinant_original, cvm_coefficient, cvm_size); calculate_modified_determinant(cvm_determinant_modified, cvm_coefficient, cvm_constant, cvm_size); } calculate_variables(cvm_variable, cvm_determinant_modified, cvm_determinant_original, cvm_size); return;}void print_results(float *pr_variable, float *pr_determinant_modified, int pr_size, float pr_determinant_original){ print_determinants(pr_determinant_modified, pr_size, pr_determinant_original); print_variables(pr_variable, pr_size, pr_determinant_original); return;} void return_memory(float *rm_variable, float *rm_constant, float *rm_determinant_modified){ delete [] rm_variable; delete [] rm_constant; delete [] rm_determinant_modified; return;}void calculate_determinant(float &cd_determinant, float cd_coefficient[][MAX_SIZE], int cd_size){ int i, j, k, l; float temp_co, temp_mat[MAX_SIZE][MAX_SIZE]; copy_matrix(cd_coefficient, temp_mat, cd_size); for (i = 0; i < cd_size; i ++) { if (temp_mat[i][i] == 0) { if (swap_rows(temp_mat, i, cd_size) == false) { cd_determinant = 0;

Page 97: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

89

return; } else cd_determinant *= -1; } if (temp_mat[i][i] != 1) { temp_co = temp_mat[i][i]; cd_determinant *= temp_mat[i][i]; for (j = i; j < cd_size; j ++) temp_mat[i][j] = temp_mat[i][j] / temp_co; } for (k = i + 1; k < cd_size; k ++) for (l = (cd_size - 1); l >= i; l --) temp_mat[k][l] = temp_mat[k][l] - (temp_mat[k][i] * temp_mat[i][l]); } return;}void calculate_modified_determinant(float cmd_determinant_modified[], float cmd_coefficient[][MAX_SIZE], float *cmd_constant, int cmd_size){ int i, j; float temp_mat[MAX_SIZE][MAX_SIZE]; for (i = 0; i < cmd_size; i ++) { copy_matrix(cmd_coefficient, temp_mat, cmd_size); for (j = 0; j < cmd_size; j ++) temp_mat[j][i] = cmd_constant[j]; calculate_determinant(cmd_determinant_modified[i], temp_mat, cmd_size); } return;}void calculate_variables(float *cv_variable, float *cv_determinant_modified, float cv_determinant_original, int cv_size){ int i; for (i = 0; i < cv_size; i ++) cv_variable[i] = cv_determinant_modified[i] / cv_determinant_original; return;}void print_determinants(float *pd_determinant_modified, int pd_size, float pd_determinant_original){ int i; cout << endl << “Here is a list of all the determinants:” << endl; cout << “Det(A) = “ << pd_determinant_original << endl; for (i = 0; i < pd_size; i ++) cout << “Det(A” << i + 1 << “) = “ << pd_determinant_modified[i] << endl; return;}void print_variables(float *pv_variable, int pv_size, float pv_determinant_original){ int i; cout << endl;

Page 98: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

90

if (pv_determinant_original == 0) cout << “Cramer’s Rule is not applicable for this matrix because Det(A) = 0.” << endl; else { cout << “Here is a list of the variables:” << endl; for (i = 0; i < pv_size; i ++) cout << “x” << i + 1 << “ = “ << pv_variable[i] << endl; } return;}void copy_matrix(float matrix1[][MAX_SIZE], float matrix2[][MAX_SIZE], int cm_size){ int i, j; for (i = 0; i < cm_size; i ++) for (j = 0; j < cm_size; j ++) matrix2[i][j] = matrix1[i][j]; return;}bool swap_rows(float matrix[][MAX_SIZE], int sr_i, int sr_size){ int initRow = sr_i, initCol = sr_i, i, j; float temp; for (i = initRow; i < sr_size; i ++) if (matrix[i][initCol] != 0) { for (j = initCol; j < sr_size; j ++) { temp = matrix[initRow][j]; matrix[initRow][j] = matrix[i][j]; matrix[i][j] = temp; } return true; } return false;}

5.2 Gauss Elimination MethodThe following note will briefly discuss the standard method of solving simultaneous equations. It will also present C source which implements the so called “partial pivoting” algorithm. An example of 3 simultaneous equations with 3 unknowns x0, x1, x2 is shown below

x0 + x1 + x2 = 02x0 + x1 + x2 = 1x0 + 2x1 + x2 = 15

We wish to determine the values of the 3 unknowns. The solution basically relies on us being able to multiply a row by a constant, and add (or subtract) two rows. So for example the first row above could be multiplied by -2 and then the second row could be added to it to give a new set of equations (but with the same solution)

x0 + x1 + x2 = 0- x1 - x2 = 1

x0 + 2x1 + x2 = 15

Page 99: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

91

The rows and factor chosen was designed to eliminate the first unknown x0 from the second equation. If we now take -1 times the first row and add the last row we get

x0 + x1 + x2 = 0- x1 - x2 = 1+ x1 + 0x2 = 15

And finally, adding the last two rows gives

x0 + x1 + x2 = 0- x1 - x2 = 1

- x2 = 16

The above is known as the elimination phase, the aim is to turn all the elements below the diagonal into zeros. The solution can now be determined by so called “back substitution”. From the last equation we see that x2 is -16. Substituting this value into the second equation gives x1 = 15, and finally substituting these two values into the first equation gives the last unknown x0 = 1.

In general a series of simultaneous equations can be written in matrix notation. If we have n equations with n unknowns (x0, x1, x2, ... xn-1) this can be written as

This is often written in a more compact way as Ax = b, where A is a matrix, x and b are vectors. The Gaussian elimination process consists of two steps, first reducing the elements below the diagonal to 0 and second, back substituting to find the solutions. In order to eliminate the appropriate element it is not sufficient to simply use a ratio based automatically on the values on the elements column as this may result in a divide by zero. The usual solution is to swap the rows around so that the element being removed has the largest magnitude.

If it still happens that all elements on the columns to be removed are zero then this reflects a “singular” matrix, that is, the n equations were not all independent and therefore there isn’t a unique solution.

The following source code implements the above algorithm in a straightforward way, there are improvements that could be made especially for sparce systems of equations (a large number of equations with mostly zero elements).

C source exampleSolve a system of n equations in n unknowns using Gaussian EliminationSolve an equation in matrix form Ax = bThe 2D array a is the matrix A with an additional column b.This is often written (A: b)A0, 0 A1, 0 A2, 0 .... An-1, 0 b0A0, 1 A1, 1 A2, 1 .... An-1, 1 b1A0, 2 A1, 2 A2, 2 .... An-1, 2 b2

Page 100: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

92

: : : : : : : : : :A0,n-1 A1,n-1 A2,n-1 .... An-1,n-1 bn-1The result is returned in x, otherwise the function returns FALSE if the system of equations is singular.*/int GSolve(double **a,int n,double *x){iint i,j,k,maxrow;double tmp;for (i=0;i<n;i++) {/* Find the row with the largest first value */ maxrow = i; for (j=i+1;j<n;j++) { if (ABS(a[i][j]) > ABS(a[i][maxrow])) maxrow = j; } /* Swap the maxrow and ith row */ for (k=i;k<n+1;k++) { tmp = a[k][i]; a[k][i] = a[k][maxrow]; a[k][maxrow] = tmp; } /* Singular matrix? */ if (ABS(a[i][i]) < EPS) return(FALSE); /* Eliminate the ith element of the jth row */ for (j=i+1;j<n;j++) { for (k=n;k>=i;k--) { a[k][j] -= a[k][i] * a[i][j] / a[i][i]; } } }/* Do the back substitution */ for (j=n-1;j>=0;j--) { tmp = 0; for (k=j+1;k<n;k++) tmp += a[k][j] * x[k]; x[j] = (a[n][j] - tmp) / a[j][j]; } return(TRUE);}A simple calling program which solves the example earlier is#define ABS(x) (x < 0 ? -(x) : (x))#define EPS 0.00001#define TRUE 1#define FALSE 0int main(int argc,char **argv){ int i,n = 3; double x[3] = {0.0,0.0,0.0}; double **a; a = (double **)malloc((n+1)*sizeof(double *));

Page 101: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

93

for (i=0;i<n+1;i++) a[i] = (double *)malloc(n*sizeof(double)); a[0][0] = 1; a[1][0] = 1; a[2][0] = 1; a[3][0] = 0; a[0][1] = 2; a[1][1] = 1; a[2][1] = 1; a[3][1] = 1; a[0][2] = 1; a[1][2] = 2; a[2][2] = 1; a[3][2] = 15; GSolve(a,n,x); WriteSolution(a,n,x);}void WriteSolution(double **a,int n,double *x){ int j,k; for (j=0;j<n;j++) { for (k=0;k<n+1;k++) { printf(“%10.3f “,a[k][j]); } printf(“ | %10.3f\n”,x[j]); } printf(“\n”);}

5.3 Gauss Elimination with Partial PivotingIf some pivot (in Gaussian elimination without pivoting) is exactly equal to 0, then the elimination fails (divide by 0), and if some pivot is very small in magnitude relative to other numbers in the matrix A, then the computation may be numerically unstable.

After k −1 steps of the forward elimination, we have computed the reduced matrix

Two common pivoting strategies:

Partial pivoting

Choose as the pivot for step k, where

If m ≠ k, then interchange rows m and kMatrix formulation of partial pivoting:

Page 102: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

94

where P1 is a permutation matrix that does the appropriate row interchange at step 1

Then

Thus, after n-1 steps, we have

Complete pivoting

Choose as the pivot for step k, where

If m ≠ k or p ≠ k, then interchange rows m and k and columns p and k

Matrix formulation of complete pivoting:

and so on. After n −1 steps,

is upper triangular (but note that is not lower triangular).

#include<stdio.h>#include<stdlib.h>#include<math.h>

typedef double ** Matrix;typedef double * Row;typedef double * Col;typedef double Elem; Matrix allocate_matrix(int n);Col allocate_col(int n);Row allocate_row(int n);void free_matrix(Matrix M, int n);

Page 103: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

95

void pivot_partial(Matrix A, Col S,Col B, int n);void forward_elimination(Matrix A,Col B,int n);Col back_substitution(Matrix A, Col B, int n);Col scale_factor(Matrix A,int n);void gauss(Matrix A, Col B, int n); void swap_rows(Row *r1, Row*r2);void print_matrix(Matrix M, int n, char * name);void print_col(Col C, int n, char *name);void print_row(Row R, int n, char *name); int main(int argc, char *argv[]){ FILE *ifp; int n,i,j; Matrix A; Col B; if(argc < 2) { printf(“\nInput filename not passed \n”); exit(1); } ifp = fopen(argv[1],”r”); if(ifp == NULL) { printf(“\nCould not open file %s\n”,argv[1]); exit(1); } fscanf(ifp,”%i”,&n); printf(“\nDimension = %i\n”,n); A = allocate_matrix(n); for( i = 1; i <= n; ++i) for(j = 1; j <= n; ++j) fscanf(ifp,”%lf”, &A[i][j]); B = allocate_col(n); for(j = 1; j <= n; ++j) fscanf(ifp,”%lf”,&B[j]); fclose(ifp); print_matrix(A,n,”A”); print_col(B,n,”B”); gauss(A,B,n); free_matrix(A,n); free(B + 1); printf(“\nDone”); getchar(); return 0;}void print_matrix(Matrix M, int n, char * name){ int i,j; printf(“\n[%s] = “,name); printf(“\n\n”); for(i = 1; i <= n; i++)

Page 104: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

96

{ for(j = 1; j <= n; ++j) printf(“%6lG “,M[i][j]); printf(“\n”); }} void print_col(Col C, int n, char * name){ int j; printf(“\n[%s] = “,name); printf(“\n\n”); for(j = 1; j <= n; ++j) printf(“%6lg\n”,C[j]);}void print_row(Row R, int n, char * name){ int i; printf(“\n[%s] = “,name); for(i = 1; i <= n; ++i) printf(“%6lg “,R[i]); printf(“\n”);}Matrix allocate_matrix(int n){ Matrix A; int i,j; A = malloc(n * sizeof(Row)); if(!A) { printf(“\nError : Could not allocate memory for matrix\n”); exit(1); } --A; for(i = 1; i <= n; ++i) { A[i] = malloc(n * sizeof(Elem)); if(!A[i]) { printf(“\nError : Could not allocate memory for matrix\n”); exit(1); } --A[i]; } return A;}void free_matrix(Matrix M, int n){ int i; for(i = 1; i <= n; ++i) free(M[i] + 1); free(M + 1);}Col allocate_col(int n)

Page 105: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

97

{ Col B; B = malloc(n * sizeof(Elem)); if(!B) { printf(“\nError : could not allocate memory\n”); exit(1); } --B; return B;}Row allocate_row(int n){ Row B; B = malloc(n * sizeof(Elem)); if(!B) { printf(“\nError : could not allocate memory\n”); exit(1); } --B; return B;}Col scale_factor(Matrix A, int n){ int i,j; Col S ; S = allocate_col(n); for(i = 1; i <= n; ++i) { S[i] = A[i][1]; for(j = 2; j <= n; ++j) { if(S[i] < fabs(A[i][j])) S[i] = fabs(A[i][j]); } } return S;} void pivot_partial(Matrix A, Col S,Col B, int n){ int i,j; Elem temp; for(j = 1; j <= n; ++j) { for(i = j + 1; i <= n; ++i) { if(S[i] == 0) { if(B[i] == 0) printf(“\nSystem doesnt have a unique solution”); else printf(“\nSystem is inconsistent”);

Page 106: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

98

exit(1); } if(fabs(A[i][j]/S[i]) > fabs(A[j][j]/S[j])) { swap_rows(&A[i],&A[j]); temp = B[i]; B[i] = B[j]; B[j] = temp; } } if(A[j][j] == 0) { printf(“\nSingular System Detected\n”); exit(1); } }} void swap_rows(Row *r1, Row*r2){ Row temp; temp = *r1; *r1 = *r2; *r2 = temp; } void forward_elimination(Matrix A,Col B,int n){ int i,j,k; double m; for(k = 1; k <= n-1; ++k) { for(i = k + 1; i <= n; ++i) { m = A[i][k] / A[k][k]; for(j = k + 1; j <= n; ++j) { A[i][j] -= m * A[k][j]; if(i == j && A[i][j] == 0) { printf(“\nSingular system detected”); exit(1); } } B[i] -= m * B[k]; } }}Col back_substitution(Matrix A, Col B, int n){ int i,j; Elem sum; Col X = allocate_col(n); X[n] = B[n]/A[n][n]; for(i = n - 1; i >= 1; --i)

Page 107: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

99

{ sum = 0; for(j = i + 1; j <= n; ++j) sum += A[i][j] * X[j]; X[i] = (B[i] - sum) / A[i][i]; } return X;}void gauss(Matrix A, Col B, int n){ int i,j; Col S, X; S = scale_factor(A,n); pivot_partial(A,S,B,n); forward_elimination(A,B,n); X = back_substitution(A,B,n); print_col(X,n,”X”); free(S + 1); free(X + 1);}

5.4 Gauss-Jordan Method This method can be used to solve systems of linear equations involving two or more variables. However, the system must be changed to an augmented matrix. This method can also be used to find the inverse of a 2x2 matrix or larger matrices, 3x3, 4x4 etc.

An Augmented Matrix is used to solve a system of linear equations.

When given a system of equations, to write in augmented matrix form, the coefficients of each variable must be taken and put in a matrix.

Page 108: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

100

For example, for the following system

There are three different operations known as Elementary Row Operations used when solving or reducing a matrix, using Gauss-Jordan elimination method.

Interchanging two rows. • Add one row to another row, or multiply one row first and then adding it to another. • Multiplying a row by any constant greater than zero•

Identity Matrix-is the final result obtained when a matrix is reduced. This matrix consists of ones in the diagonal starting with the first number.

The numbers in the last column are the answers to the system of equations•

The pattern continues for bigger matrices.•

Solving a system using Gauss-JordanThe best way to go is to get the ones first in their respective column, and then using that one to get the zeros • in that column. It is very important to understand that there is no exact procedure to follow when using the Gauss-Jordan method • to solve for a system.

Page 109: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

101

Put the new row 2 in the matrix; note that though row 1 was multiplied by -3, row 1 didn’t change in our • matrix.

Using a similar procedure of multiplying and adding rows, obtain the following matrix

Page 110: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

102

Finally, the matrix can be solved in two different ways:Using the 1 in column 3, obtain the other zeros and the solutions•

Solve by using back substitution• The solution to the last row is z = 2, the answer can be substituted into the equation produced by the second • row. y − z = −1 Substituting into this equation, it simplifies to:

Page 111: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

103

y−2= −1y=1

Again, substituting the answer for z into the first equation will give the answer for x. •

x+ z=3x+ 2= 3

x= 1

* Driver for routine for gaussj.c*//* Program gauss_driver.c */ #include <stdio.h>#include <stdlib.h>#include “nr.h”#include “nrutil.h” /* Utilities program, listed in Appendix B of NRC book */ /* NRC stands for Numerical Recipes in C */ #define MAXSTR 80 int main(void){ int j,k,l,m,n,NP,MP; float **a,**ai,**u,**b,**x,**t; char dummy[MAXSTR]; FILE *fp;* a is the coefficient matrix.* ai=a before gaussj is called, ai = inverse(a) after the function call.* u = ai*a, we define this to test the program, * if correct u should be the unit matrix.* b = matrix of dimension n*m where m is the number of r.h.s. vectors for which * you want to solve A.x = b.* x = b before gaussj is called, x is the solution vector after gaussj is called. * t is an n*m matrix defined to test the solution. * Read NP to allocate space for the matrices. printf(“Input the dimension of the largest square matrix to be used\n”); scanf(“%d”,&NP); printf(“Input the maximum number of r.h.s. vectors\n”); scanf(“%d”,&MP); a=matrix(1,NP,1,NP); /* These commands have the same function as calloc */ ai=matrix(1,NP,1,NP);/* They are provided by the utility programs in NRC */ u=matrix(1,NP,1,NP); b=matrix(1,NP,1,MP); x=matrix(1,NP,1,MP); t=matrix(1,NP,1,MP); if ((fp = fopen(“gaussj.dat”,”r”)) == NULL) nrerror(“Data file gaussj.dat not found\n”); /* See a typical data file appended */ while (!feof(fp)) { fgets(dummy,MAXSTR,fp); fgets(dummy,MAXSTR,fp); fscanf(fp,”%d %d “,&n,&m); fgets(dummy,MAXSTR,fp); for (k=1;k<=n;k++) for (l=1;l<=n;l++) fscanf(fp,”%f “,&a[k][l]);

Page 112: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

104

fgets(dummy,MAXSTR,fp); for (l=1;l<=m;l++) for (k=1;k<=n;k++) fscanf(fp,”%f “,&b[k][l]); /* save matrices for later testing of results */ for (l=1;l<=n;l++) { for (k=1;k<=n;k++) ai[k][l]=a[k][l]; for (k=1;k<=m;k++) x[l][k]=b[l][k]; } /* Call gaussj: note that after the call, a is replaced by its inverse and b is replaced by the solution vector */ gaussj(ai,n,x,m); printf(“\nInverse of matrix a : \n”); for (k=1;k<=n;k++) { for (l=1;l<=n;l++) printf(“%12.6f”,ai[k][l]); printf(“\n”); } /* check inverse */ printf(“\na times a-inverse:\n”); for (k=1;k<=n;k++) { for (l=1;l<=n;l++) { u[k][l]=0.0; for (j=1;j<=n;j++) u[k][l] += (a[k][j]*ai[j][l]); } for (l=1;l<=n;l++) printf(“%12.6f”,u[k][l]); printf(“\n”); } /* check vector solutions */ printf(“\nCheck the following for equality:\n”); printf(“%21s %14s\n”,”original”,”matrix*sol’n”); for (l=1;l<=m;l++) { printf(“vector %2d: \n”,l); for (k=1;k<=n;k++) { t[k][l]=0.0; for (j=1;j<=n;j++) t[k][l] += (a[k][j]*x[j][l]); printf(“%8s %12.6f %12.6f\n”,” “, b[k][l],t[k][l]); } } } fclose(fp); free_matrix(t,1,NP,1,MP); free_matrix(x,1,NP,1,MP); free_matrix(b,1,NP,1,MP); free_matrix(u,1,NP,1,NP); free_matrix(ai,1,NP,1,NP); free_matrix(a,1,NP,1,NP); return 0;}

Page 113: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

105

5.5 Gauss Siedel MethodWe seek the solution to set of linear equations:

In matrix terms, the Gauss-Seidel iteration can be expressed as

Where, D,L and U represent the diagonal, lower triangular, and upper triangular parts of the coefficient matrix A and K is the iteration count. This matrix expression is mainly of academic interest, and is not used to program the method. Rather, an element-based approach is used:

Note that the computation of uses only those elements of that have already been computed and only those elements of that have yet to be advanced to iteration k+1 .This means that no additional storage is required, and the computation can be done in place ( replaces ). While this might seem like a rather minor concern, for large systems it is unlikely that every iteration can be stored. Thus, unlike the Jacobi method, we do not have to do any vector copying should we wish to use only one storage vector. The iteration is generally continued until the changes made by iteration are below some tolerance.

AlgorithmChose an initial guessfor k:=1 step until convergence do

for i:= 1 step until n do

for j:=1 step until i-1 do

end (j-loop) for j:=i+1 step until n do

end (j-loop)

end (i-loop)

Page 114: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

106

check if convergence is reached end (k-loop)

Code for Gauss Seidel Method in C Programming

#include<stdio.h>#include<conio.h>#include<math.h>#define ESP 0.0001#define X1(x2,x3) ((17 - 20*(x2) + 2*(x3))/20)#define X2(x1,x3) ((-18 - 3*(x1) + (x3))/20)#define X3(x1,x2) ((25 - 2*(x1) + 3*(x2))/20)

void main(){double x1=0,x2=0,x3=0,y1,y2,y3;int i=0;clrscr();printf(“\n__________________________________________\n”);printf(“\n x1\t\t x2\t\t x3\n”);printf(“\n__________________________________________\n”);printf(“\n%f\t%f\t%f”,x1,x2,x3);do{y1=X1(x2,x3);y2=X2(y1,x3);y3=X3(y1,y2); if(fabs(y1-x1)<ESP && fabs(y2-x2)<ESP && fabs(y3-x3)<ESP ){printf(“\n__________________________________________\n”);printf(“\n\nx1 = %.3lf”,y1);printf(“\n\nx2 = %.3lf”,y2);printf(“\n\nx3 = %.3lf”,y3);i = 1;}else{x1 = y1; x2 = y2; x3 = y3;printf(“\n%f\t%f\t%f”,x1,x2,x3);}}while(i != 1);getch();}

OUT PUT

x1 x2 x3__________________________________________

0.000000 0.000000 0.0000000.850000 -1.027500 1.010875

Page 115: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

107

1.978588 -1.146244 0.8802052.084265 -1.168629 0.8662792.105257 -1.172475 0.8636032.108835 -1.173145 0.8631452.109460 -1.173262 0.8630652.109568 -1.173282 0.863051__________________________________________x1 = 2.110

x2 = -1.173

x3 = 0.863

Page 116: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

108

SummaryCramer’s rule is an alternative way of solving a set of simultaneous linear equations by matrix methods.• A set of simultaneous equations can be solved either by matrix inversion or by applying Cramer’s rule.• Cramer’s rule has the advantage that less tedious computations are necessary if we are only interested in the • solution value of one of the variables.Identity Matrix consists of ones in the diagonal starting with the first number.• Gauss-Jordan method can be used to solve systems of linear equations involving two or more variables• There are three different operations known as Elementary Row Operations used when solving or reducing a • matrix, using Gauss-Jordan elimination method.

ReferencesTrefethen, N. L. and Bau, D., 1997. • Numerical linear algebra, SIAM.Press, H. W., 2007. • Numericalrecipes:theartofscientificcomputing, Cambridge University Press.Lal, K. A., 2007. • Gauss Elimination Method [Online] Available at: <http://nptel.iitm.ac.in/courses/Webcourse-contents/IIT-KANPUR/mathematics-2/node18.html> [Accessed 21 November 2011].Mathews, H. J., 2004. • Jacobi andGauss-Seidel Iteration [Online] Available at: <http://math.fullerton.edu/mathews/n2003/gaussseidelmod.html> [Accessed 21 November 2011].IntuitiveMath, 2008. • Algebra - Matrices - Gauss Jordan Method Part 1 Augmented Matrix [Video Online] Available at: <http://www.youtube.com/watch?v=JwT1FrLzEOQ> [Accessed 21 November 2011].Richard, N., 2008. • UsingCramer’sRuletoSolveEquation [Video Online] Available at: <http://www.youtube.com/watch?v=84foGKp0QgU> [Accessed 21 November 2011].

Recommended ReadingSprott, C. J. and Numerical Recipes Software (Firm), 1991. • Numericalrecipes:routinesandexamplesinBASIC, Cambridge University Press.Hildebrand, B. F., 1965• . Methods of applied mathematics, Courier Dover Publications.Logan, D. H., 1997. • Applied mathematics, Wiley.

Page 117: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

109

Self Assessment_________________ is an alternative way of solving a set of simultaneous linear equations by matrix 1. methods.

Cramer’s rulea. Euler’s rule b. Gauss-Jordon c. Simpson’s 1/3d.

A set of simultaneous equations can be solved either by matrix inversion or by applying________________.2. Cramer’s rulea. Euler’s rule b. Gauss-Jordon c. Simpson’s 1/3d.

Identity Matrix consists of ones in the diagonal starting with the_______________.3. second number a. first numberb. mid-number c. last number d.

______________ method can be used to solve systems of linear equations involving two or more variables.4. Cramer’s rulea. Euler’s rule b. Gauss-Jordon c. Simpson’s 1/3d.

“Malloc” is used for which purpose?5. Allocate memory blocka. Free memory b. Empty ROMc. Release ROM d.

What does “NULL” value represent?6. A variable with no valuea. A variable with full valueb. A variable with negative value c. A variable with infinite valued.

An _____________ Matrix is used to solve a system of linear equations7. Augmenteda. Zerob. Integerc. Nulld.

Page 118: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

110

In general a series of simultaneous equations can be written in __________ notation8. matrixa. templateb. derivativec. polynomiald.

The ________________ process consists of two steps, first reducing the elements below the diagonal to 0 and 9. second, back substituting to find the solutions.

Simpson’s 3/8a. Simpson’s 1/3b. Gaussian eliminationc. Cramerd.

There are three different operations known as ______________and is used when solving or reducing a matrix, 10. using Gauss-Jordan elimination method.

Elementary Row Operationsa. Elementary Column Operationsb. Elementary Integer Operationsc. Elementary Pivot Operationsd.

Page 119: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

111

Chapter VI

Numerical Solution of 1st and 2nd Order Differential Equations

Aim

The aim of this chapter is to:

define Taylor’s series•

explain Euler’s method•

describe Runge-Kutta method for 1• st and 2nd order differential equation

Objectives

The objectives of this chapter are to:

elucidate Euler’s method•

explain modified Euler’s method•

describe Picards method•

Learning outcome

At the end of this chapter, you will be able to:

derive a program to show Taylor’s series-e^x in C•

derive code for program of Euler’s method in c programming•

derive a • code for Runge Kutta for a set of first order differential equations

Page 120: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

112

6.1 Taylor SeriesIn theory, the infinite Taylor series can be used to evaluate a function, given its derivative function and its value at some point. Consider the nonlinear first-order ODE:

(6.1.1)

The Taylor series for y(t) at t = t0 is

(6.1.2)

Equation (2) can be written in the simpler appearing form

(6.1.3)

Where etc., , and, for convenience, denotes , not .

Equation (6.1.3) can be employed to evaluate y(t) if y0 and the values of the derivatives at t0 can be determined. The value of y0 is the initial condition specified in equation (6.1.1). The first derivative can be determined by evaluating the derivative function f(t, y) at t0: = f(t0, y0). The higher-order derivatives in equation (6.1.3) can be determined by successively differentiating the lower-order derivatives, starting with y’. Thus,

(4a)

(6.1.4b)

(6.1.4c)

Recall that dy/dt=y’.Substituting Eq. (6.1.4c) into Eq. (6.1.4a) yields

(6.1.4d)

In a similar manner

Page 121: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

113

(6.1.5a)

(6.1.5b)

Higher-order derivatives become progressively more complicated. It is not practical to evaluate a large number of the higher-order derivatives. Consequently, the Taylor series must be truncated. The remainder term in a finite Taylor series is:

(6.1.6)

Where t0 < τ < t. Truncating the remainder term yields a finite truncated Taylor series.Error estimation is difficult, since τ is unknown.

Example: Let’s solve the radiation problem presented in section 6.1 by the Taylor series method.Recall equation (6.1)

(6.1.7)

Where α = 4.0 × 10-12 (K3-s)-1. The Taylor series for T(t) is given by

(6.1.8)

Where ∆t= t− t0 = t. From Eq. (6.7), T0 =2500.0, and

(6.1.9)

Solving for the higher-order derivatives yields

(6.1.10 a)

Page 122: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

114

(6.1.10 b)

(6.1.10 c)

(6.1.11a)

(6.1.11b)

Page 123: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

115

(6.1.11c)

(6.1.12a)

(6.1.12b)

Substituting the above values into equation (6.8) yields

(6.1.13)

The exact solution and the solution obtained from Eq. (6.1.13) are tabulated in Table below, where T(t) (1), T(t)(2), etc. denote the Taylor series through the first, second, etc. derivative terms. These results are also illustrated in the figure 6.1.

Page 124: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

116

From the figure below, it is obvious that the accuracy of the solution improves as the number of terms in the Taylor series increases. However, even with four terms, the solution is not very accurate for t > 2.0 s. The Taylor series method is not an efficient method for solving initial-value ODE.

Even though the Taylor series method is not an efficient method for solving initial value ODEs, it is the basis of many excellent numerical methods. The solution by the Taylor series method is quite accurate for small values of t. Therein lies the basis for more accurate methods of solving ODEs. Simply put, use the Taylor series method for a small step in the neighbourhood of the initial point. Then re-evaluate the coefficients (i.e., the derivatives) at the new point. Successive re-evaluation of the coefficients as the solution progresses yields a much more accurate solution. This concept is the underlying basis of most numerical methods for solving initial-value ODEs.

t,s Texact(t),K T(t) (1), K T(t) (2), K T(t) (3), K T(t) (4),K0.0 2500.00 2500.00 2500.00 2500.00 2500.001.0 2360.83 2343.77 2363.29 2360.45 2360.892.0 2248.25 2187.53 2265.65 2242.87 2249.983.0 2154.47 2031.30 2207.06 2130.17 2166.214.0 2074.61 1875.06 2187.53 2005.27 2119.185.0 2005.42 1718.83 2207.06 1851.07 2129.18

2500

2400

2300

2200

2100

2000

1900

1800

17000 1 2 3 4 5

Time t, s

Tem

pera

ture

T. K

Exact solution

First order

Second order

Third order

Fourth order

Fig. 6.1 Solution by the Taylor series method

Program to show Taylor’s series-e^x in C

#include <stdio.h> #include <conio.h> #include<math.h> long int factorial(int n);

Page 125: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

117

void main() { int x,i;float s,r; char c; clrscr();printf(“You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!..x^x/x!”); printf(“To which term you want its sum of ? “); scanf(“%d”,&x); s=0;for (i=1;i<=x;i++) { s=s+((float)pow(x,i)/(float)factorial(i)); } printf(“The sum of %d terms is %f”,x,1+s); fflush(stdin); getch(); } long int factorial(int n) { if (n<=1) return(1); else n=n*factorial(n-1); return(n); }

6.2 Euler’s MethodThe first derivative provides a direct estimate of the slope at xi in the figure below:

Predicted

True

error

h

ti ti+1 t

y

Fig. 6.2 Euler’s method

Where f(xi, yi) is the differential equation evaluated at xi and yi.

(6.2.1)

This formula is referred to as Euler’s (or the Euler-Cauchy or the point-slope) method.

Page 126: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

118

A new value of y is predicted using the slope (equal to the first derivative at the original value of x) to extrapolate linearly over the step size h (figure 6.1).

Example

Use Euler’s method to numerically integrate the equation

from x=0 to x=4 with a step size of 0.5. The initial condition at x=0 is y=1.

Solution Equation (6.1) can be used to implement Euler’s method:

Where y(0)=1 and the slope estimate at x=0 is

Therefore,

The true solution at x= 0.5 is

Thus, the error is

or, expressed as percent relative error, . For the second step,

The true solution at x=1.0 is 3.0, and therefore, the percent relative error is -95.8%.

Algorithm for Euler’s methodAlgorithms for one-step techniques such as Euler’s method are extremely simple to program.

New value = old value +slope× step size

Page 127: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

119

The only way in which the methods differ is in the calculation of the slope.

Use Euler’s method to integrate , with the initial condition that y=1 at x=0. You would like to integrate out to x=4 using a step size of 0.5 and display all the results. A simple pseudo code to accomplish this task could be written as in section 6.1.

Although this program will do the job of duplicating the results, it is not very well designed. First, and foremost it is not very modular. Although this is not very important for such a small program, it would be critical if we desired to modify and improve the algorithm.

Further there are a number of issues related to the way we have set up the iterations.

For example, suppose that the step size were to be made very small to obtain better accuracy. In such cases, because every computed value is displayed, the number of output values might be very large. Further, the algorithm is predicated on the assumption that the calculation interval is evenly divisible by the step size. Finally, the accumulation of x in the line x=x+dx can be subject to quantising error.

Pseudo code for a “dumb” version of Euler’s method.

set integration rangexi=0xf=4initialise variablesx=xiy=1set step size and determinenumber of calculation stepsdx=0.5nc=(xf−xi)/dxoutput initial conditionPRINT x, yloop to implement Euler’s method and display resultsDOFOR i=1. nc

y=y+ dydx. dxx=x+ dxPRINT x.yEND DO

Code for Program of Euler’s method in C Programming#include<stdio.h>#include <math.h>#include<conio.h>//dy/dx = xy#define F(x,y) (x)*(y)void main(){double y1,y2,x1,a,n,h;int j;clrscr();printf(“\nEnter the value of range: “);scanf(“%lf %lf”,&a,&n);printf(“\nEnter the value of y1: “);

Page 128: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

120

scanf(“%lf”,&y1);printf(“\n\nEnter the h: “);scanf(“%lf”,&h);printf(“\n\n y1 = %.3lf “,y1);for(x1=a,j=2; x1<=n+h; x1=x1+h,j++){y2= y1 + h * F(x1,y1); printf(“\n\n x = %.3lf => y%d = %.3lf “,x1,j,y2);y1=y2;}getch();}OUT PUTEnter the value of range: 1 1.5Enter the value of y1: 5Enter the h: 0.1y1 = 5.000x = 1.000 => y2 = 5.500x = 1.100 => y3 = 6.105x = 1.200 => y4 = 6.838x = 1.300 => y5 = 7.726x = 1.400 => y6 = 8.808x = 1.500 => y7 = 10.129

6.3 Modified Euler’s MethodA fundamental source of error in Euler’s method is that the derivative at the beginning of the interval is assumed to apply across the entire interval. Two simple modifications are available to help circumvent this shortcoming.

Heun’s methodOne method to improve the estimate of the slope involves the determination of two derivatives for the interval-one at the initial point and another at the end point. The two derivatives are then averaged to obtain an improved estimate of the slope for the entire interval. This approach, called Heun’s method is depicted graphically in figure 6.2.

Recall that in Euler’s method, the slope at the beginning of an interval.

is used to extrapolate linearly to :

For the standard Euler method we would stop at this point. However, in Heun’s method the calculated in eq. (6.3.1) is not the final answer, but an intermediate prediction. This is why we have distinguished it with a superscript 0. Equation (6.3.2) is called a predictor equation.

Page 129: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

121

Slope = f(xi+1, y0i + 1 )

Slope = f(xi, yi )

xi x

y

xi+1(a)

(b)xi x

y

xi+1

f(xi, yi ) + f(xi+1, y0i + 1 )Slope =

2

Fig. 6.3 Graphical depiction of Heun’s method (a) Predictor and (b) corrector

It provides an estimate of that allows the calculation of an estimated slope at the end of the interval:

Thus, the two slopes equation. (6.3.1) and (6.3.3) can be combined to obtain an average slope for the interval:

This average slope is then used to extrapolate linearly from to using Euler’s method

which is called a corrector equationThe Heun method is a predictor-corrector approach.

The Midpoint (or Improved Polygon) MethodFigure below illustrates another simple modification of Euler’s method. Called the mid-point method (or the improved polygon or the modified Euler), this technique uses Euler’s method to predict a value of y at the midpoint of the interval.

Page 130: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

122

y

x

(a)

xi xi+1/2

Slope = f(xi+1/2, yi + 1/2 )

y

x

(b)

xi xi+1

Slope = f(xi+1/2, yi + 1/2 )

Fig. 6.4 Graphical depiction of midpoint method

Then, this predicted value is used to calculate a slope at the midpoint:

which is assumed to represent a valid approximation of the average slope for the entire interval. This slope is then used to extrapolate linearly from

Observe that because is not on both sides (eq 6.3.8) cannot applied iteratively to improve the solution.

Code for modified Euler’s method in C Programming#include<stdio.h>#include <math.h>#include<conio.h>#define F(x,y) (x)*(x)+(y)void main(){ double y0,x0,y1,x1,y1_0,a,n,h,f,f1; int j,count,flag; clrscr(); printf(“\nEnter the value of x0: “); scanf(“%lf”,&x0); printf(“\nEnter the value of y0: “); scanf(“%lf”,&y0); printf(“\nEnter the value of h: “); scanf(“%lf”,&h); printf(“\nEnter the value of last point: “); scanf(“%lf”,&n);

Page 131: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

123

for(x1=x0+h,j=1; x1<=n+h; x1=x1+h,j++) { count=0; flag=0; f=F(x0,y0); y1_0 = y0 + (h * f); printf(“\n\n * * y%d_0 = %.3lf * *”,j,y1_0); do { count++; f=F(x0,y0); f1=F(x1,y1_0); y1 = y0 + h/2 * ( f + f1); printf(“\n\n * * x = %.3lf => y%d_%d = %.3lf * *”,x1,j,count,y1); if(fabs(y1-y1_0)<0.00001) { printf(“\n\n\n\n * * * * y%d = %.3lf * * * *\n\n”,j,y1); flag=1; } else y1_0 = y1; }while(flag!=1); y0 = y1; }getch();}

OUT PUTEnter the value of x0: 0Enter the value of y0: 1Enter the value of h: 0.05Enter the value of last point: 0.1* * y1_0 = 1.050 * * * * x = 0.050 => y1_1 = 1.051 * * * * x = 0.050 => y1_2 = 1.051 * * * * x = 0.050 => y1_3 = 1.051 * ** * * * y1 = 1.051 * * * ** * y2_0 = 1.104 * * * * x = 0.100 => y2_1 = 1.105 * * * * x = 0.100 => y2_2 = 1.106 * * * * x = 0.100 => y2_3 = 1.106 * * * * * * y2 = 1.106 * * * *

6.4 Runge-Kutta Method for 1st and 2nd Order Differential EquationThe Runge-Kutta (pronounced rung-ah cut-ah) method for solving differential equations is fairly difficult to understand and rather involved algebra to derive the equations. The gist of the method is to approximate the tangent to the curve not with a single secant (as is done in the Euler method) but with a weighted average of some small number of secants. There is, however, a law of diminishing returns: the computational effort increases linearly with the order of the method, but the increase in the solution accuracy does not. For most problems the fourth-order Runge-Kutta method gives the highest accuracy-effort ratio.

Page 132: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

124

The fourth-order Runge-Kutta solution of the first-order differential equation of the form:

is found by iterating the equation:

Where the constants k1, k2, k3 and k4 are given by:

On examination of (16), one sees that the constant depends on only and while depends on only and , and so forth. Therefore, the right-hand-side of (15) ultimately depends only upon and. This was also the property of the Euler method equation (equation (2)) that allowed it to be iterated. Thus, the fourth-order Runge-Kutta solution of (14) is obtained by iterating (15), beginning at the known initial condition.

The second-order Runge-Kutta method is a good illustration of the approach. The general method is represented by:

(6.4.3)

Where

and a, b, αand β are constants to be selected. If we set a=l and b=0, we get the simple Euler method. If a=b=1/2 and α=β=1, we recover the modified Euler method.

The second-order Runge-Kutta formula is obtained by setting the values of the four parameters a, b, αand β to make the expression for yi+1 agree with the Taylor series through the second-order in x. The Runge-Kutta methods are derived by rewriting k2 in terms of the function f at [xi,yi].

This can be done using a Taylor series expansion of f(xi,yi). The general form of a Taylor series for a function of two variables is:

Page 133: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

125

where the x and y subscripts stand for partial derivatives. This allows us to approximate k2 as

Substituting this expression for k2 into equation (6.4.3) gives

(6.4.4)

where f and its derivatives are evaluated at xi, yi. We want to match the terms of equation (6.4.4) to the first 3 terms of the Taylor series for yi+1,

(6.4.5)

The substitution for y’’after the second equal sign comes directly from dy/dxf(x,y) by taking the derivative of the right-hand side. For the terms of equations (6.4.4) and (6.4.5) to match, we must take a+b = l, αβ = 1/2, and βb = 1/2. These relationships do not uniquely specify the four parameters, but once we choose one, the other three are set. If we pick a = 1/2, then b = 1/2, β = 1 and α=1, this combination of values gives the modified Euler method.

/* Runge Kutta for a set of first order differential equations */

#include <stdio.h>#include <math.h>

#define N 2 /* number of first order equations */#define dist 0.1 /* stepsize in t*/#define MAX 50.0 /* max for t */FILE *output; /* internal filename */main(){double t, y[N];int j; void runge4(double x, double y[], double step); /* Runge-Kutta function */double f(double x, double y[], int i); /* function for derivatives */output=fopen(“osc.dat”, “w”); /* external filename */

y[0]=1.0; /* initial position */y[1]=0.0; /* initial velocity */fprintf(output, “0\t%f\n”, y[0]); for (j=1; j*dist<=MAX ;j++) /* time loop */

Page 134: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

126

{ t=j*dist; runge4(t, y, dist);

fprintf(output, “%f\t%f\n”, t, y[0]);}

fclose(output);}

void runge4(double x, double y[], double step){double h=step/2.0, /* the midpoint */ t1[N], t2[N], t3[N], /* temporary storage arrays */ k1[N], k2[N], k3[N],k4[N]; /* for Runge-Kutta */int i; for (i=0;i<N;i++) t1[i]=y[i]+0.5*(k1[i]=step*f(x, y, i));for (i=0;i<N;i++) t2[i]=y[i]+0.5*(k2[i]=step*f(x+h, t1, i));for (i=0;i<N;i++) t3[i]=y[i]+ (k3[i]=step*f(x+h, t2, i));for (i=0;i<N;i++) k4[i]= step*f(x+step, t3, i);

for (i=0;i<N;i++) y[i]+=(k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;}

double f(double x, double y[], int i){if (i==0) return(y[1]); /* derivative of first equation */if (i==1) return(-0.2*y[1]-y[0]); /* derivative of second equation */}

6.5 Picards MethodConsider first order differential equation

With the initial condition

Integrating (6.5.1) with respect to x between x0 and x, we have

or

Page 135: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

127

Now, we solve (6.5.2) by the method of successive approximation to find out the solution of (1). The first approximate solution (approximation) y1 of y is given by

Similarly, the second approximation y2 is given by

for the nth aproximation yn is given by

with y(x0)=y0.Hence, this method gives a sequence of approximation y1, y2................... yn and it can be proved f(x,y) is bounded in some regions containing the point (x0,y0) and if f(x, y) satisfies the Lipchitz condition, namely

where k is a constant.

Example Use Picard’s method to obtain y for x = 0.2. Given

with initial condition y=1 when x=0

Solution:

Here, f(x,y)=x-y, x0=0, y0=1

We have first approximation

Second approximation

Page 136: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

128

Third approximation

Fourth approximation

Fifth Approximation

When, x=0.2, we get

y1= .82y2=.83867y3=.83740y4=.83746y5=.83746

Thus, y=.837 when x=.2

Page 137: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

129

Summary In theory, the infinite Taylor series can be used to evaluate a function, given its derivative function and its value • at some point.Algorithms for one-step techniques such as Euler’s method are extremely simple to • A fundamental source of error in Euler’s method is that the derivative at the beginning of the interval is assumed • to apply across the entire intervalOne method to improve the estimate of the slope involves the determination of two derivatives for the interval-• one at the initial point and another at the end pointThe Runge-Kutta (pronounced rung-ah cut-ah) method for solving differential equations is fairly difficult to • understand and rather involved algebra to derive the equationsThe gist of the method is to approximate the tangent to the curve not with a single secant (as is done in the Euler • method) but with a weighted average of some small number of secants.

ReferencesLapidus, L. and Seinfeld, H. J., 1971. • Numerical solution of ordinary differential equations, Academic Press.Butcher, C. J., 2008. • Numerical methods for ordinary differential equations, John Wiley and Sons.Pearce, J. J., 1996. • TaylorSeries [Online] Available at: < http://www.math.unh.edu/~jjp/taylor/taylor.html> [Accessed 17 November 2011].Livetoad.org, • Picard iteration [Online] Available at: < http://livetoad.org/Courses/Documents/214a/Notes/picard_iterates.pdf> [Accessed 17 November 2011]. Mathbyfives, 2011. • Runge-Kutta Method.mov [Video Online] Available at: < http://www.youtube.com/watch?v=0iRZqmzs9dk> [Accessed 17 November 2011].USF, 2009. • Euler’s Method: Example [Video Online] Available at: <http://www.youtube.com/watch?v=4CqaepeaJHA> [Accessed 17 November 2011].

Recommended ReadingSchiavone, P., Constanda, C. and Mioduchowski, A., 2002. • Integral methods in science and engineering, Springer.Braun, M., 1993. • Differential equations and their applications: an introduction to applied mathematics, Springer.Dienes, P., 1931. • The Taylor series: an introduction to the theory of functions of a complex variable, The Clarendon Press.

Page 138: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

130

Self Assessment The ___________ method for solving differential equations is fairly difficult to understand and rather involved 1. algebra to derive the equations.

Runge-Kuttaa. Euler’s method b. Taylors series c. Heun Method d.

The infinite _______________ can be used to evaluate a function, given its derivative function and its value 2. at some point.

Runge-Kuttaa. Euler’s method b. Taylors series c. Heun Method d.

Which of the following statements is true?3. It is not practical to evaluate a large number of the higher-order derivatives.a. It is not practical to evaluate a large number of the first-order derivatives. b. It is not practical to evaluate a large number of the second-order derivatives. c. It is not practical to evaluate a large number of the thrid-order derivatives.d.

The ____________ method is not an efficient method for solving initial-value ODE.4. Runge-Kuttaa. Euler’s method b. Taylors series c. Heun Method d.

Algorithms for one-step techniques such as ___________ are extremely simple to program. 5. Runge-Kuttaa. Euler’s method b. Taylors series c. Heun Method d.

The ______________ is a predictor-corrector approach. 6. Runge-Kuttaa. Euler’s method b. Taylors series c. Heun Method d.

A fundamental source of error in _____________ is that the derivative at the beginning of the interval is assumed 7. to apply across the entire interval.

Runge-Kuttaa. Euler’s method b. Taylors series c. Heun Method d.

Page 139: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

131

One method to improve the estimate of the __________ involves the determination of two derivatives for the 8. interval-one at the initial point and another at the end point.

derivativesa. slopeb. curve c. graph d.

Which of the following is first order ODE?9.

a.

b.

c.

d.

Which of the following is Euler’s method for slope?10. New value = old value +slopea. × step sizeNew value = new value x slopeb. × step sizeNew value = old value +slope+ step sizec. New value = old value +old sloped. × step size

Page 140: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

132

Chapter VII

Numerical Integration

Aim

The aim of this chapter is to:

elucidate trapezoidal rule •

explain Simpson’s 1/3 rule•

describe Simpson’s 3/8 rule•

Objectives

The objectives of this chapter are to:

elucidate composite Simpson’s 3/8 rule•

derive trapezoidal rule formula•

derive Simpson’s 1/3 rule formula•

Learning outcome

At the end of this chapter, you will be able to:

understand trapezoidal rule •

identify the theorem for Simpson’s 3/8 rule•

recog• nise the theorem for composite Simpson’s 3/8 rule

Page 141: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

133

7.1 Trapezoidal RuleFor definite integrals such as

or

we can’t use the Fundamental Theorem of Calculus to evaluate them since there are no elementary functions that are antiderivatives of or . The best we can do is to use approximation methods for such integrals.

The trapezoidal rule is a numerical method that approximates the value of a definite integral. We consider the definite integral

We assume that f(x) is continuous on [a, b] and we divide [a, b] into n subintervals of equal length

using the n + 1 points

x0 = a, x1 = a + ∆x, x2 = a + 2∆x, …, xn = a + n∆x = b

we can compute the value of f(x) at these points.

y0 = f(x0), y1 = f(x1), y2 = f(x2),…, yn = f(xn)

We approximate the integral by using n trapezoids formed by using straight line segments between the points (xi−1, yi−1) and (xi , yi) for 1 ≤ i ≤ n as shown in the figure below.

y

yn

xa=x0 x1 x2 x3 ∆x xn-2 xn-1 xn=b

The area of a trapezoid is obtained by adding the area of a rectangle and a triangle.

Page 142: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

134

y0 y0

∆x ∆x

y1

y1 - y0

By adding the area of the n trapezoids, we obtain the approximation

which simplifies to the trapezoidal rule formula

Example 1Use the trapezoidal rule with n=8 to estimate

Solution: For n=8, we have . We compute the value of y0, y1, …, ys

x 1 1.5 2 2.5 3 3.5 4 4.5 5

Therefore,

#include<stdio.h>#include<conio.h>#include<math.h>void main(){ float x[10],y[10],sum=0,h,temp; int i,n,j,k=0; float fact(int); clrscr(); printf(“\nhow many record you will be enter: “); scanf(“%d”,&n);

Page 143: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

135

for(i=0; i<n; i++) { printf(“\n\nenter the value of x%d: “,i); scanf(“%f”,&x[i]); printf(“\n\nenter the value of f(x%d): “,i); scanf(“%f”,&y[i]); } h=x[1]-x[0]; n=n-1; for(i=0;i<n;i++) { if(k==0) { sum = sum + y[i]; k=1; } else sum = sum + 2 * y[i]; } sum = sum + y[i]; sum = sum * (h/2); printf(“\n\n I = %f “,sum);getch();}

OUT PUTHow many record you will be enter: 6Enter the value of x0: 7.47Enter the value of f(x0): 1.93Enter the value of x1: 7.48Enter the value of f(x1): 1.95Enter the value of x2: 7.49Enter the value of f(x2): 1.98Enter the value of x3: 7.50Enter the value of f(x3): 2.01Enter the value of x4: 7.51Enter the value of f(x4): 2.03Enter the value of x5: 7.52Enter the value of f(x5): 2.06I = 0.099652

Page 144: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

136

7.2 Simpson’s 1/3 RuleThe trapezoidal rule was based on approximating the integrand by a first order polynomial, and then integrating the polynomial over interval of integration. Simpson’s 1/3 rule is an extension of Trapezoidal rule where the integrand is approximated by a second order polynomial.

y f(x)dx f(x)

xba

a

b

Fig. 7.1 Integration of a function

Hence

∫∫ ≈=b

a

b

a

dxxfdxxfI )()( 2

where )(2 xf is a second order polynomial given by

22102 )( xaxaaxf ++= .

Choose

)),(,( afa ,2

,2

++ bafba

and ))(,( bfb

as the three points of the function to evaluate ,0a 1a and 2a .

22102 )()( aaaaaafaf ++==

2

2102 2222

+

+

+

+=

+

=

+ baabaaabafbaf

22102 )()( babaabfbf ++==

Solving the above three equations for unknowns, ,0a 1a and 2a give

22

22

0 2

)()(2

4)()(

baba

afbaabfbaabfbabfbfaa

+−

++

+

−+=

221 2

)(2

4)(3)(32

4)(

baba

bbfbabfabfbafbaafaafa

+−

+

+

−++

+

−−=

Page 145: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

137

222 2

)(2

2)(2

baba

bfbafafa

+−

+

+

−=

Then

∫≈b

a

dxxfI )(2

( )∫ ++=

b

a

dxxaxaa 2210

b

a

xaxaxa

++=

32

3

2

2

10

32)(

33

2

22

10abaabaaba −

+−

+−=

Substituting values of ,0a 1a and 2a give

+

+

+−

=∫ )(2

4)(6

)(2 bfbafafabdxxfb

a

Since for Simpson 1/3 rule, the interval [ ]ba, is broken into 2 segments, the segment width

2abh −

=

Hence the Simpson’s 1/3 rule is given by

+

+

+≈∫ )(2

4)(3

)( bfbafafhdxxfb

a

Since the above form has 1/3 in its formula, it is called Simpson’s 1/3 rule.

A C++ Program to estimate the Integral value of the function at the given points from the given data using Simpson’s 1/3 Rule

//--------------------------- Header Files ----------------------------//# include <iostream.h> # include<stdlib.h> # include <string.h> # include<stdio.h> # include<conio.h> # include <math.h>

//------------------------ Global Variables ---------------------------// const int max_size=13; int n=0; int top=-1; int choice=0;

Page 146: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

138

long double h=0; long double a=0; long double b=0; long double estimated_value=0; long double xn[max_size]={0}; long double fx[max_size]={0}; char Fx[100]={NULL}; char Stack[30][30]={NULL}; char Postfix_expression[30][30]={NULL};

//------------------------ Funcion Prototypes -------------------------//void push(const char *); void convert_ie_to_pe(const char *); const char* pop( ); const long double evaluate_postfix_expression(const long double); void show_screen( ); void clear_screen( ); void get_input( ); void apply_simpsons_rule( ); void show_result( );

//------------------------------ main( ) ------------------------------// int main( ) { clrscr( ); textmode(C4350); show_screen( ); get_input( ); apply_simpsons_rule( ); show_result( );

getch( ); return 0; }//------------------------ Function Definitions ------------------------////-------------------------- show_screen ( ) ---------------------------//void show_screen( ) {cprintf(“\n********************************************************************************”); cprintf(“***************************- -**************************”); cprintf(“*--------------------------- “); textbackground(1); cprintf(“ Numerical Integration “); textbackground(8); cprintf(“ --------------------------*”); cprintf(“*-**********************************************-*”); cprintf(“********************************************************-*”); for(int count=0;count<42;count++) cprintf(“*-* *-*”); gotoxy(1,46); cprintf(“********************************************************-*”); cprintf(“*------------------------------------------------------------------------------*”);

Page 147: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

139

cprintf(“*****************************************************”); gotoxy(1,2); }//------------------------- clear_screen( ) ---------------------------// void clear_screen( ) { for(int count=0;count<37;count++) { gotoxy(5,8+count); cout<<” “; } gotoxy(1,2); }//-------------------------- push (const char*) ------------------------//void push(const char* Operand) { if(top==(max_size-1)) { cout<<”Error : Stack is full.”<<endl; cout<<”\n Press any key to exit.”; getch( ); exit(0); } else { top++; strcpy(Stack[top],Operand); } } //------------------------------ pop( ) -------------------------------//const char* pop( ) { char Operand[40]={NULL};

if(top==-1) { cout<<”Error : Stack is empty.”<<endl; cout<<”\n Press any key to exit.”; getch( ); exit(0); } else { strcpy(Operand,Stack[top]); strset(Stack[top],NULL); top--; } return Operand; }//-------------------- convert_ie_to_pe(const char*) ------------------// void convert_ie_to_pe(const char* Expression) { char Infix_expression[100]={NULL};

Page 148: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

140

char Symbol_scanned[30]={NULL}; push(“(“); strcpy(Infix_expression,Expression); strcat(Infix_expression,”+0)”); int flag=0; int count_1=0; int count_2=0; int equation_length=strlen(Infix_expression); if(Infix_expression[0]==’(‘) flag=1; do { strset(Symbol_scanned,NULL); if(flag==0) { int count_3=0; do { Symbol_scanned[count_3]=Infix_expression[count_1]; count_1++; count_3++; } while(count_1<=equation_length && Infix_expression[count_1]!=’(‘ && Infix_expression[count_1]!=’+’ && Infix_expression[count_1]!=’-’ && Infix_expression[count_1]!=’*’ && Infix_expression[count_1]!=’/’ && Infix_expression[count_1]!=’^’ && Infix_expression[count_1]!=’)’); flag=1; } else if(flag==1) { Symbol_scanned[0]=Infix_expression[count_1]; count_1++; if(Infix_expression[count_1]!=’(‘ && Infix_expression[count_1]!=’^’ && Infix_expression[count_1]!=’*’ && Infix_expression[count_1]!=’/’ && Infix_expression[count_1]!=’+’ && Infix_expression[count_1]!=’-’ && Infix_expression[count_1]!=’)’) flag=0; if(Infix_expression[count_1-1]==’(‘ && (Infix_expression[count_1]==’-’ || Infix_expression[count_1]==’+’)) flag=0; } if(strcmp(Symbol_scanned,”(“)==0) push(“(“); else if(strcmp(Symbol_scanned,”)”)==0) {

Page 149: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

141

while(strcmp(Stack[top],”(“)!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } pop( ); } else if(strcmp(Symbol_scanned,”^”)==0 || strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0 || strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0) { if(strcmp(Symbol_scanned,”^”)==0) { } else if(strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0) { while(strcmp(Stack[top],”^”)==0 || strcmp(Stack[top],”*”)==0 || strcmp(Stack[top],”/”)==0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } else if(strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0) { while(strcmp(Stack[top],”(“)!=0) { strcpy(Postfix_expression[count_2],pop( )); count_2++; } } push(Symbol_scanned); } else { strcat(Postfix_expression[count_2],Symbol_scanned); count_2++; } } while(strcmp(Stack[top],NULL)!=0);

strcat(Postfix_expression[count_2],”=”); count_2++; } //---------- evaluate_postfix_expression(const long double) -----------//

const long double evaluate_postfix_expression(const long double x) { long double function_value=0;

Page 150: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

142

int count_1=-1; char Symbol_scanned[30]={NULL}; do { count_1++; strcpy(Symbol_scanned,Postfix_expression[count_1]); if(strcmp(Symbol_scanned,”^”)==0 || strcmp(Symbol_scanned,”*”)==0 || strcmp(Symbol_scanned,”/”)==0 || strcmp(Symbol_scanned,”+”)==0 || strcmp(Symbol_scanned,”-”)==0) { char Result[30]={NULL}; char Operand[2][30]={NULL}; strcpy(Operand[0],pop( )); strcpy(Operand[1],pop( )); long double operand[2]={0}; long double result=0; char *endptr;

for(int count_2=0;count_2<2;count_2++) { int flag=0; if(Operand[count_2][0]==’-’) { int length=strlen(Operand[count_2]); for(int count_3=0;count_3<(length-1);count_3++) Operand[count_2][count_3]=Operand[count_2][(count_3+1)]; Operand[count_2][count_3]=NULL; flag=1; } if(strcmp(Operand[count_2],”x”)==0) operand[count_2]=x; else if(strcmp(Operand[count_2],”e”)==0) operand[count_2]=2.718282; else if(strcmp(Operand[count_2],”sinx”)==0) operand[count_2]=sinl(x); else if(strcmp(Operand[count_2],”cosx”)==0) operand[count_2]=cosl(x); else if(strcmp(Operand[count_2],”tanx”)==0) operand[count_2]=tanl(x); else if(strcmp(Operand[count_2],”lnx”)==0) operand[count_2]=logl(x); else if(strcmp(Operand[count_2],”logx”)==0) operand[count_2]=log10l(x); else operand[count_2]=strtod(Operand[count_2],&endptr); if(flag) operand[count_2]*=-1; } switch(Symbol_scanned[0]) { case ‘^’ : result=powl(operand[1],operand[0]);

Page 151: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

143

break; case ‘*’ : result=operand[1]*operand[0]; break; case ‘/’ : result=operand[1]/operand[0]; break; case ‘+’ : result=operand[1]+operand[0]; break; case ‘-’ : result=operand[1]-operand[0]; break; } gcvt(result,25,Result); push(Result); } else if(strcmp(Symbol_scanned,”=”)!=0) push(Symbol_scanned); } while(strcmp(Symbol_scanned,”=”)!=0); char Function_value[30]={NULL}; char *endptr;

strcpy(Function_value,pop( )); function_value=strtod(Function_value,&endptr); return function_value; }//----------------------------- get_input( ) --------------------------// void get_input( ) { do { clear_screen( ); gotoxy(6,9); cout<<”Number of Sub-Intervals :”; gotoxy(6,10); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(20,13); cout<<”[ min. n = 2 | max. n = 12 ]”; gotoxy(6,12); cout<<”Enter the max. number of sub-intervals = n = “; cin>>n; if(n<2 || n>12) { gotoxy(12,25); cout<<”Error : Wrong Input. Press <Esc> to exit or any other key”;

gotoxy(12,26); cout<<” to try again.”; n=int(getche( )); if(n==27) exit(0); } } while(n<2 || n>12); gotoxy(6,16);

Page 152: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

144

cout<<”Enter the value of Lower limit = a = “; cin>>a; gotoxy(6,18); cout<<”Enter the value of Upper Limit = b = “; cin>>b; h=((b-a)/n); gotoxy(6,24); cout<<”Input Mode :”; gotoxy(6,25); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(8,28); cout<<”Press : “; gotoxy(10,30); cout<<”- ‘Y’ or <Enter> to enter function”; gotoxy(10,32); cout<<”- ‘N’ or <Any other key> to enter values of the function”; gotoxy(8,35); cout<<”Enter your choice : “; char Choice=NULL; Choice=getch( ); if(Choice==’y’ || Choice==’Y’ || int(Choice)==13) { choice=1; gotoxy(28,35); cout<<”Y”; } else { gotoxy(28,35); cout<<”N”; } gotoxy(25,43); cout<<”Press any key to continue...”; getch( );

if(choice) { clear_screen( ); gotoxy(6,10); cout<<”Non-Linear Function :”; gotoxy(6,11); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(6,37); cout<<”Note : Write the function with proper Braces ( ) e.g; 2x+3 as (2*x)+3”; gotoxy(6,40); cout<<”Available Operators : ^ (raised to power) , * , / , + , -”; gotoxy(6,42); cout<<”Available Operands : x , e , sinx , cosx , tanx , lnx , logx ,”; gotoxy(6,44); cout<<” n = any number”; gotoxy(6,14); cin>>Fx; convert_ie_to_pe(Fx);

Page 153: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

145

} clear_screen( ); gotoxy(6,9); cout<<”Data Points & Values of Function :”; gotoxy(6,10); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(25,12); cout<<”ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿”; gotoxy(25,13); cout<<”³ x ³ f(x) ³”; gotoxy(25,14); cout<<”ÃÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´”; gotoxy(25,15); cout<<”³ ³ ³”; for(int count_1=0;count_1<=n;count_1++) { gotoxy(25,(wherey( )+1)); cout<<”³ ³ ³”; gotoxy(25,(wherey( )+1)); cout<<”³ ³ ³”; } gotoxy(25,(wherey( )+1)); cout<<”ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ”; xn[0]=a; for(int count_2=0;count_2<n;count_2++) xn[(count_2+1)]=(xn[count_2]+h); gotoxy(25,16); for(int count_3=0;count_3<=n;count_3++) { gotoxy(27,wherey( )); cout<<xn[count_3]; if(choice) { fx[count_3]=evaluate_postfix_expression(xn[count_3]); gotoxy(43,wherey( )); cout<<fx[count_3]; } else { gotoxy(43,wherey( )); cin>>fx[count_3]; } if(choice) gotoxy(25,(wherey( )+2)); else gotoxy(25,(wherey( )+1)); } gotoxy(25,43); cout<<”Press any key to continue...”;

getch( ); }//---------------------- apply_simpsons_rule( ) -----------------------//

Page 154: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

146

void apply_simpsons_rule( ) { long double temp=0; estimated_value=(fx[0]+fx[n]); estimated_value*=h; estimated_value/=3; temp=0; for(int count_1=2;count_1<=(n-2);count_1+=2) temp+=fx[count_1]; temp*=(2*h); temp/=3; estimated_value+=temp; temp=0; for(int count_2=1;count_2<=(n-1);count_2+=2) temp+=fx[count_2]; temp*=(4*h); temp/=3; estimated_value+=temp; }//----------------------------- show_result( ) ------------------------//void show_result( ) { clear_screen( ); gotoxy(6,9); cout<<”Simpson’s 1/3 Rule :”; gotoxy(6,10); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(8,12); cout<<” bô”; gotoxy(10,13); cout<<”³f(x)dx ÷ (h/3)(f0+fn) + (2h/3)[f2+f4+...+f(n-2)] +”; gotoxy(50,15); cout<<”(4h/3)[f1+f3+...+f(n-1)]”; gotoxy(8,14); cout<<” aõ”; gotoxy(6,17); cout<<” bô”; gotoxy(6,19); cout<<” aõ”; gotoxy(6,18); cout<<”Estimation of ³f(x)dx :”; gotoxy(6,20); cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”; gotoxy(8,23); cout<<”Estimated Integral Value = “; cout<<estimated_value; gotoxy(1,2); }

Page 155: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

147

7.3 Simpson’s 3/8 RuleThe numerical integration technique known as “Simpson’s 3/8 rule” is credited to the mathematician Thomas Simpson (1710-1761) of Leicestershire, England. He has also worked in the areas of numerical interpolation and probability theory.

Theorem (Simpson’s 3/8 Rule): Consider y=f(x) over [x0, x3], where x1= x0+h, x2=x0+2h,

This is a numerical approximation to the integral of f(x) over [x0, x3] and we have the expression

The remainder term for Simpson’s 3/8 rule is , where c lies somewhere between x0 and x3 and have equality

Composite Simpson’s 3/8 RuleOur next method of finding the area under a curve y=f(x) is by approximating that curve with a series of cubic segments that lie above the intervals . When several cubics are used, we call it the composite Simpson’s 3/8 rule.

Theorem (Composite Simpson’s 3/8 Rule):Consider y=f(x) over [a, b]. Suppose that the interval is subdivided into 3m subintervals of equal width by using the equally spaced sample points Xk=x0+kh for k= 0, 1, 2,…, 3m. The composite Simpson's 3/8 rule for 3m subintervals is

is an numerical approximation to the integral, and

Furthermore, if , then there exists a value c with a< c< b so that the error term Esc (f, h) ha the form

This is expressed using the “big O” notation

Page 156: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

148

When the step size is reduced by a factor of the remainder term should be reduced by approximately = 0.0625.

Example: Let f[x] be 1 (a) Numerically approximate the integral by using Simpson’s 3/8 rule with m = 1, 2, 41 (b) Find the analytic value of the integral (i.e. find the “true value”)1 (c) Find the error for the Simpson’ 3/8 rule approximations

Solution 1(a)

3

2.5

2

1.5

1

0.5

y

x0.25 0.5 0.75 1 1.25 1.5 1.75 2

We will use stimulated hand computations for the solution

Page 157: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

149

Solution 1(b)The integral of can be determined

The value of the definite integral

Solution 1 (c)val - t16−0.000002358959196

Code for Simpson’s 3/8 rule in C Programming#include<stdio.h>#include<conio.h>#include<math.h>void main(){float x[10],y[10],sum=0,h,temp;int i,n,j,k=0,l=0;float fact(int);clrscr();printf(“\nhow many record you will be enter: “);scanf(“%d”,&n);for(i=0; i<n; i++){printf(“\n\nenter the value of x%d: “,i);scanf(“%f”,&x[i]);printf(“\n\nenter the value of f(x%d): “,i);scanf(“%f”,&y[i]);

Page 158: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

150

}h=x[1]-x[0];n=n-1;sum = sum + y[0];for(i=1;i<n;i++){ if(k==0 || l==0){ sum = sum + 3 * y[i];if(k==1){l=1;}k=1;}else{sum = sum + 2 * y[i];k=0;l=0;}}sum = sum + y[i];sum = sum * (3*h/8);printf(“\n\n I = %f “,sum);getch();}OUT PUTHow many record you will be enter: 10Enter the value of x0: 0.1Enter the value of f(x0): 1.001Enter the value of x1: 0.2Enter the value of f(x1): 1.008Enter the value of x2: 0.3Enter the value of f(x2): 1.027Enter the value of x3: 0.4Enter the value of f(x3): 1.064Enter the value of x4: 0.5Enter the value of f(x4): 1.125Enter the value of x5: 0.6Enter the value of f(x5): 1.216Enter the value of x6: 0.7Enter the value of f(x6): 1.343Enter the value of x7: 0.8Enter the value of f(x7): 1.512Enter the value of x8: 0.9Enter the value of f(x8): 1.729Enter the value of x9: 1.0Enter the value of f(x9): 2 I = 1.149975

Page 159: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

151

SummaryThe trapezoidal rule is a numerical method that approximates the value of a definite integral• The trapezoidal rule was based on approximating the integrand by a first order polynomial, and then integrating • the polynomial over interval of integration.The numerical integration technique known as “Simpson’s 3/8 rule” is credited to the mathematician Thomas • Simpson.

ReferencesCheney, W. E. and Kincaid, R. D., 2007. • Numerical mathematics and computing, Cengage Learning.Chapra, C. S., 2007. • Numrecial Methods For Engg, 5th ed., Tata McGraw-Hill Education.Simpson’s 3/8Rule forNumerical Integration• [Online] Available at: < http://ktuce.ktu.edu.tr/~pehlivan/numerical_analysis/chap06/Simpson38Rule.pdf> [Accessed 21 November 2011].Fischer, L., 2008. • TRAPEZOIDALRULE [Online] Available at: < http://www.syntax-example.com/Code/trapezoidal-rule-2392.aspx> [Accessed 21 November 2011].PatrickJMT.com, 2009. • The Trapezoid Rule for Approximating Integrals [Video Online] Available at: < http://www.youtube.com/watch?v=8z6JRFvjkpc> [Accessed 21 November 2011].Numericalmethodsguy, 2009. • Simpson’sOneThirdRuleDerivation [Video Online] Available at: <http://www.youtube.com/watch?annotation_id=annotation_263056&feature=iv&src_vid=gt3QZgMNq3s&v=qAVk2P5-Nvc> [Accessed 21 November 2011].

Recommended ReadingJain, K. M., 2003. • NumericalMethodsFor ScientificAndEngineeringComputation, 4th ed., New Age International.Antia, M. H., 2002. • Numerical methods for scientists and engineers, Vol. 1, Birkhäuser.Rajaraman, V., 2004. • ComputerOrientedNumericalMethods, PHI Learning Pvt. Ltd.

Page 160: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

152

Self Assessment The _________________ was based on approximating the integrand by a first order polynomial.1.

Cramer’s rulea. Euler’s rule b. Simpson’s 1/3c. trapezoidal ruled.

____________ is an extension of Trapezoidal rule. 2. Cramer’s rulea. Euler’s rule b. Simpson’s 1/3c. trapezoidal ruled.

The ______________ is a numerical method that approximates the value of a definite integral.3. Cramer’s rulea. Euler’s rule b. Simpson’s 1/3c. trapezoidal ruled.

Which of the following is a definite integral?4.

a.

b.

22102 )( xaxaaxf ++=c.

d.

The numerical integration technique known as “Simpson’s 3/8 rule” is credited to the mathematician 5. _____________.

Thomas Simpsona. Henry Simpsonb. John Simpsonc. Simpsond.

Page 161: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

153

Which of the following is the formula for Trapezoidal rule?6.

a.

22102 )( xaxaaxf ++=b.

c.

d.

Which of the following expresses Simpson 1/3 rule?7.

a.

+

+

+≈∫ )(2

4)(3

)( bfbafafhdxxfb

a

b.

22102 )( xaxaaxf ++=c.

22102 )( xaxaaxf ++=d.

_______________ has also worked in the areas of numerical interpolation and probability theory8. Thomas Simpsona. Henry Simpsonb. John Simpsonc. Simpsond.

Simpson’s 1/3 rule is an extension of Trapezoidal rule where the integrand is approximated by a _______________ 9. order polynomial.

firsta. secondb. thirdc. fourthd.

When several cubics are used in Simpson’s 3/8 rule, it is known as _________10. Composite Simpson’s 3/8 rulea. Varied Simpson’s 3/8 ruleb. First order Simpson’s 3/8 rulec. Second order Simpson’s 3/8 ruled.

Page 162: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

154

Chapter VIII

Linear Programming

Aim

The aim of this chapter is to:

define linear programming model•

explain transportation problems•

describe PERT/CPM•

Objectives

The objectives of this chapter are to:

elucidate linear programming model• and their graphical solutions

explain critical• path (CPM) computations

describe phases for project planning with CPM-PERT•

Learning outcome

At the end of this chapter, you will be able to:

understand linear programming model•

derive a C program to implement transportation problem•

comprehand t• he concept of CPM/PERT

Page 163: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

155

8.1 Linear Programming Model and their Graphical SolutionsYou can solve linear programming problems involving just two variables by drawing a picture. The method works for problems with more than two variables, but it is hard to visualize the higher dimensional problems.

There are essentially two things you need to know in order to and graphical solutions to linear programming problems. First, you need to be able to graph the solution to linear inequalities in the plane. Second, you need to be able to see how the relationship between these points and the value of the objective function.

As a warm up, remember how to graph a line. You and two points that are on the line and then connect them. For example, if the line is described by the equation: 2x1 + x2 = 2, then you can observe that the points (x1; x2) = (1; 0) and (x1; x2) = (0; 2) are on the line. Connecting those leads to a straight line.

The inequality 2x1 + x2 ≥ 2, consists of all of the points above and to the right of the straight line. (In general, inequalities are satisfied by points on one side of the line. In order to determine which set consists of the point that satisfies the inequality, it is tested by checking an arbitrary point not on the line. For example, (x1; x2) = (0; 0) does not satisfy the inequality 2x1 + x2 ≥ 2.

Consequently the set of points that satisfies the inequality consists of the points on the side of the line 2x1 + x2 = 2 that does not contain (0; 0). Now you can figure out how to graph one inequality. If your linear programming problem had only one constraint, you would be in business. LPs can have many constraints (and typically do). In order to complete the process, you graph the constraints one at a time. The feasible region is the intersection. Consider, for example, the set determined by the five inequalities.

This is region bounded by the quadrilateral pictured. (The four corners are (0; 2), (1; 0), (2; 0), and (1; 4).)

There are several things to note. The first three lines describe one inequality each. The fourth line describes two: x1 ≥ 0 and x2 ≥ 0:

If you have five inequalities, you would expect the feasible set of have five sides. This set has only four. The reason is that the constraint that x1 ≥ 0 is redundant. If you satisfy the other four constraints, then you automatically satisfy x1 ≥ 0. (In fact, if the first two constraints hold, then x1 must be non-negative.)

There are five constraints. One problem is that it is not clear what it means to make sense. In linear algebra courses, you want to have as many equations as unknowns because then (and only then) should you expect the system of equations to have a unique solution. Here we are dealing with inequalities, not equations. You can convince yourself that when you have two (or more) variables, it is possible to have solutions to an arbitrarily large number of distinct linear inequalities. (You describe a polygon having n sides as the set of points that satisfies n linear inequalities.) Furthermore, I do not want a unique solution to the constraints.

This would mean that the feasible set had only one point. So the optimisation problem would be simple. (In the diet problem, if the feasible set had only one point, that would mean that there was only one possible way in which you could meet the nutritional constraints. Of course this is impossible - you could meet the constraints by eating more of everything - but the point is that you should expect feasible sets to be large.) In the example, the feasible set has four corners. These corners are determined by the intersection of pairs of constraints, solved as equations. That is, (0; 2) is the solution to

Page 164: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

156

2x1 + x2 = 22x1 + x2 = 2;(1; 4) is the solution to2x1 + x2 = 24x1 + x2 = 8;

(1; 0) is the solution to 2x1 + x2 = 2 and x2 = 0, and (2; 0) is the solution to 4x1 + x2 = 8 and x2 = 0. This is what typically happens. That is, the feasible region of a linear programming problem has corners determined by solving subsets of the constraints as equations (here you do want to use as many constraints as you have variables). Once you have these corners, you get the feasible set by connecting the dots and identifying the region that satisfies all of the constraints. Warnings: The feasible set may be empty. (Imagine that you replaced the constraint that x1 0 with one that said that x1 1.) There is nothing mathematically mysterious about this. It means that you need to b e careful about which side of a constraint line is in the feasible set. The feasible set may be unbounded. That is, it may go out forever in one or more directions. (After all, having no constraints is perfectly ok.) The only way to have a problem that has an unbounded solution is to have an unbounded feasible set

8.2 Transportation ProblemsA scooter production company produces scooters at the units situated at various places (called origins) and supplies them to the places where the depot (called destination) are situated.

Here the availability as well as requirements of the various depots are finite and constitute the limited resources. This type of problem is known as distribution or transportation problem in which the key idea is to minimize the cost or the time of transportation.

Let there be three units, producing scooter, say, A1, A2 and A3 from where the scooters are to be supplied to four depots say B1, B2, B3 and B4.

Let the number of scooters produced at A1, A2 and A3 be a1, a2 and a3, respectively and the demands at the depots be b1, b2, b3 and b4 respectively.

We assume the conditiona1+ a2+ a3= b1+ b2+b3+ b4

i.e., all scooters produced are supplied to the different depots. Let the cost of transportation of one scooter from A1 to B1 be c11. Similarly, the cost of transportations in other casus are also shown in the figure below and Table 1.

a1

a2

a3

A1

A2

A3

b1

b2

b3

b4

B1

B2

B3

B4

x11(c11)x

12(c12)

x13 (c

13 )

x14 (c

14 )

x 31(c 31

)

x 21(c 21

)

x22(c22)

x23(c

23)

x 32(c 32

)

x 33(c 33

)

x34(c34)

x24 (c

24 )

Page 165: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

157

Total number of scooters to be transported from A1 to all destination, i.e., B1, B2, B3 and B4 must be equal to a1

Similarly, from A2 and A3 the scooters transported be equal to a2 and a3 respectively.

and

On the other hand it should be kept in mind that the total number of scooters delivered to B1 from all units must be equal to b1 i.e.,

Similarly,

With the help of the above information we can construct the following table:

Depot Unit To B1 To B2 To B3 To B4 Stock

From A1 x11(c11) x12(c12) x13(c13) x14(c14) a1

From A2 x21(c21) x22(c22) x23(c23) x24(c24) a2

From A3 x31(c31) x32(c32) x33(c33) x34(c34) a3

Requirement b1 b2 b3 b4

The cost of transportation from Ai (i=1, 2, 3) to Bj (j=1, 2, 3, 4) will be equal to

Where the symbol put before signifies that the quantities must be summed over all i=1, 2, 3 and all j= 1, 2, 3, 4.

Thus we come across a linear programming problem given by equations (1) to (7) and a linear function (8). We have to find the non-negative solutions of the system such that it minimizes the function (8).

Page 166: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

158

A c program to implement transportation problem#include<stdio.h>#include<conio.h>main(){int flag=0,flag1=0;int s[10],d[10],sn,eop=1,dm,a[10][10];int i,j,sum=0,min,x[10][10],k,fa,fb;clrscr();/* Getting The Input For the Problem*/

printf(“Enter the number of Supply“);scanf(“%d”,&sn);printf(“Enter the number of Demand“);scanf(“%d”,&dm);printf(“Enter the Supply Values“);for(i=0;i<sn;i++)scanf(“%d”,&s[i]);printf(“Enter the Demand Values“);for(j=0;j<sn;j++)scanf(“%d”,&d[j]);printf(“Enter the elements of the array“);for(i=0;i<sn;i++){for(j=0;j<dm;j++){scanf(“%d”,&a[i][j]);}}/* Calculation For the Transportation */i=0;j=0;for(i=0,j=0;i<sn,j<dm;){if(s[i]<d[j]) // Check supply less than demand{x[i][j]=a[i][j]*s[i]; // Calculate amount * supplyd[j]=d[j]-s[i]; // Calculate demand - supplyi++; // Increment i for the deletion of the row orcolumn}else if(s[i]>=d[j]) //Check the supply greater than equal to demand{x[i][j]=a[i][j]*d[j]; // Calculate amount * demands[i]=s[i]-d[j]; // Calculate supply - demandj++; // Increment j for the deletion of the row or

Page 167: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

159

column}}/* The Cost Matrix is Estimated here */printf(“Given Cost Matrix is :“);for(fa=0;fa<sn;fa++){for(fb=0;fb<dm;fb++){printf(“%d “,a[fa][fb]);}printf(““);}/* The Allocated Cost Matrix is */

printf(“Allocated Cost Matrix is “);for(fa=0;fa<sn;fa++){for(fb=0;fb<dm;fb++){printf(“%d “,x[fa][fb]);sum=sum+x[fa][fb];}printf(““);}/* Transportation Cost Estimated and Sum is Printed*/printf(“The Transportation cost:%d“,sum);getch();}

8.3 PERT/CPMCPM (Critical Path Method) and PERT (Program Evaluation and Review Technique) are network-based methods designed to assist in the planning, scheduling, and control of projects. A project is defined as a collection of interrelated activities with each activity consuming time and resources.

The objective of CPM and PERT is to provide analytic meanso for scheduling the activities. Figure below summarises the steps of the techniques. First, we define the activities of the project, their precedence relationships, and their time requirements. Next, the precedence relationships among the activities are represented by a network.l11e third step involves specific computations to develop the time schedule for the project. During the actual execution of the project things may not proceed as planned, as some of the activities may be expedited or delayed. When this happens, the schedule must be revised to reflect the realities on the ground. This is the reason for including a feedback loop between the time schedule phase and the network phase, as shown in figure below

Page 168: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

160

Project activities

Network calculation

Time schedule

Time

Network

Fig. 8.1 Phases for project planning with CPM-PERT

The two techniques, CPM and PERT, which were developed independently, differ in that CPM assumes deterministic activity durations and PERT assumes probabilistic durations.

Network RepresentationEach activity of the project is represented by an arc pointing in the direction of progress in the project. The nodes of the network establish the precedence relationships among the different activities.Three rules are available for constructing the network.

Rule 1: Each activity is represented by one, and only one, arc.Rule 2: Each activity must be identified by two distinct end nodes.

Figure below shows how a dummy activity can be used to represent two concurrent activities, A and B. By definition, a dummy activity, which normally is depicted by a

1

2

3

AB 1

11

2

22

3

33

A

AA

AB

BB

B

Fig. 8.2 Use of dummy activity to produce unique representation of concurrent activities

dashed arc, consumes no time or resources. Inserting a dummy activity in one of the four ways shown in the figure above, we maintain the concurrence of A and B, and provide unique end nodes for the two activities.

Rule 3: To maintain the correct precedence relationships, the following questions must be answered as each activity is added to the network:

What activities must immediately precede the current activity?• What activities must follow the current activity?• What activities must occur concurrently with the current activity?•

Page 169: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

161

For example, consider the following segment of a project:Activity • C starts immediately after A and B have been completed.Activity • E starts only after B has been completed.

8.3.1 Critical Path (CPM) ComputationsThe end result in CPM is the construction of the time schedule for the project. To achieve this objective conveniently, we carry out special computations that produce the following information:

Total duration needed to complete the project.• Classification of the activities of the project as • critical and noncritical.

An activity is said to be critical if there is no “leeway” in determining its start and finish times. A noncritical activity allows some scheduling slack, so that the start time of the activity can be advanced or delayed within limits without affecting the completion date of the entire project.

To carry out the necessary computations, we define an event as a point in time at which activities are terminated and others are started. In terms of the network, an event corresponds to a node. Define

= Earliest occurrence time of event j

= Latest occurrence time of event j

= Duration of activity (i, j)

The definitions of the earliest and latest occurrences of event j are specified relative to the start and completion dates of the entire project.

The critical path calculations involve two passes: The forward pass determines the earliest occurrence times of the events, and the backward pass calculates their latest occurrence times.

Page 170: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

162

SummaryYou can solve linear programming problems involving just two variables by drawing a picture. The method • works for problems with more than two variables, but it is hard to visualise the higher dimensional problems.CPM (Critical Path Method) and PERT (Program Evaluation and Review Technique) are network-based methods • designed to assist in the planning, scheduling, and control of projects.The objective of CPM and PERT is to provide analytic means for scheduling the activities.• The two techniques, CPM and PERT, which were developed independently, differ in that CPM assumes • deterministic activity durations and PERT assumes probabilistic durations.The end result in CPM is the construction of the time schedule for the project• .

ReferenceSharma, C. S., 2006. • OperationResearch:Pert,Cpm&CostAnalysis, Discovery Publishing House.Lyer, S. P., 2008. • OperationResearch, Tata McGraw-Hill Education.Kumar, N. D., • OptimizationMethods:LinearProgramming-GraphicalMethod Available at: < http://nptel.iitm.ac.in/courses/Webcourse-contents/IISc-BANG/OPTIMIZATION%20METHODS/pdf/Module_3/M3L2_LN.pdf>. [Accessed 21 November 2011].Baker, L. S., 2004. • Critical Path Method (CPM) [Online] Available at: <http://hspm.sph.sc.edu/courses/j716/cpm/cpm.html>. [Accessed 21 November 2011].Dr. Zelbst, P., 2010. • Critical Path and PERT Method [Video Online] Available at: <http://www.youtube.com/watch?v=xLNyqqiXw7E>. [Accessed 21 November 2011].Nptelhrd, 2009. • Lec-3LinearProgrammingSolutions-GraphicalMethods [Video Online] Available at: < http://www.youtube.com/watch?v=XEA1pOtyrfo>. [Accessed 21 November 2011].

Recommended ReadingSivarethinamohan, R., 2008. • OperationsResearch, Tata McGraw-Hill Education.Natarajan, R, Balasubramani, P & Tamilarasi, A., 2006. • OperationResearch, Pearson Education India.Shah, H. N, Gor, M. R & Soni, H., 2007. • OperationResearch, PHI Learning Pvt. Ltd.

Page 171: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

163

Self AssessmentWhat does CPM stand for?1.

Critical Path Methoda. Close Path Method b. Copy Path Method c. Curve Path Methodd.

What does PERT stand for?2. Perk Even and Review Techniquea. Private Evaluation and Review Technique b. Picture Evaluation and Review Technique c. Program Evaluation and Review Techniqued.

A _______________ is defined as a collection of interrelated activities with each activity 3. consuming time and resources.

Programa. Path b. Project c. Pictured.

The end result in ______________ is the construction of the time schedule for the project. 4. CPMa. PERT b. Project c. Path d.

A scooter production company produces scooters at the units situated at various places called _________.5. Origin a. Destination b. Path c. Rules d.

One needs to be able to graph the solution to linear inequalities in the _____________ in order to draw graphical 6. solutions to linear programming problems.

Planea. Cone b. Spatial c. Sphere d.

__________ assumes deterministic activity durations. 7. CPMa. PERT b. Project c. Path d.

Page 172: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

164

_________ assumes probabilistic durations.8. CPMa. PERT b. Project c. Path d.

The critical path calculations involve two passes, the forward pass determines the earliest occurrence times of 9. the ____________.

event a. graphs b. codes c. program d.

When is “Printf” syntax used?10. To print statement a. To remove statement b. To push value c. To pop value d.

Page 173: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

165

Application I

Lagrange Linear Interpolation Using Basis Functions

Linear Lagrange (N = 1) is the simplest form of Lagrange Interpolation

Where

V0(x) V1(x)

(x)x0 x1

1.0

Given the following data: X0 = 2 f0 = 1.5 X1 = 5 f1 = 4.0

Find the linear interpolating function g (X)Langrage basic functions are: •

Interpolating function • g(x) is:

Page 174: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

166

41.5 V0(x)

x0 = 2 x1 = 5x

2

44.0 V1(x)

x0 = 2 x1 = 5x

2

g(x) = 1.5 V0(x) + 4.0 V1(x)

x0 = 2 x1 = 5

Source: CE, 2004. LAGRANGE INTERPOLATION [Online] Available at:< http://www.nd.edu/~jjwteach/441/PdfNotes/lecture6.pdf> [Accessed 21 November 2011].

QuestionsWhat is Lagrange’s Interpolation method?1. AnswerLagrange's interpolation method is a simple and clever way of finding the unique Nth-order polynomial that exactly passes through N+1 distinct samples of a signal. Once the polynomial is known, its value can easily be interpolated at any point using the polynomial equation.

Write the Lagrange’s Interpolation algorithm.2. Answer:This is again an Nth degree polynomial approximation formula to the function f(x), which is known at discrete points xi, i = 0, 1, 2 . . . Nth. The formula can be derived from the Vandermonds determinant but a much simpler way of deriving this is from Newton's divided difference formula. If f(x) is approximated with an Nth degree polynomial then the Nth divided difference of f(x) constant and (N+1)th divided difference is zero. That isf [x0, x1, . . . xn, x] = 0

From the second property of divided difference we can writef0 + fn fx = 0________________________________________ ________________________________________ + . . . + ________________________________________ (x0 - x1) . . . (x0 - xn)(x0 - x) (xn - x0) . . . (xn - xn-1)(xn - x) (x - x0) . . . (x - xn) or (x - x1) . . . (x - xn) (x - x0) . . . (x - xn-1) f(x) = ________________________________________ f0 + . . . + ________________________________________ fn (x0 - x1) . . . (x0 - xn) (xn - x0) . . . (xn - xn-1)

n ( n )fi

Page 175: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

167

S | | x - xj j = 0 (xi - xj) i = 0 j ¹ 1

Since Lagrange's interpolation is also an N th degree polynomial approximation to f(x) and the Nth degree polynomial passing through (N+1) points is unique hence the Lagrange's and Newton's divided difference approximations are one and the same. However, Lagrange's formula is more convenient to use in computer programming and Newton's divided difference formula is more suited for hand calculations

Compute 3. f(0.3) for the data

x 0 1 3 4 7f 1 3 49 129 813

using Lagrange’s interpolation formula (Analytic value is 1.831)

Using Lagrange's interpolation formula:

(x - x1) (x - x2)(x- x3)(x - x4) (x - x0)(x - x1) (x - x2)(x- x3) f(x) = ________________________________________ f0+ . . . + ________________________________________ f4 (x0 - x1) (x0 - x2)(x0 - x3)(x0 - x4) (x4 - x0)(x4 - x1)(x4 - x2)(x4 - x3)

(0.3 - 1)(0.3 - 3)(0.3 - 4)(0.3 - 7) (0.3 - 0)(0.3 - 3)(0.3 - 4)(0.3 - 7) = ________________________________________ 1+ ________________________________________ 3 + (-1) (-3)(-4)(-7) 1 x (-2)(-3)(-6)

(0.3 - 0)(0.3 - 1)(0.3 - 4)(0.3 - 7) (0.3 - 0)(0.3 - 1)(0.3 - 3)(0.3 - 7) ________________________________________ 49 + ________________________________________ 129 + 3x 2x (-1)(-4) 4x 3x 1 (-3)

(0.3 - 0)(0.3 - 1)(0.3 - 3)(0.3 - 4) ________________________________________ 8137x 6x 4x 3 = 1.831

Page 176: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

168

Application II

Newton Forward Interpolation on Equispaced Points

Use Newton Interpolation which is based on developing difference tables for a given set of data points.

The degree interpolating polynomial obtained by fitting data points will be identical to that obtained using Lagrange formulae.

Newton interpolation is simply another technique for obtaining the same interpolating polynomial as was obtained using the Lagrange formulae.

Forward Difference Tables

We assume equi-spaced points

f f2 = f(x2)

f1 = f(x1) f3 = f(x3)

f0 = f(x0) fN = f(xN)

x0

0 1 2 3 N (i)

h = interval size

x1 x2 x3 xN

x

Forward differences are now defined as follows:

(Zeroth order forward difference)

(First order forward difference)

(Second order forward difference)

(Third order forward difference)

(kth order forward difference)

Page 177: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

169

Typically we set up a difference table

i fi ∆fi ∆2fi ∆3fi ∆4fi

0 f0 ∆f0 = f1 – f0 ∆2f0 = ∆f1-∆f0 ∆3f0 = ∆2f1-∆2f0 ∆4f0 = ∆3f1-∆

3f0

1 f1 ∆f1 = f2 – f1 ∆2f1 = ∆f2-∆f1 ∆3f1 = ∆2f2-∆2f1

2 f2 ∆f2 = f3 – f2 ∆2f2 = ∆f3-∆f2

3 f3 ∆f3 = f4 – f3

4 f4

Note that to compute higher order differences in the tables, we take forward differences of previous order differences instead of using expanded formulae.

The order of the differences that can be computed depends on how many total datapoints, x0,..., xN are available.

N + 1 data points can develop up to Nth order forward difference

Source: CE, 2004. NEWTONFORWARDINTERPOLATIONONEQUISPACEDPOINTS [Online] Available at:< http://www.nd.edu/~jjwteach/441/PdfNotes/lecture7.pdf> [Accessed 21 November 2011].

QuestionsDevelop a forward difference table for the data given1.

i xi fi ∆fi ∆2fi ∆3fi ∆4fi ∆5fi

0 2 -7 4 5 5 3 1

1 4 -3 9 10 8 4

2 6 6 19 18 12

3 8 25 37 30

4 10 62 67

5 12 129

For the data and forward difference table presented in Question 1. 2. Develop g (x) using 3 points (x � 0 = 2, x1 =4 and x2 = 6 ) and estimate e(x)

For the data and forward difference table presented in Question 1. 3. Develop g (x) using 3 different points x � 0 = 6, x1 = 8, x2 = 1

Page 178: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

170

Application III

Schedule Risk Analysis SimplifiedThe critical path method (CPM) of scheduling a project is a key tool for project management. A schedule “network” represents the project strategy. Activities, where the work is accomplished, are linked by relationships showing how the work is planned. Strings of linked predecessor and successor activities constitute “paths” through the network. When two or more paths are to be done simultaneously, they are described as parallel paths.

CPM computes the shortest project completion duration and or completion date from the longest path through the network. The “longest pole in the tent” is called the “critical path.” Any delay on the critical path will delay the project. The critical path method

(CPM) of scheduling is traditional and well accepted.

Project managers need to understand some key reservations about the standard CPM, and how to use a schedule risk analysis to provide information crucial to a project’s success, before they embark on their project:

The project duration calculated by CPM is accurate only if everything goes according to plan. This is rare in • real projects.In many cases the completion dates CPM produces are unrealistically optimistic and highly likely to be overrun, • even if the schedule logic and duration estimates are accurately implemented.The CPM completion date is not even the most likely project completion date, in almost all cases.• The path identified as the “critical path” using traditional CPM techniques may not be the one that will be most • likely to delay the project and which may need management attention.

Step 1: CPM Schedule – the Foundation of a Risk AnalysisCPM analysis of the project schedule is the key building block of a quantified risk assessment. Case 1 presents a very simple project and a typical schedule risk analysis. It illustrates how the CPM completion date can easily be overrun. It shows how a risk analysis can illuminate the issues in CPM and point to their resolution. For the first step, a project with two activities and a finish milestone is shown in Figure 1

A102A101 F

Fig. 1 Project network with single path

Suppose the durations are set at 50 working days for A101 and 80 working days for A102. CPM shows that this simple project will take 130 working days (50 + 80 = 130).

Step 2: Determine the Activity

Duration RangesThe activity durations that are used to calculate the critical path are often thought of as the “best guess” or “most likely” amount of time needed to complete the work given the planned resources. Experienced project managers know that the work might take more or less time than the estimate they have assumed for the CPM calculation. Duration ranges for each activity are the low (optimistic) and high (pessimistic) durations that the work on the activity might take under different possible extreme scenarios. Triangular distributions are often used in risk analysis because they are easy to specify (just needing three points and a straightedge) and to use in analysis. Also, the project managers may not know enough to specify any other distribution type, although other distribution shapes are often available in the software.

Page 179: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

171

Task Name

Total Project

A101

A102

Finish

ID

1

2

3

4

Min Dur Duration Max Dur

0d 130c 0d

40c 50c 100c

70c 80c 100c

0d 0d 0d

Apr May Jun Jul Aug Sep Apr May Jun Jul Aug SepOct Nov Dec Jan Feb Mar

Qtr 2, 1996 Qtr 3, 1996 Qtr 4, 1996 Qtr 1, 1997 Qtr 2, 1997 Qtr 3, 1997

12/11

Fig. 2 Simple 2-activity project

Suppose that the analyst in Case 1 chooses a simple triangular distribution for each activity, using the data in Figure 2. The distributions are shown in Figure 3. They make it clear that both CPM durations are optimistic, and that the A101 duration of 50 working days is quite optimistic.

Activity A101

probability

Working days Working days40 50 100 70 80 100

probability

Activity A102

Fig. 3 Probability distributions

Step 3: Simulate the Project ScheduleOnce the activities’ duration ranges and distributions have been determined, the schedule risk analysis can determine how risky the entire project schedule is. The most common method of determining schedule overrun risk is to simulate the project by solving (or iterating) it hundreds or thousands of times on the computer.

Source: Deltek, Inc., 2007. Schedule Risk Analysis Simplified [Online] Available at: < http://www.coepm.net/wp-content/uploads/Whitepapers/wp_scheduleriskanlysisA4_web.pdf>. [Accessed 20 October 2011].

QuestionsExplain Critical Path computation?1. Explain Linear Programming Formulation of CPM?2. How CPM helps solving risk factor in Project scheduling?3.

Page 180: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

172

Bibliography

ReferencesBabu, R., 2010. • Engineering Mathematics-I, Pearson Education India.Baker, L. S., 2004. • Critical Path Method (CPM) [Online] Available at: <http://hspm.sph.sc.edu/courses/j716/cpm/cpm.html>. [Accessed 21 November 2011].Bradie, B., 2006. • A Friendly Introduction to Numerical Analysis, Pearson Education India.Bramer, B., • Errors in numerical computation [Online] Available at: <http://www.cse.dmu.ac.uk/~bb/Teaching/ComputerSystems/ErrorsInComputation/ErrorsInComputation.htm> [Accessed 17 November 2011].burny1, 2010. • Interpolation - Newtons forward/difference methods [Video Online] Available at: <http://www.youtube.com/watch?v=Uzc3Tc2ZUXw> [Accessed 17 November 2011].burny1, 2010. • RootfindingBisection/Newton/Secant/FalsePositionandOrderofconvergence [Video Online] Available at: <http://www.youtube.com/watch?v=mztGjg04rV8> [Accessed 17 November 2011].Butcher, C. J., 2008. • Numerical methods for ordinary differential equations, John Wiley and Sons.Chapra, C. S., 2007. • Numrecial Methods For Engg, 5th ed., Tata McGraw-Hill Education.Cheney, W. E. and Kincaid, R. D., 2007. • Numerical mathematics and computing, Cengage Learning.Curve Fitting and Method of Least Squares, [Online] Available at: <http://www.emathzone.com/tutorials/basic-• statistics/curve-fitting-and-method-of-least-squares.html>[Accessed 6 January 2012].Dahlquist, G. and Björck, A., 2008. • Numericalmethodsinscientificcomputing, SIAM.Dr. Zelbst, P., 2010. • Critical Path and PERT Method [Video Online] Available at: <http://www.youtube.com/watch?v=xLNyqqiXw7E>. [Accessed 21 November 2011].Fischer, L., 2008. • TRAPEZOIDALRULE [Online] Available at: < http://www.syntax-example.com/Code/trapezoidal-rule-2392.aspx> [Accessed 21 November 2011].Gupta, S. K., 2006. Numerical Methods for Engineers, New Age International.• KaNaungThinkTank, 2007. Least Square Method Part two, [Video Online] Available at: <http://www.youtube.• com/watch?v=P-d5pUB4yQw>[Accessed 6 January 2012].Kumar, N. D., • OptimizationMethods:LinearProgramming-GraphicalMethod Available at: < http://nptel.iitm.ac.in/courses/Webcourse-contents/IISc-BANG/OPTIMIZATION%20METHODS/pdf/Module_3/M3L2_LN.pdf> [Accessed 21 November 2011].Lal, K. A., 2007. • Lagrange’s Interpolation formula [Online] Available at: <http://nptel.iitm.ac.in/courses/Webcourse-contents/IIT-KANPUR/mathematics-2/node113.html> [Accessed 17 November 2011].Lapidus, L. and Seinfeld, H. J., 1971. • Numerical solution of ordinary differential equations, Academic Press.Leaf, B., • How to Calculate Relative Error [Video Online] Available at: <http://www.ehow.com/video_4951553_calculate-relative-error.html> [Accessed 17 November 2011].LearnChemE, 2011. Least Squares Method, [Video Online] Available at: <http://www.youtube.com/• watch?v=yVkpq20OtcE>[Accessed 6 January 2012].Least Square Method, [Online] Available at: <http://www.efunda.com/math/leastsquares/leastsquares.cfm> • [Accessed 6 January 2012].Lipschutz, S. and Lipson, M., 2000. Schaum’s outline of theory and problems of probability, 2nd ed., McGraw-• Hill Professional, 2000.Livetoad.org, • Picard iteration [Online] Available at: <http://livetoad.org/Courses/Documents/214a/Notes/picard_iterates.pdf> [Accessed 17 November 2011].Lyer, S. P., 2008. • OperationResearch, Tata McGraw-Hill Education. Mathbyfives, 2011. • Runge-Kutta Method.mov [Video Online] Available at: <http://www.youtube.com/watch?v=0iRZqmzs9dk> [Accessed 17 November 2011].

Page 181: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

173

MathMedics, LLC, 1999. • The Bisection Method [Online] Available at: <http://www.sosmath.com/calculus/limcon/limcon07/limcon07.html> [Accessed 17 November 2011].Nptelhrd, 2009. • Lec-3LinearProgrammingSolutions-GraphicalMethods [Video Online] Available at: < http://www.youtube.com/watch?v=XEA1pOtyrfo>. [Accessed 21 November 2011].Numerical Computation• [pdf] Available at: <http://www.newagepublishers.com/samplechapter/001997.pdf> [Accessed 17 November 2011].Numericalmethodsguy, 2009. • Newton-Raphson Method: Example [Video Online] Available at: <http://www.youtube.com/watch?v=lFYzdOemDj8> [Accessed 17 November 2011].Numericalmethodsguy, 2009. • Simpson’sOneThirdRuleDerivation [Video Online] Available at: <http://www.youtube.com/watch?annotation_id=annotation_263056&feature=iv&src_vid=gt3QZgMNq3s&v=qAVk2P5-Nvc> [Accessed 21 November 2011].PatrickJMT.com, 2009. • The Trapezoid Rule for Approximating Integrals [Video Online] Available at: < http://www.youtube.com/watch?v=8z6JRFvjkpc> [Accessed 21 November 2011].Pearce, J. J., 1996. • Taylor Series [Online] Available at: <http://www.math.unh.edu/~jjp/taylor/taylor.html> [Accessed 17 November 2011].Reddy, Y. P., • INTERPOLATION [pdf] Available at: <http://www.sakshieducation.com/(Scfzi10iftouq0lbbjwswlr55))/Engg/EnggAcademia/CommonSubjects/MathMethods-Interpolation.pdf> [Accessed 17 November 2011].Saaty, L. T. and Bram, J., 1964. • Nonlinear mathematics, Courier Dover Publications.Sastry, S. S., 2005. • Introductory methods of numerical analysis, PHI Learning Pvt. Ltd.Sharma, C. S., 2006. • OperationResearch:Pert,Cpm&CostAnalysis, Discovery Publishing House.Simpson’s 3/8Rule forNumerical Integration• [Online] Available at: < http://ktuce.ktu.edu.tr/~pehlivan/numerical_analysis/chap06/Simpson38Rule.pdf> [Accessed 21 November 2011].Smith, D. M., 1998. • The Method of False Position [Online] Available at: <http://web.mit.edu/10.001/Web/Course_Notes/NLAE/node5.html> [Accessed 17 November 2011].Szabados, J. and Vértesi, P., 1990. • Interpolation of functions, World Scientific.USF, 2009. • Euler’s Method: Example [Video Online] Available at: <http://www.youtube.com/watch?v=4CqaepeaJHA> [Accessed 17 November 2011].USF, 2009. • Truncation Error: Definition [Video Online] Available at: <http://www.youtube.com/watch?v=pfV9kqh4Mpg> [Accessed 17 November 2011].USF, 2010. • Newton’sDividedDifferencePolynomial:LinearInterpolation:Example [Video Online] Available at: <http://www.youtube.com/watch?v=qtraD3e6Ka0> [Accessed 17 November 2011].

Recommended ReadingAhmed, S. Q., 2009. • NUMERICALANDSTATISTICALTECHNIQUES, Ane Books Pvt Ltd.Antia, M. H., 2002. • Numerical methods for scientists and engineers, Vol. 1, Birkhäuser.Braun, M., 1993. • Differential equations and their applications: an introduction to applied mathematics, Springer.Burden, L. R. and Faires, J. D., 2010. • Numerical Analysis, Cengage Learning.Cheney, W. E. and Kincaid, R. D., 2007. • Numerical mathematics and computing, Cengage Learning.Dienes, P., 1931. • The Taylor series: an introduction to the theory of functions of a complex variable, The Clarendon Press.Hauser, J. R., 2009. Numerical Methods for Nonlinear Engineering Models, Springer.• Islam, N., 2003. • Numerical Analysis, Academic Publishers.Jain, K. M., 2003. • NumericalMethodsFor ScientificAndEngineeringComputation, 4th ed., New Age International.Natarajan, R, Balasubramani, P & Tamilarasi, A., 2006. • OperationResearch, Pearson Education India.

Page 182: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

174

Nixon, M. S., Aguado, A. S., 2008. Feature extraction and image processing, 2nd ed., Academic Press.• Rajaraman, V., 2004. • ComputerOrientedNumericalMethods, PHI Learning Pvt. Ltd.Rao, S., 2008. • NumericalMethodsForScientistsAndEngineers, 3rd, ed., PHI Learning Pvt. Ltd.Saaty, L. T., 1981. • Modern nonlinear equations, Courier Dover Publications.Sastry, S. S., 2004. • Engineering Mathematics, 3rd ed., PHI Learning Pvt. Ltd.Schiavone, P., Constanda, C. and Mioduchowski, A., 2002. • Integral methods in science and engineering, Springer.Shah, H. N, Gor, M. R & Soni, H., 2007. • OperationResearch, PHI Learning Pvt. Ltd.Sing, R. R., 2010. • Engineering Mathematics, Tata McGraw-Hill Education.Sinha, S. and Pradhan, S., • NumericalAnalysis&StatisticalMethods, Academic Publishers.Sivarethinamohan, R., 2008. • OperationsResearch, Tata McGraw-Hill Education.Stark, P., 1970. Introduction to numerical• methods. Macmillan.

Page 183: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

175

Self Assessment Answers

Chapter Ia1. b2. c3. d4. a5. b6. c7. d8. c9. a10.

Chapter IIa1. b2. c3. d4. a5. a6. b7. c8. d9. a10.

Chapter IIIa1. d2. a3. b4. a5. d6. c7. a8. c9. a10.

Chapter IVa1. b2. c3. d4. a5. b6. c7. d8.

Page 184: Computational Mathematics - Jaipur National Universityjnujprdistance.com/assets/lms/LMS JNU/B.Sc.(IT)/Sem...significant figure than that given in the data, perform the last operation

Computational Mathematics

176

Chapter Va1. a2. a3. c4. a5. a6. a7. a8. c9. a10.

Chapter VIa1. c2. a3. c4. c5. c6. b7. b8. a9. a10.

Chapter VIId1. c2. d3. a4. a5. a6. b7. a8. b9. a10.

Chapter VIIIa1. d2. c3. a4. a5. a6. a7. b8. a9. a10.