Numerical Integration

It is a programme , using which you can solve problems using simpson's one-third and trapezoidal rule....


#include<stdio.h>
#include<math.h>
void simpsons();
void trapezoidal();


void main()
{
int a;

printf("It is an application which can helps u to solve problems using simpson's one-third rule and trapezoidal rule....\nthis application uses for calculate area under a curve when the equation of f(x) is unknown.....\n");

printf("\nfor using simpson's one-third rule press 1\nfor using trapezoidal rule press 2\n\n");

scanf("%d",&a);

if(a==1)
{
simpsons();
}
else if(a==2)
{
trapezoidal();
}
else
{
printf("\n enter a valid keyword");
}

}


void simpsons()
{
float h,max,min,odd=0,even=0,res,sum=0,x[100],y[100];
int n,i;

printf("SIMPSON'S ONE-THIRD RULE :\nhow many intervals you want?   ");
scanf("%d",&n);

for(;n%2!=0;)
{
printf("\nplease enter a even number\n");
scanf("%d",&n);
}

printf("now enter the value of x and its corresponding value of y.\n\n");

for(i=0;i<=n;i++)
{
printf("\nx[%d]=",i);
scanf("%f",&x[i]);
printf("y[%d]=",i);
scanf("%f",&y[i]);
}

min=x[0];
max=x[n];


h=(max-min)/n;

for(i=1;i<=n-1;i=i+2)
{
odd=odd+y[i];
}

for(i=2;i<=n-2;i=i+2)
{
even=even+y[i];
}

sum=y[0]+y[n]+4*odd+2*even;

res=(h*sum)/3;
printf("\nresult = %f ",res);

return;

}

void trapezoidal()
{
float h,max,min,even=0,res,sum=0,x[100],y[100];
int n,i;

printf("TRAPEZOIDAL RULE:\nhow many intervals you want?   ");
scanf("%d",&n);


printf("now enter the value of x and its corresponding value of y.\n\n");

for(i=0;i<=n;i++)
{
printf("x[%d]=",i);
scanf("%f",&x[i]);
printf("y[%d]=",i);
scanf("%f",&y[i]);
}

min=x[0];
max=x[n];


h=(max-min)/n;


for(i=1;i<=n-1;i++)
{
even=even+y[i];
}

sum=y[0]+y[n]+2*even;

res=(sum*h)/2;
printf("\nresult = %f ",res);

return;

}

Post a Comment

0 Comments