#include <cmath>
#include <iomanip>
#include <iostream>
// E01 | slides S09-S10 | Second-difference error, not an SOR project solution.
// Compile: g++ -std=c++17 -Wall -Wextra -Wpedantic -O2 examples/01_second_difference_error.cpp -o bin/01_second_difference_error
// Hand reference: f''(1)=12, D2(0.5)=12.5, D2(0.25)=12.125; error ratio=4.
double f(double x) { return x*x*x*x; }
double d2(double h) { return (f(1+h)-2*f(1)+f(1-h))/(h*h); }
int main() {
  const double a=d2(0.5), b=d2(0.25);
  const double e1=std::abs(a-12), e2=std::abs(b-12);
  std::cout<<std::setprecision(14)<<"analytic="<<12<<" d2_h05="<<a
           <<" d2_h025="<<b<<" error_h05="<<e1<<" error_h025="<<e2
           <<" ratio="<<e1/e2<<'\n';
  if (std::abs(a-12.5)>1e-12 || std::abs(b-12.125)>1e-12 || std::abs(e1/e2-4)>1e-12) return 1;
  std::cout<<"PASS E01\n";
}
