求解常微分方程初值問題之改進Euler法:預報-校正公式

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

using namespace std;

class euler
{
private:
 int i, n;
 double f, h, x, x_last, yc, yp;

public:
 double func(double z, double t)
 {
  f = (1 + z) * t * t / 2;
  return f;
 }
 void pc();
};

void main()
{
 euler predictor_corrector;
 predictor_corrector.pc();
}

void euler::pc()
{
 cout << "\n輸入初始條件:" << endl;
 cout << "\n輸入x0:";
 cin >> x;
 cout << "\n輸入y0:";
 cin >> yc;
 cout << "\n輸入y需要的x值:";
 cin >> x_last;
 cout << "\n輸入等分數:";
 cin >> n;
 h = (x_last - x) / n;
 for (i = 0; i < n; i++)
 {
  yp = yc + h * func(x, yc);
  yc += 0.5 * h * (func(x, yc) + func((x + h), yp));
  x += h;
  cout.precision(10);
  cout << x << setw(15) << yc << endl;
 }
}

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