计算机爆炸代码,C数值积分代码在一台计算机上工作,但在另一台计算机上爆炸...

我已经使用rk4方法编写了一个简单摆锤的数值积分代码。 Here's an image of expected result. 它运行在我的笔记本电脑上,运行Ubuntu 14.04,64位(它给出了一个正弦波),但不能在运行Debian 8的我的PC上运行,也是64位。 Here's an image of the wrong plot. 为什么会发生这种情况的任何原因?C数值积分代码在一台计算机上工作,但在另一台计算机上爆炸

下面的代码:

#include

#include

#include

#include

int N = 2;

float h = 0.001;

struct t_y_couple {

float t;

float *y;

};

struct t_y_couple integrator_rk4(float dt, float t, float *p1);

void oscnetwork_opt(float t, float *y, float *dydt);

int main(void) {

/* initializations*/

struct t_y_couple t_y;

int i, iter, j;

// time span for which to run simulation

int tspan = 20;

// total number of time iterations = tspan*step_size

int tot_time = (int)ceil(tspan/h);

// Time array

float T[tot_time];

// pointer definitions

float *p, *q;

// vector to hold values for each differential variable for all time

// iterations

float Y[tot_time][2];

// N = total number of coupled differential equations to solve

// initial conditions vector for time = 0

Y[0][0] = 0;

Y[0][1] = 3.14;

// set the time array

T[0] = 0;

// This loop calls the RK4 code

for (i = 0; i < tot_time - 1; i++) {

p = &Y[i][0]; // current time

q = &Y[i + 1][0]; // next time step

// printf("\n\n");

// for (j=0;j

// call the RK4 integrator with current time value, and current

// values of voltage

t_y = integrator_rk4(h, T[i], p);

// Return the time output of integrator into the next iteration of time

T[i + 1] = t_y.t;

// copy the output of the integrator into the next iteration of voltage

q = memcpy(q, t_y.y, (2) * sizeof(float));

printf("%f ", T[i + 1]);

for (iter = 0; iter < N; iter++)

printf("%f ", *(p + iter));

printf("\n");

}

return 0;

}

struct t_y_couple integrator_rk4(float dt, float t, float y[2]) {

// initialize all the pointers

float y1[2], y2[2], y3[2], yout[2];

float tout, dt_half;

float k1[2], k2[2], k3[2], k4[2];

// initialize iterator

int i;

struct t_y_couple ty1;

tout = t + dt;

dt_half = 0.5 * dt;

float addition[2];

// return the differential array into k1

oscnetwork_opt(t, y, k1);

// multiply the array k1 by dt_half

for (i = 0; i < 2; i++)

y1[i] = y[i] + (k1[i]) * dt_half;

// add k1 to each element of the array y

// do the same thing 3 times

oscnetwork_opt(t + dt_half, y1, k2);

for (i = 0; i < 2; i++)

y2[i] = y[i] + (k2[i]) * dt_half;

oscnetwork_opt(t + dt_half, y2, k3);

for (i = 0; i < 2; i++)

y3[i] = y[i] + (k3[i]) * dt_half;

oscnetwork_opt(tout, y3, k4);

// Make the final additions with k1,k2,k3 and k4 according to the RK4 code

for (i = 0; i < 2; i++) {

addition[i] = ((k1[i]) + (k2[i]) * 2 + (k3[i]) * 2 + (k4[i])) * dt/6;

}

// add this to the original array

for (i = 0; i < 2; i++)

yout[i] = y[i] + addition[i];

// return a struct with the current time and the updated voltage array

ty1.t = tout;

ty1.y = yout;

return ty1;

}

// function to return the vector with coupled differential variables for each

// time iteration

void oscnetwork_opt(float t, float y[2], float *dydt) {

int i;

dydt[0] = y[1];

dydt[1] = -(1) * sin(y[0]);

}

+2

我们需要查看包含在int main()中的*最小*位代码,以显示此问题以帮助您。 –

+2

这看起来像未定义的行为。也许有些变量没有初始化,你可以在一台计算机上而不是另一台上运行。如果没有看到你的实际节目,我们不能再多说了。 –

+1

你应该发布某种代码片段,因为你的问题太模糊。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值