对于我的一个班级,我应该编写一个程序来实现一个微分方程来找到一根两端都浸在冰浴中的 100 度棒的温度。给出了差分方程来解决它。我有一个数组,它在 x 方向上拆分为杆的段,在 y 方向上拆分为时间的迭代。r 值决定了冷却棒的冷却速度。可能值得注意的是,实际程序的实现方式并不那么重要,该程序旨在运行在计算机集群上,而分配的重点是向我们介绍在集群上发布作业。该程序仍然必须是正确的(显然),我什至无法达到这一点。
我的程序有问题,因为我的数组似乎开始在时间迭代中的(看似)随机点处获取垃圾数据。我有 Netbeans 并尝试过调试它,但上次我这样做时我无法查看数组变量的不同元素(使调试过程实际上毫无用处),现在我什至可以让它在我的断点处停止。一段时间以来,我一直在努力解决这个问题,并希望比我聪明得多的人能够简单地“看到”问题并帮助我。
谢谢。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
int segmentsl;
int segmentst;
float rvalue;
int i,j;
float k;
//Pull in initial data
printf("Enter as integers without spaces: Number of Segments - Length, Number of Segments - time ,value\n");
scanf("%d,%d,%f", &segmentsl,&segmentst,&rvalue);
float tempvstime[segmentsl][segmentst];
//at t0 -> temp = 100sin(pi*x)
for(i = 0; i < segmentsl; i++){
k = (float)(i/(segmentsl-1));
tempvstime[i][0] = 100*sin(M_PI*k);
printf("%f,",tempvstime[i][0]);
}
printf("\n\n\nEND OF INITIALIZATION \n\n\n");
for(j = 0; j < (segmentst-1); j++){
for(i = 0; i < segmentsl; i++){
if(i == 0 || i == (segmentsl - 1)){tempvstime[i][j] = 0;}
else{
tempvstime[i][j+1] = (rvalue*tempvstime[i-1][j]) + ((1-(2*rvalue))*tempvstime[i][j]) + (rvalue*tempvstime[i+1][j]);
}
}
}
for(j = 0; j < segmentst; j++){
for(i = 0; i < segmentsl; i++){
printf("%f,",tempvstime[i][j]);
if(i == segmentsl - 1){printf("\n");}
}
}
}
这是一些示例输出:
Enter as integers without spaces: Number of Segments - Length, Number of Segments - time ,value
6,10,0.5
0.000000,58.778526,95.105652,95.105652,58.778522,0.000000,
END OF INITIALIZATION
0.000000,58.778526,95.105652,95.105652,58.778522,0.000000,
0.000000,47.552826,76.942093,76.942085,47.552826,0.000000,
0.000000,38.471046,62.247456,62.247459,38.471043,0.000000,
0.000000,31.123728,50.359253,50.359249,31.123730,0.000000,
0.000000,25.179626,40.741489,40.741493,25.179625,0.000000,
0.000000,20.370745,32.960560,32.960556,-nan,0.000000,
0.000000,16.480280,26.665649,-nan,-nan,0.000000,
0.000000,13.332825,-nan,-nan,-nan,0.000000,
0.000000,-nan,-nan,-nan,-nan,0.000000,
0.000000,-nan,-nan,-nan,-nan,0.000000,