Hot Plate

I honestly don't know what to do to make this output the information I want it to

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;

const int userSize = 10; // 
const double userChange = 0.1;
const double userNeighbors = 4.0;
const int userLeft = 0;
const int userRight = 9;
const int userWidth = 10;
const int decimalPrecision = 4;
string fileName = "Inputplate.txt";

void Initial(double hotPlate[][userSize])
{
	for (int userRow = 0; userRow < userSize; userRow++)
	{
		for (int userCol = 0; userCol < userSize; userCol++)
		{
			if (userRow == userLeft || userRow == userRight)
			{
				if (userCol == userLeft || userCol == userRight)
				{
					hotPlate[userRow][userCol] = 0;
				}
				else
				{
					hotPlate[userRow][userCol] = 100;
				}
			}
			else
			{
				hotPlate[userRow][userCol] = 0;
			}
		}
	}
}

void Print(double hotPlate[][userSize])
{
	for (int userRow = 0; userRow < userSize; userRow++)
	{
		for (int userCol = 0; userCol < userSize; userCol++)
		{
			cout << fixed << setprecision(decimalPrecision) << setw(userWidth) << hotPlate[userRow][userCol];
			if (userCol < userSize - 1) {
				cout << ",";
			}
		}
		cout << endl;
	}
}

void FirstAverage(double hotPlate[][userSize], double copyHotplate[][userSize])
{

	double averageTemp = 0.0;
	int userRow = 0;
	int userCol = 0;

	for (int userRow = 1; userRow < userSize - 1; userRow++)
	{
		for (int userCol = 1; userCol < userSize - 1; userCol++)
		{
			averageTemp = (hotPlate[userRow - 1][userCol] + hotPlate[userRow + 1][userCol] + hotPlate[userRow][userCol - 1] + hotPlate[userRow][userCol + 1]) / userNeighbors;
			copyHotplate[userRow][userCol] = averageTemp;

		}

	}




}


void MyAverage(double hotPlate[][userSize], double copyHotplate[][userSize]) {

	bool keepGoing = false;
	const double userChange = 0.1;
	const double userNeighbors = 4.0;

	do {
		keepGoing = false;
		for (int userRow = 1; userRow < userSize - 1; userRow++) {
			for (int userColumn = 1; userColumn < userSize - 1; userColumn++) {

				copyHotplate[userRow][userColumn] = (hotPlate[userRow + 1][userColumn] + hotPlate[userRow - 1][userColumn] + hotPlate[userRow][userColumn + 1] + hotPlate[userRow][userColumn - 1]) / userNeighbors;
				if (abs(hotPlate[userRow][userColumn] - copyHotplate[userRow][userColumn]) > userChange) {
					keepGoing = true;
				}
			}
		}
		for (int userRow = 1; userRow < userSize - 1; userRow++) {
			for (int column = 1; column < userSize - 1; column++) {
				hotPlate[userRow][column] = copyHotplate[userRow][column];
			}
		}
	} while (keepGoing);

}

void SaveFile(double hot_plate[][userSize])
{
	ofstream out;
	out.open("Hotplate.csv");

	for (int userRow = 0; userRow < userSize; userRow++)
	{
		for (int userCol = 0; userCol < userSize; userCol++)
		{
			out << fixed << fixed << setprecision(decimalPrecision) << setw(userWidth) << hot_plate[userRow][userCol];
			if (userCol < userSize - 1) {
				out << ",";
			}


		}
		out << endl;

	}

	out.close();
}

int main()
{
	double hotPlate[userSize][userSize];
	double copyHotplate[userSize][userSize];
	bool keepGoing = false;
	const double userChange = 0.1;
	const double userNeighbors = 4.0;

	cout << "Hotplate simulator" << endl << endl;
	cout << "Printing initial plate..." << endl;
	Initial(hotPlate);
	Initial(copyHotplate);
	Print(hotPlate);

	cout << endl << endl;

	cout << "Printing plate after one iteration..." << endl;
	FirstAverage(hotPlate, copyHotplate);
	Print(copyHotplate);

	cout << endl << endl;

	cout << "Printing final plate..." << endl;
	MyAverage(hotPlate, copyHotplate);
	Print(hotPlate);

	cout << endl << "Outputting final plate to file \"Hotplate.csv\"" << endl;
	SaveFile(hotPlate);

	ifstream in;
	in.open(fileName);

	cout << endl << "Printing input plate after 3 updates..." << endl;
	for (int k = 1; k < 3; k++) {
		keepGoing = false;
		for (int row = 1; row < userSize - 1; row++) {
			for (int column = 1; column < userSize - 1; column++) {

				copyHotplate[row][column] = (hotPlate[row + 1][column] + hotPlate[row - 1][column] + hotPlate[row][column + 1] + hotPlate[row][column - 1]) / userNeighbors;
				if (abs(hotPlate[row][column] - copyHotplate[row][column]) > userChange) {
					keepGoing = true;
				}
			}
			for (int row = 1; row < userSize - 1; row++) {
				for (int column = 1; column < userSize - 1; column++) {
					hotPlate[row][column] = copyHotplate[row][column];
				}
			}
			Print(copyHotplate);
		}
	}

	return 0;
}


This is how the output should look...

Hotplate simulator

Printing initial plate...
    0.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,  100.000,  100.000,  100.000,    0.000

Printing plate after one iteration...
    0.000,  100.000,  100.000,  100.000,    0.000
    0.000,   25.000,   25.000,   25.000,    0.000
    0.000,    0.000,    0.000,    0.000,    0.000
    0.000,   25.000,   25.000,   25.000,    0.000
    0.000,  100.000,  100.000,  100.000,    0.000

Printing final plate...
    0.000,  100.000,  100.000,  100.000,    0.000
    0.000,   49.854,   62.305,   49.854,    0.000
    0.000,   37.305,   49.707,   37.305,    0.000
    0.000,   49.854,   62.305,   49.854,    0.000
    0.000,  100.000,  100.000,  100.000,    0.000

Outputting final plate to file "Hotplate.csv"...

Printing input plate after 3 updates...
    0.000,  100.000,  100.000,  100.000,    0.000
    0.000,   -2.853,   62.304,  -49.853,    0.000
    0.000,   37.304,   49.707,   37.304,    0.000
    0.000,   49.853,   -2.304,    1.853,    0.000
    0.000,  100.000,  100.000,  100.000,    0.000


And this is how it shows up instead...

Hotplate simulator

Printing initial plate...
    0.0000,  100.0000,  100.0000,  100.0000,  100.0000,  100.0000,  100.0000,  100.0000,  100.0000,    0.0000
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000,    0.0000
    0.0000,  100.0000,  100.0000,  100.0000,  100.0000,  100.0000,  100.0000,  100.0000,  100.0000,    0.0000
Duplicate of:

http://www.cplusplus.com/forum/general/239965/

Please DON'T post multiple threads for the same question.
@Matriz, do you want to maybe explain what your structures are, your iteration method and reasons for the "keepGoing" ? What's in your "inputplate" file ?

As it stands, your question is basically, "Here, look. Fix?"

Edit: also, having identical variable names both in global and local scope isn't helping anyone.
Last edited on
Google "steady-state temperature distribution rectangular plate" or, possibly, "Laplace equation".

For this particular problem, every node should end up as the average of the 4 nodes surrounding it,
Ti,j=(Ti-1,j+Ti+1,j+Ti,j-1+Ti,j+1)/4
apart from the boundary conditions which are here set as 0 and 100.

It looks like the iteration method is either Jacobi method (RHS uses values from the end of the last complete iteration; requires a copy first):
http://mathworld.wolfram.com/JacobiMethod.html
or Gauss-Seidel (RHS uses the latest updated value)
http://mathworld.wolfram.com/Gauss-SeidelMethod.html
(This code appears to be an unholy mix of the two.)

In the model answer:
Printing input plate after 3 updates...
    0.000,  100.000,  100.000,  100.000,    0.000
    0.000,   -2.853,   62.304,  -49.853,    0.000

there is absolutely no way of getting those negatives, even for early iterations, so some of your specification is wrong.


"keepGoing" if change at any node on this iteration is greater than some tolerance, userChange=0.1 (This is a tad large).
Last edited on
Topic archived. No new replies allowed.