interpole.C

00001 /*
00002  * Comparison of various interpolation of a function
00003  *
00004  */
00005 
00006 /*
00007  *   Copyright (c) 2005 Eric Gourgoulhon
00008  *
00009  *   This file is part of LORENE.
00010  *
00011  *   LORENE is free software; you can redistribute it and/or modify
00012  *   it under the terms of the GNU General Public License as published by
00013  *   the Free Software Foundation; either version 2 of the License, or
00014  *   (at your option) any later version.
00015  *
00016  *   LORENE is distributed in the hope that it will be useful,
00017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *   GNU General Public License for more details.
00020  *
00021  *   You should have received a copy of the GNU General Public License
00022  *   along with LORENE; if not, write to the Free Software
00023  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  */
00026 
00027 /*
00028  * $Id: interpole.C,v 1.2 2005/11/14 14:11:22 e_gourgoulhon Exp $
00029  * $Log: interpole.C,v $
00030  * Revision 1.2  2005/11/14 14:11:22  e_gourgoulhon
00031  * log(int ) -> log(double)
00032  * + suppressed plot_close.
00033  *
00034  * Revision 1.1  2005/11/14 01:56:59  e_gourgoulhon
00035  * First version
00036  *
00037  *
00038  * $Header: /cvsroot/Lorene/School05/Monday/interpole.C,v 1.2 2005/11/14 14:11:22 e_gourgoulhon Exp $
00039  *
00040  */
00041 
00042 #include <iostream>
00043 #include <fstream>
00044 
00045 using namespace std ;
00046 
00047 #include <stdlib.h>
00048 #include <math.h>
00049 
00050 #include "grid.h"
00051 #include "plot.h"
00052 
00053 double ff(double x) ; 
00054 
00055 int main() {
00056 
00057     // For the plots
00058     int ng = 1000 ;      // Number of points to draw the curves
00059     double* xg = new double[ng] ; 
00060     double* yg = new double[ng] ; 
00061     for (int j=0; j<ng; j++) xg[j] = -1. + 2. * double(j) / double(ng-1) ; 
00062     
00063     int nfig = -1 ; // Figure index
00064 
00065     ofstream funif("err_unif.d") ;
00066     ofstream fleg("err_leg.d") ;
00067     ofstream fcheb("err_cheb.d") ;
00068     
00069     int nn = 0 ; 
00070     while (nn >= 0) {
00071         cout << "Maximum degree N of polynomials ? (negative value = exit) " 
00072             << endl ; 
00073         cin >> nn ; 
00074         while ( cin.get()!='\n' ) ;
00075         if (nn < 0) continue ; 
00076         
00077         double* xc = new double[nn+1] ;     // to draw points
00078         double* yc = new double[nn+1] ;     
00079 
00080         // The three types of grid:
00081         
00082         Grid_uniform x_unif(nn+1) ; 
00083         Grid_Legendre_GL x_leg(nn+1) ; 
00084         Grid_Chebyshev_Gauss x_cheb(nn+1) ;
00085         
00086         cout << "Lebesgue constants: " << endl ; 
00087         cout << "   uniform grid : " << x_unif.lebesgue_constant() << endl ; 
00088         cout << "   Legendre GL grid : " << x_leg.lebesgue_constant() << endl ; 
00089         cout << "   Chebyshev Gauss grid : " << x_cheb.lebesgue_constant() 
00090             << " <-> theor. lower / upper bound: " 
00091             << 2./M_PI * log(double(nn+1)) + 0.9625 << " / " 
00092             << 2./M_PI * log(double(nn+1)) + 1 << endl ; 
00093         
00094         // Interpolation through the uniform grid :
00095         
00096         nfig++ ; 
00097         for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; 
00098         plot_profile(yg, ng, 2, 1, nfig, -1.2, 1.2) ;     
00099         
00100         double err = 0 ; 
00101         for (int j=0; j<ng; j++) {
00102             double y = x_unif.interpole(ff, xg[j]) ; 
00103             double diff = fabs(y - yg[j]) ; 
00104             if (diff > err) err = diff ; 
00105             yg[j] = y ; 
00106         }
00107         cout << "Error uniform interpolation: " << err << endl ; 
00108         funif << nn << "  " << err << endl ; 
00109         plot_profile(yg, ng, 3, 1, nfig) ;     
00110         
00111         for (int i=0; i<=nn; i++) {
00112             xc[i] = x_unif(i) ; 
00113             yc[i] = x_unif.interpole(ff, xc[i]) ; 
00114         }
00115         plot_point_set(nn+1, xc, yc, 3, nfig) ; 
00116         
00117         // plot_close(nfig) ; // closing required if EPS figure 
00118         
00119         // Interpolation through the Legendre GL grid :
00120         
00121         nfig++ ; 
00122         for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; 
00123         plot_profile(yg, ng, 2, 1, nfig, -0.2, 1.2) ;     
00124         
00125         err = 0 ; 
00126         for (int j=0; j<ng; j++) {
00127             double y = x_leg.interpole(ff, xg[j]) ; 
00128             double diff = fabs(y - yg[j]) ; 
00129             if (diff > err) err = diff ; 
00130             yg[j] = y ; 
00131         }
00132         cout << "Error Legendre GL interpolation: " << err << endl ; 
00133         fleg << nn << "  " << err << endl ; 
00134         plot_profile(yg, ng, 3, 1, nfig) ;     
00135         
00136         for (int i=0; i<=nn; i++) {
00137             xc[i] = x_leg(i) ; 
00138             yc[i] = x_leg.interpole(ff, xc[i]) ; 
00139         }
00140         plot_point_set(nn+1, xc, yc, 3, nfig) ; 
00141         
00142         // plot_close(nfig) ; // closing required if EPS figure 
00143 
00144         // Interpolation through the Chebyshev Gauss grid :
00145         
00146         nfig++ ; 
00147         for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; 
00148         plot_profile(yg, ng, 2, 1, nfig, -0.2, 1.2) ;     
00149         
00150         err = 0 ; 
00151         for (int j=0; j<ng; j++) {
00152             double y = x_cheb.interpole(ff, xg[j]) ; 
00153             double diff = fabs(y - yg[j]) ; 
00154             if (diff > err) err = diff ; 
00155             yg[j] = y ; 
00156         }
00157         cout << "Error Chebyshev Gauss interpolation: " << err << endl ; 
00158         fcheb << nn << "  " << err << endl ; 
00159         plot_profile(yg, ng, 3, 1, nfig) ;     
00160         
00161         for (int i=0; i<=nn; i++) {
00162             xc[i] = x_cheb(i) ; 
00163             yc[i] = x_cheb.interpole(ff, xc[i]) ; 
00164         }
00165         plot_point_set(nn+1, xc, yc, 3, nfig) ; 
00166         
00167         // plot_close(nfig) ; // closing required if EPS figure 
00168 
00169         delete [] xc ; 
00170         delete [] yc ; 
00171        
00172     }
00173     
00174     plot_close_all() ; 
00175     
00176     funif.close() ; 
00177     fleg.close() ; 
00178     fcheb.close() ; 
00179 
00180     delete [] xg ; 
00181     delete [] yg ; 
00182 
00183     return EXIT_SUCCESS ; 
00184 }
00185 
00186 double ff(double x) {
00187 //    return 1. / (1. + 25.*x*x) ; 
00188     return 1. / (1. + 16.*x*x) ; 
00189 //    return cos(exp(2*x)) ; 
00190 }

Generated on Tue Dec 6 14:48:44 2011 for POLYNOM by  doxygen 1.4.6