18
Numerical Methods Laboratory Manual( V1.1) Subject Code: CS307 Semester: III Year: 2015- 16 (Spring Semester) August 6, 2015 Faculty: Mr. Bhaskar Mondal Department of Computer Science and Engineering National Institute of Technology Jamshedpur Jamshedpur, Jharkhand, India- 831014

Numerical methods lab manual

Embed Size (px)

Citation preview

Page 1: Numerical methods lab manual

Numerical MethodsLaboratory Manual( V1.1)

Subject Code: CS307

Semester: III

Year: 2015- 16 (Spring Semester)

August 6, 2015

Faculty:Mr. Bhaskar Mondal

Department of Computer Science and EngineeringNational Institute of Technology Jamshedpur

Jamshedpur, Jharkhand, India- 831014

Page 2: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

Contents

1 Course Description 2

2 Objective 2

3 Lab Summery Report 3

4 Scope 3

5 Day 1. Mat Lab Exercise 45.1 The Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45.2 Switching Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

6 Day 2: Mat Lab Exercise 4

7 Day 3: Vector and matrix operations 67.1 Try these Matlab Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67.2 Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77.3 Excercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

8 Day 4:Transcendental Equations 98.1 The Bisection Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

8.1.1 Algorithm - Bisection Scheme . . . . . . . . . . . . . . . . . . . . . . . . . . 98.1.2 Numerical Example : . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

8.2 Regula-Falsi Method or False Position Scheme . . . . . . . . . . . . . . . . . . . . . 108.3 The Newton–Raphson Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108.4 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

9 Linear Algebraic Equations 129.1 Gauss Elimination Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129.2 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

10 Interpolation and Approximation 1410.1 NEWTON’S DIVIDED DIFFERENCE FORMULA . . . . . . . . . . . . . . . . . . 1410.2 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

11 Numerical Integration 1511.1 Excercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

12 Numerical Solution of Ordinary Differential Equation 1612.1 Excercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Version: 1.5 Page 1

Page 3: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

1 Course DescriptionMat Lab Introduction to Mat LabMatrix OperationsErrors Floating point Arithmetic, Errors in numerical computation,Zeros of a Single Transcendental Equation Zeros of polynomial using Bisection Method, Iteration

Method, Regula-Falsi method, Newton Raphson method, Secant method, Rate of convergenceof iterative methods.

Simultaneous Linear Equations: Solutions of system of Linear equations, Gauss Elimination directmethod and pivoting, Ill Conditioned system of equations, Refinement of solution. Gauss Seidaliterative method, Rate of Convergence

Interpolation and approximation : Finite Differences, Difference tables Polynomial Interpolation:Newton’s forward and backward formula

Interpolation with unequal intervals: Langrange’s Interpolation, Newton Divided difference formula,Hermite’s Interpolation Approximation of function by Taylor’s series and Chebyshev polyno-mial.

Numerical Differentiation and Integration : Introduction, Numerical Differentiation, Numerical In-tegration, Trapezoidal rule, Simpson’s rules

Solution of differential equations Euler’s Method, Taylor’s Method, Runge-Kutta methods.Frequency Chart: Different frequency chart like Histogram, Frequency curve, Pi-chart.

2 ObjectiveThe objective of the lab work is to familiarize you with implementation of numerical methodsusing Matlab.All numerical methods involve approximations due to either limits in the algorithm or physicallimits in the computer hardware. Errors associated with measurements or calculations can becharacterized with reference to accuracy and precision. Accuracy refers to how closely a com-puted or measured value agree with the true value. Precision refers to how close measured orcomputed values agree with each other after repeated sampling.

An overlying goal with the course is the realization of the necessity of numerical methods inorder to simulate technological and scientific processes based on mathematical models.After completing this course, the students should be able to• identify various mathematical problems and reformulate these in a way suitable for numer-

ical treatment• select a suitable numerical method for the treatment of the given problem• motivate the choice of a method by describing its advantages and limitations• select an algorithm leading to efficient computation and implement this in a programming

language, suitable for scientific computing, e.g. Matlab• provide an estimate of the accuracy of the results• utilize standard functions from e.g. Matlab’s library for calculation, visualization and effi-

cient programming• apply computer science for solving practical problems.

Version: 1.5 Page 2

Page 4: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

3 Lab Summery Report• Maintain Index/ content properly.• Brief descriptions including algorithm used and flowchart of the work you did for each

exercise.• Copies of the main Matlab commands you used for the exercises, along with the numerical

results.• You must calculate and mention computational Complexity of each experiment.• You must provide Test Cases/sample Input and Output at the end of exercise, Print the

plot files (.jpg) corresponding with the different exercises(if any).• Explanations of anything unusual or interesting, or points of confusion that you were unable

to resolve outside lab.• If you believe I have an error in a lab, please inform me of it. Explain why you think it is

an error and, if you like, suggest a correction.

4 Scope

Version: 1.5 Page 3

Page 5: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

5 Day 1. Mat Lab Exercise

5.1 The EnvironmentThe current version of Matlab opens up with a banner area at the top with a large number ofchoices, and five smaller windowpanes. The largest of the small windowpanes is the “CommandWindow” and you will be typing Matlab commands into that window, following the prompt .Among the smaller windowpanes are:Current Folder containing a list off files.Workspace containing a list of variables that Matlab is currently using.Command history containing a list of most recent commands.An informational window about the currently-chosen file.

5.2 Switching Directory• cd: change directory” commands• pwd: find the name of your current directory• dir/ ls: listing of the files in the current directory

Some more Commands:• ans: The result of the previous calculation.• computer: The type of computer you are on.• eps: The smallest positive number ∈ that both satisfies the expression 1+ ∈> 1 and can

be represented on this computer.• i, j: The imaginary unit (). Better avoid using i or j as subscripts or loop indices.• inf: Infinity (). This will be the result of dividing an integer by 0.• NaN: “Not a Number.” This will be the result of dividing 0 by 0, or inf by inf, multiplying

0 by inf, etc. pi:• realmax, realmin: The largest and smallest real numbers that can be represented on

this computer. version: The version of Matlab you are running.

6 Day 2: Mat Lab ExerciseFind answer by using Mat Lab and save your commend history in a file.(diary mydiary.txt)

1. What are the values of the reserved variables pi, eps, realmax, and realmin?2. Use the format long command to display pi in full precision and “format short” to

return Matlab to its default, short, display.The internal precision of any variable is always about 15 decimal digits. The value for piprinted in short format is 3.1416. What is pi − 3.1416? this value is not zero. You foundthe value of pi printed using format long in the previous part of this exercise. What is thedifference between the printed value and pi? This value might not be zero, but it is stillmuch smaller than the value of pi− 3.1416.

3. Set the variable a=1, the variable b=1+eps, and the variable c=2+eps. What is thedifference in the way that Matlab displays these values? Can you tell from the form of theprinted value that a and b are different?

4. Will the command format long cause all the decimal places in b to be printed, or is therestill some missing precision?

5. Choose a value and set the variable x to that value.What is the square of x? Its cube?

Version: 1.5 Page 4

Page 6: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

6. Choose an angle and set the variable theta to its value (a number).What is sinθ ? and cosθ ? Angles can be measured in degrees or radians. Which of thesehas Matlab used?

7. Matlab variables can also be given “character” or “string” values. A string is a sequence ofletters, numbers, spaces, etc., surrounded by single quotes (’). In your own words, what isthe difference between the following two expressions?a1=’sqrt(4)’a2=sqrt(4)

8. The Matlab eval function is used to Evaluate a string as if it were typed at the commandline. If a1 is the string given above, what is the result of the command eval(a1)? Ofa3=6*eval(a1)?

9. Use the save command to save all your variables. Check your “Current Directory” to seethat you have created the file matlab.mat.

10. Use the clear command. Check that there are no variables left in the “current workspace”(windowpane is empty).

11. Restore all the variables with load and check that the variables have been restored to the“Current workspace” windowpane.

Program 1. Write down a program to round off a number.Estimate amount of Absolute error, Relative Error and Percentage error. where• Absolute error due to round off of k digits Ea = |X −X ′| < 1

210n−k

• Relative Error due to round off of k digits Er = |X−X′X| < 1

2101−k

• Percentage error due to round off of k digits Ep = Er × 100Test Case: Estimate different types of errors if the number |X = 0.00545828| is rounded off to

three decimal digits.

Solution: Abs Error = |0.545828− 0.546| × 10−2

= 0.172× 10−2

Version: 1.5 Page 5

Page 7: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

7 Day 3: Vector and matrix operations

7.1 Try these Matlab Commands1. Use the linspace function to Generate linearly spaced vectors called myvector con-

taining exactly 1000 values with values evenly spaced between -1 and 1. Do not print all1000 values!syntax:y = linspace(a,b,n)What expression will yield the value of the element of myvector? What is this value?Double-click on the variable myvector in the “Current workspace” windowpane to viewit as a vector and confirm its length is 1000.

2. Use the numel function to again confirm the vector has length 1000.3. Produce a plot of a sinusoid on the interval using the command

plot(myvector,sin(2*pi*myvector))4. Define the following vectors and matrices:

rowVec1 = [ -1 -4 -9]

colVec1 =

298

mat1 =1 3 57 9 02 4 6

5. Compute colV ec2 = (pi/4)× colV ec16. The cosine function can be applied to a vector to yield a vector of cosines. ComputecolV ec2 = cos(colV ec2)

7. Note that the values of colVec2 have been overwritten. Are these the values you expect?colV ec3 = colV ec1 + colV ec2

8. Tray to Compute illegal = colV ec1 + rowV ec1;9. The Euclidean norm of a matrix or a vector is available using norm. Compute norm(colV ec3)

10. Compute colvec4 = mat1 * colVec111. A single quote following a matrix or vector indicates a transpose.

mat1Transpose = mat1’rowVec2 = colVec3’Warning: The single quote really means the complex-conjugate transpose (or Hermitianadjoint). If you want a true transpose applied to a complex matrix you must use “.”’.

12. Transposes and Multiplicationmat2 = mat1 * mat1’rowVec3 = rowVec1 * mat1dotProduct = colVec3’ * colVec1euclideanNorm = sqrt(colVec2’ * colVec2)

13. Matrix determinant:determinant = det( mat1 )tr = trace( mat1 )

14. smallest element in a vector rowVec1: min(rowVec1)15. max functions work along one dimension at a time max(mat1)16. compute the max norm of a vector (compose vector and matrix functions) max(abs(rowVec1))17. How to find the single largest element of a matrix?

Version: 1.5 Page 6

Page 8: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

7.2 Exercise 1A magic square is a matrix all of whose row sums, column sums and the sums of the two diago-nals are the same. (One diagonal of a matrix goes from the top left to the bottom right, the otherdiagonal goes from top right to bottom left.) Consider the matrixA=magic(100);The matrix A has 100 row sums (one for each row), 100 column sums (one for each column)and two diagonal sums. These 202 sums should all be exactly the same, and you could verifythat they are the same by printing them and “seeing” that they are the same. It is easy to misssmall differences among so many numbers, though. Instead, verify that A is a magic squareby constructing the 100 column sums (without printing them) and computing the maximum andminimum values of the column sums. Do the same for the 100 row sums, and compute the twodiagonal sums. Check that these six values are the same. If the maximum and minimum valuesare the same, the flyswatter principle says that all values are the same.Hints: Use the Matlab min and max functions.Recall that sum applied to a matrix yields a row vector whose values are the sums of thecolumns. The Matlab function diag extracts the diagonal of a matrix, and the composed func-tion sum(diag(fliplr(A))) computes the sum of the other diagonal. Suppose we want atable of integers from 0 to 13, their squares and cubes. We could start with integers = 0 : 13

but now we’ll get an error when we try to multiply the entries of integers by themselves. squareIntegers =integers ∗ integers

Realize that Matlab deals with vectors, and the default multiplication operation with vectors isrow-by-column multiplication. What we want here is element-by-element multiplication, so weneed to place a period in front of the operator:

squareIntegers = integers. ∗ integers

Now we can define cubeIntegers and fourthIntegers in a similar way.

cubeIntegers = squareIntegers .* integersfourthIntegers = squareIntegers .* squareIntegers

Finally, we would like to print them out as a table. integers, squareIntegers, etc. are row vectors,so make a matrix whose columns consist of these vectors and allow Matlab to print out the wholematrix at once.

tableOfPowers=[integers’, squareIntegers’, cubeIntegers’, fourthIntegers’]

Watch out when you use vectors. The multiplication, division and exponentiation operatorsall have two possible forms, depending on whether you want to operate on the arrays, or on theelements in the arrays. In all these cases, you need to use the period notation to force elementwiseoperations. Compute the squares of the values in integers alternatively using the exponentiationoperator as:sqIntegers = integers .2

and check that the two calculations agree with the command

Version: 1.5 Page 7

Page 9: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

norm(sqIntegers-squareIntegers) which should result in zero.Remark: Addition, subtraction, and division or multiplication by a scalar never require the dot in frontof the operator, although you will get the correct result if you use one.

The index notation can also be used to refer to a subset of elements of the array. With the start:increment:finishnotation, we can refer to a range of indices. Two-dimensional vectors and matrices can be constructedby leaving out some elements of our three-dimensional ones. For example, submatrices an be con-structed from tableOfPowers. (The end function in Matlab means the last value of that dimension.)tableOfCubes = tableOfPowers(:,[1,3])tableOfEvenCubes = tableOfPowers(1:2:end,[1,3])tableOfOddFourths = tableOfPowers(2:2:end,1:3:4)

Note: [1:3:4] is the same as [1,4].You have already seen the Matlab function magic(n). Use it to construct a matrix.A = magic(10)What commands would be needed to generate the four matrices in the upper left quarter AUL, theupper right quarter AUR, the lower left quarter ALL, and the lower right quarter ALR.It is possible to construct vectors and matrices from smaller ones in the same way they can be con-structed from numbers. Reconstruct the matrix A in the previous exercise as

B=[AUL AURALL ALR

;

and show that A and B are the same by showing that norm(A-B) is zero.

7.3 Excercise 2• Flow chart & Mat Lab program of Addition of Matrices using for loops.• Making of Identity Matrix.• Flow chart & Mat Lab program for Multiplication of Matrices• Flow chart & Mat Lab program of Inversion of Matrix .

Version: 1.5 Page 8

Page 10: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

8 Day 4:Transcendental Equations

8.1 The Bisection MethodBisection method is the simplest among all the numerical schemes to solve the transcendental equa-tions. This scheme is based on the intermediate value theorem for continuous functions .Consider a transcendental equation f(x) = 0 which has a zero (solution) in the interval [a, b] andf(a) ∗ f(b) < 0. Bisection scheme computes the zero, say c, by repeatedly halving the interval [a, b].That is, starting with c = (a+ b)/2the interval [a, b] is replaced either with [c, b] or with [a, c] depending on the sign of f(a)× f(c) . Thisprocess is continued until the zero is obtained. Since the zero is obtained numerically the value of cmay not exactly match with all the decimal places of the analytical solution of f(x) = 0 in the interval[a, b]. Hence any one of the following mechanisms can be used to stop the bisection iterations :

• C1. Fixing a priori the total number of bisection iterations N i.e., the length of the intervalor the maximum error after N iterations in this case is less than |b− a|/2N .• C2. By testing the condition |ci− ci− 1| (where i are the iteration number) less than some

tolerance limit, say epsilon, fixed a priori.• C3. By testing the condition |f(ci)| less than some tolerance limit alpha again fixed a priori.

8.1.1 Algorithm - Bisection Scheme

Algorithm 1 Algorithm - Bisection SchemeData: Given a function f(x) continuous on an interval [a, b] and f(a) ∗ f(b) < 0doc = (a+ b)/2if f(a) ∗ f(c) < 0 then

b = celse

a = cendwhile none of the convergence criteria C1, C2 or C3 is satisfied

8.1.2 Numerical Example :

Find a root of f(x) = 3x + sin(x) − exp(x) = 0. Consider the function f (x) in the interval [0, 0.5]since f (0) * f (0.5) is less than zero.Then the bisection iterations are given bySo one of the roots of 3x+ sin(x)− exp(x) = 0 is approximately 0.3605.

Iteration No. a b c f(a) ∗ f(c)1 0 0.5 0.25 0.287 (+ve)2 0.25 0.5 0.393 -0.015 (-ve)3 0.65 0.393 0.34 9.69 E-3 (+ve)4 0.34 0.393 0.367 -7.81 E-4 (-ve)5 0.34 0.367 0.354 8.9 E-4 (+ve)6 0.354 0.367 0.3605 -3.1 E-6 (-ve)

You may use a function signum

Version: 1.5 Page 9

Page 11: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

8.2 Regula-Falsi Method or False Position Scheme

Algorithm 2 Algorithm - False Position SchemeData: Given a function f(x) continuous on an interval [a, b] and f(a) ∗ f(b) < 0doc = af(b)−bf(a)

f(b)−f(a)if f(a) ∗ f(c) < 0 then

b = celse

a = cendwhile none of the convergence criteria C1, C2 or C3 is satisfied

8.3 The Newton–Raphson MethodIn numerical analysis, Newton’s method (also known as the Newton–Raphson method), named afterIsaac Newton and Joseph Raphson, is a method for finding successively better approximations to theroots (or zeroes) of a real-valued function.

x : f(x) = 0,The Newton–Raphson method in one variable is implemented as follows:Given a function f defined over the reals x, and its derivative f ′, we begin with a first guess x0 for aroot of the function f. Provided the function satisfies all the assumptions made in the derivation of theformula, a better approximation x1 isx1 = x0 − f(x0)

f ′(x0),

Geometrically, (x1, 0) is the intersection with the x-axis of the tangent to the graph of f at (x0, f(x0)).The process is repeated asxn+1 = xn − f(xn)

f ′(xn),

until a sufficiently accurate value is reached. Rate of Convergence of Newton–Raphson Method

8.4 Exercise

1. Find the real root of the equations x3 − 2x− 5 = 0, using bisection method.2. Find the real root of the equations x3 − x− 4 = 0 by bisection method.3. Find the real root of the equations x4 − x− 10 = 0 by bisection method.4. Find the real root of the equations x3 − 3x− 5 = 0 by bisection method.5. Find the real root of the equations x2 − 5x+ 2 = 0 by bisection method.6. Find the smallest positive root of the equation x3 − 2x + 0.5 = 0 by Newton- Raphson

Method.7. Find the root between (2, 3) of x3 − 2x− 5 = 0, by Regula Falsi method8. Find a root which lies between 1and 2 of f(x) = x3 + 2x2 + 10x − 20 by Regula Falsi

method9. Find the root of the equation x3 − 5x − 7,that lies between 2 and 3 , correct to 4 decimal

places, using the method of Regula Falsi method.10. Find the root of the equation 4sinx = ex that lies between 0 and 0.5. correct to 4 decimal

places, using the method of Regula Falsi method.11. Find the smallest positive root of the equation 3x3 − 9x2 + 8 = 0, correct to 4 places to

decimals, using Newton-Raphson methods.

Version: 1.5 Page 10

Page 12: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

12. Find the real root of the equation by bisection method. x3 − x− 4 = 013. Find the real root of the equation by bisection method x4 − x− 10 = 014. Find the real root of the equation by bisection method x3 − 3x− 5 = 015. Find the real root of the equation by bisection method x2 − 5x+ 2 = 016. Find the smallest positive root of the equation x3 − 2x + 0.5 = 0 by Newtons- Raphson

Method17. Find the root between (2, 3) of x3 − 2x− 5 = 0, by Regula Falsi method18. Find a root which lies between 1and 2 of f(x) = x3 + 2x2 + 10x − 20 by Regula Falsi

method

Version: 1.5 Page 11

Page 13: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

9 Linear Algebraic EquationsSimulation of linear Algebraic equations are very common in various of engineering and science.In general Matrix invertion method or CRAMER rules are used to solve there equations, but thesemethods involve a great amount of complexity, when the system of equations holds a large amount ofunknowns.There are two types of numerical methods to solve linear Algebraic equations are shown bellow:

1. Direct Methods: The unknowns will be eliminated one by one and hence the given systemof equation will be transformed into equivalent upper-triangular matrix.• Gauss Elimination Method• Gauss Jardan Method• Triangularisation or Factorisation Method.

2. Indirect Methods: Solution is obtained by successive approximation.• Gauss Seidel Method• Gauss Jacobi Method• Relaxation Method

9.1 Gauss Elimination MethodThe unknowns are eliminated one by one by transforming the given system of equation into equivalentsystem with upper-triangular matrix.Solving of a system of linear algebraic equations appears frequently in many engineering problems.Most of numerical techniques which deals with partial differential equations, represent the governingequations of physical phenomena in the form of a system of linear algebraic equations. Gauss elimi-nation technique is a well-known numerical method which is employed in many scientific problems.Consider an arbitrary system of linear algebraic equations as follows:a11x1 + a12x2 + . . . + a1nxn = c1a21x1 + a22x2 + . . . + a2nxn = c2...an1x1 + an2x2 + . . . + annxn = cn

where xi are unknowns and aij are coefficients of unknowns and ci are equations’ constants. Thissystem of algebraic equation can be written in the matrix form as follows:[A]x = CWhere [A] is the matrix of coefficient.x is the vector of unknowns and C is the vector of constants.Gauss elimination method eliminate unknowns’ coefficients of the equations one by one. Therefore thematrix of coefficients of the system of linear equations is transformed to an upper triangular matrix.The last transformed equation has only one unknown which can be determined easily. This evaluatedunknown can be used in the upper equation for determining the next unknown and so on. Finally thesystem of linear equations can be solved by back substitution of evaluated unknowns. For exampleconsider the following system of algebraic equations:

Version: 1.5 Page 12

Page 14: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

System of equations Row operations Augmented matrix2x+ y − z = 8

3x− y + 2z = −11

−2x+ y + 2z = −3

2 1 −1 8−3 −1 2 −11−2 1 2 −3

2x+ y − z = 8

1

2y +

1

2z = −1

2y + z = 5

L2 +3

2L1 → L2

L3 + L1 → L3

2 1 −1 80 1

212

10 2 1 5

2x+ y − z = 8

1

2y +

1

2z = −1

−z = 1

L3 + (−4)L2 → L3

2 1 −1 80 1

212

10 0 −1 1

Now the matrix is in triangular(echelon) form

2x+ y = 7

1

2y =

3

2−z = 1

L2 +1

2L3 → L2

L1 − L3 → L1

2 1 0 70 1

20 3

2

0 0 −1 1

2x+ y = 7

y = 3

z = −1

L2 → L2

−L3 → L1

2 1 0 70 1 0 30 0 1 −1

x = 2

y = 3

z = −1

L1 − L2 → L1

1

2L1 → L1

1 0 0 20 1 0 30 0 1 −1

The algorithm of the Gauss elimination method can be written as follows:

Algorithm 3 Algorithm - Gauss elimination method1: For k = 1 to n− 1 do step 2 to step 52: For i = k + 1 to n do step 3 to step 53: For j = k + 1 to n do step 44: aij = aij–aik × akj/akk5: ci = ci–aik × ck/akk6: xn = cn/ann7: For i = n− 1 to 1 do step 8 to step 118: s = 09: For j = i+ 1 to n do step 10

10: s = s+ aij × xj11: xi = (ci − s)/aii12: End.

ILL Condition of Linear System: If a small change of value of the coefficient results a large change incalculated value of the unknowns then the system is in Ill condition.

9.2 Exercise

Version: 1.5 Page 13

Page 15: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

10 Interpolation and Approximation

10.1 NEWTON’S DIVIDED DIFFERENCE FORMULALet (x0, y0), (x2, y2), (x3, y3), .....(xn, yn) be given set of (n+ 1) points. The 1st divided difference aredefined by the following relation:

[xn, xn] =(

yn−yn−1

xn−xn−1

)

and

The tabular representation of divided difference is as

x y ∆ ∆2 ∆3 ∆4

x0 y0

[x0, x1]x1 y1 [x0, x1, x2]

[x0, x2] [x0, x1, x2, x3]x2 y2 [x1, x2, x3] [x0, x1, x2, x3, x4]

[x2, x3] [x1, x2, x3, x4]x3 y3 [x2, x3, x4]

[x3, x4]x4 y4

Newton’s Divided Difference FormulaLet f(x) be a function taking the values y0, y1, ..........yn crossponding to the values x0, x1, x3, .........xn,then Newton’s Divided Difference Formula is given by:y = f(x) = f(x0) + (x − x0)[x0, x1] + (x − x0)(x − x1)[x0, x1, x2] + (x − x0)(x − x1)(x −x2)[x0, x1, x2, x3] + ......+ (x− x0)(x− x1)(x− x2)....(x− xn−1)[x0, x1, x2, .........xn]

10.2 Exercise1. Write Program for Divided Difference Method2. Write Program for Newton Forward Method3. Write Program for Newton Forward Method4. Using the following data, find f(x) a polynomial in (x) and hence find f(4)

x : 0 1 2 5y : 2 3 12 147

Answer : 78

Version: 1.5 Page 14

Page 16: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

11 Numerical Integration

11.1 Excercise1. Write Program for Trapezoidal Rule2. Write Program for Simpson’s 1

3Rule

3. Write Program for Simpson’s 38

Rule

Version: 1.5 Page 15

Page 17: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

12 Numerical Solution of Ordinary Differential Equation

12.1 Excercise1. Write Program for Taylor’s Method2. Write Program for Euler’s Method3. Write Program for Improved Euler’s Method4. Runge Kutta Method

Version: 1.5 Page 16

Page 18: Numerical methods lab manual

Faculty: Bhaskar Mondal, Email:[email protected] Numerical Methods Lab. Manual

T1 Steven Chapra, Raymond Canale Applied Numerical Methods with MATLAB for Engineersand Scientists, 3/e, McGraw-Hill Higher Education

T2 B. S. Grewal Numerical Methods in Engineering and Science with Programs in C and C+ +Khanna Press ISBN Number: 817409248X, 9788174092489, 978-8174092489.

Evaluation Scheme

ECNo.

EvaluationComponent

Duration WeightageData &Time

Nature ofComponent

You May Meet Me:Every day 5:00pm.You may mail me at [email protected]; (always mention your Roll Number followed by Subject atthe subject field.)

Version: 1.5 Page 17