Statistics
| Revision:

root / branches / threaded2 / examples / threaded / mforest / trapzd.c @ 1099

History | View | Annotate | Download (455 Bytes)

1
/* note #undef's at end of file */
2
#define FUNC(x) ((*func)(x))
3

    
4
float trapzd(float (*func)(float), float a, float b, int n)
5
{
6
        float x,tnm,sum,del;
7
        static float s;
8
        int it,j;
9

    
10
        if (n == 1) {
11
                return (s=0.5*(b-a)*(FUNC(a)+FUNC(b)));
12
        } else {
13
                for (it=1,j=1;j<n-1;j++) it <<= 1;
14
                tnm=it;
15
                del=(b-a)/tnm;
16
                x=a+0.5*del;
17
                for (sum=0.0,j=1;j<=it;j++,x+=del) sum += FUNC(x);
18
                s=0.5*(s+(b-a)*sum/tnm);
19
                return s;
20
        }
21
}
22
#undef FUNC