00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00058 int ng = 1000 ;
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 ;
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] ;
00078 double* yc = new double[nn+1] ;
00079
00080
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
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
00118
00119
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
00143
00144
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
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
00188 return 1. / (1. + 16.*x*x) ;
00189
00190 }