您的位置:首页 > 其它

求解常微分方程初值问题之Runge_Kutta法

2013-08-13 11:06 267 查看
//用RKG法求解微分方程

#include <iostream>

#include <math.h>

#include <iomanip>

using namespace std;

class rkg

{

private:

int i, n;

double a, b, c, d, f, h, k1, k2, k3, k4, x, xf, y;

public:

double func(double z, double t)

{

f = 0.9 * t - 0.09 * t * t;

return f;

}

void solution();

};

void main()

{

rkg runge;

runge.solution();

}

void rkg::solution()

{

cout << "\n输入初始条件:" << endl;

cout << "\n输入x0:";

cin >> x;

cout << "\n输入y0:";

cin >> y;

cout << "\n输入xf:";

cin >> xf;

cout << "\n输入等分数:";

cin >> n;

h = (xf - x) / n;

a = (sqrt(2.0) - 1) / 2;

b = (2 - sqrt(2.0)) / 2;

c = -sqrt(2.0) / 2;

d = 1 + sqrt(2.0) / 2;

cout.precision(4);

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

{

k1 = h * func(x, y);

k2 = h * func((x + h / 2), (y + k1 / 2));

k3 = h * func((x + h / 2), (y + a * k1 + b * k2));

k4 = h * func((x + h), (y + c * k2 + d * k3));

y += (k1 + 2 * b * k2 + 2 * d * k3 + k4) / 6;

cout << (x + h) << setw(10) << y << endl;

x += h;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: