11-15-2010, 06:09 PM
|
Registered Hack
|
|
Join Date: Mar 2008
Posts: 4,642
|
|
Anyone wanna tear apart my novice C program?
I am using Bloodshed. I think I am close, but the matrix is all zeros and I need to format the printing output. I would just like to start getting values before I clean it up.
This is the assignment:
Quote:
Blocks of ice are placed around the edge of a square sheet of metal. After some time, the entire sheet is 0° C. A hot iron rod, whose temperature is fixed at 100°, is brought into contact with the center of the sheet. Eventually temperatures stabilize and the sheet has a temperature gradient going from 0° all along the edges to 100° at the center.
We can calculate, approximately, the temperature gradient in the metal sheet using the following well known technique:
- Divide the sheet into a matrix of small squares.
- For each square in the matrix, approximate the temperature of the square by averaging the temperatures of the four nearest neighbors.
- Repeat the previous step until temperatures stabilize.
Although the edges and center do participate in the approximation, they remain fixed at 0° and 100°, respectively. In other words, you must never change the temperatures of the center and edge cells. Use an 11x11 double precision matrix for the process, calculate the temperatures of all the internal squares (except for the center), Repeat the process until the maximum change (new temp. - old temp.) for each/every of the 80 interior cells, is less than 0.001 (the fabs function works well here). Then print out the temperatures, as shown below. Done correctly, your printout will be exactly like the following one.
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 2.04 4.08 6.02 7.57 8.22 7.57 6.02 4.08 2.04 0.00
0.00 4.08 8.25 12.42 16.03 17.75 16.03 12.42 8.25 4.08 0.00
0.00 6.02 12.42 19.40 26.38 30.72 26.38 19.40 12.42 6.02 0.00
0.00 7.57 16.03 26.38 39.37 52.37 39.37 26.38 16.03 7.57 0.00
0.00 8.22 17.75 30.72 52.37 100.00 52.37 30.72 17.75 8.22 0.00
0.00 7.57 16.03 26.38 39.37 52.37 39.37 26.38 16.03 7.57 0.00
0.00 6.02 12.42 19.40 26.38 30.72 26.38 19.40 12.43 6.02 0.00
0.00 4.08 8.25 12.42 16.03 17.75 16.03 12.43 8.25 4.08 0.00
0.00 2.04 4.08 6.02 7.57 8.22 7.57 6.02 4.08 2.04 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
In your output indicate the number of iterations it takes to converge.
|
#include
#include
#define S 11
int main(int argc, char *argv[])
{
double M[S][S], denom, delta, delta1, top, bottom, left, right;
int i, j, k, l, row, col;
col=1;
row=1;
top=bottom=left=right=0;
do
{
for (i=0; i<9; i++)
{
M[5][5]=100.00000;
M[i][0]=M[i][10]=M[0][col]=M[10][col]=0;
top=M[i][col];
denom=top+bottom+left+right;
M[row][col]=denom/4;
M[4][5]=delta;
printf("\n%Lf", M[row][col]);
for (j=i+2; j<11; )
{
M[5][5]=100.00000;
M[i][0]=M[i][10]=M[0][col]=M[10][col]=0;
bottom=M[j][col];
M[row][col]=denom/4;
M[4][5]=delta1;
printf("\n%Lf", M[row][col]);
for (k=i; k<9; )
{
M[5][5]=100.00000;
M[i][0]=M[i][10]=M[0][i]=M[10][i]=0;
left=M[row][k];
M[row][col]=denom/4;
M[4][5]=delta1;
printf("\n%Lf", M[row][col]);
for (l=i+2; l<11; )
{
M[5][5]=100.00000;
M[i][0]=M[i][10]=M[0][col]=M[10][col]=0;
right=M[row][l];
M[row][col]=denom/4;
M[4][5]=delta1;
printf("\n%Lf", M[row][col]);
}
}
}
}
row+=1;
col+=1;
}
while (M[4][5]<.01 && delta1-delta>0.001 || M[9][9]==0);
printf("%Lf\n", M[1][5]);
printf("\n%Lf", denom);
system("PAUSE");
return 0;
}
|