求解常微分方程初值问题之多变量Runge_Kutta_Gill法
2013-08-13 11:09
211 查看
//用RKG法求解常微分方程组
#include <iostream>
#include <math.h>
#include <iomanip>
#include <fstream>
using namespace std;
class s_ode
{
private:
int i, j, l, n;
double a, b, c, d, h, x, x1, xi, xf;
double *b1, *b2, *b3, *g, *y, *y1;
public:
s_ode()
{
a = (sqrt(2.0) - 1) / 2;
b = (2 - sqrt(2.0)) / 2;
c = -sqrt(2.0) / 2;
d = 1 + sqrt(2.0) / 2;
}
void func(double p, double *q, double *r)
{
r[0] = -0.08 * pow(q[0], 0.5) - 2 * pow(q[0], 0.2) * q[1];
r[1] = -3.5e-6 * pow(q[0], 0.2) * q[1] + 1.6e-6 * pow(q[2], 0.3);
r[2] = 2 * pow(q[0], 0.2) * q[1] - 0.16 * pow(q[2], 0.3);
}
void solution();
~s_ode()
{
delete[] b1, b2, b3, g, y, y1;
}
};
void main()
{
s_ode multivar;
multivar.solution();
}
void s_ode::solution()
{
n = 3;
b1 = new double
;
b2 = new double
;
b3 = new double
;
g = new double
;
y = new double
;
y1 = new double
;
l = 1000;
xi = 0.0;
xf = 7.0;
y[0] = 0.95;
y[1] = 0.05;
y[2] = 0.0;
h = (xf - xi)/ l;
x = xi;
ofstream fout("simul_ode.txt");
fout.precision(4);
cout.precision(4);
for (i = 0; i < l; i++)
{
for (j = 0; j < n; j++)
{
y1[j] = y[j];
}
x1 = x;
func(x, y, g);
for (j = 0; j < n; j++)
{
b1[j] = g[j];
y[j] = y1[j] + h * g[j] / 2;
}
x = x1 + h / 2;
func (x, y, g);
for (j = 0; j < n; j++)
{
b2[j] = g[j];
y[j] = y1[j] + h * (a * b1[j] + b * g[j]);
}
func(x, y, g);
for (j = 0; j < n; j++)
{
b3[j] = g[j];
y[j] = y1[j] + h * (c * b2[j] + d * g[j]);
}
x = x1 + h;
func(x, y, g);
for (j = 0; j < n; j++)
{
y[j] = y1[j] + h * (b1[j] + g[j] + 2 * (b * b2[j] + d * b3[j])) / 6;
}
fout << x << setw(10) << y[0] << setw(15) << y[1] << setw(15) << y[2] << endl;
cout << x << setw(10) << y[0] << setw(15) << y[1] << setw(15) << y[2] << endl;
}
fout.close();
}
#include <iostream>
#include <math.h>
#include <iomanip>
#include <fstream>
using namespace std;
class s_ode
{
private:
int i, j, l, n;
double a, b, c, d, h, x, x1, xi, xf;
double *b1, *b2, *b3, *g, *y, *y1;
public:
s_ode()
{
a = (sqrt(2.0) - 1) / 2;
b = (2 - sqrt(2.0)) / 2;
c = -sqrt(2.0) / 2;
d = 1 + sqrt(2.0) / 2;
}
void func(double p, double *q, double *r)
{
r[0] = -0.08 * pow(q[0], 0.5) - 2 * pow(q[0], 0.2) * q[1];
r[1] = -3.5e-6 * pow(q[0], 0.2) * q[1] + 1.6e-6 * pow(q[2], 0.3);
r[2] = 2 * pow(q[0], 0.2) * q[1] - 0.16 * pow(q[2], 0.3);
}
void solution();
~s_ode()
{
delete[] b1, b2, b3, g, y, y1;
}
};
void main()
{
s_ode multivar;
multivar.solution();
}
void s_ode::solution()
{
n = 3;
b1 = new double
;
b2 = new double
;
b3 = new double
;
g = new double
;
y = new double
;
y1 = new double
;
l = 1000;
xi = 0.0;
xf = 7.0;
y[0] = 0.95;
y[1] = 0.05;
y[2] = 0.0;
h = (xf - xi)/ l;
x = xi;
ofstream fout("simul_ode.txt");
fout.precision(4);
cout.precision(4);
for (i = 0; i < l; i++)
{
for (j = 0; j < n; j++)
{
y1[j] = y[j];
}
x1 = x;
func(x, y, g);
for (j = 0; j < n; j++)
{
b1[j] = g[j];
y[j] = y1[j] + h * g[j] / 2;
}
x = x1 + h / 2;
func (x, y, g);
for (j = 0; j < n; j++)
{
b2[j] = g[j];
y[j] = y1[j] + h * (a * b1[j] + b * g[j]);
}
func(x, y, g);
for (j = 0; j < n; j++)
{
b3[j] = g[j];
y[j] = y1[j] + h * (c * b2[j] + d * g[j]);
}
x = x1 + h;
func(x, y, g);
for (j = 0; j < n; j++)
{
y[j] = y1[j] + h * (b1[j] + g[j] + 2 * (b * b2[j] + d * b3[j])) / 6;
}
fout << x << setw(10) << y[0] << setw(15) << y[1] << setw(15) << y[2] << endl;
cout << x << setw(10) << y[0] << setw(15) << y[1] << setw(15) << y[2] << endl;
}
fout.close();
}
相关文章推荐
- 求解常微分方程初值问题之Runge_Kutta_Fehlberg法
- inux乱码问题:LANG变量的秘诀
- 有关QT中声明一个vector为全局变量的初始化问题
- 父shell和子shell 变量问题
- java线程安全问题之静态变量、实例变量、局部变量
- case语句跳过变量初始化的问题
- mac关于java .bash_profile环境变量配置的问题
- 教你彻底解决Eclipse自动补全变量名的问题
- 环境变量设置问题导致的command no…
- nodejs的变量问题
- C++类对象成员变量和成员函数内存分配的问题
- GetDlgItem(ID)和控件变量直接moveWindow 和GetClientRect 的问题
- crontab 环境变量问题
- 细谈 Web Api 图片上传,在使用 Task.ContinueWith 变量无法赋值问题的解决办法!
- [C#]变量初始化问题:字段初始值无法引用非静态字段、方法或属性
- 静态变量没有重置为 null 的问题
- 变量初始化问题--一个年轻工程师的见解(2007年工作一年时写的)
- JavaScript高级程序设计学习笔记--变量、作用域和内存问题(一) (变量、作用域)
- spring mybatis解决application.properties注入变量问题
- 静态库中全局(静态)变量的初始化问题