Tuesday, August 2, 2016

PROGRAM FOR TRAPEZOIDAL RULE

/*PROGRAM FOR TRAPEZOIDAL RULE*/


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
float x0,xf,y,h;
float f(float);
clrscr();
printf("\n\nEnter the values of x0 & xf::");
scanf("%f%f",&x0,&xf);
printf("\n\nEnter the values of n::");
scanf("%d",&n);
h=(xf-x0)/n;
y=f(x0)+f(xf);
for(i=1;i<=n-1;i++)
{
y=y+2*f(x0+i*h);
}
y=y*h/2;
printf("\n\nThe result is::%f",y);
getch();
}
float f(float x)
{
return(1/(1+x*x));
}


/* ******************OUTPUT*****************


Enter the values of x0 & xf::0
1
Enter the values of n::6
The result is::0.784241*/

ALGORITHM OF TRAPEZOIDAL RULE


ALGORITHM OF TRAPEZOIDAL RULE

1-Enter value of lower limit (a) & upper limit (b)
2-Enter the no. of sub-intervals (n)
3-h = (b-a) / n
4- s = y(a) + y(b)
5- for (j=1 to n-1)
  {
   s = s+2*y(a+i*h)
   }
6-Result = (h/2) * s
7- Print result
8- Exit ( )

ALGORITHM OF SIMPSON 3/8 RULE

ALGORITHM OF SIMPSON 3/8 RULE


1-Enter value of upper limit (a) and lower limit(b)
2-Enter value of sub-interval (n)
3-h = (b-a) / n
4-s = y(a) +y(b)
5-for (j = 1 to n-1)
   if (j % 3 = = 0)
   s = s+2*y(a+j*h)
   else
   s= s +3*y(a+j*h)
6- result = 3*(h/8) * s
7-Print result
8-exit ( )

ALGORITHM OF SIMPSON’S 1/3 RULE


ALGORITHM OF SIMPSON’S 1/3 RULE

1-Enter value of upper limit & lower limit
2-Enter value of sub-interval(n)
3-h=(b-a)/n
4-s=y(a)+y(b)
5-For (j=1 to n-1) dø
   if( j%2==0)
  {
   s= s+2*y(a+j*h)
  else
  s=s+4*y(a+j*h)
  }
6-Result = (h/3)*s
7-Print  “result”
8- exit( )

PROGRAM FOR SIMPSON 3/8 RULE

/*PROGRAM FOR SIMPSON 3/8 RULE*/


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
float x0,xf,y,h;
float f(float);
clrscr();
printf("\n\nEnter the values of x0 & xf::");
scanf("%f%f",&x0,&xf);
printf("\n\nEnter the values of n::");
scanf("%d",&n);
if(n%3==0)
{
h=(xf-x0)/n;
y=f(x0)+f(xf);
for(i=1;i<=n-1;i++)
{
if(i%3==0)
y=y+2*f(x0+i*h);
else
y=y+3*f(x0+i*h);
}
y=(3*(y*h))/8;
printf("\n\nThe result is::%f",y);
}
else
goto X;
getch();
}
float f(float x)
{
return(1/(1+x*x));
}
/***************OUTPUT********************
Enter the values of x0 & xf::0
6
Enter the values of n::6

The result is::1.357081 */

PROGRAM FOR SIMPSON 1/3 RULE

/*PROGRAM FOR SIMPSON 1/3 RULE*/


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
float x0,xf,y,h;
float f(float);
clrscr();
printf("\n\nEnter the values of x0 & xf::");
scanf("%f%f",&x0,&xf);
printf("\n\nEnter the values of n::");
scanf("%d",&n);
h=(xf-x0)/n;
y=f(x0)+f(xf);
for(i=1;i<=n-1;i++)
{
y=y+2*f(x0+i*h);
}
y=y*h/2;
printf("\n\nThe result is::%f",y);
getch();
}
float f(float x)
{
return(1/(1+x*x));
}


/*************OUTPUT******************


 Enter the values of x0 & xf::0 6  
Enter the values of n::6
 The result is::1.410799 */               

PROGRAM FOR REGULA FALSI

/*PROGRAM FOR REGULA FALSI*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x0,x1,x2,e;
int n,i,g=0;
float f(float);
clrscr();
printf("\n enter the value of x0,x1,e,n:\n");
scanf("%f%f%f%d",&x0,&x1,&e,&n);
if((f(x0)*f(x1)<0)
{
for(i=1;i<=n;i++)
{
x2=(x0*f(x1)-x1*f(x0)/(f(x1)-f(x0));
}
if(fabs(f(x2))<e)
{
g=1;
break;
}
if(f(x2)*f(x0)<0)
x1=x2;
else
{
x0=x2;
printf("Iteration: %f\n",x2);
}
 }
 }
 if(g==0)
 printf("\n Root does not found in n iteration upto desired accuracy");
 else
 printf("\n ANSWER:Root=%f",x2);
 getch();
 }
 float f(float x)
 {
 return(x*x*x-2*x-5);
 }


 /****************************OUTPUT*************************/
 Enter the value of x0,x1,e,n:
 1
 3
 .000006
 80
 Iteration:1.545455
 Iteration:1.859163
 Iteration:2.002119
 Iteration:2.059644
 Iteration:2.081572
 Iteration:2.089974
 Iteration:2.092782
 Iteration:2.093899
 Iteration:2.094311
 Iteration:2.094463
 Iteration:2.094519
 Iteration:2.094539
 Iteration:2.094547
 Iteration:2.094550
 Iteration:2.094551
 ANSWER ROOT IS=2.094551*/

PROGRAM FOR NEWTON’S FORWARD DIFF. FORMULA

/*PROGRAM FOR NEWTON’S FORWARD DIFF. FORMULA*/


#include<stdio.h>
#include<conio.h>
#define n 4
void main()
{
float nr=1,dr=1,p,x,y;
float ax[n],ay[n],delta[n-1][n-1];
int i=0,j=0,k;
clrscr();
for(i=0;i<n;i++)
{
printf("\n Enter the value of ax and ay:\n");
scanf("%f%f",&ax,&ay);
}
printf("\n Enter the value of x:");
scanf("%f",&x);
for(i=0;i<n-1;i++)
{
delta[i][0]=(ay[i+1]-ay[i]);
printf("\n %f",delta[i][0]);
}
for(j=1;j<n-1;j++)
{
for(i=0;i<(n-1-j);i++)
{
delta[i][j]=delta[i+1][j-1]-delta[i][j-1];
printf("\n\n%f",delta[i][j]);
}
}
for(i=0;i<n-1;i++)
{
printf("\n\n");
}
for(i=0;i<n-1-j;i++)
{
printf("%f",delta[j][i]);
}
p=(x-ax[0]/ax[1]-ax[0]);
y=ay[0];
for(k=0;k<n-1;k++)
{
nr=nr*(p-k);
dr=dr*(k+1);
y+=((nr/dr)*delta[0][k]);
}
printf("ANSWER: VALUE OF Y AT%f IS %f",x,y);
getch();
}


/*OUTPUT
Enter the value of ax and ay:
35
13229
Enter the value of ax and ay:
45
31368
Enter the value of ax and ay:
55
55593
Enter the value of ax and ay:
65
87089
Enter the value of x:50
18139.000000
24225.000000
31496.000000
6086.000000
7271.000000
1185.000000
ANSWER: VALUE OF y AT 50.000000 IS 42645.687500*/