0

对于我的一个班级,我应该编写一个程序来实现一个微分方程来找到一根两端都浸在冰浴中的 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,
4

2 回答 2

4

除了整数除法

k = (float)(i/(segmentsl-1));

您设置了错误的端点:

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]); 
    }
  }
}

如果i是 0 或最后一个索引,则设置tempvstime[i][j],对于另一个i,设置tempvstime[i][j+1]

tempvstime[i][j+1]还应该为端点设置。否则,在下一次迭代中,您使用未初始化的(垃圾)值,调用未定义的行为。

通过这种更改和设置时的浮点除法k,我得到了合理的值:

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,20.370747,0.000000,
0.000000,16.480280,26.665649,26.665653,16.480278,0.000000,
0.000000,13.332825,21.572968,21.572964,13.332827,0.000000,
0.000000,10.786484,17.452894,17.452896,10.786482,0.000000,
0.000000,8.726447,14.119690,14.119688,8.726448,0.000000,
于 2013-06-15T22:28:06.150 回答
0

我注意到这一行:

k = (float)(i/(segmentsl-1));

正在执行整数除法。因为i<segmentsl它会i/segmentsl是 0。这可能不是你想要的。

在你的程序中,你也可以使用tempvstime[i][j+1]and tempvstime[i][j],而两者都应该是j+1

下面是您的程序的修改版本,它可以正确运行并且更改了变量名称并且数学中的括号更少。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void){
    int width, maxt;
    float rvalue;
    int i,t;
    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", &width,&maxt,&rvalue);

    float tempvstime[width][maxt];

    fprintf(stderr,"Initializing with %d segments...",width);
    //at t0 -> temp = 100sin(pi*x)
    for(i = 0; i < width; ++i)
      tempvstime[i][0] = 100*sin((M_PI*i)/(width-1));
    fprintf(stderr,"done.\n");

    for(t = 0; t <(maxt-1); ++t){
      for(i = 0; i < width; ++i){
        if( i == 0 || i == (width-1) ){
          tempvstime[i][t+1] = 0;
        } else {
          tempvstime[i][t+1] = rvalue*tempvstime[i-1][t] + (1-2*rvalue)*tempvstime[i][t] + rvalue*tempvstime[i+1][t];
        }
      }
    }

    for(t = 0; t < maxt; t++){
      for(i = 0; i < width; i++)
        printf("%f,",tempvstime[i][t]);
      printf("\n");
    }

}
于 2013-06-15T22:24:15.327 回答