Computer Based Numerical and Statistical Teciques (CBNST)

Computer Based Numerical and Statistical Techniques (CBNST)

List of Programs Write in C Language

  1. To find the real root of the Algebraic and Transcendental equations using Bisection method.
  2. To find the real root of the Algebraic and Transcendental equations using Regular-Falsie position method.
  3. To find the real root of the Algebraic and Transcendental equations using Iteration method.
  4. To find the real root of the Algebraic and Transcendental equations using Newton Raphson method.
  5. To implement Newton’s Forward Interpolation formula.
  6. To implement Newton’s Backward Interpolation formula.
  7. To Implement Lagrange’s Interpolation Formula.
  8. To implement Numerical Integration using Trapezoidal rule.
  9. To implement Numerical Integration using Simpson 1/3 rule.
  10. To implement Numerical Integration Simpson 3/8 rule.


1.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.

1.Program: Bisection Method.

#include <stdio.h>
#include <conio.h>
#define MAX 2
float f(float x);
void main()
{
float a,b,c;
int i,j,n;
clrscr();
printf(“\nEnter the number of iterations: “);
scanf(“%d”,&n);
for(i=-MAX;i<MAX;i++)
{
a=i;
b=i+1;
if(f(a)*f(b)<=0)
{
//if(f(a)>f(b)) { t=a; a=b; b=t; }
printf(“\nThe initial approximate limits are: “);
printf(“%f %f”,a,b);
printf(“\n a b c f(c)”);
for(j=0;j<n;j++)
{
c=(a+b)/2;
printf(“\n%10.6f %10.6f %10.6f %10.6f”,a,b,c,f(c));
if((f(b)*f(c))<0) a=c; else b=c;
}
getch();
}
}
}
float f(float x)
{
return(x*x*x-x*x-3);
}

2.Algorithm of False Position or Regula-Falsi Method.

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.

2.Program: False Position or Regula-Falsi Method.

#include <stdio.h>
#include <conio.h>
#define MAX 4
float f(float x);
void main()
{
float a,b,c;
int i,j,n;
clrscr();
printf(“\nEnter the number of iterations: “);
scanf(“%d”,&n);
for(i=-MAX;i<MAX;i++)
{
a=i;
b=i+1;
if(f(a)*f(b)<=0)
{
printf(“\nThe initial approximate limits are: “);
printf(” %f %f”,a,b);
printf(“\n a b c f(c)”);
for(j=0;j<n;j++)
{
c=a-(f(a)/(f(b)-f(a)))*(b-a);
printf(“\n%10.6f %10.6f %10.6f %10.6f”,a,b,c,f(c));
a=b; b=c;
}
getch();
}
}
}
float f(float x)
{
return(x*x*x-2*x-5);
}

3.Algorithm For Iteration Method.

Step-1. Read x0, e, n
x0 is the initial guess, e is the allowed error in root, n is total iterations to be allowed for convergence.
Step-2. x1 ← g(x0)
Steps 4 to 6 are repeated until the procedure converges to a root or iterations reach n.
For i = 1 to n in steps of 1 do x0 ← x1
x1 ← g(x0)
If ≤ e then, GO TO 9 end for.
Write “Does not converge to a root”, x0, x1 Stop
Write “converges to a root‟, i, x1
Stop.

3.Program: Iteration Method.

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x);
void main()
{
int i,n;
float x=0;
clrscr();
printf(“Enter the number of Iteration:-“);
scanf(“%d”,&n);
printf(“n x f(x)”);
for(i=0;i<n;i++)
{
printf(“\n%d %f %f”,i+1,x,f(x));
x=f(x);
}
getch();
}
float f(float x)
{
return ((cos(x)+2)/3);
}

4.Algorithm of Newton-Raphson Method.

Start of the program.
Input the variables x0, n for the task.
input Epsilon & delta
for i= 1 and repeat if i <= n f0 = f(x0)
dfo = df(x1)
if | dfo | <= delta
a. Print slope too small
b. Print x0, f0, df0, i
c. End of program
x1 = x0 –(f0/df0)
if | (x1-x0/x1) | < epsilon
a. Print convergent
b. Print x1, f(x1), i
c. End of program
Step-10. x0 = x1
Step-11. End loop

4.Program: Newton Raphson Method.

#include <stdio.h>
#include <conio.h>
#define MAX 4
float f(float x);
float f1(float y);
void main()
{
float a,b,c,d;
int i,j,n;
clrscr();
printf(“\nEnter the number of iterations: “);
scanf(“%d”,&n);
for(i=-MAX;i<MAX;i++)
{
a=i;
b=i+1;
if(f(a)*f(b)<=0)
{
printf(“\nThe initial approximate value is:”);
printf(“\n%f %f”,a,b);
c=(a+b)/2.0;
printf(“\n%f”,c);
printf(“\nn c f(c)”);
for(j=0;j<n;j++)
{
printf(“\n%-5d%15.6f%15.6f”,j+1,c,f(c));
d=c-(f(c)/f1(c));
c=d;
}
getch();
}
}
}
/* Function is x^2-2x+1 */
float f(float x)
{
return(x*x-3*x+1);
}
/* Differential of Function is 2x-2 */
float f1(float y)
return(2*y-3);
}

5.Algorithm For Newton’s Forward Method of Interpolation

Step-1. Start of the program
Step-2. Input number of terms n
Step-3. Input the array ax
Step-4. Input the array ay
Step-5. h=ax[1] – ax[0]
Step-6. for i=0; i<n-1; i++
Step-7. diff[i] [1]=ay[i + 1] – ay[i]
Step-8. End Loop i
Step-9. for j=2; j<=4; j++
Step-10. for i = 0; i <n – j; i++
Step-11. diff[i][j]=diff [i + 1] [j – 1]-diff [i][j – 1]
Step-12. End Loop i
Step-13. End Loop j
Step-14. i=0
Step-15. Repeat Step 16 until ax[i]<x
Step-16. i=i + 1
Step-17. i=i – 1;
Step-18. p=(x – ax [i])/h
Step-19. y1=p*diff[i – 1][1]
Step-20. y2=p*(p+1)*diff [i – 1][2]/2
Step-21. y3=(p+1)*p*(p-1)*diff[i –2 ][3]/6
Step-22. y4=(p+2)*(p+1)*p*(p – 1)*diff[i – 3][4]/24
Step-23. y=ay[i]+y1+y2+y3+y4
Step-24. Print output x, y
Step-25. End of program.

5.Program: Newton’s Forward Method of Interpolation

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n;
float xy[10][10],h,p,px=1,x,y;
clrscr();
/* Data read opratio. */
printf(“Enter the number of data : “);
scanf(“%d”,&n);
printf(“Enter the data : \n”);
for(i=0;i<n;i++)
{
printf(“x(%d) : “,i+1);
scanf(“%f”,&xy[i][0]);
printf(“y(%d) : “,i+1);
scanf(“%f”,&xy[i][1]);
}
/* Forming difference table */
for(j=2;j<n+1;j++)
for(i=0;i<n-1;i++)
xy[i][j]=xy[i+1][j-1]-xy[i][j-1];
/* Printing table */
printf(“\nThe difference table is :-“);
printf(“\nx f(x) “);
for(i=0;i<n-1; i++)
printf(” ^%d “,i+1);
for(i=0;i<n;i++)
{
printf(“\n”);
for(j=0;j<n+1-i;j++)
{
printf(“%.4f “,xy[i][j]);
}
}
/* Take the valuen of x for f(x) */
printf(“\nEnter the value of ‘x’ : “);
scanf(“%f”,&x);
/* Calculate the value of f(x) */
//h=1;
h=xy[1][0]-xy[0][0];
p=(x-xy[0][0])/h;
y=xy[0][1];
for(i=1;i<n;i++)
{
px*=(p-(i-1))/i;
y+=xy[0][i+1]*px;
}
/* Printing the result */
printf(“\nThe value of function at x = %f is %f”,x,y);
getch();
}

6.Algorithm For Newton’s Backward Method of Interpolation

Step-1. Start of the program.
Step-2. Input number of terms n
Step-3. Input the array ax
Step-4. Input the array ay
Step-5. h=ax[1]-ax[0]
Step-6. for i=0; i<n–1; i++
Step-7. diff[i][1]=ay[i+1]–ay[i]
Step-8. End Loop i
Step-9. for j = 2; j < = 4; j + +
Step-10. for i=0; i<n–j; i++
Step-11. diff[i][j]=diff[i+1][j–1]–diff [i][j–1]
Step-12. End Loop i
Step-13. End Loop j
Step-14. i=0
Step-15. Repeat Step 16 until (!ax[i]<x)
Step-16. i=i+1
Step-17. x0=mx[i]
Step-18. sum=0
Step-19. y0=my[i]
Step-20. fun=1
Step-21. p=(x–x0)/h
Step-22. sum=y0
Step-23. for k=1; k<=4; k++
Step-24. fun=(fun*(p–(k–1)))/k
Step-25. sum=sum+fun*diff[i][k]
Step-26. End loop k
Step-27. Print Output x,sum
Step-28. End of Program

6.Program: Newton’s Backward Method of Interpolation

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n;
float xy[10][11],h,p,px=1,x,y;
clrscr();
printf(“Enter the number of data : “);
scanf(“%d”,&n);
printf(“Enter the data : \n”);
for(i=0;i<n;i++)
{
printf(“x(%d) : “,i+1);
scanf(“%f”,&xy[i][0]);
printf(“y(%d) : “,i+1);
scanf(“%f”,&xy[i][1]);
}
for(j=2;j<n+1;j++)
for(i=0;i<n-1;i++)
xy[i][j]=xy[i+1][j-1]-xy[i][j-1];
printf(“The difference table is :-“);
printf(“\nx f(x) “);
for(i=0;i<n-1; i++)
printf(“^%d “,i+1);
for(i=0;i<n;i++)
{
printf(“\n”);
for(j=0;j<n+1-i;j++)
printf(“%.4f “,xy[i][j]);
}
printf(“\nEnter the value of ‘x’ : “);
scanf(“%f”,&x);
h=xy[n-1][0]-xy[n-2][0];
printf(“\n\n H=%f”,h);
p=(x-xy[n-1][0])/h;
y=xy[n-1][1];
for(i=1;i<n;i++)
{
px*=(p+(i-1))/i;
y+=xy[n-1-i][i+1]*px;
}
printf(“The value of function at x = %f is Y=%f”,x,y);
getch();
}

7.Algorithm of Lagrange’s Interpolation Formula.

Step-1. Start of the program
Step-2. Input number of terms n
Step-3. Input the array ax
Step-4. Input the array ay
Step-5. for i=0; i<n; i++
Step-6. nr=1
Step-7. dr=1
Step-8. for j=0; j<n; j++
Step-9. if j !=i
a. nr=nr*(x-ax[j])
Step-10. b.dr*(ax[i]-ax[j])
Step-11. End Loop j
Step-12. y+=(nr/dr)*ay[i]
Step-13. End Loop i
Step-14. Print Output x, y
Step-15. End of Program

7.Program: Lagrange’s Interpolation Formula.

#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n;
float x[10],y[10],x1,y1=0,nr,dr;
clrscr();
printf(“\nEnter the number of data : “);
scanf(“%d”,&n);
printf(“\nEnter the data : \n”);
for(i=0;i<n;i++)
{
printf(“x(%d) : “,i+1);
scanf(“%f”,&x[i]);
printf(“y(%d) : “,i+1);
scanf(“%f”,&y[i]);
}
printf(“\nEnter the value of ‘x’ : “);
scanf(“%f”,&x1);
for(i=0;i<n;i++)
{
nr = dr = 1;
for(j=0;j<n;j++)
{
if(i!=j)
{
nr *= (x1-x[j]);
dr *= (x[i]-x[j]);
}
}
y1 += (nr/dr)*y[i];
}
printf(“\nValue of function at x=%f is : %f”,x1,y1);
getch();
}

8.Algorithm of Trapezoidal Rule

Step-1. Start of the program.
Step-2. Input Lower limit a
Step-3. Input Upper Limit b
Step-4. Input number of sub intervals n
Step-5. h=(b-a)/n
Step-6. sum=0
Step-7. sum=fun(a)+fun(b)
Step-8. for i=1; i<n; i++
Step-9. sum +=2*fun(a+i)
Step-10. End Loop i
Step-11. result =sum*h/2;
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.

8.Algorithm of Trapezoidal Method

#include <stdio.h>
#include <conio.h>
float f(float x);
void main()
{
float k=0.0,h,x,a,b;
int i;
clrscr();
/* Input Data */
printf(“The function f(x): x”);
printf(“\nEnter the lower and upper limits”);
printf(“\na = “);
scanf(“%f”,&a);
printf(“b = “);
scanf(“%f”,&b);
printf(“Enter the difference ‘h’ = “);
scanf(“%f”,&h);
/* Calculate the integral */
printf(“\nx f(x)”);
for(i=0,x=a;x<b-h;x=x+h,i++)
{
k += f(x) + f(x+h);
printf(“\n%.6f%15.6f”,x,f(x));
}
/* Printing portion */
printf(“\n%f%15.6f”,x,f(x+h));
printf(“\nThe definite integral is %15.6f”,(h/2.0)*k);
getch();
}
float f(float x)
{
return(x);
}

9.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

9.Program: Simpson’s 1/3rd Method of Numerical Integration

#include <stdio.h>
#include <conio.h>
float f(float x);
void main()
{
float k=0.0,h,x,a,b;
int i,n;
clrscr();
printf(“Enter the function f(x): x”);
printf(“\nEnter the lower and upper limits”);
printf(“\na = “);
scanf(“%f”,&a);
printf(“b = “);
scanf(“%f”,&b);
printf(“Enter number of strips(in multiple of ‘2’) : “);
scanf(“%d”,&n);
h = (b-a)/n;
printf(“\nx f(x)”);
for(x=a;x<b;x=x+2*h)
{
k += f(x) + 4*f(x+h) + f(x+2*h);
printf(“\n%.6f%15.6f”,x,f(x));
printf(“\n%.6f%15.6f”,x+h,f(x+h));
}
printf(“\n%f%15.6f”,x,f(x+h));
printf(“\nThe definite integral is %15.6f”,(h/3.0)*k);
getch();
}
float f(float x)
{
return(1/(x*x+1));
}

10.Algorithm of Simpson’s 3/8th Rule

Step-1. Start of the program.
Step-2. Input Lower limit a
Step-3. Input Upper limit b
Step-4. Input number of sub intervals n
Step-5. h = (b – a)/n
Step-6. sum = 0
Step-7. sum = fun(a) + fun (b)
Step-8. for i = 1; i < n; i++
Step-9. If( i%3=0)
Step-10. sum + = 2*fun(a + i*h)
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

10.Program: Simpson’s 3/8th Method of Numerical Integration

#include<stdio.h>
#include<conio.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*x));
return(val);
}



C Language E-Book


Basic C Programming
Advance C Programming Part 1
Advance C Programming Part 2
CBNST
About me