Code for Simpson's 3/8 rule

#include <stdio.h>
#include <conio.h>
#include <math.h>

float f(float x)
{
  return (x*x+(sin(x)/x));
  //f(x)=x*x+sin(x)/x;
}

void main()
{
  float a,b,h,x,sum=0;
  int n,i,k;
  printf("Enter a and b: ");
  scanf("%f%f",&a,&b);
  printf("Here, n=3 for Simpson's 3/8 rule");
  printf("\nn>3 for Composite Simpson's 3/8 rule\n");
  printf("So, Enter n: ");
  scanf("%d",&n);
  h=(b-a)/n;
  for(x=a,i=0,k=3;x<=b,i<=n;x=x+h,i++,k=k+3)
  {
    if(i==0||i==n)
      sum=sum+f(x);
    else if(i==k)
      sum=sum+2*f(x);
    else
      sum=sum+3*f(x);
  }
  sum=3*h/8*sum;
  printf("\nI=%f",sum);
  getch();
}

OUTPUT:


No comments:

Post a Comment