#include<stdio.h>
#include<conio.h>
#include<math.h>
float sim_1(float,float,int);
float sim_2(float,float,int);
int i;
float sum;
float fun(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
float x0,x1,sum,result;
int n,cho=0;
clrscr();
printf("enter the lower and upper limit \n");
scanf("%f%f",&x0,&x1);
printf("enter number of intervals:\n");
scan:scanf("%d",&n);
if(cho==0)
{
top:printf("\t enter choice \n");
printf("\n 1.for simpsons 1/3 rule\n");
printf("\n 2.for simpsons 3/8 rule\n");
scanf("%d",&cho);
}
switch(cho)
{
case 1:if(n%2==0)
result=sim_1(x0,x1,n);
else
{
printf("wrong choice of interval");
printf("please enter even number");
goto scan;
}
break;
case 2:if(n%3==0)
result=sim_2(x0,x1,n);
else
{
printf("wrong choice of intervals \n ");
printf("\n please enter a multiple of 3 \n");
goto scan;
}
break;
default:printf("\n wrong choice enter again:");
goto top;
}
printf("\n the result=%f",result);
getch();
}
float sim_1(float x0,float x1,int n)
{
float result,h;
h=(x1-x0)/n;
sum=fun(x0)+fun(x1);
for(i=1;i<n;i++)
{
if(i%2==0)
sum=sum+2*fun(x0+i*h);
else
sum=sum+4*fun(x0+i*h);
}
result=sum*(h/3);
return(result);
}
float sim_2(float x0,float x1,int n)
{
float result,h;
h=(x1-x0)/n;
sum=fun(x0)+fun(x1);
for(i=1;i<n;i++)
{
if(i%3==0)
sum=sum+2*fun(x0+i*h);
else
sum=sum+3*fun(x0+i*h);
}
result=sum*(3*h/8);
return(result);
}
#include<conio.h>
#include<math.h>
float sim_1(float,float,int);
float sim_2(float,float,int);
int i;
float sum;
float fun(float x)
{
return(1/(1+pow(x,2)));
}
void main()
{
float x0,x1,sum,result;
int n,cho=0;
clrscr();
printf("enter the lower and upper limit \n");
scanf("%f%f",&x0,&x1);
printf("enter number of intervals:\n");
scan:scanf("%d",&n);
if(cho==0)
{
top:printf("\t enter choice \n");
printf("\n 1.for simpsons 1/3 rule\n");
printf("\n 2.for simpsons 3/8 rule\n");
scanf("%d",&cho);
}
switch(cho)
{
case 1:if(n%2==0)
result=sim_1(x0,x1,n);
else
{
printf("wrong choice of interval");
printf("please enter even number");
goto scan;
}
break;
case 2:if(n%3==0)
result=sim_2(x0,x1,n);
else
{
printf("wrong choice of intervals \n ");
printf("\n please enter a multiple of 3 \n");
goto scan;
}
break;
default:printf("\n wrong choice enter again:");
goto top;
}
printf("\n the result=%f",result);
getch();
}
float sim_1(float x0,float x1,int n)
{
float result,h;
h=(x1-x0)/n;
sum=fun(x0)+fun(x1);
for(i=1;i<n;i++)
{
if(i%2==0)
sum=sum+2*fun(x0+i*h);
else
sum=sum+4*fun(x0+i*h);
}
result=sum*(h/3);
return(result);
}
float sim_2(float x0,float x1,int n)
{
float result,h;
h=(x1-x0)/n;
sum=fun(x0)+fun(x1);
for(i=1;i<n;i++)
{
if(i%3==0)
sum=sum+2*fun(x0+i*h);
else
sum=sum+3*fun(x0+i*h);
}
result=sum*(3*h/8);
return(result);
}
No comments:
Post a Comment