Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Numerical methods for solving partial differential equations typically require that the geometry be subdivided into a mesh of simple shapes such as rectangles. By doing

Numerical methods for solving partial differential equations typically require that the geometry be subdivided into a mesh of simple shapes such as rectangles. By doing so, a complex problem can be reduced to a collection of simple problems defined on each of the simple shapes. In formulating the solution for a particular simple shape, the algorithms typically need to identify the neighboring shapes. Consider the mesh comprising a grid of 24 rectangles shown below. The neighbors of rectangle 14 are rectangles 7,8,9,13,15,19,20, and 21. Likewise the neighbors of rectangle 6 are rectangles 5,11, and 12.
Your task is to write a MATLAB function that enumerates the neighbors of a rectangle in an mn mesh of rectangles. Assume that the rectangles are numbered starting at 1in the upper left corner and increasing from left to right and top to bottom as shown in the example.
Your function should be called getNeighbors. It should take exactly three input arguments in this order: m,n, and r. Here m is the number of rows in the mesh, n is the number of columns, and r is the rectangle whose neighbors must be identified. The function should return exactly one argument, an array containing the numbers of the neighbors in increasing order. Write your code so that the array is directly generated in the correct order; do not use the sort function. For example, getNeighbors (4,6,14) should return the array [7891315192021].
Your program should validate the input arguments and return and empty array (i.e.,[]) if any of the inputs are invalid. More specifically, your program must check that m and n are not less than 2,r is the number of a rectangle that exists in the mesh, and all input values are integers. For example, the following should return an empty array because there is no rectangle with number 100 for the given mesh size: getNeighbors (4,6,100).
Test your function using the testGetNeighbors test stub.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions