Find an approximate a definite integral of a function f(x) such that the error is approximately EPS |
A common method to estimate error in numerical methods is using finer step size:
|
        RectangleRule(f, a, b, N)
that computes the estimate integral for step size h = (b-a)/N
Inputs: function f interval a, b precision EPS N = 1; V1 = RectangleRule(f, a, b, N); // Last estimate N = 2*N; V2 = RectangleRule(f, a, b, N); // New estimate // Keep doing it as long as the error exceeds threshold.... while ( abs(V2 - V1) > EPS ) { V2 = V1; // Remember the last estimate N = 2*N; V2 = RectangleRule(f, a, b, N); // Compute NEW estimate } |
Here, we will again use the technique to define/declare a function that has another function as parameter.
The material was discussed here: click here
extern RectangleRule(float (float), float, float, int); float RectangleRule_Prec(float f(float), float a, float b, float EPS) { /* ------------------------------ "Local" Variables ------------------------------ */ int N; float V1, V2; // Successive estimates N = 1; V1 = RectangleRule(f, a, b, N); // Use RectangleRule() N = 2*N; V2 = RectangleRule(f, a, b, N); // Use RectangleRule() again while ( abs(V2 - V1) > EPS ) { V1 = V2; N = 2*N; V2 = RectangleRule(f, a, b, N); } return(V2); } |
CC -c rect-rule4.C
CC -c rect-rule-prec.C
CC -c rect-rule-func.C
CC -c rect-rule2b.C
CC -o rect-rule4 rect-rule4.o rect-rule-prec.o \
rect-rule-func.o rect-rule2b.o
OR less efficient (but easier to type):
CC -o rect-rule4 rect-rule4.C rect-rule-prec.C \
rect-rule-func.C rect-rule2b.C