CBNST Lab File
CBNST Lab File
TECHNOLOGY,
GREATER NOIDA
INDEX
S.No
Name of Program
10
11
12
Program 1
Date
Remark
Program 2
Program in C to find the best approximate value by Bisection method.
#include<stdio.h>
#include<math.h>
float fun (float x)
{
return (x*x*x + 3*x - 5);
}
void bisection (float *x, float a, float b, int *itr)
{
*x=(a+b)/2;
++(*itr);
printf("Iteration no. %3d X = %7.5f\n", *itr, *x);
}
void main ()
{
int itr = 0, maxmitr;
float x, a, b, allerr, x1;
printf("\nEnter the values of a, b, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &a, &b, &allerr, &maxmitr);
bisection (&x, a, b, &itr);
do {
if (fun(a)*fun(x) < 0)
b=x;
else
a=x;
bisection (&x1, a, b, &itr);
if (fabs(x1-x) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x1);
return 0;
}
x=x1;
}
while (itr < maxmitr);
printf("Either the solution does not converge or more iterations are required");
return 1; }
Program 3
Program in C to implement Regula Falsi method.
#include<stdio.h>
#include<math.h>
float f(float x)
{
return cos(x) - x*exp(x);
}
void regula (float *x, float x0, float x1, float fx0, float fx1, int *itr)
{
*x = x0 - ((x1 - x0) / (fx1 - fx0))*fx0;
++(*itr);
printf("Iteration no. %3d X = %7.5f \n", *itr, *x);
}
void main ()
{
int itr = 0, maxmitr;
float x0,x1,x2,x3,allerr;
printf("\nEnter the values of x0, x1, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &x0, &x1, &allerr, &maxmitr);
regula (&x2, x0, x1, f(x0), f(x1), &itr);
do {
if (f(x0)*f(x2) < 0)
x1=x2;
else
x0=x2;
regula (&x3, x0, x1, f(x0), f(x1), &itr);
if (fabs(x3-x2) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x3);
return 0;
}
x2=x3;
}
while (itr<maxmitr);
printf("Solution does not converge or iterations not sufficient:\n");
return 1; }
Program 4
Program in C to implement Newton Raphson method.
#include<stdio.h>
#include<math.h>
double F(double x)
{
return ((x)*(x)-5);
}
double Fd(double x)
{
return (2*(x));
}
int main()
{
double fncvalue;
double x0,h,err,root,x1;
int miter,iter=1;
clrscr();
printf("Enter the first approximation ,the max error and the maximum number of
iterations\n");
scanf("%lf%lf%d",&x0,&err,&miter);
while(iter<=miter)
{
h=F(x0)/Fd(x0);
x1=x0-h;//x1=x0-f(x)/f'(x)
printf("The approximation's value after %d iteration is %.12lf\n",iter,x1);
if(fabs(h)<err)
{
root=x1;
break;
}
else
x0=x1;
iter++;
}
if(root==x1)
{
printf("The root is: %.12lf\n",root);
fncvalue = F(root);
printf("Value of F(root) is: %.12lf",fncvalue);
}
else
printf("The unsufficent number of iteration);
getch();
}
Program 5
Program in C to implement Iterative Method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
float F(float x)
{
return (x*x*x + 1)/2;
}
float f(int x)
{
return x*x*x -2*x + 1;
}
void main()
{
int i=0,n;
float x1,x2,x0;
float f1,f2,f0,error,maxerr;
clrscr();
printf("\n Enter the no. of iterations and maximum error ");
scanf("%d%f",&n,&maxerr);
for(x1=1; ;x1++)
{
f1=f(x1);
if(f1>0)
break;
}
for(x0=x1-1; ;x0--)
{
f0=f(x0);
if(f0<0)
break;
}
x2=(x0+x1)/2;
printf("\n\t The 1 approximation to the root is : %f",x2);
for(;i<n-1;i++)
{
f2=F(x2);
printf("\n\t The %d approximatrion to the root is : %f",i+2,f2);
x2=F(x2);
error=fabs(f2-f1);
if(error<maxerr)
break;
f1=f2;
}
if(error>maxerr)
printf("\n\t number of iterations are not sufficient.");
printf("\n\t now root = %.4f",f2);
getch();
}
Program 6
Program in C to implement Newton Backward method.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
main()
{
float x[20],y[20],f,s,d,h,p;
int j,i,k,n;
printf("enter the value of the elements :");
scanf("%d",&n);
printf("enter the value of x: nn");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
}
printf("enter the value of y: nn");
for(i=1;i<=n;i++) {
scanf("%f",&y[i]);
}
h=x[2]-x[1];
printf("enter the searching point f:");
scanf("%f",&f);
s=(f-x[n])/h;
d=y[n];
p=1;
for(i=n,k=1;i>=1,k<n;i--,k++)
{
for(j=n;j>=1;j--)
{
y[j]=y[j]-y[j-1];
}
p=p*(s+k-1)/k;
d=d+p*y[n];
}
printf("for f=%f ,ans is=%f",f,d);
getch(); }
Program 7
Program in C to implement Newton Forward method.
#include<stdio.h>
#define MAX 100
#define ORDER 4
void main()
{
float ax[MAX+1], ay [MAX+1], diff[MAX+1][ORDER+1], nr=1.0, dr=1.0,x,p,h,yp;
int n,i,j,k;
printf("\nEnter the value of n:\n");
scanf("%d",&n);
printf("\nEnter the values in form x,y:\n");
for (i=0;i<=n;i++)
scanf("%f %f",&ax[i],&ay[i]);
printf("\nEnter the value of x for which the value of y is wanted: \n");
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<=ORDER;j++)
for(i=0;i<=n-j;i++)
diff[i][j] = diff[i+1][j-1] - diff[i][j-1];
i=0;
while (!(ax[i]>x))
i++;
i--;
p = (x-ax[i])/h;
yp = ay[i];
for (k=1;k<=ORDER;k++)
{
nr *=p-k+1;
dr *=k;
yp +=(nr/dr)*diff[i][k];
}
printf("\nWhen x = %6.1f, corresponding y = %6.2f\n",x,yp);
getch();
}
Program 8
Program in C to find the best approximate value by Gauss Backward method.
#include<stdio.h>
#include<conio.h>
#define max 30
void main()
{
float table[max][max];
int count, mid, i, j;
float x, n, h, y, p ;
for(i=0; i<max; i++)
for(j=0; j<max; j++)
table[i][j]=0;
printf("\n How many values are to be entered? \n");
scanf("%d",&count);
printf("\n Enter the values of x and y in tabular format: \n\n");
for(i=0; i<=2*count-2; i+=2)
for(j=0; j<=1; j++)
scanf("%f",&table[i][j]) ;
printf("\n Find the value of y at x = ? \n");
scanf("%f",&x);
for(i=2; i<=count; i++)
for(j=i-1, n=1; n<=count+1-i; j+=2, n++)
{
table[j][i]=table[j+1][i-1] - table[j-1][i-1];
}
if(count%2==1)
mid=count-1;
else
mid=count;
h=table[mid+2][0] - table[mid][0] ;
n=p=(x-table[mid][0])/h;
if(x>table[mid][0])
{
printf("\n CAN'T APPLY GAUSS FORWARD FORMULA \n");
}
else
{
printf("\n------ APPLYING GAUSS BACKWARD FORMULA ------\n");
y=table[mid][1] + n*table[mid-1][2];
for(i=3; i<=count; i++)
if(i%2==1)
{
n*=(p+(int)i/2)/(i-1);
y+=n*table[mid][i];
}
else
{
n*=(p-(int)i/2 +1)/(i-1);
y+=n*table[mid-1][i];
}
}
printf("\n CENTRAL DIFFERENCE TABLE \n");
for(i=0; i<=2*count-2; i++)
{
if(i%2==0)
printf("%2.3f %2.3f",table[i][0], table[i][1]);
else
printf("
");
for(j=2; j<=count; j++)
if((j%2==0 && i%2==1) || (j%2==1 && i%2==0))
{
if(i>=j-1 && i<2*count-j)
printf(" %2.3f",table[i][j]);
}
else
printf(" ");
printf("\n");
}
printf("\n\n At x=%f, y=%f \n",x,y);
getch();
}
Program 9
Program in C to find the best approximate value by Gauss Forward method.
#include<stdio.h>
#include<conio.h>
#define max 30
void main()
{
float table[max][max];
int count, mid, i, j;
float x, n, h, y, p ;
for(i=0; i<max; i++)
for(j=0; j<max; j++)
table[i][j]=0;
printf("\n How many values are to be entered? \n");
scanf("%d",&count);
printf("\n Enter the values of x and y in tabular format: \n\n");
for(i=0; i<=2*count-2; i+=2)
for(j=0; j<=1; j++)
scanf("%f",&table[i][j]) ;
printf("\n Find the value of y at x = ? \n");
scanf("%f",&x);
for(i=2; i<=count; i++)
for(j=i-1, n=1; n<=count+1-i; j+=2, n++)
{
table[j][i]=table[j+1][i-1] - table[j-1][i-1];
}
if(count%2==1)
mid=count-1;
else
mid=count;
h=table[mid+2][0] - table[mid][0] ;
n=p=(x-table[mid][0])/h;
if(x>table[mid][0])
{
printf("\n APPLYING GAUSS FORWARD FORMULA \n");
y=table[mid][1] + n*table[mid+1][2];
for(i=3; i<=count; i++)
if(i%2==1)
{
n*=(p-(int)i/2)/(i-1);
y+=n*table[mid][i];
}
else
{
n*=(p+(int)i/2 -1)/(i-1);
y+=n*table[mid+1][i];
}
}
else
{
printf("\n CAN'T APPLY GAUSS FORWARD FORMULA ------\n");
}
printf("\n CENTRAL DIFFERENCE TABLE \n");
for(i=0; i<=2*count-2; i++)
{
if(i%2==0)
printf("%2.3f %2.3f",table[i][0], table[i][1]);
else
printf("
");
for(j=2; j<=count; j++)
if((j%2==0 && i%2==1) || (j%2==1 && i%2==0))
{
if(i>=j-1 && i<2*count-j)
printf(" %2.3f",table[i][j]);
}
else
printf(" ");
printf("\n");
}
printf("\n\n At x=%f, y=%f \n",x,y);
getch();
}
Program 10
Program in C to implement Lagranges method of interpolation.
#include<stdio.h>
main()
{
float x[100],y[100],a,s=1,t=1,k=0;
int n,i,j,d=1;
printf("\n\n Enter the number of the terms of the table: ");
scanf("%d",&n);
printf("\n\n Enter the respective values of the variables x and y: \n");
for(i=0; i<n; i++)
{
scanf ("%f",&x[i]);
scanf("%f",&y[i]);
}
printf("\n\n The table you entered is as follows :\n\n");
for(i=0; i<n; i++)
{
printf("%0.3f\t%0.3f",x[i],y[i]);
printf("\n");
}
while(d==1)
{
printf(" \n\n\n Enter the value of the x to find the respective value of y\n\n\n");
scanf("%f",&a);
for(i=0; i<n; i++)
{
s=1;
t=1;
for(j=0; j<n; j++)
{
if(j!=i)
{
s=s*(a-x[j]);
t=t*(x[i]-x[j]);
}
}
k=k+((s/t)*y[i]);
}
printf("\n\n The respective value of the variable y is: %f",k);
printf("\n\n Do you want to continue?\n\n Press 1 to continue and any other key to
exit");
scanf("%d",&d);
}
}
Program 11
Program in C to Solve Equation using Bessel's Interpolation Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<conio.h>
void main()
{
int n;
int i,j;
float ax[10];
float ay[10];
float x;
float y;
float h;
float p;
float diff[20][20];
float y1,y2,y3,y4;
clrscr();
printf("\t\t !! BESSELS INTERPOLATION FORMULA !! ");
printf("\n\n Enter the no. of terms -> ");
scanf("%d",&n);
printf("\n\n Enter the value in the form of x -> ");
for(i=0;i<n;i++)
{
printf("\n Enter the value of x%d -> ",i+1);
scanf("%f",&ax[i]);
}
printf("\n\n Enter the value in the form of y -> ");
for(i=0;i<n;i++)
{
printf("\n Enter the value of y%d -> ",i+1);
scanf("%f",&ay[i]);
}
printf("\n\n Enter the value of x for ");
printf("\n which u 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][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("\n When x = %6.2f , y = %6.8f",x,y);
printf("\n\n\n\t\t\t !! PRESS ENTER TO EXIT !! ");
getch();
}
Program 12
Program in C to Solve Equation using Stirling's Interpolation Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
float x[10],y[10];
float delta(int i,int n)
{
if(n==1)
return(y[i+1]-y[i]);
else
return(delta(i+1,n-1)-delta(i,n-1));
}
void main()
{
int n,i,j,k=1,m,l;
float vx,vy=0,p,q;
clrscr();
printf("Enter how many no you want to enter:");
scanf("%d",&n);
printf("Enter values of x and y:-\n");
printf("x y\n");
for(i=0;i<n;i++)
{
scanf("%f %f",&x[i],&y[i]);
}
printf(" x y\n\n");
for(i=0;i<n;i++)
{
printf("%4.2f %4.2f",x[i],y[i]);
for(j=0;j<(n-k);j++)
{
printf(" %4.2f ",delta(i,j+1));
}
printf("\n\n");
k++;
}
for(i=0;i<n;i++)
{
printf("%.2f\t",y[i]);
}
printf("\n\nEnter value of x:");
scanf("%f",&vx);
m=0;
k=fabs(vx-x[0]);
for(i=1;i<n;i++)
{
if(k>(fabs(vx-x[i])))
{
m=i;
}
}
p=float((vx-x[m])/(x[1]-x[0]));
vy=y[m];
q=1;
k=-1;
if(m<((n/2)+(n%2)))
l=(2*m)+1;
else
l=((n-m-1)*2)+1;
for(i=1;i<(l-1);i++)
{
if(i%2==1)
{
q=p;
j=2;
k=i/2;
}
else
{
q=float((p*p)/2);
j=3;
k=(i/2)-1;
}
for(;j<=i;j++)
{
q=float((q*(p-k))/j);
if(k<0)
{
k++;
}
k=-k;
}
if(i%2==0)
{
vy=vy+(q*delta(m-(i/2),i));
}
else
{
vy=vy+(q*((delta(m-(i/2),i)+delta(m-(i/2)-1,i))/2));
}
printf("\nq:%f",q);
}
printf("\n\nAns is %f,l=%d",vy,l);
printf("m=%d",m);
getch();
}