legendre.C

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

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