I have trouble writing a program that must calculate the temperature of a central point of a rectangular region after certain time steps.
For which you create a matrix of (N + 1) x (M + 1) where N = 2M and the center point of the region is (N / 2) +1 and (M / 2) +1. The upper cells have an initial temperature of 20 ºC, the rest of the cells have an initial temperature of 0 ºC, and in the cells of the contour the temperature is invariant.
The temperature in each cell and time step (Tk) is calculated with the temperature in the previous time step (Tk-1) of the adjacent cells, following the following formula:
(Where (i, j) is the value of a cell)
T = (4 * [(i + 1, j) + (i-1, j) + (i, j + 1) + (i, j-1)] + [(i-1, j- 1) + (i-1, j + 1) + (i + 1, j + 1) + (i + 1, j-1)]) / 10
So far I have everything clear and I have no major problem. The problem begins when to calculate the temperature after each step of time adds the temperature already calculated in the time jump itself instead of using the temperature of the previous time jump, ie:
To calculate the temperature of the cell (1,6), use the adjacent cells, but previously you calculated the cell (1,5), and instead of using the temperature of (1,5) before the calculation, the sum after said calculation giving the wrong result.
So far I have written the following:
int main( ) {
int N = 20;
int M = 10;
int A = 2 * N + 1;
int B = 2 * M + 1;
int matriz[A][B];
for( int i = 1; i < A; i++ ) {
for( int j = 0; j < B; j++ ) {
matriz[0][j] = 20;
matriz[i][j] = 0;
}
}
for( int i = 1; i < ( A - 1 ); i++ ) {
for( int j = 1; j < ( B - 1 ); j++ ) {
int n = i - 1;
int e = j + 1;
int s = i + 1;
int o = j - 1;
int T = ( 4 * ( matriz[n][j] + matriz[s][j] + matriz[i][o] + matriz[i][e] ) + ( matriz[n][e] + matriz[n][o] + matriz[s][e] + matriz[s][o] ) ) / 10;
matriz[i][j] += matriz[i][j] + T;
}
}
std::cout << matriz[N + 1][M + 1];
return 0;
}