gpt4 book ai didi

c - 差分方程爆炸 - C

转载 作者:太空宇宙 更新时间:2023-11-04 06:34:00 25 4
gpt4 key购买 nike

在我的类(class)中,我应该编写一个程序来实现微分方程来计算两端浸入冰浴中的 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,

最佳答案

除中的整数除法

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,

关于c - 差分方程爆炸 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17128378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com