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
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
00061 cout << leg.gauss_lobatto_nodes() << endl ;
00062
00063
00064 int ng = 1000 ;
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] ;
00070 double* yc = new double[nn+1] ;
00071
00072 int nfig = 0 ;
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
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
00111
00112
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
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
00133
00134
00135
00136
00137
00138
00139
00140 petite_pause() ;
00141
00142 plot_close_all() ;
00143
00144 delete [] cf ;
00145
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
00166
00167 return 1. / (1. + 25.*x*x) ;
00168
00169
00170 }