So we are suppose to use a 2D array 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 a (square) plate.
Im having trouble with updating the elements after initializing them and then updating until it becomes stable. My numbers are off and should look like this.
First of all, if you want your answers to look like that then use a 5*5 array and not 20*20.
Secondly, you are only iterating once. You need to iterate many times. During the development stage, do a fixed (fairly large) number of iterations. Then change your code to compute the temperature change over one iteration (added over all nodes, and ignoring sign). When this global change is sufficiently small then you can stop iterating.
NEIGHBORS as a constant is OK sort of but it should be an int, size_t, unsigned in or similar rather than a double.
Printing values in the array would be better handled with a function to avoid duplication.
Stability can possibly be determined automatically by selecting a cell (say at the center) and recording the change in value from one iteration to the next. When the temperature increment/change is less than a user selected limit value then the iterations stop.
So we arent suppose to use functions yet since we are just learning it so I dont neccesarily get them too much. The number i posted were an array of 5 x 5 but its just cause printing out a 20 x 20 is a lot more. So i Have to print it after the first iteration and then finally when it is stable.
So, if you aren't supposed to use functions then just write what you want in main and remove the function call and function.
You can have whatever size array you want and the best way to change it is by only changing the value in the line where you declare the value of SIZE_OF_ARRAY.
PS @OP You will still need to prepare the values in the array by hand, the way you have done so far, i.e. by typing in 5x5 values. 20x20 takes a while.
However you could automate it by writing extra lines to setup the boundary conditions and use random numbers to fill in the initial state values. Maybe for a later time and just stay with 5x5 for testing/development.
So, does that mean it's working the way you want? It might be a good idea to add a counter variable so you can show how many iterations were required to get to the stable state.
I've run your program on my machine and also on the cpp shell here with change = .1 and .00001 and the results are nothing like the answer you have posted.
Check, but looks OK unless you take all 8 neighbors:
@rantiv,
Perhaps if you wrote a complete sentence describing your current situation and whether you want any help would go a long way towards getting a more meaningful response to your last two words and question mark. :)
My numbers are off and should look like this.
0.0000, 100.0000, 100.0000, 100.0000, 0.0000
0.0000, 40.6250, 50.0000, 40.6250, 0.0000
0.0000, 25.0000, 31.2500, 25.0000, 0.0000
0.0000, 40.6250, 50.0000, 40.6250, 0.0000
0.0000, 100.0000, 100.0000, 100.0000, 0.0000
@Rantiv - are you absolutely sure about this?
Take the middle element (31.25). If I average its 4 neighbours (25, 25, 50, 50) I would get
37.5, not 31.25.
In fact, on symmetry grounds I would rather expect the middle value (AND ALL THE DIAGONAL ELEMENTS) to be 50.00. That is presuming you want BOTH top and bottom boundaries to be at temperature 100: check your (required) boundary conditions.