求解常微分方程初值问题之多变量Runge_Kutta_Gill法

//用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[n];
 b2 = new double[n];
 b3 = new double[n];
 g = new double[n];
 y = new double[n];
 y1 = new double[n];
 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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值