Numerical Integration



Syllabus

Numerical Methods Lab

Code: CS 382

Contact: 3P

Credit: 2

Assignments on Interpolation: Newton forward & backward, Lagrange

Assignments on Numerical Integration: Trapezoidal Rule, Simson’s 1/3 Rule, Weddle’s Rule

Assignments on Numerical solution of a system of linear equation: Gauss elimination, Gauss Jacobi, Matrix Inversion, Gauss Seidal

Assignments on Algebric Equation: Bisection, Secant, Regular-falsi, Newton Raphson

Assignments on Ordinary Differential Equation: Taylor Series, Euler’s method, Runga-Kutta

Assignments on Statistical Problem: Mean, Median, Mode, Standard deviation ( for simple & frequency type data), Correlation & Regression

SOLUTION OF ALGEBRIC AND TRANSCENDENTAL EQUATION

Experiment - 1

Bisection Method

INTRODUCTION:

In Engineering practice, it is necessary to find out the roots of equations in the form f(x) = 0. If it is a quadratic / cubic / biquadrate expressions, then the formulae are available to find out the roots. But, when it is polynomial of higher degree or an expression involving the transcendental functions or combinations of all, such as, 1+ex, 1+sinx+4x, x tan x-ex, x2-ex etc. For this, algebraic methods are not available, hence to find out the roots, we have to try some other approximate methods. These approximate methods are called as Numerical Methods.

BISECTION METHODS/HALF INTERVAL SEARCH METHOD:

This methods is based on theorem, that if a function f(x) is continuous between a and b, and f(a) * f(b) < 0 i.e. f(a) and f(b) are of opposite signs, then there is exits of at least one root between a and b. Its approximate value will be given by, x0=(a+b)/2. If f(x0) = 0, we conclude that x0 is a root of equation f(x) = 0. Otherwise the root lies either between x0 and b or between a and x0, depending on whether f(x0) is –ve or +ve. (Considering f(a) is –ve and f(b) is +ve).

HARDWARE AND SOFTWARE:

Hardware: Desktop, minimum 128MB RAM, a suitable HDD

Software: Windows XP/Linux, Turbo C/ C++ or Borland C/ C++ compiler

Problem:

Find the root of x3-x-4 = 0 using Bisection method upto accuracy 0.001.

Solution:

f(x) = x3-x-4

f(0) = -4, f(1) = -4. f(2) = 2.

Here, f(a) = -ve and f(b) = +ve

Root lies between interval [a,b] i.e. [1,2]

Mid point of the interval is x0 = (a+b)/2 = (1+2)/2 = 1.5

So, f(1.5) = -2.215, this new value is giving –ve answer of f(x), so replacing the value which is already giving –ve answer of f(x) from previous interval by this new value giving –ve value of f(x) to get close interval than previous interval. Hence new interval [1.5,2]. Repeat this procedure,we get

|Iteration |Value of a |Value of b | | | |

|No. |(of which |(of which |x0 = (a+b)/2 |f(x0) |[a-b] |

| |f(a) is –ve) |f(a) is –ve) | | | |

|1. |1 |2 |1.5 |-2.125 |1 |

|2. |1.5 |2 |1.75 |-0.39 |0.5 |

|3. |1.75 |2 |1.875 |0.716 |0.25 |

|4. |1.75 |1.875 |1.8125 |0.141846 |0.125 |

|5. |1.75 |1.8125 |1.78125 |-0.129608 |0.0625 |

|6. |1.78125 |1.8125 |1.796875 |0.004803 |0.03125 |

|7. |1.78125 |1.796875 |1.789062 |-0.062730 |0.015625 |

|8. |1.789062 |1.796875 |1.792969 |-0.029046 |0.0078125 |

|9. |1.792069 |1.796875 |1.794922 |-0.012142 |0.0039063 |

|10. |1.794922 |1.796875 |1.795898 |-0.003675 |0.001953 |

Number of iterations required to achieve accuracy 0.001 is,

[pic]

Here accuracy of 0.001953 is achieved at the end of 10th iteration; hence value of root is 1.795898

Algorithm for Bisection Method:

Define f(x) =

Enter the desired accuracy, e

do

{

Read a,b

} while (f(a) * f(b) > 0)

k = 0

do

{

x0 = (a+b)/2

if (f(a) * f(b) < 0) then

b = x0

else

a = x0

endif

k = k + 1

} while (|b-a|) > e)

Print the value of root, x0 and no. of iterations performed, k.

Stop.

Program for Bisection Method:

/* Bisection Method */

#include

#include

#include

/*function*/

float f(float x)

{

float f;

/* f=x*x*x-cos(x)*cos(x); */

f=pow(2.7182828,x)-3*cos(x)+1;

return(f);

}

/* Main Program */

main()

{

float a,b,e,x0,N;

int k;

clrscr();

printf(“\n Enter accuracy”);

scanf(“%f”,&e);

do

{

printf(“\n Enter interval a,b”);

scanf(“%f%f”,&a,&b);

}while(f(a)*f(b)>0.0);

k=0;

do

{

x0=(a+b)/2.0;

printf(“\nI=%d a=%f b=%f x0=%f f(x0)=%f |b- a|=%f”,k+1,a,b,x0,f(x0),fabs(b-a));

if (f(a)*f(x0)e);

printf(“\n Root of the equation is %f”,x0);

printf(“\n Actual no.of iterations required to achieve desired accuracy,%d”,k);

getch();

}

Output:Enter accuracy 0.001

Enter interval a, b 1 2

I= a= b= x0= f(x0)= |b-a|=

Root of the equation 1.7968

Actual no.of iterations required to achieve desired accuracy 10

Experiment 2

FALSE POSITION METHOD / REGULA FALSI METHOD

In this method initial interval is chosen such that, f(x0)*f(x1)0)

k = 0

do{

[pic]

if (f(x0)*f(x2)e)

Print the value of root, x2 and no. of iterations performed, k.

Stop.

Program for False Position Method:

/* False Position Method */

#include

#include

#include

/* function */

float f(float x)

{

float f;

/* f = (x*x*x)-x-4.0; */

/* f = sin(x)-x+2; */

f =tan(x)+tanh(x);

/* f = pow(2.7182818,x)-3*cos(x)+1; */

return(f);

}

/* Main Program */

main()

{

float x1,x2,e,x0,N;

int k;

clrscr();

printf(“\n Enter accuracy”);

scanf(“%f”,&e);

do

{

Printf(“\n Enter interval x0,x1”);

Scanf(“%f%f”,&x0,&x1);

}while(f(x0)*f(x1)>0.0);

k=0;

do

{

x2 =x0 – ((f(x0)*(x1-x0))/(f(x1)-f(x0)));

printf(“\nI=%d x0=%f x1=%f x2=%f f(x2)=%f f(x0)=%f f(x1)=%f”, k+1,x0,x1,x2,f(x2),f(x0),f(x1));

if (f(x0)*f(x2)e);

printf(“\n Root of the equation is %f”, x2);

printf(“\n Actual no. of iteration required to achieve desired accuracy, %d”, k);

getch();

}

Output:

Enter accuracy 0.0001

Enter interval x0,x1 1 2

I= x0= x1= x2= f(x2)= f(x0)= f(x1)=

Root of the equation is 1.796297

Actual no. of iterations required to achieve desired accuracy 5

Experiment - 3

NEWTON-RAPHSON METHOD

In this method the real root of the equation f(x) = 0 can be computed rapidly, when the derivative of f(x) can be easily found and is a simple expression. When an approximate value of a real root of an equation is known, a closer approximation to the root can be obtained by an iterative process, as explained below:

Let x0 be an approximate value of a root of the equation f(x) = 0.

Let x1 be the exact root closer to x0, so that x1 = x0 + h, where h is small.

Since x1 is the exact root of f(x) = 0, we have f(x1) = 0, i.e., f(x0 + h) = 0

i.e., [pic], by Taylor’s theorem

Since h is small, h2 and higher powers of h may be omitted.

Hence [pic] approximately

[pic] [pic] approximately

[pic] [pic] approximately.

The value of x1 thus obtained will be a closer approximation to the actual root of f(x) = 0 than x0.

Taking x1 as an approximate value of the root, a still better approximation x2 can be obtained by using the formula

[pic]

The iterative process is continued until we get the required accuracy, i.e., until |xn + 1 – xn| is less than a prescribed small value.

The iterative formula

[pic]

is called the Newton-Raphson formula.

HARDWARE AND SOFTWARE:

Hardware: Desktop, minimum 128MB RAM, a suitable HDD

Software: Windows XP/Linux, Turbo C/ C++ or Borland C/ C++ compiler

Problem:

Find the root of x3-x-4 = 0, using Newton Raphson method correct upto 5 decimals.

Solution:

f(x) = x3-x-4 = 0,

f’(x) = 3x2-1

Checking the convergence condition

[pic] so process is convergent.

f(x) = x3-x-4, f(0) = -4, f(1) = -4, f(2) = 2.

Root lies between [1,2]

Initial guess value x0 = (a+b)/2 = (1+2)/2 = 1.5

[pic]

Repeating the same procedure, we get,

|It. No. |x0 |f(x0) |f’(x0) |[pic] |

|1. |1.5 |-2.125 |5.75 |1.8695652 |

|2. |1.8695652 |0.660776 |9.4858223 |1.7994524 |

|3. |1.7994524 |0.027226 |8.7140869 |1.7963219 |

|4. |1.796328 |0.000052 |8.6803825 |1.7963219 |

Value of the root is 1.7963219

Algorithm for Newton-Raphson Method:

Define f(x) = , and derivative of f(x) i.e. Df(x) =

Enter desired accuracy, e and initial guess, x0

k = 0

do

{

x(k+1) = x(k)-[f(x(k))/Df(x(k))]

k=k+1

}while (|x(k+1)-x(k)|>=e)

Print root of the equation is, x(k+1), and no. of iterations, k

Stop.

Program for Newton Raphson Method:

/* Newton Raphson Method */

#include

#include

#include

/* function */

float f(float x)

{

float f;

/* f=(x*x*x)-x-4.0; */

f=pow(2.7182818,x)*cos(x)-1.2;

return(f);

}

float df(float x)

{

float df;

/* df=(3*x*x)-1.0; */

df=-pow(2.7182818,x)*sin(x)+pow(2.7182818,x)*cos(x);

return(df);

}

/* Main Program */

main()

{

float x[51],e;

int k;

clrscr();

printf(“\n Enter accuracy”);

scanf(“%f”,&e);

printf(“\n Enter initial guess x0”);

scanf(“%f”,&x[0]);

k=0;

do

{

x[k+1]=x[k]-(f(x[k])/(df(x[k])));

printf(“\nI=%d x0=%f f(x0)=%f f’(x0)=%f x1=%f |x1-x0|=%f”, k+1,x[k],f(x[k]),x[k+1],fabs(x[k+1]-x[k]));

k=k+1;

}while(fabs(x[k]-x[k-1])>=e);

printf(“\n Root of the equation is %f”,x[k];

printf(“\n Actual no. of iterations required=%d”,k);

getch();

}

Output:

Enter accuracy 0.0001

Enter initial guess x0 1.5

I= x0= f(x0)= f’(x0)= x1= |x1-x0|=

Root of the equation is 1.7963219

Actual no. of iterations required to achieve desired accuracy 5

Experiment -4

ITERATIVE METHOD

For finding the roots of the equation F(X) = 0, we rewrite this equation in the form x = Q(x). There are many ways of doing this. For ex. x3 – x2 – 1 = 0 can be expressed as,

x = (1+x)-1/2

x = (1-x3)1/2

x = (1-x2)1/3, etc.

Let, x0 be an approximate value of the desired root [pic]. Substituting it for x on the right side of x = Q(x), we obtain the first approximation x1 = Q(x).

The successive approximation are then given by, x2 = Q(x2)

x3 = Q(x3)

- - - - - - -

- - - - - - -

xn = Q(xn-1)

The sequence of approximations x0,x1,x2,--------,xn, always does not converage to get the root.

The condition for convergence is |Q’(x)|=e)

Print root of the equation is x1 and no. of iterations, k.

Stop.

Program for Iterative Method:

/* Iterative Method */

#include

#include

#include

/* function */

float f(float x)

{

float f;

/* f=(x*x*x)-x-4; f=pow((x+4.0),(1./3.)); f=log(1.4/cos(x)); */

f=(1+(2/x));

/* f=pow((x+2),0.5); */

return(f);

}

/* Main Program */

main()

{

float x0,x1,e;

int k;

clrscr();

printf(“\n Enter accuracy”);

scanf(“%f”,&e);

printf(“\n Enter initial guess x0”);

scanf(%f”,&x0);

k=0;

x1=f(x0);

do

{

x0=x1;

x1=f(x0);

printf(“\nI=%d x0=%f f(x0)=%f |x1-x0|=%f”, k+1,x0,f(x0),fabs(x1-x0));

k=k+1;

}while(fabs(x1-x0)>=e);

printf(“\n Root of the equation is %f”, x1);

printf(“\n Actual no. of iterations required=%d”,k);

getch();

}

Output:

Enter accuracy 0.0001

Enter initial guess

I= x0= f(x0)= |x1-x0|=

Root of the equation is

Actual no. of iterations required=

Numerical Integration

a

The∫ f(x) is obtained analytically, when f(x) is continuous or not explicitly known,

B

Therefore simple form or explicitly known. But many times f(x) may be complicated or not explicitly known, therefore integration would be difficult hence numerical methods are preferred to evaluate above integral, as they are very simple and can be solved with greater ease. Also a comparison of some numerical solutions with the corresponding analytical solutions will show how close the numerical method is to the analytical solution. It is required to compute the value of the definite ingegral.

Experiment - 5

Trapezoidal rule

Putting n = 1, in the general formula I= n.h[y0 + n(2n-3)/12∆2 y0+ n(n-2) 2/24 ∆3 y0+----] all differences higher than the first degree will become zero and we obtain,

x1

∫ y.dx = h.[y0 + (1/2) ∆y0] = h.[y0+(1/2).(y1-y0)] = (h/2).[y0 + y1]

x0

This is the formula for first step from x0 to x1 of one strip of width, h)

For the next interval [x1,x2], we obtain similarly,

x2

∫ y.dx = (h/2).[y1 + y2]

x1

(this is the formula for 2nd step from x1 to x2 of one strip of width, h) and so on.

For last interval [xn-1,xn ] we have ,

xn

∫ y.dx = (h/2).[y n-1 + yn]

This is the formula for nth step from xn-1 to xn of one strip of width, h)

Combining all these expressions, we obtain the Trapezoidal rule.

HARDWARE AND SOFTWARE:

Hardware: Desktop, minimum 128MB RAM, a suitable HDD

Software: Windows XP/Linux, Turbo C/ C++ or Borland C/ C++ compiler

Problem:

6

Evaluate ∫ [1/(1+x)]dx by Trapezoidal rule

0

Algorithm for Trapezoidal Rule:

Define f(x) =

Enter the values of lower and upper limit of x i.e. x0, xn and also enter number of intervals N.

Ns = 1

h = {(xn-x0)/N }

sum = 0

do

{

sum = sum + (h/2).[f(x0) + f(x0 + h)]

x0 = x0 +h

}while(x0 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download