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 #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
00065 int ng = 1000 ;
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] ;
00071 double* yc = new double[nn+1] ;
00072
00073 int nfig = 0 ;
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
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
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
00131
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
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
00191
00192
00193
00194
00195 return cos(exp(2*x)) ;
00196
00197
00198
00199 }