求解常微分方程初值問題之Milne預報-校正法

//實現Milne預報-校正法
#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

class milne
{
private:
 int i, n;
 double f, h, x, x_last, y, y1, y2, y3, yc, yp;
 double f0, f1, f2;

public:
 double func(double z, double t)
 {
  f = -t + sin(z);
  return f;
 }
 void pc();
};

void main()
{
 milne cotes_pc;
 cotes_pc.pc();
}

void milne::pc()
{
 cout << "\n輸入初始條件:";
 cout << "\n輸入x0:";
 cin >> x;
 cout << "\n輸入y0:";
 cin >> y;
 cout << "\n輸入y需要的x值:";
 cin >> x_last;
 cout << "\n輸入等分數:";
 cin >> n;
 h = (x_last - x) / n;
 cout << "\n輸入在x = " << (x - h) << "處的y(-1):";
 cin >> y1;
 cout << "\n輸入在x = " << (x - 2 * h) << "處的y(-2):";
 cin >> y2;
 cout << "\n輸入在x = " << (x - 3 * h) << "處的y(-3):";
 cin >> y3;
 f0 = func(x, y);
 f1 = func((x - h), y1);
 f2 = func((x - 2 * h), y2);
 for (i = 0; i < n; i++)
 {
  yp = y3 + (4 * h / 3) * (2 * f2 - f1 + 2 * f0);
  y3 = y2;
  y2 = y1;
  y1 = y;
  y = y2 + (h / 3) * (f1 + 4 * f0 + func((x + h), yp));
  f2 = f1;
  f1 = f0;
  x += h;
  f0 = func(x, y);
  cout << x << setw(15) << y << endl;
 }
}

發佈了0 篇原創文章 · 獲贊 8 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章