You are on page 1of 11

NACP.

txt
/* TO SORT A LIST OF 5 NUMBERS IN ASCENDING ORDER*/
#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,t;
int n[5]; clrscr();
//to enter the data
printf("\n enter the 5 numbers to be sorted\n");
for(i=0;i<5;i++)
scanf("%d",&n[i]);
//the sorting operation
for(j=0;j<5;j++)
for(i=0;i<4-j;i++)
if(n[i]>n[i+1])
{t=n[i];
n[i]=n[i+1];
n[i+1]=t;
}
//to print the sorted list
printf("output:");
for(i=0;i<5;i++)
printf("\n%d ",n[i]);
getch();
}
/*......STANDARD DEVIATION AND VARIATION ........*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n;
float x[20],sum;
float am,var,sd;
int i;
printf("\n enter the no. of terms=");
scanf("%d",&n);
printf("\n enter %d values \n",n);
for(i=0;i<n;i++)
scanf("%f",&x[i]);
sum=0.0;
for(i=0;i<n;i++)
sum = sum+x[i];
am = sum/n;
sum = 0.0;
for(i=0;i<n;i++)
sum = sum+((x[i]-am)*(x[i]-am));
var = sum/n;
sd = sqrt(var);
printf("\n ARITHMETIC MEAN, STD. DEVIATION AND VARIANCE =");
printf("%f\t%f\t%f",am,var,sd);
getch();
}
OUTPUT:
Enter the no. of terms= 5
Enter 5 values
4
9
8
6
12
Arithmetic MEAN, STD. DEVIATION and VARIANCE =7.800000 7.360000
2.712932
/*BISECTION METHOD*/
Page 1

NACP.txt
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{ float fun(float m);
float x1,x2,x3,p,q,r;
int i=0;
clrscr();
l10: printf("\nequation:x*(exp(x)-1) ");
printf("\nenter the app value of x1,x2:");
scanf("%f %f",&x1,&x2);
if(fun(x1)*fun(x2)>0)
{ printf("\n wrong values entered...enter again:\n");
goto l10;}
else
printf("\n the root lies b/w %f & %f",x1,x2);
printf("\n n x1
x2
x3
f(x1)
f(x2)
f(x3)");
l15: x3=(x1+x2)/2;
p=fun(x1);
q=fun(x2);
r=fun(x3);
i=i++;
printf("\n%d
%f
%f
%f
%f
%f
%f",i,x1,x2,x3,p,q,r);
if((p*r)>0)
x1=x3;
else
x2=x3;
if((fabs((x2-x1)/x2))<=0.001)
{printf("\n root of the equ is %f",x3);
getch();
exit(0);}
else goto l15;
}
float fun(float m)
{float g;
g=(m*(exp(m))-1);
return(g);
}
/*output:
equation:x*(exp(x)-1)
enter the app value of x1,x2:0.5 1
the root lies b/w 0.500000 & 1.000000
n
x1
x2
x3
1
0.500000
1.000000
0.750000
2
0.500000
0.750000
0.625000
3
0.500000
0.625000
0.562500
4
0.562500
0.625000
0.593750
5
0.562500
0.593750
0.578125
6
0.562500
0.578125
0.570312
7
0.562500
0.570312
0.566406
8
0.566406
0.570312
0.568359
9
0.566406
0.568359
0.567383
10
0.566406
0.567383
0.566895
root of the equ is 0.566895

f(x1)
-0.175639
-0.175639
-0.175639
-0.012782
-0.012782
-0.012782
-0.012782
-0.002035
-0.002035
-0.002035

/*NEWTON RAPHSON*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{float f(float a);
float df(float a);
int i=1;
float x0,x1,p,q;
float error =0.0001,delta =0.001;
clrscr();
printf("\n\nThe equation is X^3+1.2X^2-5X-7.2");
Page 2

f(x2)
1.718282
0.587750
0.167654
0.167654
0.075142
0.030619
0.008780
0.008780
0.003364
0.000662

f(x3)
0.587750
0.167654
-0.012782
0.075142
0.030619
0.008780
-0.002035
0.003364
0.000662
-0.000687

NACP.txt
printf("\nenter the initial value of x0:");
scanf("%f",&x0);
printf("\n i
x0
x1
f(x0)
df(x0)\n ");
if (fabs (df(x0))<delta)
{printf("slope is very small and= %f",df(x0));}
else a10: p=f(x0);q=df(x0);
x1=x0-(p/q);
i++;
printf("\n %d\t%f\t%f\t%f\t%f\n",i,x0,x1,p,q);
if(fabs((x1-x0)/x1)<=error)
{printf("\nThe root of equation X^3+1.2X^2-5X-7.2 is %f",x0);
getch();
exit(0);}
else
{x0=x1;
goto a10;}
}
/* Funcion sub program */
float f(float a)
{float g;
g = pow(a,3)+((1.2)*(pow(a,2)))-5*a-7.2;
return(g);}
float df(float a)
{float g1;
g1 = (3*pow(a,2))+(2.4*a)-5;
return(g1);
}
output:
The equation is X^3+1.2X^2-5X-7.2
enter the initial value of x0:2
i

x0

x1

f(x0)

df(x0)

2.000000

2.372881

-4.400000

11.800000

2.372881

2.313010

1.052938

17.586615

2.313010

2.311227

0.029603

16.601265

2.311227

2.311225

0.000026

16.572248

The root of equation X^3+1.2X^2-5X-7.2 is 2.311227


/*REGULA-FALSE METHOD */
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{return(3*x-cos(x)-1);
}
void main()
{ float f(float x);
double x1,x2,m;
int c=0;
clrscr();
printf("\n enter the first approximation :");
scanf("%f",&x1);
printf("\n enter the second approximation :");
scanf("%f",&x2);
if(f(x1)*f(x2)<0.0)
{
m=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
while(fabs(f(m))>=0.0001)
{
c++;
Page 3

NACP.txt
if(f(x1)*f(m)<0.0)
x2=m;
else
x1=m;
m=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
printf("\n\n the %d,ilteration is %f ",c,m);
}
printf("\n\n the answer is repeated at %d ilteration is %f",c,m);
}
else
printf("enter valid initial approximation :");
getch();
}
OUTPUT:
Enter the first approximation : 0
Enter the second approximation : 1
The 1,ilteration is 0.605959
The 2,ilteration is 0.607057
The 3,ilteration is 0.607100
The answer is repeated at 3 ilteration is 0.607100

/*NEWTON GREGORY FORWARD INTERPOLATION*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,j,k;
float mx[10],my[10],x,y=0,h,p,diff[20][20],y1,y2,y3,y4;
clrscr();
printf("\nenter no. of terms:");
scanf("%d",&n);
printf("\nenter values x\ty:\n");
for(i=0;i<n;i++)
scanf("%f%f",&mx[i],&my[i]);
printf("\nenter value of x at which y is to be calculated:");
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;i++)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
i=0;
do
{i++;
}while(mx[i]<x);
i--;
p=(x-mx[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=my[i]+y1+y2+y3+y4;
printf("\nwhen x=%6.4f,y=%6.8f",x,y);
getch();
}
OUTPUT:
enter no. of terms:5
enter values x y:
3
13
5
23
11
899
Page 4

NACP.txt
27
34

17315
35606

enter value of x at which y is to be calculated:7


when x=7.0000,y=899.00000000
programme no.24
/*NEWTON GREGORY BACKWARD INTERPOLATION*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,j,k;
float mx[10],my[10],x,x0=0,y0,sum=0,fun=1,h,p,diff[20][20],y1,y2,y3,y4;
clrscr();
printf("\nenter no. of terms:");
scanf("%d",&n);
printf("\nenter values x\ty:\n");
for(i=0;i<n;i++)
scanf("%f%f",&mx[i],&my[i]);
printf("\nenter value of x at which y is to be calculated:");
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;i++)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
i=0;
while(!mx[i]>x)
i++;
x0=mx[i];
y0=my[i];
p=(x-x0)/h;
sum=y0;
for(k=1;k<=4;k++)
{
fun=(fun*(p-(k-1)))/k;
sum=sum+fun*diff[i][k];
}

NACP.txt
#define max 50
void main()
{
float ax[max],ay[max],nr,dr,x,y=0;
int i,j,n;
clrscr();
printf("\nEnter No. of Points:");
scanf("%d",&n);
printf("\nEnter the given set of values:\nx\ty\n");
for(i=0;i<n;i++)
scanf("%f%f",&ax[i],&ay[i]);
printf("\nEnter the value of x at which f(x)is required:");
scanf("%f",&x);
for(i=0;i<n;i++)
{
nr=dr=1;
for(j=0;j<n;j++)
if(j!=i)
{
nr*=x-ax[j];
dr*=ax[i]-ax[j];
}
y+=(nr/dr)*ay[i];
}
printf("\nwhen x=%5.2f then y=%5.2f",x,y);
getch();
}
output:
Enter No. of Points:6
Enter the given set of values:
x
y
4
18
5
100
7
294
10
900
11
1210
13
2028
Enter the value of x at which f(x)is required:8
when x= 8.00 then y=445.62

printf("\nwhen x=%6.4f,y=%6.8f",x,sum);
getch();
}
OUTPUT:
enter no. of terms:5
enter values x y:
20
41
40
103
60
168
80
218
100
235

/* NEWTON DIVIDED DIFFERENCE METHOD */


#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
float ax[20], ay[20], diff[30],temp=1;
int n,j,m,z=0,A=0,k=0;
clrscr();
cout<<"Enter the number of points=";
cin>>n;
for (int i=0;i<n;i++)
{

enter value of x at which y is to be calculated:70


when x=70.0000,y=196.00000000
/*LAGRANGE METHOD OF INTERPOLATION*/
#include<stdio.h>
#include<conio.h>
#include<math.h>

cout<<"Enter ( x"<<i+1<<" ,y"<<i+1<<" )\n";


cin>>ax[i]>>ay[i];
}
cout<<"Enter the value of x=";
cin>>ax[n];
// creating difference table
for (i=0;i<n-1;i++)
Page 5

Page 6

NACP.txt
{
diff[i]= (ay[i+1]-ay[i])/(ax[i+1]-ax[i]);
}
if(n>1)
{
m=n-1;
A=0;
for(j=0;j<n-2;j++)
{
for(z=0;z<m-1;i++,z++)
{
diff[i]= (diff[z+1+A]-diff[z+A])/(ax[z+j+2]-ax[z]);
}
A+=m;
m--;
}
}
//printing difference table
cout<<"\n difference table is as follows:\n";
for(z=0;z<i;z++)
cout<<"\n"<<diff[z];
// now calculating value of y for x
ay[n]=ay[0];
m=n;
A=0;
for (z=0;z<n-1;z++)
{
temp*=(ax[n]-ax[z]);
ay[n]+=temp*diff[z+A];
A+=m-2;
m--;
}
cout<<"\n\n The value of y for x = "<<ax[n]<<" is :"<<ay[n];
getch();
}

OUTPUT:
Enter the number of points=5
Enter (x1 ,y1 ) 5 150
Enter (x2 ,y2 ) 7 392
Enter (x3 ,y3 ) 11 1452
Enter (x4 ,y4 ) 13 2366
Enter (x5 ,y5 ) 17 5202
Enter the value of x=9
The difference table is as follows:
121
265
457
709
24
32
42
1
1
0
The value of y for x = 9 is: 810
/******BESSEL'S METHOD OF INTERPOLATION******/
#include<stdio.h>
Page 7

NACP.txt
#include<conio.h>
#include<math.h>
void main()
{
int n , i , j ;
float ax[10] , ay[10] , x , y=0 , h , p , diff[20][20] , y1 , y2 , y3 , y4
;
clrscr();
printf("\nEnter the noumber of item : ");
scanf("%d" , &n);
printf("\nEnter the value in the form of x\n");
for(i = 0 ; i < n ; i++)
{ printf("\nEnter the value of x%d\t" , i+1);
scanf("%f" , &ax[i]);
}
printf("\nEnter the value in the form of y\n");
for(i = 0 ; i < n ; i++)
{ printf("\nEnter the value of y%d\t" , i+1);
scanf("%f" , &ay[i]);
}
printf("\nEnter the value of x for which 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 <= n ; j++)
for(i = 0 ; i < -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][2] + diff[i - 1][2]) / 4;
y3 = p * (p - 1) * (p - 0.5) * (diff[i-1][3]) / 6;
y4 = (p + 1) * p * (p - 1) * (p - 2) * (diff[i-2][4] + diff[i - 1][4])
/ 48;
y = ay[i] + y1 +y2 + y3 + y4;
printf("\nWhen x = %6.4f , y = %6.8f" , x , y);
getch();
}
OUTPUT :Enter number of item : 4
Enter the value in the form of x
Enter
Enter
Enter
Enter

the
the
the
the

value
value
value
value

of
of
of
of

x1
x2
x3
x4

20
24
28
32

Enter the value in the form of y


Enter
Enter
Enter
Enter

the
the
the
the

value
value
value
value

of
of
of
of

y1
y2
y3
y4

24
32
35
40

Enter the value of x for which you want the value of y :- 25


When x = 25.0000 , y = 32.945313
/******STIRLING METHOD OF INTERPOLATION******/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{ int n , i , j ;
float ax[10] , ay[10] , x , y=0 , h , p , diff[20][20] , y1 , y2 ,
Page 8

NACP.txt
y3 ,y4;
clrscr();
printf("\nEnter the noumber of item : ");
scanf("%d" , &n);
printf("\nEnter the value in the form of x\n");
for(i = 0 ; i < n ; i++)
{printf("\nEnter the value of x%d\t" , i+1);
scanf("%f" , &ax[i]); }
printf("\nEnter the value in the form of y\n");
for(i = 0 ; i < n ; i++)
{printf("\nEnter the value of y%d\t" , i+1);
scanf("%f" , &ay[i]); }
printf("\nEnter the value of x for which 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 <= n ; j++)
for(i = 0 ; i < -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]) / 12 ;
y4 = p * p * (p * p - 1) * diff[i-2][4] / 24;
y = ay[i] + y1 +y2 + y3 + y4;
printf("\nWhen x = %6.4f , y = %6.8f" , x , y);
getch();
}
OUTPUT :
Enter the number of item : 4
Enter the value in the form of x
Enter
Enter
Enter
Enter

the
the
the
the

value
value
value
value

of
of
of
of

Enter the value in


Enter the value of
Enter the value of
Enter the value of
Enter the value of
Enter the value of
When x = 25.0000 ,

x1
x2
x3
x4

20
24
28
32

the form of y
y1
24
y2
32
y3
35
y4
40
x for which you want the value of y :- 25
y = 33.31251144

/* TO SORT A LIST OF 5 NUMBERS IN ASCENDING ORDER*/


#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,t;
int n[5]; clrscr();
//to enter the data
printf("\n enter the 5 numbers to be sorted\n");
for(i=0;i<5;i++)
scanf("%d",&n[i]);
//the sorting operation
for(j=0;j<5;j++)
for(i=0;i<4-j;i++)
Page 9

NACP.txt

if(n[i]>n[i+1])
{t=n[i];
n[i]=n[i+1];
n[i+1]=t;
}
//to print the sorted list
printf("output:");
for(i=0;i<5;i++)
printf("\n%d ",n[i]);
getch();

/*......STANDARD DEVIATION AND VARIATION ........*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int n;
float x[20],sum;
float am,var,sd;
int i;
printf("\n enter the no. of terms=");
scanf("%d",&n);
printf("\n enter %d values \n",n);
for(i=0;i<n;i++)
scanf("%f",&x[i]);
sum=0.0;
for(i=0;i<n;i++)
sum = sum+x[i];
am = sum/n;
sum = 0.0;
for(i=0;i<n;i++)
sum = sum+((x[i]-am)*(x[i]-am));
var = sum/n;
sd = sqrt(var);
printf("\n ARITHMETIC MEAN, STD. DEVIATION AND VARIANCE =");
printf("%f\t%f\t%f",am,var,sd);
getch();
}
OUTPUT:
Enter the no. of terms= 5
Enter 5 values
4
9
8
6
12
Arithmetic MEAN, STD. DEVIATION and VARIANCE =7.800000 7.360000
2.712932
/*BISECTION METHOD*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{ float fun(float m);
float x1,x2,x3,p,q,r;
int i=0;
clrscr();
l10: printf("\nequation:x*(exp(x)-1) ");
printf("\nenter the app value of x1,x2:");
scanf("%f %f",&x1,&x2);
if(fun(x1)*fun(x2)>0)
{ printf("\n wrong values entered...enter again:\n");
goto l10;}
Page 10

NACP.txt
else
printf("\n the root lies b/w %f & %f",x1,x2);
printf("\n n x1
x2
x3
f(x1)
f(x2)
f(x3)");
l15: x3=(x1+x2)/2;
p=fun(x1);
q=fun(x2);
r=fun(x3);
i=i++;
printf("\n%d
%f
%f
%f
%f
%f
%f",i,x1,x2,x3,p,q,r);
if((p*r)>0)
x1=x3;
else
x2=x3;
if((fabs((x2-x1)/x2))<=0.001)
{printf("\n root of the equ is %f",x3);
getch();
exit(0);}
else goto l15;
}
float fun(float m)
{float g;
g=(m*(exp(m))-1);
return(g);
}
/*output:
equation:x*(exp(x)-1)
enter the app value of x1,x2:0.5 1

NACP.txt
{x0=x1;
goto a10;}
}
/* Funcion sub program */
float f(float a)
{float g;
g = pow(a,3)+((1.2)*(pow(a,2)))-5*a-7.2;
return(g);}
float df(float a)
{float g1;
g1 = (3*pow(a,2))+(2.4*a)-5;
return(g1);
}

the root lies b/w 0.500000 & 1.000000


n
x1
x2
x3
1
0.500000
1.000000
0.750000
2
0.500000
0.750000
0.625000
3
0.500000
0.625000
0.562500
4
0.562500
0.625000
0.593750
5
0.562500
0.593750
0.578125
6
0.562500
0.578125
0.570312
7
0.562500
0.570312
0.566406
8
0.566406
0.570312
0.568359
9
0.566406
0.568359
0.567383
10
0.566406
0.567383
0.566895
root of the equ is 0.566895

The root of equation X^3+1.2X^2-5X-7.2 is 2.311227

f(x1)
-0.175639
-0.175639
-0.175639
-0.012782
-0.012782
-0.012782
-0.012782
-0.002035
-0.002035
-0.002035

f(x2)
1.718282
0.587750
0.167654
0.167654
0.075142
0.030619
0.008780
0.008780
0.003364
0.000662

f(x3)
0.587750
0.167654
-0.012782
0.075142
0.030619
0.008780
-0.002035
0.003364
0.000662
-0.000687

/*NEWTON RAPHSON*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{float f(float a);
float df(float a);
int i=1;
float x0,x1,p,q;
float error =0.0001,delta =0.001;
clrscr();
printf("\n\nThe equation is X^3+1.2X^2-5X-7.2");
printf("\nenter the initial value of x0:");
scanf("%f",&x0);
printf("\n i
x0
x1
f(x0)
df(x0)\n ");
if (fabs (df(x0))<delta)
{printf("slope is very small and= %f",df(x0));}
else a10: p=f(x0);q=df(x0);
x1=x0-(p/q);
i++;
printf("\n %d\t%f\t%f\t%f\t%f\n",i,x0,x1,p,q);
if(fabs((x1-x0)/x1)<=error)
{printf("\nThe root of equation X^3+1.2X^2-5X-7.2 is %f",x0);
getch();
exit(0);}
else
Page 11

output:
The equation is X^3+1.2X^2-5X-7.2
enter the initial value of x0:2
i

x0

x1

f(x0)

df(x0)

2.000000

2.372881

-4.400000

11.800000

2.372881

2.313010

1.052938

17.586615

2.313010

2.311227

0.029603

16.601265

2.311227

2.311225

0.000026

16.572248

/*REGULA-FALSE METHOD */
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{return(3*x-cos(x)-1);
}
void main()
{ float f(float x);
double x1,x2,m;
int c=0;
clrscr();
printf("\n enter the first approximation :");
scanf("%f",&x1);
printf("\n enter the second approximation :");
scanf("%f",&x2);
if(f(x1)*f(x2)<0.0)
{
m=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
while(fabs(f(m))>=0.0001)
{
c++;
if(f(x1)*f(m)<0.0)
x2=m;
else
x1=m;
m=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
printf("\n\n the %d,ilteration is %f ",c,m);
}
printf("\n\n the answer is repeated at %d ilteration is %f",c,m);
}
else
printf("enter valid initial approximation :");
getch();
}
Page 12

NACP.txt
OUTPUT:
Enter the first approximation : 0
Enter the second approximation : 1
The 1,ilteration is 0.605959
The 2,ilteration is 0.607057
The 3,ilteration is 0.607100
The answer is repeated at 3 ilteration is 0.607100

/*NEWTON GREGORY FORWARD INTERPOLATION*/


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,j,k;
float mx[10],my[10],x,y=0,h,p,diff[20][20],y1,y2,y3,y4;
clrscr();
printf("\nenter no. of terms:");
scanf("%d",&n);
printf("\nenter values x\ty:\n");
for(i=0;i<n;i++)
scanf("%f%f",&mx[i],&my[i]);
printf("\nenter value of x at which y is to be calculated:");
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;i++)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
i=0;
do
{i++;
}while(mx[i]<x);
i--;
p=(x-mx[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=my[i]+y1+y2+y3+y4;
printf("\nwhen x=%6.4f,y=%6.8f",x,y);
getch();
}
OUTPUT:
enter no. of terms:5
enter values x y:
3
13
5
23
11
899
27
17315
34
35606
enter value of x at which y is to be calculated:7
when x=7.0000,y=899.00000000
programme no.24
/*NEWTON GREGORY BACKWARD INTERPOLATION*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
Page 13

NACP.txt
int n,i,j,k;
float mx[10],my[10],x,x0=0,y0,sum=0,fun=1,h,p,diff[20][20],y1,y2,y3,y4;
clrscr();
printf("\nenter no. of terms:");
scanf("%d",&n);
printf("\nenter values x\ty:\n");
for(i=0;i<n;i++)
scanf("%f%f",&mx[i],&my[i]);
printf("\nenter value of x at which y is to be calculated:");
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;i++)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
i=0;
while(!mx[i]>x)
i++;
x0=mx[i];
y0=my[i];
p=(x-x0)/h;
sum=y0;
for(k=1;k<=4;k++)
{
fun=(fun*(p-(k-1)))/k;
sum=sum+fun*diff[i][k];
}
printf("\nwhen x=%6.4f,y=%6.8f",x,sum);
getch();
}
OUTPUT:
enter no. of terms:5
enter values x y:
20
41
40
103
60
168
80
218
100
235
enter value of x at which y is to be calculated:70
when x=70.0000,y=196.00000000
/*LAGRANGE METHOD OF INTERPOLATION*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 50
void main()
{
float ax[max],ay[max],nr,dr,x,y=0;
int i,j,n;
clrscr();
printf("\nEnter No. of Points:");
scanf("%d",&n);
printf("\nEnter the given set of values:\nx\ty\n");
for(i=0;i<n;i++)
scanf("%f%f",&ax[i],&ay[i]);
printf("\nEnter the value of x at which f(x)is required:");
scanf("%f",&x);
for(i=0;i<n;i++)
Page 14

NACP.txt
{
nr=dr=1;
for(j=0;j<n;j++)
if(j!=i)
{
nr*=x-ax[j];
dr*=ax[i]-ax[j];
}
y+=(nr/dr)*ay[i];
}
printf("\nwhen x=%5.2f then y=%5.2f",x,y);
getch();
}
output:
Enter No. of Points:6
Enter the given set of values:
x
y
4
18
5
100
7
294
10
900
11
1210
13
2028
Enter the value of x at which f(x)is required:8
when x= 8.00 then y=445.62
/* NEWTON DIVIDED DIFFERENCE METHOD */
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
float ax[20], ay[20], diff[30],temp=1;
int n,j,m,z=0,A=0,k=0;
clrscr();
cout<<"Enter the number of points=";
cin>>n;
for (int i=0;i<n;i++)
{
cout<<"Enter ( x"<<i+1<<" ,y"<<i+1<<" )\n";
cin>>ax[i]>>ay[i];
}
cout<<"Enter the value of x=";
cin>>ax[n];
// creating difference table
for (i=0;i<n-1;i++)
{
diff[i]= (ay[i+1]-ay[i])/(ax[i+1]-ax[i]);
}
if(n>1)
{
m=n-1;
A=0;
for(j=0;j<n-2;j++)
{
for(z=0;z<m-1;i++,z++)
{
diff[i]= (diff[z+1+A]-diff[z+A])/(ax[z+j+2]-ax[z]);
}
Page 15

NACP.txt
A+=m;
m--;
}
}
//printing difference table
cout<<"\n difference table is as follows:\n";
for(z=0;z<i;z++)
cout<<"\n"<<diff[z];
// now calculating value of y for x
ay[n]=ay[0];
m=n;
A=0;
for (z=0;z<n-1;z++)
{
temp*=(ax[n]-ax[z]);
ay[n]+=temp*diff[z+A];
A+=m-2;
m--;
}
cout<<"\n\n The value of y for x = "<<ax[n]<<" is :"<<ay[n];
getch();
}

OUTPUT:
Enter the number of points=5
Enter (x1 ,y1 ) 5 150
Enter (x2 ,y2 ) 7 392
Enter (x3 ,y3 ) 11 1452
Enter (x4 ,y4 ) 13 2366
Enter (x5 ,y5 ) 17 5202
Enter the value of x=9
The difference table is as follows:
121
265
457
709
24
32
42
1
1
0
The value of y for x = 9 is: 810
/******BESSEL'S METHOD OF INTERPOLATION******/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n , i , j ;
float ax[10] , ay[10] , x , y=0 , h , p , diff[20][20] , y1 , y2 , y3 , y4
;
clrscr();
printf("\nEnter the noumber of item : ");
scanf("%d" , &n);
printf("\nEnter the value in the form of x\n");
for(i = 0 ; i < n ; i++)
{ printf("\nEnter the value of x%d\t" , i+1);
scanf("%f" , &ax[i]);
}
printf("\nEnter the value in the form of y\n");
Page 16

NACP.txt
NACP.txt
for(i = 0 ; i < n ; i++)
scanf("%f" , &x);
{ printf("\nEnter the value of y%d\t" , i+1);
h = ax[1] - ax[0];
scanf("%f" , &ay[i]);
}
for(i = 0 ; i < n-1 ; i++)
printf("\nEnter the value of x for which you want the value of y :- ");
diff[i][1] = ay[i+1] - ay[i];
scanf("%f" , &x);
for(j = 2 ; j <= n ; j++)
h = ax[1] - ax[0];
for(i = 0 ; i < -j ; i++)
for(i = 0 ; i < n-1 ; i++)
diff[i][j] = diff[i+1][j-1] - diff[i][j-1];
diff[i][1] = ay[i+1] - ay[i];
i=0;
for(j = 2 ; j <= n ; j++)
do
for(i = 0 ; i < -j ; i++)
{ i++;
diff[i][j] = diff[i+1][j-1] - diff[i][j-1];
} while(ax[i] < x);
i--;
i=0;
p = (x - ax[i]) / h;
do
y1 = p * (diff[i][1] + diff[i - 1][1]) / 2;
{i++;
y2 = p * p * (diff[i - 1][2]) / 2;
}while(ax[i] < x); i--;
y3 = p * (p * p - 1) * (diff[i - 1 ][3] + diff[i - 2][3]) / 12 ;
p = (x - ax[i]) / h;
y4 = p * p * (p * p - 1) * diff[i-2][4] / 24;
y1 = p * diff[i][1];
y = ay[i] + y1 +y2 + y3 + y4;
y2 = p * (p - 1) * (diff[i][2] + diff[i - 1][2]) / 4;
printf("\nWhen x = %6.4f , y = %6.8f" , x , y);
y3 = p * (p - 1) * (p - 0.5) * (diff[i-1][3]) / 6;
getch();
y4 = (p + 1) * p * (p - 1) * (p - 2) * (diff[i-2][4] + diff[i - 1][4]) }
/ 48;
y = ay[i] + y1 +y2 + y3 + y4;
OUTPUT :
printf("\nWhen x = %6.4f , y = %6.8f" , x , y);
Enter the number of item : 4
getch();
}
Enter the value in the form of x
OUTPUT :Enter number of item : 4
Enter the value of x1
20
Enter the value of x2
24
Enter the value in the form of x
Enter the value of x3
28
Enter the value of x4
32
Enter the value of x1
20
Enter the value of x2
24
Enter the value in the form of y
Enter the value of x3
28
Enter the value of y1
24
Enter the value of x4
32
Enter the value of y2
32
Enter the value of y3
35
Enter the value in the form of y
Enter the value of y4
40
Enter the value of x for which you want the value of y :- 25
Enter the value of y1
24
When x = 25.0000 , y = 33.31251144
Enter the value of y2
32
Enter the value of y3
35
/* CROUTS METHOD */
Enter the value of y4
40
# include<stdio.h>
# include<conio.h>
Enter the value of x for which you want the value of y :- 25
# define n 4
typedef float matrix[n][n];
When x = 25.0000 , y = 32.945313
matrix l,u,a;
float b[n],x[n],y[n];
void urow(int i)
/******STIRLING METHOD OF INTERPOLATION******/
{
#include<stdio.h>
float s;
#include<conio.h>
int j,k;
#include<math.h>
for(j=i;j<n;j++)
void main()
{
{ int n , i , j ;
s=0;
float ax[10] , ay[10] , x , y=0 , h , p , diff[20][20] , y1 , y2 ,
for(k=0;k<n-1;k++)
y3 ,y4;
s+=u[k][j]*l[i][k];
clrscr();
u[i][j]=a[i][j]-s;
printf("\nEnter the noumber of item : ");
}
scanf("%d" , &n);
}
printf("\nEnter the value in the form of x\n");
for(i = 0 ; i < n ; i++)
void lcol(int j)
{printf("\nEnter the value of x%d\t" , i+1);
{
scanf("%f" , &ax[i]); }
float s;
printf("\nEnter the value in the form of y\n");
int i,k;
for(i = 0 ; i < n ; i++)
for(i=j+1;i<n;i++)
{printf("\nEnter the value of y%d\t" , i+1);
{
scanf("%f" , &ay[i]); }
s=0;
printf("\nEnter the value of x for which you want the value of y
for(k=0;k<=j;k++)
:- ");
s+=u[k][j]*l[i][k];
Page 17
Page 18

NACP.txt
l[i][j]=(a[i][j]-s)/u[j][j];

NACP.txt
printf("\n\t\tL\n");
printmat(l);
solveLY_B();
solveUX_Y();
printf("The solution is:\n");
for(i=0;i<n;i++)
printf("x[%d]=%6.4f\n",i+1,x[i]);
getch();

}
}
void printmat(matrix m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%8.4f",m[i][j]);
printf("\n");
}
}

}
OUTPUTEnter the elements of augmented matrix
5 1 1 1 4
1 7 1 4 6
1 1 6 1 -5
1 1 1 4 -6

void readmat()
{
int i,j;
printf("Enter the elements of augmented matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
scanf("%f",&b[i]);
}
}
void solveLY_B()
{
int i,j;
float s;
for(i=0;i<n;i++)
{
s=0;
for(j=0;j<i;j++)
s+=l[i][j]*y[j];
y[i]=b[i]-s;
}
}
void solveUX_Y()
{
int i,j;
float s;
for(i=n-1;i>=0;i--)
{
s=0;
for(j=i+1;j<n;j++)
s+=u[i][j]*x[j];
x[i]=(y[i]-s)/u[i][i];
}
}
void main()
{
int i,j,m;
clrscr();
readmat();
for(i=0;i<n;i++)
l[i][i]=1.0;
for(m=0;m<n;m++)
{
urow(m);
if(m<n-1)
lcol(m);
}
printf("\n\t\tU\n");
printmat(u);
Page 19

U
1.0000
6.8000
0.0000
0.0000
L
1.0000 0.0000
0.2000 1.0000
0.2000 0.1176
0.2000 0.1176
The solution is:
x[1]=1.0000
x[2]=2.0000
x[3]=-1.0000
x[4]=-2.0000
5.0000
0.0000
0.0000
0.0000

1.0000
0.8000
5.7059
0.0000

1.0000
3.8000
0.3529
3.3093

0.0000
0.0000
1.0000
0.1237

0.0000
0.0000
0.0000
1.0000

/* MILNE'S PREDICTOR CORRECTOR */


# include<stdio.h>
# include<conio.h>
# include<math.h>
float x[5],y[5],h;
float f(int i)
{
return x[i]-y[i]*y[i];
}
void correct()
{
y[4]=y[2]+(h/3)*(f(2)+4*f(3)+f(4));
printf("%23s %8.4f %8.4f \n","",y[4],f(4));
}
main()
{
float xr,aerr,yc;
int i;
clrscr();
puts("Enter the values of x0,xr,h,allowed error");
scanf("%f %f %f %f",&x[0],&xr,&h,&aerr);
puts("Enter the values of y[i],i=0,3");
for(i=0;i<=3;i++) scanf("%f",&y[i]);
for(i=1;i<=3;i++) x[i]=x[0]+i*h;
puts(" x Predicated
Corrected");
puts("
y
f
y
f");
while(1)
{
if(x[3]==xr) return;
x[4]=x[3]+h;
y[4]=y[0]+(4*h/3)*(2*(f(1)+f(3))-f(2));
printf("%6.2f %8.4f %8.4f\n",x[4],y[4],f(4));
correct();
while(1)
{
yc=y[4];
Page 20

NACP.txt
correct();
if(fabs(yc-y[4])<=aerr)break;
}
for(i=0;i<=3;i++)
{
x[i]=x[i+1];
y[i]=y[i+1];
}
}
getch();
}
OUTPUT
Enter the values of x0,xr,h,allowed error
0 1 .2 .0001
Enter the values of y[i],i=0,3
0 .02 .0795 .1762
x
Predicated
Corrected
y
f
y
f
0.80
0.3049
0.7070
0.3046
0.7072
0.3046
0.7072
1.00
0.4554
0.7926
0.4556
0.7925
0.4556
0.7925

NACP.txt
Enter
Enter
Enter
Enter
Enter
Enter

the
the
the
the
the
the

values of x
value of x1
value of x2
value of x3
value of x4
value of x5

:
:
:
:
:
:

Enter
Enter
Enter
Enter
Enter
Enter

the
the
the
the
the
the

values of y :
value of y1: 14
value of y2: 27
value of y3: 40
value of y4: 55
value of y5: 68

1
2
3
4
5

Equation of the STRAIGHT LINE of the form y=a+b*x is :


y=0 + 13.6

/* CURVE FITTING - STRAIGHT LINE */


# include<iostream.h>
# include<conio.h>
# include<math.h>
void main()
{
int i=0,ob;
float x[10],y[10],xy[10],x2[10],sum1=0,sum2=0,sum3=0,sum4=0;
double a,b;
printf("\n Enter the no. of observations :");
scanf(%d,&ob);
printf("\n Enter the values of x :\n");
for (i=0;i<ob;i++)
{
Printf("\n Enter the value of x[%d]",i+1);
Scanf(%f,&x[i]);
sum1+=x[i];
}
Printf(\n Enter the values of y :\n");
for (i=0;i<ob;i++)
{
Printf("\n Enter the value of y[%d]",i+1);
Scanf(%f,&y[i];
sum2+=y[i];
}
for(i=0;i<ob;i++)
{
xy[i]=x[i]*y[i];
sum3+=xy[i];
}
for(i=0;i<ob;i++)
{ x2[i]=x[i]*x[i];
sum4+=x2[i];
}
a=(sum2*sum4-sum3*sum1)/(ob*sum4-sum1*sum1);
b=(sum2-ob*a)/sum1;
printf("\n\n Equation of the STRAIGHT LINE of the form y=a+b*x is );
printf("\n\n\t\t\t y=[%f] + [%f]x.a,b);
}
OUTPUT:
Enter the no. of observations : 5
Page 21

Page 22

You might also like