当前位置:文档之家› 数值分析龙格库塔法代码

数值分析龙格库塔法代码

龙格-库塔方法

一、龙格-库塔方法介绍:https://www.doczj.com/doc/7518397602.html,/kecheng/jisff/dzja/ch8/ch8-2.htm

二、方法说明:程序采用四阶龙格-库塔公式计算

三、程序

#include
using namespace std;

const double a=0.0; //起点a
const double b=1.0; //终点b
const double h=0.2; //步长
const int t=(b-a)/h;


double (*k)[4]=new double[t][4];
double derfun(const double &x,const double &y); //f(x,y)

int main()
{
typedef struct{
double x;
double y;
}data;
data *pd=new data[t+1];
pd->x=0; pd->y=1; //原函数初值
for (int i=0;ik[i][0]=derfun(pd[i].x,pd[i].y);
k[i][1]=derfun(pd[i].x+h/2,pd[i].y+h/2*k[i][0]);
k[i][2]=derfun(pd[i].x+h/2,pd[i].y+h/2*k[i][1]);
k[i][3]=derfun(pd[i].x+h,pd[i].y+h*k[i][2]);
pd[i+1].x=pd[i].x+h;
pd[i+1].y=pd[i].y+h/6*(k[i][0]+2*k[i][1]+2*k[i][2]+k[i][3]);
}
for (int i=0;icout << pd[i].x << ' ' << pd [i].y << ' ' << k[i][0] << ' ' << k[i][1] << ' ' << k[i][2] << ' ' << k[i][3] << endl;
}
cout << pd[t].x << ' ' << pd[t].y << endl;
system ("pause");
}

double derfun(const double &x,const double &y) //f(x,y)定义
{
return y-2*x/y;
}


相关主题
文本预览
相关文档 最新文档