| 1 |
/* |
| 2 |
* Test code for class Grid |
| 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: es.C,v 1.2 2014/10/06 15:09:47 j_novak Exp $ |
| 29 |
* $Log: es.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/es.C,v 1.2 2014/10/06 15:09:47 j_novak Exp $ |
| 38 |
* |
| 39 |
*/ |
| 40 |
|
| 41 |
|
| 42 |
#include <iostream> |
| 43 |
|
| 44 |
using namespace std ; |
| 45 |
|
| 46 |
#include <cstdlib> |
| 47 |
#include <cmath> |
| 48 |
|
| 49 |
#include "grid.h" |
| 50 |
#include "plot.h" |
| 51 |
|
| 52 |
void petite_pause() ; |
| 53 |
double ff(double x) ; |
| 54 |
|
| 55 |
int main() { |
| 56 |
|
| 57 |
// For the plots |
| 58 |
int ng = 1000 ; // Number of points to draw the curves |
| 59 |
double* xg = new double[ng] ; |
| 60 |
double* yg = new double[ng] ; |
| 61 |
for (int j=0; j<ng; j++) xg[j] = -1. + 2. * double(j) / double(ng-1) ; |
| 62 |
|
| 63 |
// Nodes |
| 64 |
int nb_nodes = 1 ; |
| 65 |
while (nb_nodes > 0) { |
| 66 |
cout << "Number of nodes ? (negative value = exit) " << endl ; |
| 67 |
cin >> nb_nodes ; |
| 68 |
while ( cin.get()!='\n' ) ; |
| 69 |
if (nb_nodes <= 0) continue ; |
| 70 |
|
| 71 |
int nn = nb_nodes - 1 ; |
| 72 |
|
| 73 |
double* xx = new double[nb_nodes] ; |
| 74 |
for (int i=0; i<=nn; i++) xx[i] = -1 + 2 * double(i) / double(nn) ; |
| 75 |
|
| 76 |
Grid xcoloc(nb_nodes,xx) ; // Construction of the grid |
| 77 |
|
| 78 |
cout << "Grid: " << xcoloc << endl ; |
| 79 |
cout << "Lebesgue constant : " << xcoloc.lebesgue_constant() << endl ; |
| 80 |
int nfig = 0 ; // index of next figure |
| 81 |
// xcoloc.plot(3, nfig) ; // draws the point in 3=green |
| 82 |
|
| 83 |
// Lagrange polynomials |
| 84 |
|
| 85 |
for (int i=0; i<=nn; i++) { |
| 86 |
for (int j=0; j<ng; j++) yg[j] = xcoloc.lagrange(i, xg[j]) ; |
| 87 |
// nfig++ ; // new figure |
| 88 |
plot_profile(yg, ng, i%15+2, 1, nfig, -3., 3., "Lagrange polynomials") ; |
| 89 |
xcoloc.plot(1, nfig) ; |
| 90 |
plot_point(xcoloc(i), 0., i%15+2, nfig) ; |
| 91 |
double xp = xcoloc(i) ; |
| 92 |
double yp = xcoloc.lagrange(i, xp) ; |
| 93 |
plot_point_set(1, &xp, &yp, i%15+2, nfig) ; |
| 94 |
// plot_close(nfig) ; // Figure needs to be closed before the |
| 95 |
// next one if it's an EPS file. |
| 96 |
} |
| 97 |
|
| 98 |
// Nodal polynomial |
| 99 |
|
| 100 |
for (int j=0; j<ng; j++) yg[j] = xcoloc.nodal_polynomial(xg[j]) ; |
| 101 |
|
| 102 |
nfig++ ; // new figure |
| 103 |
plot_profile(yg, ng, 2, 1, nfig, -0.1, 0.1, "Nodal polynomial") ; |
| 104 |
xcoloc.plot(2, nfig) ; |
| 105 |
|
| 106 |
// Interpolating polynomial |
| 107 |
|
| 108 |
for (int j=0; j<ng; j++) { |
| 109 |
yg[j] = ff(xg[j]) ; |
| 110 |
} |
| 111 |
nfig++ ; // new figure |
| 112 |
// plot_profile(yg, ng, 3, 1, nfig, -0.2, 1.1, "Interpolation of 1/(1+25x\\u2\\d)") ; |
| 113 |
plot_profile(yg, ng, 3, 1, nfig, -1.2, 1.2, "Interpolation of cos(2 exp(x))") ; |
| 114 |
|
| 115 |
for (int j=0; j<ng; j++) { |
| 116 |
yg[j] = xcoloc.interpole(ff, xg[j]) ; |
| 117 |
} |
| 118 |
plot_profile(yg, ng, 2, 1, nfig) ; |
| 119 |
xcoloc.plot(2, nfig) ; |
| 120 |
|
| 121 |
petite_pause() ; |
| 122 |
|
| 123 |
plot_close_all() ; |
| 124 |
|
| 125 |
delete [] xx ; |
| 126 |
|
| 127 |
} // end of while |
| 128 |
|
| 129 |
delete [] xg ; |
| 130 |
delete [] yg ; |
| 131 |
|
| 132 |
return EXIT_SUCCESS ; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
void petite_pause() { |
| 137 |
cout.flush() ; |
| 138 |
cout << "Continue = 'return'" << endl ; |
| 139 |
char cret ; |
| 140 |
cin.get(cret) ; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
double ff(double x) { |
| 145 |
// return 1. / (1. + 25.*x*x) ; |
| 146 |
return cos(exp(2*x)) ; |
| 147 |
} |