Debian Codegraph @

School05/Monday/runge.C

raw
1 /*
2 * Code to exhibit Runge's phenomenon
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: runge.C,v 1.3 2014/10/06 15:09:48 j_novak Exp $
29 * $Log: runge.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:17:02 e_gourgoulhon
34 * Added #include <stdio.h>
35 *
36 * Revision 1.1 2005/11/14 01:57:00 e_gourgoulhon
37 * First version
38 *
39 *
40 * $Header: /cvsroot/Lorene/School05/Monday/runge.C,v 1.3 2014/10/06 15:09:48 j_novak Exp $
41 *
42 */
43
44 #include <iostream>
45
46 using namespace std ;
47
48 #include <cstdlib>
49 #include <cstdio>
50 #include <cstring>
51 #include <cmath>
52
53 #include "grid.h"
54 #include "plot.h"
55
56 void petite_pause() ;
57 double ff(double x) ;
58
59 int main() {
60
61 // For the plots
62 int ng = 1000 ; // Number of points to draw the curves
63 double* xg = new double[ng] ;
64 double* yg = new double[ng] ;
65 for (int j=0; j<ng; j++) xg[j] = -1. + 2. * double(j) / double(ng-1) ;
66
67
68 for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ;
69
70 int nfig0 = 0 ; // Figure index
71 plot_profile(yg, ng, 1, 1, nfig0, -0.5, 1.5, "Interpolation of 1/(1+25x\\u2\\d)") ;
72
73 // Loop on the number of nodes
74
75 for (int k = 0; k<6; k++) {
76
77 int nb_nodes = 4*(k+1) + 1 ;
78 int nn = nb_nodes - 1 ;
79
80 int nfig1 = k+1 ; // Figure index
81
82 Grid_uniform xcoloc(nb_nodes) ; // Construction of the grid
83
84 // Grid_Chebyshev_GL xcoloc(nb_nodes) ; // other example (free of Runge phenomenon)
85
86 cout << "Grid: " << xcoloc << endl ;
87
88 // Interpolating polynomial
89
90 for (int j=0; j<ng; j++) yg[j] = ff(xg[j]) ;
91
92 char title[180] ;
93 strcpy(title, "Interpolation of 1/(1+25x\\u2\\d) , N = ") ;
94 char string_nn[4] ;
95 sprintf(string_nn, "%d", nn) ;
96 strcat(title, string_nn) ;
97 plot_profile(yg, ng, 1, 1, nfig1, -0.5, 1.5, title) ;
98
99 for (int j=0; j<ng; j++) yg[j] = xcoloc.interpole(ff, xg[j]) ;
100
101 plot_profile(yg, ng, k%15+2, 2, nfig0) ;
102 plot_profile(yg, ng, 2, 1, nfig1) ;
103
104 xcoloc.plot(2, nfig1) ;
105
106 } // end of loop on the number of nodes
107
108 petite_pause() ;
109
110 plot_close_all() ;
111
112 delete [] xg ;
113 delete [] yg ;
114
115 return EXIT_SUCCESS ;
116 }
117
118 void petite_pause() {
119 cout.flush() ;
120 cout << "Continue = 'return'" << endl ;
121 char cret ;
122 cin.get(cret) ;
123 }
124
125
126 double ff(double x) {
127 // return 2*x*x - 1. ;
128 // return 1./ (cos(6*x) + 2.) ; // other example
129 return 1. / (1. + 25.*x*x) ;
130 // if (x == double(0)) return 0.;
131 // else return exp(-1. / (25*x*x)) ;
132 }
133