# Purpose

Gain experience with 2-dimensional arrays and loops while solving a real-world problem (“steady state analysis”).

# Background

In this assignment you will compute the steady state temperature distribution over a piece of metal. This steady state analysis solves a system of partial differential equations numerically and is used in solving many physics problems. Similar matrix algorithms are also used in analyzing airplane wing and automobile pressure distributions through fluid flow dynamics simulations.

# Requirements

## What

• Use a 2D array and calculate the steady-state temperature of each element in it
• Write the array to a CSV (“comma separated values”) file
• Plot the results of the CSV using excel

## How

The problem is to determine the temperature distribution for a two-dimensional plate with constant boundary conditions. Use a 20 X 20 two dimensional array of numbers to represent the temperature at different places on the plate. The elements on the boundaries have fixed temperatures. The elements on the top and bottom row have a fixed temperature of 100 degrees. The elements on the leftmost and rightmost columns have a temperature of zero degrees. (The corner elements have a temperature of zero degrees.)

The job of the algorithm is to find the steady-state temperature distribution of the interior elements which are constantly changing to be an average of the temperature of their neighbors. Steady state means that the temperature of any cell is virtually unchanging and approximately equal to the average of the temperatures of its four neighbors on the north, south, east, and west directions. Note that the steady state temperatures will be the same regardless of what the beginning temperatures are for the interior elements.

To calculate the new temperature value of an array element, take the average of the values in the 4 direct neighbors of that cell.

For example, the cell at position array[1][2] currently has a value of 60.6 degrees. At the next iteration, it will be set to the average of its 4 neighbors (100.0 + 66.3 + 34.0 + 45.2)/4.

You should continue to iterate until no cell in the array changes more than 0.1 degree, calculating the temperature for all interior cells on each iteration. Your program should monitor the largest change for any cell in the array in order to determine when to stop iterating (See Common Loop Algorithms in section 4.7 of the book for ideas).

When you are finished, you should write your two dimensional array out to a comma separated values file like this:

0.0,100.0,100.0,100.0,
0.0,45.1,60.6,66.2,0,
0.0,45.1,60.6,66.2,0,
0.0,100.0,100.0,100.0,0,

You can import this csv file into a spreadsheet like excel and format it as a 3d graph in order to check your work.

# Think before programming

You should think about how you could break the program up into methods to make each task smaller and easier to accomplish.

Work through some small examples by hand and think about the array index you will have to use to get to adjacent array elements.

Think about how you will check to see if your code is performing correctly. Decide on what the overall answer should be and then write tests that will make sure that your methods are performing appropriately.

# Tips

You can find out more about how to write to a file in example FileIO

Start by writing a method to print out your array, then write the code to initialize the boundaries, then write the iteration code and progressively solve small pieces of your problem.

To graph your data, import the csv file into excel as a csv file, then select the data and create a 3d chart. Follow this tutorial if you're unfamiliar with Excel: Export To Excel

Some students find that xcode doesn't create a file when they use ofstream. To fix this, do the following

• First, in the menu along the top of the screen, go to “Project»Edit Scheme” and change the build type from debug to release.
• Second, hard-code the path, don't just use a filename. For example, write “/Users/[user name]/Desktop/hotplate.csv” instead of “hotplate.csv”.
• If you're still having problems, make sure the stream is closed when you're done with it (e.g. myfile.close());

# Pass-off procedure

When you have your program working, you will need to show it to a TA. The TA will evaluate your code based on the following criteria:

1. Initialize your two dimensional array correctly and print it out. (5 points)
2. Update elements in the array correctly for at least one iteration. (5 points)
3. Stop when there is less than a 0.1 degree change for all elements. (10 points)
4. Save out your array to a CSV file (5 points)
5. Plot your final array using excel (5 points)
6. Is your code properly formatted (indenting blocks, comments, spacing, reasonable names of variables)? (5 points)
LDAP: couldn't connect to LDAP server