// ch13_golden_section.cpp // Companion code for Chapter 13: Optimization and Data Fitting. // Standalone C++11; standard library only; console output without plotting. // Compile: c++ -std=c++11 -O2 -Wall -Wextra -pedantic ch13_golden_section.cpp -o ch13_golden_section // Edit the example settings in main(); routines above main() can be reused. // Fixed seeds reproduce runs with the same standard-library implementation. // Random distributions need not give identical sequences across compilers/languages. #include #include #include #include #include #include #include #include #include #include #include #include #include using Vec = std::vector; using Mat = std::vector; using Function = std::function; using ScalarFunction = std::function; const double pi = std::acos(-1.0); void require(bool condition, const std::string& message) { if (!condition) throw std::invalid_argument(message); } void finite_vector(const Vec& x) { require(!x.empty(), "A nonempty vector is required."); for (double v : x) require(std::isfinite(v), "Data must be finite."); } void check_bounds(const Vec& lo, const Vec& hi) { finite_vector(lo); finite_vector(hi); require(lo.size() == hi.size(), "Bounds must have matching dimensions."); for (std::size_t j=0; jlo[j] && std::isfinite(hi[j]-lo[j]), "Require finite upper > lower."); } double objective_value(double value) { require(std::isfinite(value), "Objective must return a finite real scalar."); return value; } double clip(double x, double lo, double hi) { return std::max(lo,std::min(hi,x)); } Vec linspace(double a, double b, int n) { require(n>=2, "Grid needs at least two points."); Vec x(n); for (int i=0; i uniform{0.0,1.0}; std::normal_distribution normal{0.0,1.0}; explicit Random(unsigned int seed) : engine(seed) {} double u() { return uniform(engine); } double z() { return normal(engine); } int index(int n) { return std::uniform_int_distribution(0,n-1)(engine); } }; struct Result { Vec point; double value=0.0; long long evaluations=0; int iterations=0; std::string status; }; void print_result(const Result& r) { std::cout << std::setprecision(12); for (std::size_t j=0; j0 && max_iter>=0, "Invalid tolerance or iteration limit."); const double rho=(std::sqrt(5.0)-1.0)/2.0; double x1=b-rho*(b-a), x2=a+rho*(b-a); double f1=objective_value(f(x1)), f2=objective_value(f(x2)); Result r; r.evaluations=2; r.status="Maximum iteration count reached."; while (b-a>tol_x && r.iterationsf2) { a=x1; x1=x2; f1=f2; x2=a+rho*(b-a); f2=objective_value(f(x2)); } else { b=x2; x2=x1; f2=f1; x1=b-rho*(b-a); f1=objective_value(f(x1)); } ++r.iterations; ++r.evaluations; } if (b-a<=tol_x) r.status="Interval-width tolerance reached."; double x=a+0.5*(b-a); r.point={x}; r.value=objective_value(f(x)); ++r.evaluations; final_a=a; final_b=b; return r; } int main() { try { ScalarFunction f=[](double x) { return (x-2)*(x-2)+1; }; double a=-1,b=4, tol_x=1e-6, final_a,final_b; int max_iter=200; Result r=golden_section(f,a,b,tol_x,max_iter,final_a,final_b); print_result(r); std::cout << "Final interval: [" << final_a << ", " << final_b << "]\nWidth: " << final_b-final_a << '\n'; return 0; } catch (const std::exception& e) { std::cerr << "Error: " << e.what() << '\n'; return 1; } }