TRAPEZOIDAL RULE

/*

1. Start
2. Define function f(x)
3. Read lower limit of integration, upper limit of integration and number of sub interval
4. Calculate: step size = (upper limit -lower limit)/number of sub interval
5. Set: integration value = f(lower limit) + f(upper limit)
6. Set: i = 1
7. If i > number of sub interval then goto
8. Calculate: k = lower limit + i * h
9. Calculate: Integration value =Integration Value + 2* f(k)
10. Increment i by 1 i.e. i = i+1 and go to step 7
11. Calculate: Integration value =Integration value * step size/2
12. Display Integration value as required answer
13. Stop


*/


 /* Trapezoidal rule.*/

#include <stdio.h>

float y(float x)

{

 return 1/(1+x*x);

}

main()

{

 float x0,xn,h,s;

 int i,n;

 puts("Enter x0,xn,no. of subintervals");

 scanf ("%f %f %d",&x0,&xn,&n);

 h = (xn-x0)/n;

 s = y(x0)+y(xn);

 for (i=1;i<=n-1;i++)

 s += 2*y(x0+i*h);

 printf ("Value of integral is % 6.4f\n",

 (h/2)*s);

}


Comments

Popular posts from this blog

Write a program to find out the FIRST of the Non‐terminals in a grammar.