Simpson 3/8 Using Function
Simpson 3/8 Using Function
S)
Sub. : Maths
Practical : Simpsons 3/8 Rule Using Function
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f (float x)
{
float z;
z=x*x-5*x+6;
return z;
}
void main()
{
int i,n;
float h,a,b,sum=0,sum1=0,sum2=0,I;
clrscr();
printf("\nEnter no. of values: \n");
scanf("%d",&n);
printf("\nEnter the values of a and b: \n");
{
scanf("%f%f", &a,&b);
}
h=(b-a)/n;
printf("\n The h= %f",h);
sum+=f(a)+f(b);
// printf("\n Sum = %f \n ", sum);
for(i=1;i<n;i++)
{
if(i%3==0)
{
sum2+=f(a+i*h);
// printf("\n Sum 2 = %f \n",sum2);
}
else
{
sum1+=f(a+i*h);
// printf("Sum 1 = %f \n",sum1);
}
}
I=((3*h/8)*(sum+3*(sum1)+2*(sum2)));
printf("\n The value of I= %f ",I);
getch();
}
Output :
Enter no. of values:
6
Enter the values of a and b:
1.5
6
The h= 0.750000
The value of I= 13.500000