approx_ortho.C

00001 /*
00002  * Approximation of a function by orthogonal polynomials
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: approx_ortho.C,v 1.2 2005/11/14 14:12:09 e_gourgoulhon Exp $
00029  * $Log: approx_ortho.C,v $
00030  * Revision 1.2  2005/11/14 14:12:09  e_gourgoulhon
00031  * Added include <assert.h>
00032  *
00033  * Revision 1.1  2005/11/14 01:56:58  e_gourgoulhon
00034  * First version
00035  *
00036  *
00037  * $Header: /cvsroot/Lorene/School05/Monday/approx_ortho.C,v 1.2 2005/11/14 14:12:09 e_gourgoulhon Exp $
00038  *
00039  */
00040 
00041 #include <iostream>
00042 #include <fstream>
00043 
00044 using namespace std ;
00045 
00046 #include <stdlib.h>
00047 #include <math.h>
00048 
00049 #include "grid.h"
00050 #include "ortho_poly.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     double* yg0 = new double[ng] ; 
00062     double* yg1 = new double[ng] ; 
00063     
00064     for (int j=0; j<ng; j++) xg[j] = -1. + 2. * double(j) / double(ng-1) ; 
00065     
00066     int nfig = -1 ; // Figure index
00067 
00068     ofstream fproj("err_proj.d") ;
00069     ofstream finter("err_inter.d") ;
00070     
00071     int nn = 0 ; 
00072     while (nn >= 0) {
00073         cout << "Maximum degree N of polynomials ? (negative value = exit) " 
00074             << endl ; 
00075         cin >> nn ; 
00076         while ( cin.get()!='\n' ) ;
00077         if (nn < 0) continue ; 
00078         
00079         double* xc = new double[nn+1] ;     // to draw points
00080         double* yc = new double[nn+1] ;     
00081 
00082         // Plot of f
00083         nfig++ ; 
00084         for (int j=0; j<ng; j++) yg0[j] = ff(xg[j]) ; 
00085         plot_profile(yg0, ng, 2, 1, nfig, -1.2, 1.2) ;     
00086 
00087         // Basis of orthogonal polynomials:
00088         
00089         Chebyshev_poly poly(nn) ;
00090 
00091         // Coefficients of the projection of f onto P_N:
00092 
00093         double* cf_proj = new double[nn+1] ; 
00094         poly.coef_projection(ff, cf_proj) ; 
00095 
00096         double err = 0 ; 
00097         for (int j=0; j<ng; j++) {
00098             double y = poly.series(cf_proj, xg[j]) ; 
00099             double diff = fabs(y - yg0[j]) ; 
00100             if (diff > err) err = diff ; 
00101             yg1[j] = y ; 
00102         }
00103         cout << "Error projection : " << err << endl ; 
00104         fproj << nn << "  " << err << endl ; 
00105         plot_profile(yg1, ng, 5, 1, nfig) ;     
00106         
00107         // Coefficients of the Gauss-Lobatto interpolant of f
00108         double* cf_inter = new double[nn+1] ; 
00109         poly.coef_interpolant_GL(ff, cf_inter) ; 
00110 
00111         err = 0 ; 
00112         for (int j=0; j<ng; j++) {
00113             double y = poly.series(cf_inter, xg[j]) ; 
00114             double diff = fabs(y - yg0[j]) ; 
00115             if (diff > err) err = diff ; 
00116             yg[j] = y ; 
00117         }
00118         cout << "Error GL interpolant : " << err << endl ; 
00119         finter << nn << "  " << err << endl ; 
00120         plot_profile(yg, ng, 3, 1, nfig) ;     
00121         
00122         for (int i=0; i<=nn; i++) {
00123             xc[i] = poly.gauss_lobatto_nodes()(i) ; 
00124             yc[i] = poly.gauss_lobatto_nodes().interpole(ff, xc[i]) ; 
00125         }
00126         plot_point_set(nn+1, xc, yc, 3, nfig) ; 
00127         
00128         // plot_close(nfig) ; // closing required if EPS figure 
00129 
00130         // Aliasing error
00131 
00132         // for (int j=0; j<ng; j++) yg[j] -= yg1[j] ; 
00133         // nfig++ ; 
00134         // plot_profile(yg, ng, 4, 1, nfig, -0.2, 0.2, "Aliasing error") ;     
00135                 
00136         
00137         // plot_close(nfig) ; // closing required if EPS figure 
00138 
00139         delete [] xc ; 
00140         delete [] yc ; 
00141         delete [] cf_proj ; 
00142         delete [] cf_inter ; 
00143        
00144     }
00145     
00146     plot_close_all() ; 
00147     
00148     fproj.close() ; 
00149     finter.close() ; 
00150 
00151     delete [] xg ; 
00152     delete [] yg ; 
00153     delete [] yg0 ; 
00154     delete [] yg1 ; 
00155 
00156     return EXIT_SUCCESS ; 
00157 }
00158 
00159 
00160 
00161 double ff(double x) {
00162 //    return 1. / (1. + 25.*x*x) ; 
00163 //    return 1. / (1. + 16.*x*x) ; 
00164     return cos(exp(2*x)) ; 
00165 }
00166 

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