Computer Science Coursework
Routine Name: solveNinePointStencil
Author: Philip Nelson
Language: C++
solveNinePointStencil
solves Poisson’s Equation for an arbitrary mesh size and forcing function
solveNinePointStencil(T a, T b, F f)
requires:
T a
- the start boundaryT b
- the end boundaryF f
- the forcing functionT
- template param for the typeN
- template param for the dimension of the meshAn array with the approximated solution
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|
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;
}
| 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