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

//用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;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值