Professional Documents
Culture Documents
DHARAN-8, SUNSARI
NEPAL
The bisection algorithm attempts to locate the value c where the plot of f crosses
over zero, by checking whether it belongs to either of the two sub-intervals [a, xm ], [xm ,
b], where xm is the midpoint
𝑎+𝑏
x m= 2
Algorithm of BISECTION METHOD.
Step-1. Start of the program.
Step-2. Input the variable x1, x2 for the task.
Step-3. Check f(x1)*f(x2)<0
Step-4. If yes proceed
Step-5. If no exit and print error message
Step-6. Repeat 7-11 if condition not satisfied
Step-7. X0=(x1+x2)/2
Step-8. If f(x0)*f(x1)<0
Step-9. X2=x0
Step-10. Else
Step-11. X1=x0
Step-12. Condition:
Step-13. if | (x1-x2)/x1) | < maximum possible error or f(x0)=0
Step-14. Print output
Step-15. End of program.
coding:
//PROGRAM: BISECTION METHOD.
//2074/03/21
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#define EPS 0.00001
#define F(x) (x)*log10(x)-1.2
void Bisect();
int count=1,n;
float root=1;
int main()
{
printf("\n Solution by BISECTION method \n");
printf("\n Equation is ");
printf("\n\t\t\t x*log(x) - 1.2 = 0\n\n");
printf("Enter the number of iterations:");
scanf("%d",&n);
Bisect();
getch();
}
void Bisect()
{
float x0,x1,x2;
float f0,f1,f2;
int i=0;
for(x2=1;;x2++)
{
f2=F(x2);
if (f2>0)
{
break;
Page 3 of 14
}
}
for(x1=x2-1;;x2--)
{
f1=F(x1);
if(f1<0)
{
break;
}
}
printf("\t\t-----------------------------------------");
printf("\n\t\t ITERATIONS\t\t ROOTS\n");
printf("\t\t-----------------------------------------");
for(;count<=n;count++)
{
x0=((x1+x2)/2.0);
f0=F(x0);
if(f0==0)
{
root=x0;
}
if(f0*f1<0)
{
x2=x0;
}
else
{
x1=x0;
f1=f0;
}
printf("\n\t\t ITERATION %d", count);
printf("\t :\t %f",x0);
if(fabs((x1-x2)/x1) < EPS)
{
printf("\n\t\t---------------------------------");
printf("\n\t\t Root = %f",x0);
printf("\n\t\t Iterations = %d\n", count);
printf("\t\t------------------------------------");
getch();
}
}
printf("\n\t\t----------------------------------------");
printf("\n\t\t\t Root = %7.4f",x0);
printf("\n\t\t\t Iterations = %d\n", count-1);
printf("\t\t------------------------------------------");
getch();
}
OUTPUT:
Solution by BISECTION method
Page 4 of 14
Equation is
x*log(x) - 1.2 = 0
Page 5 of 14
Step-1. Start of the program.
Step-2. Input the variable x0, x1,e, n for the task.
Step-3. f0=f(x0)
Step-4. f2=f(x2)
Step-5. for i=1 and repeat if i<=n
Step-6. x2 = (x1.f1-xo.f1)/(f1-f0)
Step-7. f2 = x2
Step-8. if | f2 | <=e
Step-9. print “convergent “, x2, f2
Step-10. if sign (f2)!=sign(f0)
Step-11. x1=x2 & f1 = f2
Step-12. else
Step-13. X0 = x2 & f0 = f2
Step-14. End loop
Step-15. Print output
Step-16. End the program.
Page 7 of 14
Enter the number of iteration:15
-----------------------------------------
ITERATION x2 F(x)
--------------------------------------------
1 1.169755 -3.377644
2 0.267211 0.615449
3 0.694865 -0.623979
4 0.427878 0.253484
5 0.573166 -0.176538
6 0.487164 0.090711
7 0.535763 -0.055608
8 0.507540 0.030817
9 0.523678 -0.018102
10 0.514367 0.010284
11 0.519712 -0.005956
12 0.516635 0.003411
13 0.518403 -0.001966
14 0.517386 0.001129
15 0.517971 -0.000650
--------------------------------------------
Root=0.517971
-------------------------------------------
SIMPSON’S 1/3rd RULE:
ALGORITHM OF SIMPSON’S 1/3rd RULE
Step-1. Start of the program.
Step-2. Input Lower limit a
Step-3. Input Upper limit b
Step-4. Input number of subintervals n
Step-5. h=(b–a)/n
Step-6. sum=0
Step-7. sum=fun(a)+4*fun(a+h)+fun(b)
Step-8. for i=3; i<n; i + = 2
Step-9. sum + = 2*fun(a+(i – 1)*h) + 4*fun(a+i*h)
Step-10. End of loop i
Step-11. result=sum*h/3
Step-12. Print Output result
Step-13. End of Program
Step-14. Start of Section fun
Step-15. temp = 1/(1+(x*x))
Step-16. Return temp
Step-17. End of Section fun
coding:
//PROGRAM: SIMPSON’S 1/3rd METHOD OF NUMERICAL INTEGRATION
#include<stdio.h>
#include<conio.h>
#include<math.h>
Page 8 of 14
#include<process.h>
#include<string.h>
float fun(float);
int main()
{
float result=1;
float a,b;
float sum,h;
int i,j,n;
clrscr();
printf("\n Enter the range - ");
printf("\n Lower Limit a - ");
scanf("%f",&a);
printf("\n Upper limit b - ");
scanf("%f",&b);
printf("\n\n Enter number of sub intervals - ");
scanf("%d",&n);
h=(b-a)/n;
sum=0;
sum=fun(a)+4*fun(a+h)+fun(b);
for(i=3;i<n;i+=2)
{
sum+=2*fun(a+(i-1)*h)+4*fun(a+i*h);
}
result=(sum*h)/3;
printf("\n\nValue of integral is %6.4f\t",result);
getch();
return 0;
}
float fun(float x)
{
float temp;
temp=1/(1+(x*x));
return temp;
}
Page 9 of 14
Step-11. else:
Step-12. sum + = 3*fun(a+(i)*h)
Step-13. End of loop i
Step-14. result = sum*3*h/8
Step-15. Print Output result
Step-16. End of Program
Step-17. Start of Section fun
Step-18. temp = 1/(1+(x*x))
Step-19. Return temp
Step-20. End of section fun
PROGRAM: SIMPSON’S 3/8th METHOD OF NUMERICAL INTEGRATION
#include<stdio.h>
#include<coni
o.h> float
fun(int);
void main()
{
int n,a,b,i;
float h, sum=0,
result; //clrscr();
printf("enter range");
scanf("%d",&n);
printf("enter lower
limit");
scanf("%d",&a);
printf("enter upper
limit");
scanf("%d",&b); h=(b-
a)/n;
sum=fun(a)+fun(b);
for(i=0;i<n;i++)
{
if (i%2==0)
sum+=2*fun(a+i
*h); else
sum+=3*fun(a+i*h);
}
result=sum*3/
8*h;
printf("%f",
result); getch();
}
float fun(int x)
{
float val;
val=1/(1+(x*
Page 10 of 14
x));
return(val);
}
The method overall reduces the system of linear simultaneous equations to an upper
triangular matrix. Then backward substitution is used to derive the unknowns. This is the key
concept in writing an algorithm or program, or drawing a flowchart for Gauss Elimination.
Partial pivoting or complete pivoting can be adopted in Gauss Elimination method. So, this
method is considered superior to the Gauss Jordan method.
In the Gauss Elimination method algorithm and flowchart given below, the elimination
process is carried out until only one unknown remains in the last equation. It is
straightforward to program, and partial pivoting can be used to control rounding errors.
1. Start
2. Declare the variables and read the order of the matrix n.
3. Take the coefficients of the linear equation as:
Do for k=1 to n
Do for j=1 to n+1
Read a[k][j]
End for j
End for k
4. Do for k=1 to n-1
Do for i=k+1 to n
Do for j=k+1 to n+1
a[i][j] = a[i][j] – a[i][k] /a[k][k] * a[k][j]
End for j
Page 11 of 14
End for i
End for k
5. Compute x[n] = a[n][n+1]/a[n][n]
6. Do for k=n-1 to 1
sum = 0
Do for j=k+1 to n
sum = sum + a[k][j] * x[j]
End for j
x[k] = 1/a[k][k] * (a[k][n+1] – sum)
End for k
7. Display the result x[k]
8. Stop
CODING:
#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10],sum=0.0;
printf("\nEnter the order of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\n\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf("A[%d][%d] : ", i,j);
scanf("%f",&A[i][j]);
}
}
for(j=1; j<=n; j++) /* loop for the generation of upper triangular matrix*/
{
for(i=1; i<=n; i++)
{
if(i>j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
x[n]=A[n][n+1]/A[n][n];
Page 12 of 14
/* this loop is for backward substitution*/
for(i=n-1; i>=1; i--)
{
sum=0;
for(j=i+1; j<=n; j++)
{
sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
}
printf("\nThe solution is: \n");
for(i=1; i<=n; i++)
{
printf("\nx%d=%f\t",i,x[i]); /* x1, x2, x3 are the required solutions*/
}
return(0);
}
OUTPUT:
Enter the order of matrix: 3
Enter the elements of augmented matrix row-wise:
A[1][1] : 10
A[1][2] : -7
A[1][3] : 3
A[1][4] : 5
A[2][1] : -6
A[2][2] : 8
A[2][3] : 4
A[2][4] : 7
A[3][1] : 2
A[3][2] : 6
A[3][3] : 9
A[3][4] : -1
The solution is:
x1=-7.809086
x2=-8.690904
x3=7.418178
It is important to recognize under what conditions a method can be followed and what
starting value(s) to choose from in order to ensure that the chosen method shall work
(converge).
Page 13 of 14
finding root(s) of equations - Bisection method, Newton Raphson, Fixed Point
iteration etc
solving ODEs - Euler method, Improved Euler, RK methods, Midpoint method,
Predictor Corrector methods etc
finding values of integrals - Midpoint, Trapezoidal, Simpson’s rule
interpolation - Lagrange interpolation, Newton interpolation, Spline interpolation
etc
There is huge use of numerical methods in engineering world. We can develop algorithms
and code the programs in computer and felt in real world practice. We can conclude that
without numerical methods using computer programming it is quite tough to solve the
complex engineering calculations.
Page 14 of 14