| 1 |
/* |
| 2 |
* Comparison of various interpolation of a function |
| 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: interpole.C,v 1.3 2014/10/06 15:09:48 j_novak Exp $ |
| 29 |
* $Log: interpole.C,v $ |
| 30 |
* Revision 1.3 2014/10/06 15:09:48 j_novak |
| 31 |
* Modified #include directives to use c++ syntax. |
| 32 |
* |
| 33 |
* Revision 1.2 2005/11/14 14:11:22 e_gourgoulhon |
| 34 |
* log(int ) -> log(double) |
| 35 |
* + suppressed plot_close. |
| 36 |
* |
| 37 |
* Revision 1.1 2005/11/14 01:56:59 e_gourgoulhon |
| 38 |
* First version |
| 39 |
* |
| 40 |
* |
| 41 |
* $Header: /cvsroot/Lorene/School05/Monday/interpole.C,v 1.3 2014/10/06 15:09:48 j_novak Exp $ |
| 42 |
* |
| 43 |
*/ |
| 44 |
|
| 45 |
#include <iostream> |
| 46 |
#include <fstream> |
| 47 |
|
| 48 |
using namespace std ; |
| 49 |
|
| 50 |
#include <cstdlib> |
| 51 |
#include <cmath> |
| 52 |
|
| 53 |
#include "grid.h" |
| 54 |
#include "plot.h" |
| 55 |
|
| 56 |
double ff(double x) ; |
| 57 |
|
| 58 |
int main() { |
| 59 |
|
| 60 |
// For the plots |
| 61 |
int ng = 1000 ; // Number of points to draw the curves |
| 62 |
double* xg = new double[ng] ; |
| 63 |
double* yg = new double[ng] ; |
| 64 |
for (int j=0; j<ng; j++) xg[j] = -1. + 2. * double(j) / double(ng-1) ; |
| 65 |
|
| 66 |
int nfig = -1 ; // Figure index |
| 67 |
|
| 68 |
ofstream funif("err_unif.d") ; |
| 69 |
ofstream fleg("err_leg.d") ; |
| 70 |
ofstream fcheb("err_cheb.d") ; |
| 71 |
|
| 72 |
int nn = 0 ; |
| 73 |
while (nn >= 0) { |
| 74 |
cout << "Maximum degree N of polynomials ? (negative value = exit) " |
| 75 |
<< endl ; |
| 76 |
cin >> nn ; |
| 77 |
while ( cin.get()!='\n' ) ; |
| 78 |
if (nn < 0) continue ; |
| 79 |
|
| 80 |
double* xc = new double[nn+1] ; // to draw points |
| 81 |
double* yc = new double[nn+1] ; |
| 82 |
|
| 83 |
// The three types of grid: |
| 84 |
|
| 85 |
Grid_uniform x_unif(nn+1) ; |
| 86 |
Grid_Legendre_GL x_leg(nn+1) ; |
| 87 |
Grid_Chebyshev_Gauss x_cheb(nn+1) ; |
| 88 |
|
| 89 |
cout << "Lebesgue constants: " << endl ; |
| 90 |
cout << " uniform grid : " << x_unif.lebesgue_constant() << endl ; |
| 91 |
cout << " Legendre GL grid : " << x_leg.lebesgue_constant() << endl ; |
| 92 |
cout << " Chebyshev Gauss grid : " << x_cheb.lebesgue_constant() |
| 93 |
<< " <-> theor. lower / upper bound: " |
| 94 |
<< 2./M_PI * log(double(nn+1)) + 0.9625 << " / " |
| 95 |
<< 2./M_PI * log(double(nn+1)) + 1 << endl ; |
| 96 |
|
| 97 |
// Interpolation through the uniform grid : |
| 98 |
|
| 99 |
nfig++ ; |
| 100 |
for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; |
| 101 |
plot_profile(yg, ng, 2, 1, nfig, -1.2, 1.2) ; |
| 102 |
|
| 103 |
double err = 0 ; |
| 104 |
for (int j=0; j<ng; j++) { |
| 105 |
double y = x_unif.interpole(ff, xg[j]) ; |
| 106 |
double diff = fabs(y - yg[j]) ; |
| 107 |
if (diff > err) err = diff ; |
| 108 |
yg[j] = y ; |
| 109 |
} |
| 110 |
cout << "Error uniform interpolation: " << err << endl ; |
| 111 |
funif << nn << " " << err << endl ; |
| 112 |
plot_profile(yg, ng, 3, 1, nfig) ; |
| 113 |
|
| 114 |
for (int i=0; i<=nn; i++) { |
| 115 |
xc[i] = x_unif(i) ; |
| 116 |
yc[i] = x_unif.interpole(ff, xc[i]) ; |
| 117 |
} |
| 118 |
plot_point_set(nn+1, xc, yc, 3, nfig) ; |
| 119 |
|
| 120 |
// plot_close(nfig) ; // closing required if EPS figure |
| 121 |
|
| 122 |
// Interpolation through the Legendre GL grid : |
| 123 |
|
| 124 |
nfig++ ; |
| 125 |
for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; |
| 126 |
plot_profile(yg, ng, 2, 1, nfig, -0.2, 1.2) ; |
| 127 |
|
| 128 |
err = 0 ; |
| 129 |
for (int j=0; j<ng; j++) { |
| 130 |
double y = x_leg.interpole(ff, xg[j]) ; |
| 131 |
double diff = fabs(y - yg[j]) ; |
| 132 |
if (diff > err) err = diff ; |
| 133 |
yg[j] = y ; |
| 134 |
} |
| 135 |
cout << "Error Legendre GL interpolation: " << err << endl ; |
| 136 |
fleg << nn << " " << err << endl ; |
| 137 |
plot_profile(yg, ng, 3, 1, nfig) ; |
| 138 |
|
| 139 |
for (int i=0; i<=nn; i++) { |
| 140 |
xc[i] = x_leg(i) ; |
| 141 |
yc[i] = x_leg.interpole(ff, xc[i]) ; |
| 142 |
} |
| 143 |
plot_point_set(nn+1, xc, yc, 3, nfig) ; |
| 144 |
|
| 145 |
// plot_close(nfig) ; // closing required if EPS figure |
| 146 |
|
| 147 |
// Interpolation through the Chebyshev Gauss grid : |
| 148 |
|
| 149 |
nfig++ ; |
| 150 |
for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ; |
| 151 |
plot_profile(yg, ng, 2, 1, nfig, -0.2, 1.2) ; |
| 152 |
|
| 153 |
err = 0 ; |
| 154 |
for (int j=0; j<ng; j++) { |
| 155 |
double y = x_cheb.interpole(ff, xg[j]) ; |
| 156 |
double diff = fabs(y - yg[j]) ; |
| 157 |
if (diff > err) err = diff ; |
| 158 |
yg[j] = y ; |
| 159 |
} |
| 160 |
cout << "Error Chebyshev Gauss interpolation: " << err << endl ; |
| 161 |
fcheb << nn << " " << err << endl ; |
| 162 |
plot_profile(yg, ng, 3, 1, nfig) ; |
| 163 |
|
| 164 |
for (int i=0; i<=nn; i++) { |
| 165 |
xc[i] = x_cheb(i) ; |
| 166 |
yc[i] = x_cheb.interpole(ff, xc[i]) ; |
| 167 |
} |
| 168 |
plot_point_set(nn+1, xc, yc, 3, nfig) ; |
| 169 |
|
| 170 |
// plot_close(nfig) ; // closing required if EPS figure |
| 171 |
|
| 172 |
delete [] xc ; |
| 173 |
delete [] yc ; |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
plot_close_all() ; |
| 178 |
|
| 179 |
funif.close() ; |
| 180 |
fleg.close() ; |
| 181 |
fcheb.close() ; |
| 182 |
|
| 183 |
delete [] xg ; |
| 184 |
delete [] yg ; |
| 185 |
|
| 186 |
return EXIT_SUCCESS ; |
| 187 |
} |
| 188 |
|
| 189 |
double ff(double x) { |
| 190 |
// return 1. / (1. + 25.*x*x) ; |
| 191 |
return 1. / (1. + 16.*x*x) ; |
| 192 |
// return cos(exp(2*x)) ; |
| 193 |
} |