MATH5620

Algorithms and Subroutines for Solving Differential Equations

Table of Contents

Five Point Stencil

Routine Name: solveFivePointStencil

Author: Philip Nelson

Language: C++

Description

fivePointStencil solves Poisson’s Equation for an arbitrary mesh size and forcing function

Input

solveFivePointStencil(T a, T b, F f) requires:

Output

An array with the approximated solution

Code

template <typename T, std::size_t N, typename F>
auto solveFivePointStencil(T a, T b, F f)
{
  auto mesh = generateMesh<double, N>(a, b);
  auto bv = initMeshB(mesh, f);
  auto stencil = fivePointStencil<double, N-2>();
  auto res = stencil.solveLinearSystemLU(bv);
  return res;
}

solveFivePointStencil relies on: fivePointStencil| generateMesh| initMeshB| solveLinearSystemLU|

Example

int main()
{
  auto answer = solveFivePointStencil<double, 5>(0.0, 1.0, sin);
  auto finalMat = arrayToMat(answer);
  std::cout << "Answer in Matrix Form\n" << finalMat << std::endl;
}

Result

mesh
|     0.0625     0.125     0.188 |
|      0.125      0.25     0.375 |
|      0.188     0.375     0.562 |


stencil
|         -4         1         0         1         0         0         0         0         0 |
|          1        -4         1         0         1         0         0         0         0 |
|          0         1        -4         0         0         1         0         0         0 |
|          1         0         0        -4         1         0         1         0         0 |
|          0         1         0         1        -4         1         0         1         0 |
|          0         0         1         0         1        -4         0         0         1 |
|          0         0         0         1         0         0        -4         1         0 |
|          0         0         0         0         1         0         1        -4         1 |
|          0         0         0         0         0         1         0         1        -4 |


bv
[     0.0625     0.125     0.186     0.125     0.247     0.366     0.186     0.366     0.533 ]


result
[     -0.097    -0.163    -0.154    -0.163    -0.276    -0.266    -0.154    -0.266    -0.266 ]

Answer in Matrix Form
|     -0.097    -0.163    -0.154 |
|     -0.163    -0.276    -0.266 |
|     -0.154    -0.266    -0.266 |

Last Modification date: 28 February 2018