//======================================================================= // Chapter 10, code 4: eigenvalues by Numerov shooting and bisection. // Translation of ch10_numerov_eigenvalues.f90. // Solve y'' = g(x,lambda)*y, with y(a) = y(b) = 0. // Scan lambda for endpoint sign changes, then refine each bracket. // Example: a uniform string, y'' = -lambda*y on [0,1]. // Exact eigenvalues: lambda_m = (m*pi/(b-a))^2, m = 1,2,... . // Normalize each mode so that integral(y*y dx) = 1. // // To solve another eigenvalue problem: // 1. Edit the interval, grid, number of modes, and search controls. // 2. Edit coefficient(x,lambda) immediately below the driver. // 3. Disable check_exact or replace the analytic comparisons. // Assumes zero fixed-value boundaries at BOTH ends and no y' term. // Compile: g++ -std=c++11 -O2 ch10_numerov_eigenvalues.cpp -o ch10_numerov_eigenvalues //======================================================================= #include #include #include #include #include #include #include using Coefficient = double (*)(double,double); double coefficient(double x, double lambda); bool find_modes(Coefficient fcn, int n, const std::vector& x, int modes, double lambda_min, double lambda_max, double scan_step, double tolerance, std::vector& values, std::vector>& shapes, std::vector& residuals, std::vector& iterations); bool bisect(Coefficient fcn, int n, const std::vector& x, double left, double right, double f_left, double tolerance, double& root, int& iterations); bool numerov(Coefficient fcn, int n, const std::vector& x, double lambda, std::vector& y, double& mismatch); bool normalize_mode(int n, const std::vector& x, std::vector& y); int main() { //-------------------- Problem and search: edit here ----------------- const int n = 200; // Grid intervals const int modes = 3; double a = 0.0; double b = 1.0; double lambda_min = 0.0; double lambda_max = 100.0; double scan_step = 1.0; double tolerance = 1.0e-10; // Maximum final lambda-bracket width bool check_exact = true; // Increase lambda_max if the range contains too few modes. // Make scan_step small enough to separate consecutive roots. //------------------------------------------------------------------- if (n < 2 || modes < 1 || b <= a) { std::cerr << "Use n >= 2, modes >= 1, and b > a.\n"; return 1; } if (lambda_max <= lambda_min || scan_step <= 0.0 || tolerance <= 0.0) { std::cerr << "Use an increasing search interval, positive scan step, and positive tolerance.\n"; return 1; } double h = (b-a)/n; const double pi = std::acos(-1.0); std::vector x(n+1), values(modes), residuals(modes), errors(modes,0.0); // shapes[m][i] is mode m+1 at grid point i; C++ indices start at zero. std::vector> shapes(modes,std::vector(n+1)); std::vector iterations(modes); for (int i = 0; i <= n; ++i) x[i] = a+i*h; x[n] = b; std::cout << std::uppercase << "Eigenvalues: Numerov shooting and bisection\n" << "Grid intervals: " << std::setw(8) << n << '\n' << "Grid spacing: " << std::fixed << std::setprecision(6) << std::setw(12) << h << '\n' << "Bracket tolerance:" << std::scientific << std::setprecision(4) << std::setw(12) << tolerance << '\n'; if (!find_modes(coefficient,n,x,modes,lambda_min,lambda_max,scan_step, tolerance,values,shapes,residuals,iterations)) return 1; std::cout << '\n' << std::setw(6) << "mode" << ' ' << std::setw(18) << "numerical lambda"; if (check_exact) std::cout << ' ' << std::setw(18) << "exact lambda" << ' ' << std::setw(12) << "abs error"; std::cout << ' ' << std::setw(10) << "bisections" << '\n'; for (int m = 0; m < modes; ++m) { std::cout << std::setw(6) << m+1 << std::fixed << std::setprecision(10) << ' ' << std::setw(18) << values[m]; if (check_exact) { double k = (m+1)*pi/(b-a); double exact = k*k; std::cout << ' ' << std::setw(18) << exact << std::scientific << std::setprecision(4) << ' ' << std::setw(12) << std::abs(values[m]-exact); } std::cout << ' ' << std::setw(10) << iterations[m] << '\n'; } // One file for all modes: x, y_1, y_2, ... . Comments record eigenvalues. std::ofstream data("ch10_numerov_eigenvalues.dat"); if (!data) { std::cerr << "Cannot open ch10_numerov_eigenvalues.dat.\n"; return 1; } data << std::uppercase << std::scientific << std::setprecision(16) << "# Numerov eigenfunctions: x y_1 y_2 ...\n" << "# Each mode has integral(y**2 dx) = 1 (trapezoidal rule).\n"; for (int m = 0; m < modes; ++m) data << "# mode " << std::setw(6) << m+1 << " lambda = " << std::setw(24) << values[m] << '\n'; std::cout << "\nEigenfunctions: columns are x, then the normalized modes.\n"; for (int i = 0; i <= n; ++i) { std::cout << std::fixed << std::setprecision(6) << std::setw(10) << x[i]; data << ' ' << std::setw(24) << x[i]; for (int m = 0; m < modes; ++m) { std::cout << std::scientific << std::setprecision(8) << ' ' << std::setw(16) << shapes[m][i]; data << ' ' << std::setw(24) << shapes[m][i]; if (check_exact) { double exact_shape = std::sqrt(2.0/(b-a)) *std::sin((m+1)*pi*(x[i]-a)/(b-a)); errors[m] = std::max(errors[m],std::abs(shapes[m][i]-exact_shape)); } } std::cout << '\n'; data << '\n'; } data.close(); std::cout << '\n' << std::setw(6) << "mode" << ' ' << std::setw(18) << "endpoint residual" << '\n'; for (int m = 0; m < modes; ++m) // Residuals are AFTER normalization; do not force them to zero. std::cout << std::setw(6) << m+1 << ' ' << std::setw(18) << residuals[m] << '\n'; if (check_exact) { std::cout << std::setw(6) << "mode" << ' ' << std::setw(20) << "largest shape error" << '\n'; for (int m = 0; m < modes; ++m) std::cout << std::setw(6) << m+1 << ' ' << std::setw(18) << errors[m] << '\n'; } std::cout << "Data file: ch10_numerov_eigenvalues.dat\n"; return 0; } double coefficient(double x, double lambda) { // Supply g(x,lambda) in y'' = g(x,lambda)*y. // Uniform string: g = -lambda. // Variable density rho(x): g = -lambda*rho(x) in dimensionless units. // Numerov requires this linear homogeneous form with no y' term. (void)x; return -lambda; } bool find_modes(Coefficient fcn, int n, const std::vector& x, int modes, double lambda_min, double lambda_max, double scan_step, double tolerance, std::vector& values, std::vector>& shapes, std::vector& residuals, std::vector& iterations) { // Mismatch y(b;lambda) vanishes at an eigenvalue. // A sign scan can miss two or more roots in one interval: reduce // scan_step and repeat when adapting the equation. // Analytic mode labels assume the lowest modes are found in order. int found = 0; double left = lambda_min, f_left; std::vector trial(n+1); if (!numerov(fcn,n,x,left,trial,f_left)) return false; // Handle an eigenvalue exactly at the initial endpoint once. if (f_left == 0.0) { values[found] = left; if (!normalize_mode(n,x,trial)) return false; shapes[found] = trial; residuals[found] = trial[n]; iterations[found] = 0; ++found; } while (found < modes && left < lambda_max) { double right = std::min(left+scan_step,lambda_max); if (right <= left) { std::cerr << "Scan step cannot advance lambda.\n"; return false; } double f_right; if (!numerov(fcn,n,x,right,trial,f_right)) return false; bool bracketed = (f_left < 0.0 && f_right > 0.0) || (f_left > 0.0 && f_right < 0.0); if (f_right == 0.0 || bracketed) { double root; int count; if (f_right == 0.0) { root = right; count = 0; } else if (!bisect(fcn,n,x,left,right,f_left,tolerance,root,count)) { return false; } values[found] = root; iterations[found] = count; double mismatch; if (!numerov(fcn,n,x,root,trial,mismatch)) return false; if (!normalize_mode(n,x,trial)) return false; shapes[found] = trial; residuals[found] = trial[n]; ++found; } left = right; f_left = f_right; } if (found < modes) { std::cerr << "Too few modes found: enlarge the range or reduce scan_step.\n"; return false; } return true; } bool bisect(Coefficient fcn, int n, const std::vector& x, double left, double right, double f_left, double tolerance, double& root, int& iterations) { // Stop on bracket width, not mismatch magnitude, which depends on // arbitrary shooting amplitude. Local copies preserve scan endpoints. const int max_iterations = 100; double lo = left, hi = right, flo = f_left; std::vector trial(n+1); iterations = 0; while (hi-lo > tolerance) { if (iterations >= max_iterations) { std::cerr << "Bisection iteration limit reached.\n"; return false; } double mid = lo+(hi-lo)/2.0; if (mid <= lo || mid >= hi) { std::cerr << "Lambda bracket cannot shrink; check tolerance.\n"; return false; } double fmid; if (!numerov(fcn,n,x,mid,trial,fmid)) return false; ++iterations; if (fmid == 0.0) { root = mid; return true; } if ((flo < 0.0 && fmid > 0.0) || (flo > 0.0 && fmid < 0.0)) { hi = mid; } else { lo = mid; flo = fmid; } } root = lo+(hi-lo)/2.0; return true; } bool numerov(Coefficient fcn, int n, const std::vector& x, double lambda, std::vector& y, double& mismatch) { // Numerov recurrence for y'' = g(x,lambda)*y: // (1-h*h*g[i+1]/12)*y[i+1] = // 2*(1+5*h*h*g[i]/12)*y[i] - (1-h*h*g[i-1]/12)*y[i-1]. // y[0] = 0 and y[1] = h fix an arbitrary nonzero amplitude. // No Taylor approximation of a derivative is needed here: this linear // homogeneous recurrence makes eigenvalues independent of amplitude. // This starting choice applies to the zero-value left boundary only. double h = (x[n]-x[0])/n; double c = h*h/12.0; std::vector g(n+1); for (int i = 0; i <= n; ++i) { g[i] = fcn(x[i],lambda); if (!std::isfinite(g[i])) { std::cerr << "Nonfinite Numerov coefficient.\n"; return false; } } y[0] = 0.0; y[1] = h; for (int i = 1; i < n; ++i) { double denominator = 1.0-c*g[i+1]; if (std::abs(denominator) <= std::numeric_limits::epsilon() *std::max(1.0,std::abs(c*g[i+1]))) { std::cerr << "Near-zero Numerov denominator: refine the grid.\n"; return false; } y[i+1] = (2.0*(1.0+5.0*c*g[i])*y[i]-(1.0-c*g[i-1])*y[i-1])/denominator; if (!std::isfinite(y[i+1])) { std::cerr << "Nonfinite Numerov solution: check equation, interval, and grid.\n"; return false; } } mismatch = y[n]; return true; } bool normalize_mode(int n, const std::vector& x, std::vector& y) { // Trapezoidal approximation to integral(y*y dx); keep the initial sign. double h = (x[n]-x[0])/n; double interior_sum = 0.0; for (int i = 1; i < n; ++i) interior_sum += y[i]*y[i]; double norm = std::sqrt(h*(0.5*y[0]*y[0]+interior_sum+0.5*y[n]*y[n])); if (norm <= 0.0 || !std::isfinite(norm)) { std::cerr << "Cannot normalize the eigenfunction.\n"; return false; } for (int i = 0; i <= n; ++i) y[i] /= norm; return true; }