MATH5620

Algorithms and Subroutines for Solving Differential Equations

Table of Contents

Nine Point Stencil

Routine Name: solveNinePointStencil

Author: Philip Nelson

Language: C++

Description

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

Input

solveNinePointStencil(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 = ninePointStencil<double, N-2>();
  auto res = stencil.solveLinearSystemLU(bv);
  return res;
}

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

Example

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

Result

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


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


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


result
[    -0.0169   -0.0284   -0.0267   -0.0284   -0.0483   -0.0466   -0.0267   -0.0466   -0.0477 ]

Answer in Matrix Form
|    -0.0169   -0.0284   -0.0267 |
|    -0.0284   -0.0483   -0.0466 |
|    -0.0267   -0.0466   -0.0477 |

Last Modification date: 28 February 2018