runge.C

00001 /*
00002  * Code to exhibit Runge's phenomenon 
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: runge.C,v 1.2 2005/11/14 14:17:02 e_gourgoulhon Exp $
00029  * $Log: runge.C,v $
00030  * Revision 1.2  2005/11/14 14:17:02  e_gourgoulhon
00031  * Added #include <stdio.h>
00032  *
00033  * Revision 1.1  2005/11/14 01:57:00  e_gourgoulhon
00034  * First version
00035  *
00036  *
00037  * $Header: /cvsroot/Lorene/School05/Monday/runge.C,v 1.2 2005/11/14 14:17:02 e_gourgoulhon Exp $
00038  *
00039  */
00040 
00041 #include <iostream>
00042 
00043 using namespace std ;
00044 
00045 #include <stdlib.h>
00046 #include <stdio.h>
00047 #include <string.h>
00048 #include <math.h>
00049 
00050 #include "grid.h"
00051 #include "plot.h"
00052 
00053 void petite_pause() ; 
00054 double ff(double x) ; 
00055 
00056 int main() {
00057 
00058     // For the plots
00059     int ng = 1000 ;      // Number of points to draw the curves
00060     double* xg = new double[ng] ; 
00061     double* yg = new double[ng] ; 
00062     for (int j=0; j<ng; j++) xg[j] = -1. + 2. * double(j) / double(ng-1) ; 
00063     
00064     
00065     for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; 
00066     
00067     int nfig0 = 0 ;  // Figure index
00068     plot_profile(yg, ng, 1, 1, nfig0, -0.5, 1.5, "Interpolation of 1/(1+25x\\u2\\d)") ;     
00069 
00070     // Loop on the number of nodes
00071     
00072     for (int k = 0; k<6; k++) {
00073     
00074         int nb_nodes = 4*(k+1) + 1 ; 
00075         int nn = nb_nodes - 1 ; 
00076 
00077         int nfig1 = k+1 ;  // Figure index
00078                
00079         Grid_uniform xcoloc(nb_nodes) ;    // Construction of the grid 
00080         
00081         // Grid_Chebyshev_GL xcoloc(nb_nodes) ;  // other example (free of Runge phenomenon) 
00082     
00083         cout << "Grid: " << xcoloc << endl ; 
00084         
00085         // Interpolating polynomial 
00086 
00087         for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; 
00088     
00089         char title[180] ; 
00090         strcpy(title, "Interpolation of 1/(1+25x\\u2\\d) , N = ") ; 
00091         char string_nn[4] ;
00092         sprintf(string_nn, "%d", nn) ; 
00093         strcat(title, string_nn) ; 
00094         plot_profile(yg, ng, 1, 1, nfig1, -0.5, 1.5, title) ;     
00095 
00096         for (int j=0; j<ng; j++) yg[j] = xcoloc.interpole(ff, xg[j]) ; 
00097 
00098         plot_profile(yg, ng, k%15+2, 2, nfig0) ; 
00099         plot_profile(yg, ng, 2, 1, nfig1) ; 
00100 
00101         xcoloc.plot(2, nfig1) ;   
00102 
00103     }  // end of loop on the number of nodes
00104     
00105     petite_pause() ; 
00106 
00107     plot_close_all() ; 
00108     
00109     delete [] xg ; 
00110     delete [] yg ; 
00111 
00112     return EXIT_SUCCESS ; 
00113 }
00114 
00115 void petite_pause() {
00116     cout.flush() ;
00117     cout << "Continue = 'return'" << endl ;
00118     char cret ; 
00119     cin.get(cret) ;
00120 }
00121 
00122 
00123 double ff(double x) {
00124 //    return 2*x*x - 1. ; 
00125 //     return 1./ (cos(6*x) + 2.) ;  // other example
00126     return 1. / (1. + 25.*x*x) ; 
00127 //    if (x == double(0)) return 0.;
00128 //    else return exp(-1. / (25*x*x)) ; 
00129 }
00130 

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