| 1 |
/* |
| 2 |
* Approximation of a function by orthogonal polynomials |
| 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: approx_ortho.C,v 1.3 2014/10/06 15:09:47 j_novak Exp $ |
| 29 |
* $Log: approx_ortho.C,v $ |
| 30 |
* Revision 1.3 2014/10/06 15:09:47 j_novak |
| 31 |
* Modified #include directives to use c++ syntax. |
| 32 |
* |
| 33 |
* Revision 1.2 2005/11/14 14:12:09 e_gourgoulhon |
| 34 |
* Added include <assert.h> |
| 35 |
* |
| 36 |
* Revision 1.1 2005/11/14 01:56:58 e_gourgoulhon |
| 37 |
* First version |
| 38 |
* |
| 39 |
* |
| 40 |
* $Header: /cvsroot/Lorene/School05/Monday/approx_ortho.C,v 1.3 2014/10/06 15:09:47 j_novak Exp $ |
| 41 |
* |
| 42 |
*/ |
| 43 |
|
| 44 |
#include <iostream> |
| 45 |
#include <fstream> |
| 46 |
|
| 47 |
using namespace std ; |
| 48 |
|
| 49 |
#include <cstdlib> |
| 50 |
#include <cmath> |
| 51 |
|
| 52 |
#include "grid.h" |
| 53 |
#include "ortho_poly.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 |
double* yg0 = new double[ng] ; |
| 65 |
double* yg1 = new double[ng] ; |
| 66 |
|
| 67 |
for (int j=0; j<ng; j++) xg[j] = -1. + 2. * double(j) / double(ng-1) ; |
| 68 |
|
| 69 |
int nfig = -1 ; // Figure index |
| 70 |
|
| 71 |
ofstream fproj("err_proj.d") ; |
| 72 |
ofstream finter("err_inter.d") ; |
| 73 |
|
| 74 |
int nn = 0 ; |
| 75 |
while (nn >= 0) { |
| 76 |
cout << "Maximum degree N of polynomials ? (negative value = exit) " |
| 77 |
<< endl ; |
| 78 |
cin >> nn ; |
| 79 |
while ( cin.get()!='\n' ) ; |
| 80 |
if (nn < 0) continue ; |
| 81 |
|
| 82 |
double* xc = new double[nn+1] ; // to draw points |
| 83 |
double* yc = new double[nn+1] ; |
| 84 |
|
| 85 |
// Plot of f |
| 86 |
nfig++ ; |
| 87 |
for (int j=0; j<ng; j++) yg0[j] = ff(xg[j]) ; |
| 88 |
plot_profile(yg0, ng, 2, 1, nfig, -1.2, 1.2) ; |
| 89 |
|
| 90 |
// Basis of orthogonal polynomials: |
| 91 |
|
| 92 |
Chebyshev_poly poly(nn) ; |
| 93 |
|
| 94 |
// Coefficients of the projection of f onto P_N: |
| 95 |
|
| 96 |
double* cf_proj = new double[nn+1] ; |
| 97 |
poly.coef_projection(ff, cf_proj) ; |
| 98 |
|
| 99 |
double err = 0 ; |
| 100 |
for (int j=0; j<ng; j++) { |
| 101 |
double y = poly.series(cf_proj, xg[j]) ; |
| 102 |
double diff = fabs(y - yg0[j]) ; |
| 103 |
if (diff > err) err = diff ; |
| 104 |
yg1[j] = y ; |
| 105 |
} |
| 106 |
cout << "Error projection : " << err << endl ; |
| 107 |
fproj << nn << " " << err << endl ; |
| 108 |
plot_profile(yg1, ng, 5, 1, nfig) ; |
| 109 |
|
| 110 |
// Coefficients of the Gauss-Lobatto interpolant of f |
| 111 |
double* cf_inter = new double[nn+1] ; |
| 112 |
poly.coef_interpolant_GL(ff, cf_inter) ; |
| 113 |
|
| 114 |
err = 0 ; |
| 115 |
for (int j=0; j<ng; j++) { |
| 116 |
double y = poly.series(cf_inter, xg[j]) ; |
| 117 |
double diff = fabs(y - yg0[j]) ; |
| 118 |
if (diff > err) err = diff ; |
| 119 |
yg[j] = y ; |
| 120 |
} |
| 121 |
cout << "Error GL interpolant : " << err << endl ; |
| 122 |
finter << nn << " " << err << endl ; |
| 123 |
plot_profile(yg, ng, 3, 1, nfig) ; |
| 124 |
|
| 125 |
for (int i=0; i<=nn; i++) { |
| 126 |
xc[i] = poly.gauss_lobatto_nodes()(i) ; |
| 127 |
yc[i] = poly.gauss_lobatto_nodes().interpole(ff, xc[i]) ; |
| 128 |
} |
| 129 |
plot_point_set(nn+1, xc, yc, 3, nfig) ; |
| 130 |
|
| 131 |
// plot_close(nfig) ; // closing required if EPS figure |
| 132 |
|
| 133 |
// Aliasing error |
| 134 |
|
| 135 |
// for (int j=0; j<ng; j++) yg[j] -= yg1[j] ; |
| 136 |
// nfig++ ; |
| 137 |
// plot_profile(yg, ng, 4, 1, nfig, -0.2, 0.2, "Aliasing error") ; |
| 138 |
|
| 139 |
|
| 140 |
// plot_close(nfig) ; // closing required if EPS figure |
| 141 |
|
| 142 |
delete [] xc ; |
| 143 |
delete [] yc ; |
| 144 |
delete [] cf_proj ; |
| 145 |
delete [] cf_inter ; |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
plot_close_all() ; |
| 150 |
|
| 151 |
fproj.close() ; |
| 152 |
finter.close() ; |
| 153 |
|
| 154 |
delete [] xg ; |
| 155 |
delete [] yg ; |
| 156 |
delete [] yg0 ; |
| 157 |
delete [] yg1 ; |
| 158 |
|
| 159 |
return EXIT_SUCCESS ; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
double ff(double x) { |
| 165 |
// return 1. / (1. + 25.*x*x) ; |
| 166 |
// return 1. / (1. + 16.*x*x) ; |
| 167 |
return cos(exp(2*x)) ; |
| 168 |
} |
| 169 |
|