Computer Science Coursework
Routine Name: solveFivePointStencil
Author: Philip Nelson
Language: C++
fivePointStencil
solves Poisson’s Equation for an arbitrary mesh size and forcing function
solveFivePointStencil(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 = fivePointStencil<double, N-2>();
auto res = stencil.solveLinearSystemLU(bv);
return res;
}
solveFivePointStencil relies on: fivePointStencil| generateMesh| initMeshB| solveLinearSystemLU|
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;
}
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