src/main/cpp/numerics4cpp/integration/trapezoid.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005, DoodleProject
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 
00009  * Redistributions of source code must retain the above copyright
00010  * notice, this list of conditions and the following disclaimer.
00011  * 
00012  * Redistributions in binary form must reproduce the above copyright
00013  * notice, this list of conditions and the following disclaimer in
00014  * the documentation and/or other materials provided with the
00015  * distribution.
00016  * 
00017  * Neither the name of DoodleProject nor the names of its contributors
00018  * may be used to endorse or promote products derived from this
00019  * software without specific prior written permission.
00020  * 
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
00022  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
00023  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00025  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
00026  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00030  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00031  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00032  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00033  * SUCH DAMAGE.
00034  */
00035 
00036 #ifndef _NUMERICS4CPP_INTEGRATION_TRAPEZOID_H_
00037 #define _NUMERICS4CPP_INTEGRATION_TRAPEZOID_H_
00038 
00039 #include <limits>
00040 #include "../iterativemethod.h"
00041 #include "../exception/exception.h"
00042 
00043 NUM_NAMESPACE_BEGIN
00044 
00053 template<class Function>
00054 class trapezoidal_integrator_state {
00055 public:
00056     trapezoidal_integrator_state(Function &f, double a, double b) :
00057         _function(f), _a(a), _b(b), _n(0), _k(2)
00058     {
00059         double fa = f(a);
00060         double fb = f(b);
00061         _s = (b - a) * (fa / 2.0 + fb / 2.0);
00062     }
00063 
00068     unsigned int iterations() const {
00069         return _n;
00070     }
00071         
00076     void iterate() {
00077         ++_n;
00078         
00079         // compute function at next set of points
00080         double sn = 0.0;
00081         double h = (_b - _a) / _k;
00082         for (int i = _k - 1; i >= 1; i -= 2) {
00083             double x = _a + (i * h);
00084             sn += _function(x);
00085         }
00086         sn = (0.5 * _s) + (h * sn);
00087 
00088         _k *= 2;
00089         _s = sn;
00090     }
00091 
00096     double result() const {
00097         return _s;
00098     }
00099 
00100 private:
00102     Function& _function;
00103     
00105     double _a;
00106     
00108     double _b;
00109     
00111     unsigned int _n;
00112     
00114     double _s;
00115     
00117     unsigned int _k;
00118 };
00119 
00120 
00162 template<class Function>
00163 class trapezoidal_integrator : public iterative_method {
00164 
00165 public:
00172     trapezoidal_integrator(Function& f, unsigned int iterations = 100,
00173         double relative_error = 1.0e-13)
00174         : iterative_method(iterations, relative_error), _function(f)
00175     {
00176     }
00177     
00181     ~trapezoidal_integrator() {
00182     }
00183 
00190     double integrate(double a, double b) {
00191         trapezoidal_integrator_state<Function> state(_function, a, b);
00192 
00193         double s = state.result();
00194         double sn = s;
00195         double error = std::numeric_limits<double>::max();
00196         do {
00197             state.iterate();
00198             sn = state.result();
00199             error = std::fabs(sn / s - 1.0);
00200             s = sn;
00201         } while (state.iterations() < maximum_iterations() &&
00202             error > maximum_relative_error());
00203         
00204         if (state.iterations() >= maximum_iterations()) {
00205             throw convergence_exception("Trapezoidal integration failed to converge.");
00206         }
00207         
00208         return s;
00209     }
00210     
00211 private:
00213     Function& _function;
00214 };
00215 
00216 NUM_NAMESPACE_END
00217 
00218 #endif

Generated on Wed Nov 21 22:22:15 2007 for numerics4c++ by  doxygen 1.5.3