| 1 |
/* |
| 2 |
* Code for Chebyshev expansion |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
/* |
| 7 |
* Copyright (c) 2005 Eric Gourgoulhon |
| 8 |
* |
| 9 |
* This file is part of LORENE. |
| 10 |
* |
| 11 |
* LORENE is free software; you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU General Public License as published by |
| 13 |
* the Free Software Foundation; either version 2 of the License, or |
| 14 |
* (at your option) any later version. |
| 15 |
* |
| 16 |
* LORENE is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with LORENE; if not, write to the Free Software |
| 23 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
/* |
| 28 |
* $Id: cheby.C,v 1.2 2014/10/06 15:09:47 j_novak Exp $ |
| 29 |
* $Log: cheby.C,v $ |
| 30 |
* Revision 1.2 2014/10/06 15:09:47 j_novak |
| 31 |
* Modified #include directives to use c++ syntax. |
| 32 |
* |
| 33 |
* Revision 1.1 2005/11/14 01:56:58 e_gourgoulhon |
| 34 |
* First version |
| 35 |
* |
| 36 |
* |
| 37 |
* $Header: /cvsroot/Lorene/School05/Monday/cheby.C,v 1.2 2014/10/06 15:09:47 j_novak Exp $ |
| 38 |
* |
| 39 |
*/ |
| 40 |
|
| 41 |
#include <iostream> |
| 42 |
#include <fstream> |
| 43 |
|
| 44 |
using namespace std ; |
| 45 |
|
| 46 |
#include <cstdlib> |
| 47 |
#include <cstring> |
| 48 |
#include <cmath> |
| 49 |
|
| 50 |
#include "ortho_poly.h" |
| 51 |
#include "grid.h" |
| 52 |
#include "plot.h" |
| 53 |
|
| 54 |
void petite_pause() ; |
| 55 |
double ff(double x) ; |
| 56 |
|
| 57 |
int main() { |
| 58 |
|
| 59 |
int nn = 4 ; |
| 60 |
|
| 61 |
Chebyshev_poly cheb(nn) ; |
| 62 |
|
| 63 |
cout << cheb << endl ; |
| 64 |
cout << cheb.gauss_nodes() << endl ; |
| 65 |
cout << cheb.gauss_lobatto_nodes() << endl ; |
| 66 |
|
| 67 |
// For the plots |
| 68 |
int ng = 1000 ; // Number of points to draw the curves |
| 69 |
double* xg = new double[ng] ; |
| 70 |
double* yg = new double[ng] ; |
| 71 |
for (int j=0; j<ng; j++) xg[j] = -1. + 2. * double(j) / double(ng-1) ; |
| 72 |
|
| 73 |
double* xc = new double[nn+1] ; // to draw points |
| 74 |
double* yc = new double[nn+1] ; |
| 75 |
|
| 76 |
int nfig = 0 ; // Figure index |
| 77 |
|
| 78 |
for (int i=0; i<=nn; i++) { |
| 79 |
|
| 80 |
for (int j=0; j<ng; j++) yg[j] = cheb(i, xg[j]) ; |
| 81 |
plot_profile(yg, ng, i%15 + 1, 1, nfig, -1.1, 1.1, |
| 82 |
"Chebyshev polynomials up to N=8") ; |
| 83 |
|
| 84 |
// petite_pause() ; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
Chebyshev_poly cheb1(nn+1) ; |
| 89 |
nfig++ ; |
| 90 |
for (int j=0; j<ng; j++) yg[j] = cheb1(nn+1, xg[j]) ; |
| 91 |
plot_profile(yg, ng, 2, 1, nfig, -1.1, 1.1, "Chebyshev Gauss nodes") ; |
| 92 |
|
| 93 |
cheb.gauss_nodes().plot(3, nfig) ; |
| 94 |
// cheb.gauss_lobatto_nodes().plot(4, nfig) ; |
| 95 |
|
| 96 |
double* cf = new double[nn+1] ; |
| 97 |
double* cf1 = new double[nn+1] ; |
| 98 |
|
| 99 |
cheb.coef_interpolant_GL(ff, cf) ; |
| 100 |
cheb.coef_interpolant_GL_FFT(ff, cf1) ; |
| 101 |
|
| 102 |
cout << "Coefficients of f interpolant (Gauss-Lobatto nodes): " << endl ; |
| 103 |
for (int i=0; i<=nn; i++) { |
| 104 |
cout << i << " : " << cf[i] << " " << cf1[i] |
| 105 |
<< " " << cf[i] - cf1[i] << endl ; |
| 106 |
} |
| 107 |
|
| 108 |
double* cf2 = new double[nn+1] ; |
| 109 |
cheb.coef_interpolant_Gauss(ff, cf2) ; |
| 110 |
cout << endl << |
| 111 |
"Coefficients of f interpolant (Gauss-Lobatto versus Gauss nodes)" |
| 112 |
<< endl ; |
| 113 |
for (int i=0; i<=nn; i++) { |
| 114 |
cout << i << " : " << cf[i] << " " << cf2[i] |
| 115 |
<< " " << cf[i] - cf2[i] << endl ; |
| 116 |
} |
| 117 |
|
| 118 |
nfig++ ; |
| 119 |
for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; |
| 120 |
plot_profile(yg, ng, 2, 1, nfig, -1.1, 1.1) ; |
| 121 |
|
| 122 |
for (int j=0; j<ng; j++) yg[j] = cheb.series(cf, xg[j]) ; |
| 123 |
plot_profile(yg, ng, 3, 1, nfig) ; |
| 124 |
cheb.gauss_lobatto_nodes().plot(3, nfig) ; |
| 125 |
|
| 126 |
for (int i=0; i<=nn; i++) { |
| 127 |
xc[i] = cheb.gauss_lobatto_nodes()(i) ; |
| 128 |
yc[i] = cheb.series(cf, xc[i]) ; |
| 129 |
} |
| 130 |
plot_point_set(nn+1, xc, yc, 3, nfig) ; |
| 131 |
|
| 132 |
for (int j=0; j<ng; j++) yg[j] = cheb.series(cf2, xg[j]) ; |
| 133 |
//plot_profile(yg, ng, 7, 1, nfig) ; |
| 134 |
//cheb.gauss_nodes().plot(7, nfig) ; |
| 135 |
|
| 136 |
double* cf_proj = new double[nn+1] ; |
| 137 |
cheb.coef_projection(ff, cf_proj) ; |
| 138 |
for (int j=0; j<ng; j++) yg[j] = cheb.series(cf_proj, xg[j]) ; |
| 139 |
plot_profile(yg, ng, 5, 1, nfig) ; |
| 140 |
|
| 141 |
ofstream fcoef("coef_proj.d") ; |
| 142 |
for (int i=0; i<=nn; i++) |
| 143 |
fcoef << i << " " << fabs(cf_proj[i]) << endl ; |
| 144 |
fcoef.close() ; |
| 145 |
|
| 146 |
|
| 147 |
// Check: comparison between Grid::interpole and Ortho_poly::series |
| 148 |
cout << endl << |
| 149 |
"Maximum difference between Grid::interpole and Ortho_poly::series :" |
| 150 |
<< endl ; |
| 151 |
double diff = 0 ; |
| 152 |
for (int j=0; j<ng; j++) { |
| 153 |
double diff0 = fabs( cheb.series(cf, xg[j]) |
| 154 |
- cheb.gauss_lobatto_nodes().interpole(ff, xg[j]) ) ; |
| 155 |
if (diff0 > diff) diff = diff0 ; |
| 156 |
} |
| 157 |
cout << " Gauss-Lobatto case : " << diff << endl ; |
| 158 |
|
| 159 |
diff = 0 ; |
| 160 |
for (int j=0; j<ng; j++) { |
| 161 |
double diff0 = fabs( cheb.series(cf2, xg[j]) |
| 162 |
- cheb.gauss_nodes().interpole(ff, xg[j]) ) ; |
| 163 |
if (diff0 > diff) diff = diff0 ; |
| 164 |
} |
| 165 |
cout << " Gauss case : " << diff << endl ; |
| 166 |
|
| 167 |
petite_pause() ; |
| 168 |
|
| 169 |
plot_close_all() ; |
| 170 |
|
| 171 |
delete [] cf ; |
| 172 |
delete [] cf1 ; |
| 173 |
delete [] cf2 ; |
| 174 |
delete [] cf_proj ; |
| 175 |
|
| 176 |
delete [] xc ; |
| 177 |
delete [] yc ; |
| 178 |
delete [] xg ; |
| 179 |
delete [] yg ; |
| 180 |
|
| 181 |
return EXIT_SUCCESS ; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
void petite_pause() { |
| 186 |
cout.flush() ; |
| 187 |
cout << "Continue = 'return'" << endl ; |
| 188 |
char cret ; |
| 189 |
cin.get(cret) ; |
| 190 |
} |
| 191 |
|
| 192 |
double ff(double x) { |
| 193 |
// return 2*x*x - 1. ; |
| 194 |
// return 1./ (cos(6*x) + 2.) ; // other example |
| 195 |
|
| 196 |
// return 1. / (1. + 25.*x*x) ; |
| 197 |
|
| 198 |
return cos(exp(2*x)) ; |
| 199 |
|
| 200 |
// if (x == double(0)) return 0.; |
| 201 |
// else return exp(-1. / (25*x*x)) ; |
| 202 |
} |