00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <iostream>
00041
00042 using namespace std ;
00043
00044 #include <stdlib.h>
00045 #include <math.h>
00046 #include <assert.h>
00047
00048 #include "grid.h"
00049 #include "plot.h"
00050
00051
00052
00053
00054
00055 Grid::Grid(int nb_nodes, double* xi) : nn(nb_nodes-1) {
00056
00057 assert(nb_nodes > 1) ;
00058
00059 xx = new double[nb_nodes] ;
00060
00061 for (int i=0; i<=nn; i++) {
00062 double xi1 = xi[i] ;
00063 if ((xi1 < -1) || (xi1 > 1)) {
00064 cerr << "Grid::Grid: node no. " << i << " is not in [-1,1] !" << endl ;
00065 abort() ;
00066 }
00067 for (int j=0; j<i; j++) {
00068 if (xi1 == xx[j]) {
00069 cerr << "Grid::Grid: the nodes must be different !" << endl ;
00070 abort() ;
00071 }
00072 }
00073
00074 xx[i] = xi1 ;
00075 }
00076 }
00077
00078
00079
00080
00081
00082
00083 Grid::Grid(const Grid& nod) : nn(nod.nn) {
00084
00085 xx = new double[nn+1] ;
00086 for (int i=0; i<=nn; i++) {
00087 xx[i] = nod.xx[i] ;
00088 }
00089 }
00090
00091
00092
00093
00094
00095 Grid::Grid(int nb_nodes) : nn(nb_nodes-1) {
00096
00097 assert(nb_nodes > 1) ;
00098
00099 xx = new double[nb_nodes] ;
00100
00101
00102
00103 }
00104
00105
00106
00107
00108
00109
00110 Grid::~Grid() {
00111
00112 delete [] xx ;
00113
00114 }
00115
00116
00117
00118
00119
00120
00121 void Grid::operator=(const Grid& nod) {
00122
00123 assert( nod.nn == nn ) ;
00124 for (int i=0; i<=nn; i++) {
00125 xx[i] = nod.xx[i] ;
00126 }
00127
00128 }
00129
00130
00131
00132
00133
00134
00135 int Grid::n() const {
00136
00137 return nn ;
00138
00139 }
00140
00141 double Grid::operator()(int i) const {
00142
00143 assert( (i >= 0) && (i<=nn) ) ;
00144
00145 return xx[i] ;
00146 }
00147
00148
00149
00150
00151
00152
00153 ostream& operator<<(ostream& ost, const Grid& xn) {
00154
00155 int nn = xn.n() ;
00156 ost << "Number of nodes : " << nn + 1 << endl ;
00157 for (int i=0; i<=nn; i++) {
00158 ost << " x(" << i << ") = " << xn(i) << endl ;
00159 }
00160
00161 return ost ;
00162 }
00163
00164
00165 void Grid::plot(int color, int nfig, double ymin, double ymax,
00166 const char* title, const char* label_y,
00167 const char* device) const {
00168
00169 for (int i=0; i<=nn; i++) {
00170
00171 plot_point(xx[i], 0., color, nfig, ymin, ymax, title, label_y, device) ;
00172
00173 }
00174
00175 }
00176
00177
00178
00179
00180
00181
00182 double Grid::lagrange(int i, double x) const {
00183
00184 double y = 1 ;
00185
00186 for (int j = 0; j<=nn ; j++) {
00187 if (j==i) continue ;
00188 y *= (x - xx[j]) / (xx[i] - xx[j]) ;
00189 }
00190
00191 return y ;
00192 }
00193
00194
00195
00196
00197
00198
00199 double Grid::nodal_polynomial(double x) const {
00200
00201 double y = 1 ;
00202
00203 for (int i = 0; i<=nn ; i++) {
00204 y *= x - xx[i] ;
00205 }
00206
00207 return y ;
00208
00209 }
00210
00211
00212
00213
00214
00215 double Grid::interpole(double (*f)(double), double x) const {
00216
00217 double y = 0 ;
00218
00219 for (int i = 0; i<=nn ; i++) {
00220 y += f(xx[i]) * lagrange(i, x) ;
00221 }
00222
00223 return y ;
00224
00225 }
00226
00227
00228
00229
00230
00231 double Grid::lebesgue_constant() const {
00232
00233 int np = 10000 ;
00234 double lbc = 0 ;
00235
00236 for (int j=0; j<np; j++) {
00237 double x = -1. + 2. * double(j) / double(np-1) ;
00238 double som = fabs( lagrange(0, x) ) ;
00239 for (int i=1; i<=nn; i++) {
00240 som += fabs( lagrange(i, x) ) ;
00241 }
00242 if (som > lbc) lbc = som ;
00243 }
00244
00245 return lbc ;
00246
00247 }