cheby.C

00001 /*
00002  * Code for Chebyshev expansion
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: cheby.C,v 1.1 2005/11/14 01:56:58 e_gourgoulhon Exp $
00029  * $Log: cheby.C,v $
00030  * Revision 1.1  2005/11/14 01:56:58  e_gourgoulhon
00031  * First version
00032  *
00033  *
00034  * $Header: /cvsroot/Lorene/School05/Monday/cheby.C,v 1.1 2005/11/14 01:56:58 e_gourgoulhon Exp $
00035  *
00036  */
00037 
00038 #include <iostream>
00039 #include <fstream>
00040 
00041 using namespace std ;
00042 
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <math.h>
00046 
00047 #include "ortho_poly.h"
00048 #include "grid.h"
00049 #include "plot.h"
00050 
00051 void petite_pause() ; 
00052 double ff(double x) ; 
00053 
00054 int main() {
00055 
00056     int nn = 4 ; 
00057     
00058     Chebyshev_poly cheb(nn) ;
00059     
00060     cout << cheb << endl ; 
00061     cout << cheb.gauss_nodes() << endl ; 
00062     cout << cheb.gauss_lobatto_nodes() << endl ; 
00063 
00064     // For the plots
00065     int ng = 1000 ;      // Number of points to draw the curves
00066     double* xg = new double[ng] ; 
00067     double* yg = new double[ng] ; 
00068     for (int j=0; j<ng; j++) xg[j] = -1. + 2. * double(j) / double(ng-1) ; 
00069     
00070     double* xc = new double[nn+1] ;     // to draw points
00071     double* yc = new double[nn+1] ;     
00072     
00073     int nfig = 0 ;  // Figure index
00074     
00075     for (int i=0; i<=nn; i++) {
00076         
00077         for (int j=0; j<ng; j++) yg[j] = cheb(i, xg[j]) ; 
00078             plot_profile(yg, ng, i%15 + 1, 1, nfig, -1.1, 1.1, 
00079                      "Chebyshev polynomials up to N=8") ;     
00080 
00081         // petite_pause() ; 
00082 
00083     }
00084 
00085     Chebyshev_poly cheb1(nn+1) ;
00086     nfig++ ; 
00087     for (int j=0; j<ng; j++) yg[j] = cheb1(nn+1, xg[j]) ; 
00088     plot_profile(yg, ng, 2, 1, nfig, -1.1, 1.1, "Chebyshev Gauss nodes") ;     
00089 
00090     cheb.gauss_nodes().plot(3, nfig) ; 
00091     // cheb.gauss_lobatto_nodes().plot(4, nfig) ; 
00092     
00093     double* cf = new double[nn+1] ; 
00094     double* cf1 = new double[nn+1] ; 
00095     
00096     cheb.coef_interpolant_GL(ff, cf) ; 
00097     cheb.coef_interpolant_GL_FFT(ff, cf1) ; 
00098     
00099     cout << "Coefficients of f interpolant (Gauss-Lobatto nodes): " << endl ; 
00100     for (int i=0; i<=nn; i++) {
00101         cout << i << " :  " << cf[i] << "  " << cf1[i] 
00102         << "  " << cf[i] - cf1[i] << endl ;  
00103     }
00104 
00105     double* cf2 = new double[nn+1] ; 
00106     cheb.coef_interpolant_Gauss(ff, cf2) ; 
00107     cout << endl <<
00108      "Coefficients of f interpolant (Gauss-Lobatto versus Gauss nodes)" 
00109      << endl ; 
00110     for (int i=0; i<=nn; i++) {
00111         cout << i << " :  " << cf[i] << "  " << cf2[i] 
00112         << "  " << cf[i] - cf2[i] << endl ;  
00113     }
00114 
00115     nfig++ ; 
00116     for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; 
00117     plot_profile(yg, ng, 2, 1, nfig, -1.1, 1.1) ;     
00118 
00119     for (int j=0; j<ng; j++) yg[j] = cheb.series(cf, xg[j]) ; 
00120     plot_profile(yg, ng, 3, 1, nfig) ;     
00121     cheb.gauss_lobatto_nodes().plot(3, nfig) ; 
00122 
00123     for (int i=0; i<=nn; i++) {
00124         xc[i] = cheb.gauss_lobatto_nodes()(i) ; 
00125         yc[i] = cheb.series(cf, xc[i]) ; 
00126     }
00127     plot_point_set(nn+1, xc, yc, 3, nfig) ; 
00128     
00129     for (int j=0; j<ng; j++) yg[j] = cheb.series(cf2, xg[j]) ; 
00130     //plot_profile(yg, ng, 7, 1, nfig) ;     
00131     //cheb.gauss_nodes().plot(7, nfig) ; 
00132 
00133     double* cf_proj = new double[nn+1] ; 
00134     cheb.coef_projection(ff, cf_proj) ; 
00135     for (int j=0; j<ng; j++) yg[j] = cheb.series(cf_proj, xg[j]) ; 
00136     plot_profile(yg, ng, 5, 1, nfig) ;     
00137     
00138     ofstream fcoef("coef_proj.d") ;
00139     for (int i=0; i<=nn; i++) 
00140         fcoef << i << "  " << fabs(cf_proj[i]) << endl ; 
00141     fcoef.close() ;    
00142    
00143     
00144     // Check: comparison between Grid::interpole and Ortho_poly::series
00145     cout << endl <<  
00146     "Maximum difference between Grid::interpole and Ortho_poly::series :"
00147     << endl ; 
00148     double diff = 0 ;
00149     for (int j=0; j<ng; j++) {
00150         double diff0 = fabs( cheb.series(cf, xg[j]) 
00151             - cheb.gauss_lobatto_nodes().interpole(ff, xg[j]) ) ; 
00152         if (diff0 > diff) diff = diff0 ; 
00153     }    
00154     cout << "  Gauss-Lobatto case : " << diff << endl ; 
00155 
00156     diff = 0 ;
00157     for (int j=0; j<ng; j++) {
00158         double diff0 = fabs( cheb.series(cf2, xg[j]) 
00159             - cheb.gauss_nodes().interpole(ff, xg[j]) ) ; 
00160         if (diff0 > diff) diff = diff0 ; 
00161     }    
00162     cout << "  Gauss case : " << diff << endl ; 
00163 
00164     petite_pause() ; 
00165 
00166     plot_close_all() ; 
00167     
00168     delete [] cf ; 
00169     delete [] cf1 ; 
00170     delete [] cf2 ; 
00171     delete [] cf_proj ; 
00172     
00173     delete [] xc ; 
00174     delete [] yc ; 
00175     delete [] xg ; 
00176     delete [] yg ; 
00177 
00178     return EXIT_SUCCESS ; 
00179 }
00180 
00181 
00182 void petite_pause() {
00183     cout.flush() ;
00184     cout << "Continue = 'return'" << endl ;
00185     char cret ; 
00186     cin.get(cret) ;
00187 }
00188 
00189 double ff(double x) {
00190 //    return 2*x*x - 1. ; 
00191 //     return 1./ (cos(6*x) + 2.) ;  // other example
00192 
00193 //    return 1. / (1. + 25.*x*x) ; 
00194 
00195     return cos(exp(2*x)) ; 
00196     
00197 //    if (x == double(0)) return 0.;
00198 //    else return exp(-1. / (25*x*x)) ; 
00199 }

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