Code for Poisson with Dirichlet conditions using Explicit Method as well as python code to plot it

C++ code to get the solution #include <iostream> #include <chrono> #include <functional> #include <string> #include <fstream> #include <sstream> using namespace std; //compile: g++ -std=c++14 poissonDirichlet.cpp -o solver_dirichlet // Thanks for an answer here, I am using this timer //http://stackoverflow.com/questions/728068/how-to-calculate-a-time-difference-in-c class Timer { public: Timer() : beg_(clock_::now()) {} void reset() { beg_ = clock_::now(); } double elapsed() const { return std::chrono::duration_cast<second_> (clock_::now() - beg_).count(); } private: typedef std::chrono::high_resolution_clock clock_; typedef std::chrono::duration<double, std::ratio<1> > second_; std::chrono::time_point<clock_> beg_; }; double abs(double x) { if(x < 0) return -x; return x; } // Remember we are considering number of points in x == y double ** getMesh(int n) { // we use calloc to initialize everything with zeros double ** mesh = (double **) calloc (n, sizeof(double *)); for (int i = 0; i < n; i++) { mesh[i] = (double *) calloc (n, sizeof(double)); } return mesh; } /** g is a function of (x,y) that is applied on the boundaries */ void computeBoundaries(double ** mesh, int n, function<double(double, double)> g) { int i, j; double dx = 1 / (n - 1); i = 0; for (j = 0; j < n; j++) mesh[i][j] = g(i * dx, j * dx); i = n - 1; for (j = 0; j < n; j++) mesh[i][j] = g(i * dx, j * dx); j = 0; for (i = 0; i < n; i++) mesh[i][j] = g(i * dx, j * dx); j = n - 1; for (i = 0; i < n; i++) mesh[i][j] = g(i * dx, j * dx); return; } /** f is a function that expects mesh and i, j and computes the value of that point....

September 3, 2016 · 5 min

Code a program to solve a partial differential equation using finite differences with an explicit method

The goal of this post is to create a program to solve a differential equation in 2D. I will present the programming in C++ but the concepts here are easily translated to other languages. I myself have used Julia a couple times. The equation that we are going to solve is the Poisson equation with Dirichlet boundary conditions in a $1\times 1$ square. The equation and the conditions are given by...

September 3, 2016 · 6 min