《龍格庫塔算法解微分方程組-c語言》由會員分享,可在線閱讀,更多相關(guān)《龍格庫塔算法解微分方程組-c語言(5頁珍藏版)》請在裝配圖網(wǎng)上搜索。
1、/***************************************************************************
This program is to solve the initial value problem of following system
of differential equations:
dx/dt=x+2*y,x(0)=0,
dy/dt=2*x+y,y(0)=2,
x(0.2) and y(0.2) are to be calculated
**************************************
2、**************************************/
#include
#include
#define steplength 0.1 //步?長?è可¨|根¨′據(jù)Y需¨¨要°a調(diào)ì??整?;
#define FuncNumber 2 //FuncNumber為a微?é分¤?方¤?程¨?旳ì?數(shù)oy目?;
void main()
{
float x[200],Yn[20][200],reachpoint;int i;
x[0]=0;Yn[0][0]=0;Yn[1][0]=2; //初?值|ì條??件t
3、;
reachpoint=0.2; //所¨′求¨?點ì?可¨|根¨′據(jù)Y需¨¨要°a調(diào)ì??整?;
void rightfunctions(float x ,float *Auxiliary,float *Func);
void Runge_Kutta(float *x,float reachpoint, float(*Yn)[200]);
Runge_Kutta(x ,reachpoint, Yn);
printf("x ");
for(i=0;i<=(reachpoint-x[0])/steplength;i++)
4、 printf("%f ",x[i]);
printf("\nY1 ");
for(i=0;i<=(reachpoint-x[0])/steplength;i++)
printf("%f ",Yn[0][i]);
printf("\nY2 ");
for(i=0;i<=(reachpoint-x[0])/steplength;i++)
printf("%f ",Yn[1][i]);
getchar();
}
void rightfunctions(float x ,float *Auxiliary,float *Func)//當ì?à右?¨°方¤?程¨?改?
5、變à?時o?à,ê?需¨¨要°a改?變à?;
{
Func[0]=Auxiliary[0]+2*Auxiliary[1];
Func[1]=2*Auxiliary[0]+Auxiliary[1];
}
void Runge_Kutta(float *x,float reachpoint, float(*Yn)[200])
{
int i,j;
float Func[FuncNumber],K[FuncNumber][4],Auxiliary[FuncNumber];
for(i=0;i<=(reachpoint-x[0])/steplength;i++)
6、 {
for(j=0;j
7、
K[j][1]=Func[j];
Auxiliary[j]=*(Yn[j]+i)+0.5*steplength*K[j][1];
}
rightfunctions(x[i],Auxiliary,Func);
for(j=0;j