Hi!
I'm working with 2-D arrays and my output for the final plate is off. Ex: 49.874 is supposed to be 49.815
Part 1 - Initialize and Print 2D Array (20 points)
In this lab you will write a program to determine the temperature distribution for a two-dimensional plate with constant boundary conditions. Use a 10 X 10 two-dimensional array of numbers to represent the temperature at different places on a (square) 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 0 degrees. (The corner elements have a temperature of 0 degrees.) Also assume that all interior elements start at 0 degrees. This obviously does not represent a typical hot plate, but this is a good exercise to simulate how temperature would dynamically distribute across a surface.
For part one you must initialize the 2D array and print it to the screen. Print the numbers in the array with a fixed precision of 3 and width 9.
Part 2 - Update Elements Once (20 points)
The task of the algorithm is to find the steady-state temperature distribution of the interior elements which are constantly changing to become the 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 (do not worry about the diagonal neighbors). 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 temperatures of the four neighbors of that cell from the previous iteration. This value will be placed in the output array.
For Part 2, just print the first iteration.
Part 3 - Repeat Update until Stable (25 points)
After printing the results from the first iteration, continue to iterate (calculating the temperature for all interior cells) until no cell in the array changes by more than 0.1 degrees. Your program should monitor the largest change for any cell in the array in order to determine when to stop iterating. At the end of each iteration you will copy the values from the "new values" array into the "old values" array, to be ready for the next iteration.
Once all of the values in the plate have ceased to change by more than 0.1 degrees, print the final stable plate, in the same comma-separated form used above. Do not print all the intermediate arrays, just the final one after it becomes stable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
const int SIZE_OF_ARRAY = 10;
const double MAX_TEMPERATURE = 100.0;
const int MIN_TEMPERATURE = 0;
const int NEIGHBORS = 4;
const double CHANGE = 0.1;
int main() {
double avg_temp;
double hotPlate[SIZE_OF_ARRAY][SIZE_OF_ARRAY];
double largest_change = 0;
cout << "Hotplate simulator" << endl << endl;
cout << "Printing initial plate..." << endl;
for (int row = 0; row < SIZE_OF_ARRAY; row++){
for (int column = 0; column < SIZE_OF_ARRAY; column++){
if (row == 0 || row == 9){
if (column == 0 || column == 9){
hotPlate[row][column] = 0;
}
else{
hotPlate[row][column] = 100;
}
}
else{
hotPlate[row][column] = 0;
}
}
}
for (int row = 0; row < SIZE_OF_ARRAY; row++){
for (int column = 0; column < SIZE_OF_ARRAY; column++){
if (column == 9){
cout << fixed << setprecision(3) << setw(9) << hotPlate[row][column];
}
else{
cout << fixed << setprecision(3) << setw(9) << hotPlate[row][column] << ",";
}
}
cout << endl;
}
//PART TWO 2
cout << endl;
cout << "Printing plate after one iteration..." << endl;
for (int row = 0; row < SIZE_OF_ARRAY; row++) {
for (int column = 0; column < SIZE_OF_ARRAY; column++) {
if (row == 0 || row == SIZE_OF_ARRAY - 1) {
if (column == 0 || column == SIZE_OF_ARRAY - 1) {
hotPlate[row][column] = 0.0;
}
else {
hotPlate[row][column] = 100.0;
}
}
else if (row == 1 || row == SIZE_OF_ARRAY - 2) {
if (column == 0 || column == SIZE_OF_ARRAY - 1) {
hotPlate[row][column] = 0.0;
}
else {
hotPlate [row][column] = 100 / 4;
}
}
else {
hotPlate[row][column] = 0;
}
}
}
for (int row = 0; row < SIZE_OF_ARRAY; row++) {
for (int column = 0; column < SIZE_OF_ARRAY; column++) {
cout << setprecision(3) << fixed << hotPlate[row][column];
if (column < SIZE_OF_ARRAY - 1) {
cout << ", ";
}
}
cout << endl;
}
cout << endl;
//PART THREE 3
cout << "Printing final plate..." << endl;
do{
largest_change = 0;
for (int row = 1; row < SIZE_OF_ARRAY - 1; row++){
for (int col = 1; col < SIZE_OF_ARRAY - 1; col++){
double old_value = hotPlate[row][col];
avg_temp = (hotPlate[row - 1][col] + hotPlate[row][col + 1] + hotPlate[row + 1][col] + hotPlate[row][col - 1]) / NEIGHBORS;
hotPlate[row][col] = avg_temp;
double change = abs(old_value - hotPlate[row][col]);
if (change > largest_change){
largest_change = change;
}
}
}
}
while (largest_change > CHANGE);
for (int row = 0; row < SIZE_OF_ARRAY; row++) {
for (int column = 0; column < SIZE_OF_ARRAY; column++) {
cout << setprecision(3) << fixed << hotPlate[row][column];
if (column < SIZE_OF_ARRAY -1) {
cout << ", ";
}
}
cout << endl;
}
cout << endl;
return 0;
}
| |
I should be getting...
Hotplate simulator
Printing initial plate...
0.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 0.000
Printing plate after one iteration...
0.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 0.000
0.000, 25.000, 25.000, 25.000, 25.000, 25.000, 25.000, 25.000, 25.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000
0.000, 25.000, 25.000, 25.000, 25.000, 25.000, 25.000, 25.000, 25.000, 0.000
0.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 0.000
Printing final plate...
0.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 100.000, 0.000
0.000, 49.815, 69.132, 77.446, 80.712, 80.712, 77.446, 69.132, 49.815, 0.000
0.000, 30.175, 49.348, 60.054, 64.817, 64.817, 60.054, 49.348, 30.175, 0.000
0.000, 21.619, 38.189, 48.817, 53.926, 53.926, 48.817, 38.189, 21.619, 0.000