Professional Documents
Culture Documents
ALLENHOUSE INSTITUTE OF
TECHNOLOGY
Department
Of
Computer Science & Engineering
Faculty In-charge
Head of Department
S. No
TOPIC
1.
2.
Page No.
3.
4.
5.
6.
7.
10
11
12
13
14
15
16
17
18
histogram,
19
Experiment No. 1
1 . Algorithm to deduce error involved in polynomial equation
Step-1.
Step-2.
Step-7.
STOP
Experiment No. 2
Algorithm of BISECTION METHOD.
Step-1.
Step-2.
Step-3.
Check f(x1)*f(x2)<0
Step-4.
If yes proceed
Step-5.
Step-6.
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.
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();
exit(0);
}
}
printf("\n\t\t---------------------------------------Allenhouse Institute of Technology, Kanpur
}
printf("\n\t\t\t Iterations = %d\n", count-1);
printf("\t\t-----------------------------------------getch();
Experiment No. 3
Algorithm of FALSE POSITION or REGULA-FALSI METHOD.
Step-1.
Step-2.
Step-3.
f0=f(x0)
Step-4.
f2=f(x2)
Step-5.
Step-6.
x2 = (x1.f1-xo.f1)/(f1-f0)
Step-7.
f2 = x2
Step-8.
if | f2 | <=e
Step-9.
Step-10.
if sign (f2)!=sign(f0)
Step-11.
x1=x2
Step-12.
else
Step-13.
X0 = x2 & f0 = f2
& f1 = f2
10
Step-14.
End loop
Step-15.
Print output
Step-16.
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#define EPS 0.00005
#define f(x) 3*x+sin(x)-exp(x)
void FAL_POS();
void main()
{
clrscr();
printf("\n Solution by FALSE POSITION method\n");
printf("\n Equation is ");
printf("\n\t\t\t 3*x + sin(x)-exp(x)=0\n\n");
FAL_POS();
}
void FAL_POS()
{
float f0,f1,f2;
float x0,x1,x2; int itr;
Allenhouse Institute of Technology, Kanpur
11
int i;
printf("Enter the number of iteration:");
scanf("%d",&itr);
for(x1=0.0;;)
{
f1=f(x1);
if(f1>0)
{
break;
}
else
{
x1=x1+0.1;
}
}
x0=x1-0.1;
f0=f(x0);
printf("\n\t\t-----------------------------------------");
printf("\n\t\t ITERATION\t x2\t\t F(x)\n");
printf("\t\t------------------------------------------------------");
for(i=0;i<itr;i++)
{
x2=x0-((x1-x0)/(f1-f0))*f0;
f2=f(x2);
if(f0*f2>0)
{
x1=x2;
f1=f2;
}
else
{
x0=x2;
f0=f2;
}
if(fabs(f(2))>EPS)
{
printf("\n\t\t%d\t%f\t%f\n",i+1,x2,f2);
}
}
printf("\t\t--------------------------------------------");
printf("\n\t\t\t\tRoot=%f\n",x2);
printf("\t\t-------------------------------------------");
getch();
}
12
Experiment No. 4
Algorithm of NEWTON-RAPHSON METHOD.
Step-1.
Step-2.
Step-3.
Step-4.
Step-5.
f0 = f(x0)
Step-6.
dfo = df(x1)
Step-7.
a.
b.
c.
End of program
Step-8.
x1 = x0 (f0/df0)
Step-9.
Print convergent
b.
c.
End of program
13
Step-10. x0 = x1
Step-11. End loop.
14
{
f1=f(x1); if (f1
> 0)
{
break;
}
}
x0=x1-0.01;
f0=f(x0);
printf(" Enter the number of iterations: ");
scanf(" %d",&itr);
printf(" Enter the maximum possible error: ");
scanf("%f",&EPS);
if (fabs(f0) > f1)
{
printf("\n\t\t The root is near to %.4f\n",x1);
}
15
Experiment No. 5
16
Step-3.
Step-4.
Step-5.
Step-6.
Step-7.
Step-8.
Step-9.
Step-10.
Step-11.
17
Step-1.
Read x0, e, n
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.
Step-2.
Step-3.
Step-4.
Initialize j
Step-5.
Step-6.
Step-7.
18
Step-8.
Step-9.
End if
19
clrscr();
printf("\n\t Solution by ITERATION method - ");
20
Experiment No. 6
ALGORITHM OF TRAPEZOIDAL RULE
Step-1.
Step-2.
Step-3.
Step-4.
Step-5.
h=(b-a)/n
21
Step-6.
sum=0
Step-7.
sum=fun(a)+fun(b)
Step-8.
Step-9.
sum +=2*fun(a+i)
22
i,j;
int n; clrscr();
printf("\n\n Enter the range - ");
printf("\n\n Lower Limit a - ");
scanf("%f" ,&a);
printf("\n\n Upper Limit b - ");
scanf("%f" ,&b);
printf("\n\n Enter number of subintervals - ");
scanf("%d" ,&n);
h=(b-a)/n;
sum=0;
sum=fun(a)+fun(b);
for(i=1;i<n;i++)
{
sum+=2*fun(a+i);
}
result=sum*h/2;
printf("n\n\n\n Value of the integral is %6.4f\t",result);
printf("\n\n\n Press Enter to Exit");
getch();
}
float fun(float x)
{
float temp;
temp = 1/(1+(x*x)); return
temp;
}
Experiment No. 7
ALGORITHM OF SIMPSONS 1/3
Step-1.
Step-2.
Step-3.
Step-4.
Step-5.
h=(ba)/n
rd
RULE
23
Step-6.
sum=0
Step-7.
sum=fun(a)+4*fun(a+h)+fun(b)
Step-8.
Step-9.
rd
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<string.h> float
fun(float); void main()
{
float result=1; float
a,b; float sum,h; int
i,j,n; clrscr();
Allenhouse Institute of Technology, Kanpur
24
Experiment No. 8
step-1.
Step-2.
Step-3.
Step-4.
Step-5.
h = (b a)/n
Step-6.
sum = 0
Step-7.
th
RULE
25
Step-8.
Step-9.
if i%3=0:
th
26
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);
}
Experiment No. 9
9.
Step-1.
Step-2.
Step-3.
Step-4.
Step-5.
h=ax[1] ax[0]
Step-6.
Step-7.
Step-8.
End Loop i
Step-9.
27
28
(i=0;i<n;i++)
{
printf("Enter the value of x%d - ",i+1);
scanf("%f",&ax[i]);
}
printf("\n Enter the value in the form of y - "); for
(i=0;i<n;i++)
{
printf ("Enter the value of y%d - ", i+1); scanf
("%f",&ay [i]);
}
printf("\nEnter the value of x for");
printf("\nwhich you want the value of y - ");
scanf("%f",&x);
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
{
diff[i][1]=ay[i+1]-ay[i];
}
for(j=2;j<=4;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
do
{
i++;
}
while(ax[i]<x); i--;
p=(x-ax[i])/h;
y1=p*diff[i-1][1]; y2=p*(p+1)*diff[i-1][2]/2;
y3=(p+1)*p*(p-1)*diff[i-2][3]/6;
y4=(p+2)*(p+1)*p*(p-1)*diff[i-3][4]/24;
y=ay[i]+y1+y2+y3+y4;
printf("\nwhen x=%6.4f, y=%6.8f ",x,y);
getch();
}
Allenhouse Institute of Technology, Kanpur
29
Experiment No. 10
10. Algorithm for Newtons 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<n1; 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<nj; i++
Step-11. diff[i][j]=diff[i+1][j1]diff [i][j1]
Step-12. End Loop i
Allenhouse Institute of Technology, Kanpur
30
31
}
printf("\n enter the value in the form of y - ");
for(i=0;i<n;i++)
{
printf("\n\n enter the value of y%d- ",i+1);
scanf("%f",&my[i]);
}
printf("\n enter the value of x for"); printf("\nwhich
you want the value of of y -");
scanf("%f",&x);h=mx[1]-mx[0];
for(i=0;i<n-1;i++)
{
diff[i][1]=my[i+1]-my[i];
}
for(j=2;j<=4;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
while(!mx[i]>x)
{
i++;
}
x0=mx[i];
sum=0;
y0=my[i];
fun=1; p=(xx0)/h; sum=y0;
for(k=1;k<=4;k++)
{
fun=(fun*(p-(k-1))/k);
sum=sum+fun*diff[i][k];}
printf("\n when x=%6.4f,y=%6.8f",x,sum);
printf("\n press enter to exit");
getch();
}
32
Experiment No. 11
11. Algorithm of GAUSS FORWARD interpolation formula
Step-1.
Step-2.
Step-3.
Step-4.
Step-5.
h=ax[1]-ax[0]
Step-6.
for i=0;i<n1;i++
Step-7.
diff[i][1]=ay[i+1]-ay[i]
Step-8.
End Loop i
Step-9.
for j=2;j<=4;j++
33
Step-14. i=0
Step-15. Repeat Step 16 until ax[i]<x
Step-16. i=i+1
Step-17. i=i1;
Step-18. p=(xax[i])/h
Step-19. y1=p*diff[i][1]
Step-20. y2=p*(p1)*diff[i1][2]/2
Step-21. y3=(p+1)*p*(p-1)*diff[i2][3]/6
Step-22. y4=(p+1)*p*(p1)*(p2)*diff[i3][4]/24
Step-23. y=ay[i]+y1+y2+y3+y4
Step-24. Print Output x,y
Step-25. End of program
# include <stdio.h>
# include <conio.h>
# include <math.h>
# include <process.h>
# include <string.h>
void main()
{
int n;
int i,j;
float ax[10];
float ay[10];
float x;
float nr,dr;
float y=0; float h;
float p;
float diff[20][20];
float y1,y2,y3,y4;
Allenhouse Institute of Technology, Kanpur
34
clrscr();
printf(" Enter the number of terms - ");
scanf("%d",&n);
printf("\n Enter the value in the form of x - ");
for (i=0;i<n;i++)
{
printf(" Enter the value of x%d - ",i+1);
scanf("%f",&ax[i]);
}
printf(" Enter the value in the form of y - ");
for(i=0;i<n;i++)
{
printf("Enter the value of y%d - ",i+1);
scanf("%f",&ay[i]);
}
printf("\nEnter the value of x for - ");
printf("\nwhich you want the value of y - ");
scanf ("%f",&x);
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
{
diff[i][1]=ay[i+1]-ay[i];
}
for(j=2;j<=4;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
do
{ i++;
}
while(ax[i]<x); i--;
p=(x-ax[i])/h;
y1=p*diff[i][1];
y2=p*(p-1)*diff[i-1][2]/2; y3=(p+1)*p*(p1)*diff[i-2][3]/6; y4=(p+1)*p*(p-1)*(p2)*diff[i-3][4]/24; y=ay[i]+y1+y2+y3+y4;
printf("\nwhen x=%6.4f,y=%6.8f ",x,y);
getch();
}
35
Experiment No. 12
12. Algorithm of Gausss Backward interpolation formula
Step-1.
Step-2.
Step-3.
Step-4.
Step-5.
h=ax[1]-ax[0]
Step-6.
for i=0;i<n-l;i++
Step-7.
diff[i][1]=ay[i+1]-ay[i]
Step-8.
End Loop i
Step-9.
for j=2;j<=4;j++
36
Step-14. i=0
Step-15. Repeat Step 16 until ax[i]<x
Step-16. i=i+1
Step-17. i=i1;
Step-18. p=(xax[i])/h
Step-19. y1=p*diff[i-1][1]
Step-20. y2=p*(p+1)*diff[i1][2]/2
Step-21. y3=(p+1)*p*(p-1)*diff[i2][3]/6
Step-22. y4=(p+2)*(p+1)*p*(p1)*diff[i3][4]/24
Step-23. y=ay[i]+y1+y2+y3+y4
Step-24. Print Output x,y
Step-25. End of Program
37
- ");
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
do
{ i++;
}
while (ax[i]<x); i--;
p=(x-ax[i])/h;
y1=p*diff[i-1][1]; y2=p*(p+1)*diff[i-1][2]/2;
y3=(p+1)*p*(p-1)*diff[i-2][3]/6;
y4=(p+2)*(p+1)*p*(p-1)*diff[i-3][4]/24;
y=ay[i]+y1+y2+y3+y4;
printf("\nwhen x=%6.1f,y=%6.4f ",x,y);
getch();
}
38
Experiment No. 13
13. Algorithm to implement BESSELS INTERPOLATION FORMULA
39
// no. of terms.
// Loop variables
// 'X' array limit 9
// 'Y' array limit 9
// User Query for what value of X
// Calculated value for coressponding X.
// Calc. Section
// Calc. Section
// to store Y
// Formulae variables.
clrscr();
printf("\t\t !! BESSELS INTERPOLATION FORMULA!! "); //
Input section.
Allenhouse Institute of Technology, Kanpur
40
i--; p=(x-ax[i])/h;
y1=p*diff[i][1];
y2=p*(p-1)*(diff[i][2]+diff[i-1][2])/4; y3=p*(p-1)*(p0.5)*diff[i-1][3]/6; y4=(p+1)*p*(p-1)*(p-2)*(diff[i-2]
[4]+diff[i-1][4])/48;
/ Taking sum y=ay[i]
Allenhouse Institute of Technology, Kanpur
41
+y1+y2+y3+y4;
/ Outut Section
printf("\n When x = %6.2f , y = %6.8f",x,y);
// Invoke user watch halt function
printf("\n\n\n\t\t\t !! PRESS ENTER TO EXIT !! ");
getch();
}
Experiment No. 14
14. Algorithm of Sterlings Interpolation Formula
Step-1. Start of the program.
Step-2.
Step-3.
Step-4.
Step-5.
h = ax[1]-ax[0]
Step-6.
Step-7.
Step-8.
End loop i
Step-9.
42
Step-17. i = i-1;
Step-18. p = (x-ax[i])/h
Step-19. y1= p*(diff[i][1] + diff[i-1][1])/2
Step-20. y2 = p*p*diff[i-1][2]/2
Step-21. y3 = p*(p*p-1)*(diff[i-1][3]+diff[i-2][3])/6
Step-22. y4 = p*p*(p*p-1)*diff[i-2][4]/24
Step-23. y = ay[i]+y1 + y2 + y3 + y4
Step-24. Print output
Step-25. End of program
43
scanf("%f",&x);
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
{
diff[i][1]=ay[i+1]-ay[i];
}
for(j=2;j<=4;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
i=0;
do
{ i++;
}
while(ax[i]<x); i--;
p=(x-ax[i])/h;
y1=p*(diff[i][1]+diff[i-1][1])/2; y2=p*p*diff[i-1]
[2]/2; y3=p*(p*p-1)*(diff[i-1][3]+diff[i-2][3])/6;
y4=p*p*(p*p-1)*diff[i-2][4]/24; y=ay[i]
+y1+y2+y3+y4;
printf("\n\n When x=%6.2f, y=%6.8f",x,y);
getch();
}
44
Experiment No. 15
15. Algorithm to implement Newtons Divided Difference formula.
45
46
printf("\n_____________________________________________________\n");
for(i=0;i<n;i++)
{
printf("\n %.3f",x[i]);
for(j=0;j<n-i;j++)
{
printf(" ");
printf(" %.3f",y[j][i]);
}
printf("\n");
}
i=0; do
{
if(x[i]<p && p<x[i+1]) k=1;
else i++;
}while(k != 1); f=i;
sum=0; for(i=0;i<n-1;i+
+)
{
k=f;
temp=1;
for(j=0;j<i;j++)
{
temp = temp * (p - x[k]); k++;
}
sum = sum + temp*(y[i][f]);
}
printf("\n\n f(%.2f) = %f ",p,sum);
getch();
47
Experiment No. 16
16. Algorithm of LAGRANGES INTERPOLATION FORMULA.
Step-1.
Step-2.
Step-3.
Step-4.
Step-5.
Step-6.
nr=1
Step-7.
dr=1
Step-8.
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]
48
49
Experiment No. 17
17. Algorithm to implement Numerical Differentiations.
Program to implement Numerical Differentiations.
#include<stdio.h>
#include<stdlib.h>
#include <math.h>
float F (float x);
void deriv (float f(float), float x, int n, float h, float **D)
{
int i, j;
unsigned powerof4;
for (i = 0; i <= n; i++)
{
D[i][0] = ( f(x+h) - f(x-h) ) / (2.0 * h);
powerof4 = 1;
for (j = 0; j < i; j++)
{
powerof4 <<= 2; /* this value is equivalent to pow(4,j+1) */ D[i]
[j+1] = D[i][j] + (D[i][j] - D[i-1][j]) / (powerof4 -1);
}
h *= 0.5;
}
}
float F (float x)
{
return (sin(x));
}
void main()
{
int n = 10; int i,
j;
float h = 1.0;
float **D, pi3, F(float);
Allenhouse Institute of Technology, Kanpur
50
51
Experiment No. 18
18. Algorithm to implement Least Square Method for curve fitting.
52
53
printf("\n x(%d)=%f",i,x[i]);
}
goto top;
}
else continue;
return;
}
54
Experiment No. 19
19. Algorithm to draw frequency chart like histogram, frequency curve and pie-chart etc
55
Algorithm to draw frequency chart like histogram, frequency curve and pie-chart etc
56
57