RK4摆的C代码

时间:2014-05-16 01:24:52

标签: c

我试图通过屠夫tablaeu为摆锤应用Rk4,起初我得到了逻辑结果,但现在我在错误地关闭编辑器后继续在编译器中得不到任何结果。  我尝试了#define函数,但仍然没有,不知道发生了什么 - 要么我的代码写得不好,要么有一些我无法看到的愚蠢错误,任何帮助都会受到赞赏。

#include <stdio.h>
#include <math.h>
#define N 300.0
#define L 9.81
#define g 9.81
#define pi 3.141592

#define a21 0.5
#define a31 0
#define a32 0.5
#define a41 0
#define a42 0
#define a43 1.0
#define c2 0.5
#define c3 0.5
#define c4 1.0
#define w1 1/6
#define w2 1/3
#define w3 1/3
#define w4 1/6



double g1(double y1,double y2,double t0);
double g2(double y1,double y2,double t0);

double g1(double y1,double y2,double t0)
{
return (y2);
}


double g2(double y1,double y2,double t0)
{
return (-(g/L)*(y1));
}


int main()
{


double h,t0=0,k11,k21,k12,k22,k13,k23,k14,k24;
double y1,y2;
double y[2][N];


h  = 0.1;

y[0][0] = 0;
y[1][0] = 0.1745;


for (int i = 1;  i <= N; i++)
 {
 t0 = h*(i);
 y1 = y[0][i-1];
 y2 = y[1][i-1];
 k11 = h*g1(y1,y2,t0);
 k21 = h*g2(y1,y2,t0);
 k12 = h*g1((y1+k11*a21),(y2+k21*a21),(t0+h*c2));
 k22 = h*g2((y1+k11*a21),(y2+k21*a21),(t0+h*c2));
 k13 = h*g1((y1+k12*a32+k11*a31),(y2+k22*a32+k21*a31),(t0+h*c3));
 k23 = h*g2((y1+k12*a32+k11*a31),(y2+k22*a32+k21*a31),(t0+h*c3));
 k14 = h*g1((y1+k13*a43+k12*a42+k11*a41),(y2+k23*a43+k22*a42+k21*a41),(t0+h*c4));
 k24 = h*g2((y1+k13*a43+k12*a42+k11*a41),(y2+k23*a43+k22*a42+k21*a41),(t0+h*c4));
 y[0][i] = y[0][i-1]+ w1*k11 + w2*k12 + w3*k13 + w4*k14;
 y[1][i] = y[1][i-1]+ w1*k21 + w2*k22 + w3*k23 + w4*k24;


 printf(" theta[%d]=%lf || omega[%d]=%lf", i,y[0][i],i,y[1][i]);
 printf("\n");

 }
}

0 个答案:

没有答案