Numerical Methods: Integration of given function using Trapezoidal rule in C
Source Code:
///integration of given function using Trapezoidal rule #include<stdio.h> float y(float x){ return 1/(1+x*x); } int main(){ float x0,xn,h,s; int i,n; printf("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; i++){ s += 2*y(x0+i*h); } printf("Value of integral is %6.4f\n",(h/2)*s); return 0; }
why %6.4f ?? could you explain this please. Help would be much appreciated, because so far this is the cleanest most nice structured example of the Trapezoidal rule. I need help, because i got an exercise that is about numerical integration.
Write a C-program which carries out “Numerical Integration” according to (1). Use data
type double everywhere. “Play” with three different step sizes (b-a) by splitting up the area
under f(x) into 10, 100 and 1000 trapezoids and compare the results.
Preferably, use a function for the calculation of (1) and call it from main( ).
So yeah ;/
fgdf