Lab_Num

November 13, 2017 | Author: Navjot Wadhwa | Category: Numerical Analysis, Interpolation, Eigenvalues And Eigenvectors, Equations, Computational Science
Share Embed Donate


Short Description

lab manual for matlab...

Description

NUMERICAL ANALYSIS MATLAB Practicals (Winter 2017) B.E. IV Semester Thapar University Patiala

Name:. . . . . . . . . . . . . . . Roll No:. . . . . . . . . . . . . . . Group:. . . . . . . . . . . . . . . . . . Instructor:. . . . . . . . . . . . . . .

Contents S.No. Experiment 1. Find the root of non-linear equation f (x) = 0 using bisection method. 2. Find the root of non-linear equation f (x) = 0 using fixed-point iteration methods. 3. Find the root of non-linear equation f (x) = 0 using Newton and secant methods. 4. Solve system of linear equations using Gauss elimination method. 5. Solve system of linear equations using Gauss-Seidel and SOR iterative method. 6. Find a dominant eigen-value and associated eigenvector by Power method. 7. Implement Lagrange interpolating polynomials of degree ≤ n on n + 1 discrete data points. 8. Implement Newton divided difference interpolating polynomials for n + 1 discrete data points. 9. Fit a curve for given data points by using principle of least squares. 10. Integrate a function numerically using composite trapezoidal and Simpson’s rule. 11. Find the solution of initial value problem using modified Euler and Runge-Kutta (fourth-order) methods.

Date

Signature

Numerical Analysis

UMA007

Winter 2017

Experiment 1 : Bisection Method

1. Algorithm: To determine a root of f (x) = 0 that is accurate within a specified tolerance value , given values a and b such that f (a) f (b) < 0. a+b Define c = . 2 If f (a) f (c) < 0, then set b = c, otherwise a = c. End if. Until |a − b| ≤  (tolerance value). Print root as c. Stopping Criteria: Since this is an iterative method, we must determine some stopping criteria that will allow the iteration to stop. Criterion |f (ck )| very small can be misleading since it is possible to have |f (ck )| very small, even if ck is not close to the root. b−a The interval length after N iterations is N . So, to obtain an accuracy of , we must have 2 N≥

log(b − a) − log  . log 2

2. Students are required to write a program and implement it on the following examples. √ (i) Use bisection method in computing of 29. (ii) Use the bisection method to find the indicated root of the following equations. Use an error tolerance of  = 0.0001. (a) The real root of x3 − x2 − x − 1 = 0. (b) The smallest positive root of cos x = 1/2 + sin x. (c) The real roots of x3 − 2x − 1 = 0. (iii) Determine the number of iterations necessary to solve f (x) = x3 + 4x2 − 10 = 0 with accuracy 10−3 using a = 1 and b = 2 and hence find the root with desired accuracy. Sol.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

3. An object falling vertically through the air is subjected to viscous resistance as well as to the force of gravity. Assume that an object with mass m is dropped from a height s0 and that the height of the object after t seconds is mg m2 g s(t) = s0 − t + 2 (1 − e−kt/m ), k k where g = 32.17 ft/s2 and k represents the coefficient of air resistance in lb-s/ft. Suppose s0 = 300 ft, m = 0.25 lb, and k = 0.1 lb-s/ft. Find, to within 0.01 s, the time it takes this quarter-pounder to hit the ground. Sol.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

Experiment 2 : Fixed-Point Iterations

1. Write an algorithm for fixed-point iteration method to compute the roots of the given non-linear equation f (x) = 0. Algorithm: To find a solution to x = g(x) given an initial approximation x0 :. Input: Initial approximation x0 , tolerance value , maximum number of iterations N . Output: Approximate solution α or message of failure. Step 1: Set i = 1. Step 2: While i ≤ N do Steps 3 to 6. Step 3: Setx = g(x0 ). (Compute xi .) Step 4: If |x − x0 | ≤  then OUTPUT x; (The procedure was successful.) STOP. Step 5: Set i = i + 1. Step 6: Set x0 =x. (Update x0 .) Step 7: Print the output and STOP. 2. Students are required to write a program and implement it on the following examples. √ (i) Compute 29. (ii) Find smallest and second smallest positive roots of the equation tan x = 4x, correct to 4 decimal places. (iii) Use a fixed-point iteration method to determine a solution accurate to within 10−2 for 2 sin πx+x = 0 on [1, 2]. Use initial guess x0 = 1. Sol.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

3. The equation f (x) = x3 + 4x2 − 10 = 0 has a unique root in [1, 2]. There are many ways to change the equation to the fixed-point form x = g(x) using simple algebraic manipulation. Let g1 , g2 , g3 , g4 , g5 are iteration functions obtained by the given function, then check which of the following iteration functions will converge to the fixed point? 3 2 (a) g1 (x) = x q− x − 4x + 10 10 x

− 4x √ 3 (c) g3 (x) = 0.5 r 10 − x 10 (d) g4 (x) = 4+x x3 + 4x2 − 10 (e) g5 (x) = x − . 3x2 + 8x

(b) g2 (x) =

Sol.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

Experiment 3 : Newton and Secant Methods

1. Write an algorithm for Newton and Secant method to compute the roots of the given non-linear equations. Algorithm for Newton method: To find a solution to f (x) = 0, given an initial approximation x0 . Input: Initial approximation x0 , tolerance value , maximum number of iterations N . Output: Approximate solution x or message of failure. Step 1: Set i = 1. Step 2: While i ≤ N do Steps 3 to 6. f (x0 ) Step 3: Setx = x0 − . (Compute xi .) df (x0 ) Step 4: If |x − x0 | ≤  then OUTPUT x; (The procedure was successful.) STOP. Step 5: Set i = i + 1. Step 6: Set x0 =x. (Update x0 .) Step 7: Output (’The method failed after N iterations, N =’, N ); (The procedure was unsuccessful.) STOP. Algorithm for secant method:

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

2. Students are required to write a program and implement it on the following examples. √ (i) Use secant method in computing of 17. (ii) Use Newton method to find the indicated root of the following equations. Use an error tolerance of  = 0.00001. (a) Find the root of cos x − x exp(x) = 0 (b) The smallest positive root of cos x = 1/2 + sin x. (c) The root of exp(−x)(x2 + 5x + 2) + 1 = 0. Take initial guess −2.0 and −1.0. (iii) Using Newton Method to find a non-zero solution of x = 2 sin x with an accuracy 10−3 . (d) Solve the equation 4x2 − ex − e−x = 0 which has two positive solutions x1 and x2 . Use Newton’s method to approximate the solution to within 10−5 with the following values of x0 : x0 = −10, −5, −3, 0, 1, 3, 5, 10. Sol.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

3. The accumulated value of a savings account based on regular periodic payments can be determined from the annuity due equation, P A = [(1 + i)n − 1]. i In this equation, A is the amount in the account, P is the amount regularly deposited, and i is the rate of interest per period for the n deposit periods. An engineer would like to have a savings account valued at $750, 000 upon retirement in 20 years and can afford to put $1500 per month toward this goal. What is the minimal interest rate at which this amount can be invested, assuming that the interest is compounded monthly? Sol.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

Experiment 4 : Gauss Elimination

1. Algorithm: Given a system of linear equations: Ax = b. In order to obtain the solution of the above system of equations by Gauss elimination method we use the following algorithm: • Read the order of the coefficient matrix A • Read the augmented matrix [A|B] • Calculate the multiplier m and apply the Gaussian elimination using the following loop: for i=1 to (n-1) for j=i+1 to n m = a[j][i]/a[i][i] for k=1 to (n+1) a[j][k]=a[j][k]-m*a[i][k] end k end j end i • Calculate xn and xi , i = n − 1, n − 2, . . . , 2: x[n]=a[n][n+1]/a[n][n] for i=(n-1) to 2 for j=i+1 to n sum = sum + a[i][j]*x[j] x[i]= (a[i][n+1]-sum)/a[i][i] end i • Print the solution xi , i = 0, 1, 2, . . . , n 2. Use Gaussian elimination method to find the solution of the following system of equations:

10x + 8y − 3z + u = 16 2x + 10y + z − 4u = 9 3x − 4y + 10z + u = 10 2x + 2y − 3z + 10u = 11 Sol.

c All rights reserved.

Numerical Analysis

UMA007

3. Solve the following linear system πx1 +



2x2 − x3 + x4 = 0

ex1 − x2 + x3 + 2x4 = 1 √ x1 + x2 − 3x3 + x4 = 2 √ −x1 − x2 + x3 − 5x4 = 3. Sol.

c All rights reserved.

Winter 2017

Numerical Analysis

UMA007

Winter 2017

4. Kirchhoff’s laws of electrical circuits state that both the net flow of current through each junction and the net voltage drop around each closed loop of a circuit are zero. Suppose that a potential of V volts is applied between the points A and G in the circuit and that i1 , i2 , i3 , i4 , and i5 represent current flow as shown in the diagram. Using G as a reference point, Kirchhoff’s laws imply that the currents satisfy the following system of linear equations: 5i1 + 5i2 = V, i3 − i4 − i5 = 0, 2i4 − 3i5 = 0, i1 − i2 − i3 = 0, 5i2 − 7i3 − 2i4 = 0. By taking V = 5.5, solve the system.

Sol.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

Experiment 5 : Gauss-Seidel and SOR

1. Algorithm: Given a system of linear equations: Ax = B. In order to obtain the solution of the above system of equations by Gauss-Seidel method we use the following algorithm: (a) Read the order of the coefficient matrix A. (b) Read the augmented matrix [A|B]. (c) Set x[i] = 0, i = 1, 2, . . . , n. (d) Read the tolerance tol. (e) Set xold[i] = x[i], i = 1, 2, . . . , n. (f) Apply the Gauss-Seidel iteration as follows: for i=1 to n for j=1 to n, j 6= i sum = sum + a[i][j]*x[j] end j x[i]=(a[i][n+1]-sum)/a[i][i] (g) Repeat step-5 and 6 until |x[i] − xold[i]| < tol. (h) Print the solution x[i], i = 0, 1, 2, . . . , n. 2. Write (or modify the above) algorithm for SOR method.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

3. Solve this system of equations by Gauss-Seidel starting with the initial vector [0,0,0]: 4.63x1 − 1.21x2 + 3.22x3 = 2.22 −3.07x1 + 5.48x2 + 2.11x3 = −3.17 1.26x1 + 3.11x2 + 4.57x3 = 5.11. Sol. 4. Use the SOR method with ω = 1.2 to solve the linear system with a tolerance 10−3 in the k.k∞ norm. 4x1 + x2 − x3 + x4 = −2 x1 + 4x2 − x3 − x4 = −1 −x1 − x2 + 5x3 + x4 = 0 x1 − x2 + x3 + 3x4 = 1. Sol.

c All rights reserved.

Numerical Analysis

UMA007

Experiment 6 : Power Method

1. Algorithm: (a) Start (b) Define matrix A and initial guess x (c) Calculate y = Ax (d) Find the largest element in magnitude of matrix y and assign it to K. (e) Calculate fresh value x = (1/K) ∗ y (f) If |K(n) − K(n − 1)| > error, goto step 3. (g) Stop 2. Determine the largest eigenvalue and the corresponding eigenvector of the matrix   4 1 0 1 20 1 0 1 4 using the power method. Use x0 = [1, 1, 1]T . Sol.

c All rights reserved.

Winter 2017

Numerical Analysis

UMA007

Winter 2017

3. Find the first three iterations obtained by the Power method applied to the following matrix.   1 1 0 0 1 2 0 1   0 0 3 3 0 1 2 3 Use x0 = [1, 1, 0, 1]T . Solution.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

Experiment 7 : Lagrange Interpolation

1. Algorithm: Given a set of function values: x f (x)

x0 f (x0 )

x1 f (x1 )

··· ···

xn f (xn )

To approximate the value of a function f (x) at x = p using Lagrange’s interpolating polynomial Pn (x) of degree n, given by Pn (x) = l0 (x)f (x0 ) + l1 (x)f (x1 ) + l2 (x)f (x2 ) + . . . + ln (x)f (xn ) n Y

where li (x) =

j=0;i6=j

(p − xj ) , we use the following algorithm: (xi − xj )

• Read the degree n of the polynomial Pn (x) • Read the values of x[i] and f [i], i = 0, 1, . . . , n • Read the point of interpolation p • Calculate the Lagrange’s fundamental polynomials li (x) using the following loop: for i=0 to n l[i] = 1.0 for j=0 to n if j 6= i p − x[j] l[i] = l(i) x[i] − x[j] end j end i • Calculate the approximate value of the function at x = p using the following loop: sum=0.0 for i=0 to n sum = sum + l[i] ∗ f [i] end i • Print sum.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

2. Use Lagrange’s interpolation formula to approximate the value of f (0.43), given that x f (x)

0 1

0.25 1.64872

0.5 2.71828

0.75 4.48169

Sol.

3. Use Lagrange’s interpolation formula to approximate the value of f (0.56) for the following functions, and find a bound for the absolute error on the interval [x0 , xn ]. a. f (x) = e2x cos 3x, x0 = 0, x1 = 0.3, x2 = 0.6, n = 2. b. f (x) = cos x + sin x, x0 = 0, x1 = 0.25, x2 = 0.5, x3 = 1.0, n = 3. Sol.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

4. A census of the population of the United States is taken every 10 years. The following table lists the population, in thousands of people, from 1950 to 2000, and the data are also represented in the figure. Year Population(in thousands)

1950 151326

1960 179323

1970 203302

1980 226542

1990 249633

2000 281422

a. Use Lagrange interpolation to approximate the population in the years 1940, 1975, and 2020. b. The population in 1940 was approximately 132, 165, 000. How accurate do you think your 1975 and 2020 figures are? Sol.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

Experiment 8 : Newton Interpolation

1. Algorithm Write the algorithm for Newton divided difference interpolation and then apply for the given examples on next page.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

2. The following data represents the function f (x) = ex . x f (x)

1 2.7183

1.5 4.4817

2.0 7.3891

2.5 12.1825

Estimate the value of f (2.25) using the Newton’s interpolation. Compare with the exact value. Sol. 3. The following data are parts of a table for function g(x) =

sin x . x2

x g(x)

0.4 2.4339

0.1 9.9833

0.2 4.9667

0.3 3.2836

0.5 1.9177

Calculate g(0.25) as accurately as possible (a) by interpolating directly in this table (also print the entires of divided differences), (b) by first calculating xg(x) and then interpolating directly in that table, (c) explain the difference between the results obtained in (a) and (b), respectively. Solution.

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

Experiment 9 : Least Square Approximation

√ B 1. Write an algorithm for least square approximations to fit any curve (e.g. a + bx + cx2 , A x + , · · · ) x Algorithm:

c All rights reserved.

Numerical Analysis

UMA007

Winter 2017

2. Hooke’s law states that when a force is applied to a spring constructed of uniform material, the length of the spring is a linear function of that force written as F (l) = k(l − E). The function F is the force required to stretch the spring l units, where the constant E = 5.3 is the length of the unstretched spring in inches and k is the spring constant. a. Suppose measurements are made of the length l, in inches, for applied weights F (l), in pounds, as given in the following table. F (l) 2 4 6

l 7.0 9.4 12.3

Find the least squares approximation for k. b. Additional measurements are made, giving more data: F (l) 3 5 8 10

l 8.3 11.3 14.4 15.9

Compute the new least squares approximation for k. Which of (a) or (b) best fits the total experimental data? Sol. 3. By the method of least square fit a curve of the form y = axb to the following data. x y

2 27.8

3 62.1

4 110

Sol.

c All rights reserved.

5 161

Numerical Analysis

UMA007

Winter 2017

Experiment 10 : Numerical Quadrature

1. Algorithm (Composite trapezoidal rule): Step 1 : Inputs: function f (x); end points a and b; and N number of subintervals. Step 2 : Set h = (b − a)/N . Step 3 : Set sum = 0 Step 4 : For i = 1 to N − 1 Step 5 : Set x = a + h ∗ i Step 6 : Set sum = sum+2 ∗ f (x) end Step 7 : Set sum = sum+f (a) + f (b) Step 8 : Set ans = sum∗(h/2) End 2. Algorithm (Composite Simpson rule): Step 1 : Inputs: function f (x); end points a and b; and N number of subintervals (even). Step 2 : Set h = (b − a)/N . Step 3 : Set sum = 0 Step 4 : For i = 1 to N − 1 Step 5 : Set x = a + h ∗ i Step 6 : If i%2 = 0 sum = sum+2 ∗ f (x) else sum = sum+4 ∗ f (x) end Step 7 : Set sum = sum+f (a) + f (b) Step 8 : Set ans = sum∗(h/3) End 3. Approximate the following integrals using the composite trapezoidal and Simpson rule by taking 0.25 e+1 R R 1 different subintervals (e.g. 4, 6, 10, 20). a. I = (cos x)2 dx b. dx. c. e x ln x −0.25 R 1 −x2 cos x dx. −1 e Sol.

4. The length of the curve represented by a function y = f (x) on an interval [a, b] is given by the integral Z bp 1 + [f 0 (x)]2 dx. I= a

Use the trapezoidal rule and Simpson’s rule with 4 and 8 subintervals compute the length of the curve y = tan−1 (1 + x2 ), 0 ≤ x ≤ 2. Sol.

c All rights reserved.

Numerical Analysis

UMA007

5. The equation Z 0

x

Winter 2017

1 2 √ e−t /2 dt = 0.45 2π

can be solved for x by applying Newton’s method to the function Z x 1 1 2 2 √ e−t /2 dt − 0.45 & f 0 (x) = √ e−x /2 . f (x) = 2π 2π 0 Note that Newton’s method would require the evaluation of f (xk ) at various xk which can be estimated using a quadrature formula. Find a solution for f (x) = 0 with error no more than 10−5 using Newton’s method starting with x0 = 0.5 and by means of the composite trapezoidal and Simpson’s rule.

c All rights reserved.

Numerical Analysis

UMA007

Experiment 11

1. Algorithm (Modified Euler’s method):

2. Algorithm (Runge-Kutta fourth-order method):

c All rights reserved.

Winter 2017

Numerical Analysis

UMA007

Winter 2017

3. Solve the following differential equation by the modified Euler’s method y 0 = −y + 2 cos t, y(0) = 1. Compute solution in the interval [0, 1] with mesh length 0.2. Sol.

4. Water flows from an inverted conical tank with circular orifice at the rate √ p x dx 2 = −0.6πr 2g , dt A(x) where r is the radius of the orifice, x is the height of the liquid level from the vertex of the cone, and A(x) is the area of the cross section of the tank x units above the orifice. Suppose r = 0.1 ft, g = 32.1 ft/s2 , and the tank has an initial water level of 8 ft and initial volume of 512(π/3)ft3 . Use the Runge-Kutta method of order four to find the following. a. The water level after 10 min with h = 20 s. b. When the tank will be empty, to within 1 min. Sol.

5. The following system represent a much simplified model of nerve cells dx dt dy dt

= x + y − x3 , x(0) = 0.5 x = − , y(0) = 0.1 2

where x(t) represents voltage across the boundary of nerve cell and y(t) is the permeability of the cell wall at time t. Solve this system using Runge-Kutta fourth-order method to generate the profile up to t = 0.2 with step size 0.1. Sol.

c All rights reserved.

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF