| 1 |
/* |
| 2 |
* Code for Legendre 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: legendre.C,v 1.2 2014/10/06 15:09:48 j_novak Exp $ |
| 29 |
* $Log: legendre.C,v $ |
| 30 |
* Revision 1.2 2014/10/06 15:09:48 j_novak |
| 31 |
* Modified #include directives to use c++ syntax. |
| 32 |
* |
| 33 |
* Revision 1.1 2005/11/14 01:56:59 e_gourgoulhon |
| 34 |
* First version |
| 35 |
* |
| 36 |
* |
| 37 |
* $Header: /cvsroot/Lorene/School05/Monday/legendre.C,v 1.2 2014/10/06 15:09:48 j_novak Exp $ |
| 38 |
* |
| 39 |
*/ |
| 40 |
|
| 41 |
#include <iostream> |
| 42 |
|
| 43 |
using namespace std ; |
| 44 |
|
| 45 |
#include <cstdlib> |
| 46 |
#include <cstring> |
| 47 |
#include <cmath> |
| 48 |
|
| 49 |
#include "ortho_poly.h" |
| 50 |
#include "grid.h" |
| 51 |
#include "plot.h" |
| 52 |
|
| 53 |
void petite_pause() ; |
| 54 |
double ff(double x) ; |
| 55 |
|
| 56 |
int main() { |
| 57 |
|
| 58 |
int nn = 8 ; |
| 59 |
|
| 60 |
Legendre_poly leg(nn) ; |
| 61 |
|
| 62 |
cout << leg << endl ; |
| 63 |
// cout << leg.gauss_nodes() << endl ; |
| 64 |
cout << leg.gauss_lobatto_nodes() << endl ; |
| 65 |
|
| 66 |
// For the plots |
| 67 |
int ng = 1000 ; // Number of points to draw the curves |
| 68 |
double* xg = new double[ng] ; |
| 69 |
double* yg = new double[ng] ; |
| 70 |
for (int j=0; j<ng; j++) xg[j] = -1. + 2. * double(j) / double(ng-1) ; |
| 71 |
|
| 72 |
double* xc = new double[nn+1] ; // to draw points |
| 73 |
double* yc = new double[nn+1] ; |
| 74 |
|
| 75 |
int nfig = 0 ; // Figure index |
| 76 |
|
| 77 |
for (int i=0; i<=nn; i++) { |
| 78 |
|
| 79 |
for (int j=0; j<ng; j++) yg[j] = leg(i, xg[j]) ; |
| 80 |
plot_profile(yg, ng, i%15 + 1, 1, nfig, -1.1, 1.1, |
| 81 |
"Legendre polynomials up to N=8") ; |
| 82 |
|
| 83 |
// petite_pause() ; |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
double* cf = new double[nn+1] ; |
| 89 |
|
| 90 |
leg.coef_interpolant_GL(ff, cf) ; |
| 91 |
|
| 92 |
cout << endl << |
| 93 |
"Coefficients of f interpolant (Gauss-Lobatto)" |
| 94 |
<< endl ; |
| 95 |
for (int i=0; i<=nn; i++) { |
| 96 |
cout << i << " : " << cf[i] << endl ; |
| 97 |
} |
| 98 |
|
| 99 |
nfig++ ; |
| 100 |
for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; |
| 101 |
plot_profile(yg, ng, 2, 1, nfig, -0.1, 1.1, "Approximation of a function") ; |
| 102 |
|
| 103 |
for (int j=0; j<ng; j++) yg[j] = leg.series(cf, xg[j]) ; |
| 104 |
plot_profile(yg, ng, 3, 1, nfig) ; |
| 105 |
leg.gauss_lobatto_nodes().plot(3, nfig) ; |
| 106 |
|
| 107 |
for (int i=0; i<=nn; i++) { |
| 108 |
xc[i] = leg.gauss_lobatto_nodes()(i) ; |
| 109 |
yc[i] = leg.series(cf, xc[i]) ; |
| 110 |
} |
| 111 |
plot_point_set(nn+1, xc, yc, 3, nfig) ; |
| 112 |
|
| 113 |
// for (int j=0; j<ng; j++) yg[j] = leg.series(cf2, xg[j]) ; |
| 114 |
// plot_profile(yg, ng, 7, 1, nfig) ; |
| 115 |
// leg.gauss_nodes().plot(7, nfig) ; |
| 116 |
|
| 117 |
double* cf_proj = new double[nn+1] ; |
| 118 |
leg.coef_projection(ff, cf_proj) ; |
| 119 |
for (int j=0; j<ng; j++) yg[j] = leg.series(cf_proj, xg[j]) ; |
| 120 |
plot_profile(yg, ng, 5, 1, nfig) ; |
| 121 |
|
| 122 |
|
| 123 |
// Check: comparison between Grid::interpole and Ortho_poly::series |
| 124 |
cout << endl << |
| 125 |
"Maximum difference between Grid::interpole and Ortho_poly::series :" |
| 126 |
<< endl ; |
| 127 |
double diff = 0 ; |
| 128 |
for (int j=0; j<ng; j++) { |
| 129 |
double diff0 = fabs( leg.series(cf, xg[j]) |
| 130 |
- leg.gauss_lobatto_nodes().interpole(ff, xg[j]) ) ; |
| 131 |
if (diff0 > diff) diff = diff0 ; |
| 132 |
} |
| 133 |
cout << " Gauss-Lobatto case : " << diff << endl ; |
| 134 |
|
| 135 |
// diff = 0 ; |
| 136 |
// for (int j=0; j<ng; j++) { |
| 137 |
// double diff0 = fabs( leg.series(cf2, xg[j]) |
| 138 |
// - leg.gauss_nodes().interpole(ff, xg[j]) ) ; |
| 139 |
// if (diff0 > diff) diff = diff0 ; |
| 140 |
// } |
| 141 |
// cout << " Gauss case : " << diff << endl ; |
| 142 |
|
| 143 |
petite_pause() ; |
| 144 |
|
| 145 |
plot_close_all() ; |
| 146 |
|
| 147 |
delete [] cf ; |
| 148 |
// delete [] cf2 ; |
| 149 |
delete [] cf_proj ; |
| 150 |
|
| 151 |
delete [] xc ; |
| 152 |
delete [] yc ; |
| 153 |
delete [] xg ; |
| 154 |
delete [] yg ; |
| 155 |
|
| 156 |
return EXIT_SUCCESS ; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
void petite_pause() { |
| 161 |
cout.flush() ; |
| 162 |
cout << "Continue = 'return'" << endl ; |
| 163 |
char cret ; |
| 164 |
cin.get(cret) ; |
| 165 |
} |
| 166 |
|
| 167 |
double ff(double x) { |
| 168 |
// return 2*x*x - 1. ; |
| 169 |
// return 1./ (cos(6*x) + 2.) ; // other example |
| 170 |
return 1. / (1. + 25.*x*x) ; |
| 171 |
// if (x == double(0)) return 0.; |
| 172 |
// else return exp(-1. / (25*x*x)) ; |
| 173 |
} |