NumPy: Understanding Meshed Grids

<p>When I&rsquo;m teaching my courses about scientific Python, students often have a hard time understanding the concept of meshed grids in NumPy and what&rsquo;s the difference between the various options. As this appears to be a quite common problem and as the concept of meshed grids is vital for many scientific applications, I&rsquo;m writing this post to settle these problems once and for all. ;-)</p> <p><img alt="Mathcube" src="https://miro.medium.com/v2/resize:fill:18:18/1*PMw---FG7kXxOoEkfPl-Zw.jpeg" style="height:20px; width:20px" /></p> <p><a href="https://medium.com/@mathcube7?source=post_page-----d0058230f247--------------------------------" rel="noopener follow" target="_top">Mathcube</a></p> <h2>Daily Dose of Scientific Python</h2> <p><a href="https://medium.com/@mathcube7/list/daily-dose-of-scientific-python-bc0c16acad46?source=post_page-----d0058230f247--------------------------------" rel="noopener follow" target="_top">View list</a></p> <p>102 stories</p> <p><img alt="" src="https://miro.medium.com/v2/resize:fill:175:175/0*gk6DNo1LvKaHUVKI" style="height:194px; width:194px" /></p> <p><img alt="" src="https://miro.medium.com/v2/resize:fill:175:175/0*ftVVxPZ1LY4wtMBf" style="height:194px; width:194px" /></p> <p><img alt="" src="https://miro.medium.com/v2/resize:fill:175:175/0*TaD_gJEI0bJRxCk8" style="height:194px; width:194px" /></p> <h1>The Problem</h1> <p>Consider the following code, where we define a one-dimensional equidistant grid&nbsp;<code>x</code>:</p> <p><img alt="" src="https://miro.medium.com/v2/resize:fit:630/1*CHV9bFedH9Z0033IHasDXw.png" style="height:118px; width:700px" /></p> <p>One of the beauties of NumPy is that you can easily apply functions to arrays. For example, we could calculate the sine function on that very grid by</p> <p>Both&nbsp;<code>x</code>&nbsp;and&nbsp;<code>f</code>&nbsp;are one-dimensional arrays of length 100, as we would guess:</p> <p><img alt="" src="https://miro.medium.com/v2/resize:fit:630/1*MCyyH8yjZ-hn8pw-s3HijQ.png" style="height:316px; width:700px" /></p> <p>This is all intuitive. But what if we are working in multiple dimensions? Suppose we want to use a two-dimensional grid along the x and yaxes. How do we do that? Specifically, how can we calculate the function</p> <p>on a two-dimensional grid?</p> <h1>The Solution</h1> <p>First, we define our one-dimensional coordinate values like in the one-dimensional case.</p> <p><a href="https://medium.com/@mathcube7/numpy-understanding-meshed-grids-d0058230f247">Website</a></p>
Tags: NumPy Meshed