求解常微分方程初值问题之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;
}
}
#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;
}
}
相关文章推荐
- 求解常微分方程初值问题之Runge_Kutta_Fehlberg法
- 求解常微分方程初值问题之多步Euler预报-校正法
- 求解常微分方程初值问题之Milne预报-校正法
- 求解常微分方程初值问题之改进Euler法:预报-校正公式
- 栈求解迷宫问题
- 回溯法求解N皇后问题(Java实现)
- 线性表的应用——求解两个多项式相加问题描述(顺序表求解)
- 求解关于linux下CPU信息查看的问题
- 鸡兔同笼问题java暴力求解算法
- 第十二周【项目四-利用遍历思想求解图问题(6-7)】
- 数据结构据与算法分析中的最大字数列求解问题
- JAVA求解皇后问题
- 第十二周项目4——利用遍历思想求解图问题
- matlab实现蒙特卡洛方法求解线性规划问题
- 常见的动态规划问题分析与求解
- 回溯法求解01背包问题
- 第十一周 项目4 — 利用遍历思想求解图问题(7)
- 暴力求解——POJ 1321 棋盘问题
- 回溯法求解八皇后问题
- 主元素问题求解