#include #include #include #include #include #include #define Nstep 3200 #define A -1. #define B 1. #define MAXIT 100000000 #define TOL 1e-9 #define M_PI 3.14159265358979323846 std::vector Set1DGrid(double a,double b, std::function f); void SetBC(double lower, double upper, std::vector & solution); void IterativeSolver(std::vector & u,std::vector & rhs, double tol); void WriteFile(std::string filename, std::vector f, std::vector grid); double dx = (B-A)/(Nstep-1); std::function Grid = [](double x) { return x; }; std::function RHS = [](double x) { return -M_PI*exp(x)*(M_PI*exp(x)*cos(M_PI*exp(x))+sin(M_PI*exp(x))); }; std::function FirstGuess = [](double x) { return 0; }; std::function ExactResult = [](double x) { return cos(M_PI*exp(x)); }; int main (int argc, char *argv[]){ std::vector MyGrid = Set1DGrid(A,B, Grid); std::vector rhs = Set1DGrid(A,B, RHS); std::vector u = Set1DGrid(A,B, FirstGuess); std::vector exact = Set1DGrid(A,B, ExactResult); SetBC(cos(M_PI*exp(-1)),cos(M_PI*exp(1)),u); IterativeSolver(u,rhs,TOL); WriteFile("solution.txt",u,MyGrid); WriteFile("exact.txt",exact,MyGrid); return 0; } std::vector Set1DGrid(double a,double b, std::function f){ std::vector grid(Nstep); for (int i=0;i & solution){ solution[0]=lower; solution[Nstep-1]=upper; } void IterativeSolver(std::vector & u,std::vector & rhs, double tol){ int iter=0; std::vector prevU = u; double max; while (iter<=MAXIT){ max=0; for (int i=1;i max? fabs(u[i]-prevU[i]):max; } if (max < tol){ std::cout<<"#Solution converged in "< f,std::vector grid){ std::ofstream outfile; outfile.open(filename.c_str()); if (!outfile.is_open()) { std::cout<<"Cannot open file "<