class-projects

Computer Science Coursework

View the Project on GitHub

Table of Contents

Generate Mesh

Routine Name: generateMesh

Author: Philip Nelson

Language: C++

Description

generateMesh(T a, T b) generates a 2D mesh from a to b with N steps

Input

generateMesh(T a, T b) requires:

Output

The mesh of \(i\cdot j\)

Code

template <typename T, std::size_t N>
Matrix<T, N - 2, N - 2> generateMesh(T a, T b)
{
  auto h = (b - a) / (N - 1);

  return Matrix<T, N - 2, N - 2>(
    [&](int i, int j) { return (a + (i + 1) * h) * (a + (j + 1) * h); });
}

Example

int main()
{
  std::cout << "mesh\n" << generateMesh<double, 7>(0, 1) << std::endl;
}

Result

mesh
|     0.0278    0.0556    0.0833     0.111     0.139 |
|     0.0556     0.111     0.167     0.222     0.278 |
|     0.0833     0.167      0.25     0.333     0.417 |
|      0.111     0.222     0.333     0.444     0.556 |
|      0.139     0.278     0.417     0.556     0.694 |

Last Modification date: 27 February 2018