To Compute the Value of Integration Using Simpson's 1/3rd Method of Numerical Integration - C Data Structure

C examples for Data Structure:Algorithm

Description

To Compute the Value of Integration Using Simpson's 1/3rd Method of Numerical Integration

Demo Code

#include<stdio.h>
#define MAX 50//from w ww. j a  v  a2s .  c  o m

float simpson(float x){
    return (1/(1+x*x));
}

int main()
{
  int i, j, num;
  float a, b, h, x[MAX], y[MAX], sum, result = 1;
  printf("\nIntegrand:  f(x) = 1/(1+x*x) \n");
  
  a = 7;
  b = 8;
  num = 20;
  h = (b - a)/num;
  sum = 0;
  sum = simpson(a) + 4 * simpson(a + h) + simpson(b);
  for(i=3; i < num; i+=2) {
    sum += 2 * simpson(a + (i-1) * h) + 4 * simpson(a + i * h);
  }
  result = sum * h / 3;
  printf("\nValue of Integration : %5.3f", result);
}

Related Tutorials