// 改进欧拉方法 比常规欧拉方法 和梯形公式具有更高精度,也称为预测-校正算法 //时间 4.23 上午 // f(x,y)=x-2*x/y // y(0)=1 #include"stdio.h"#include"iostream"#include"conio.h" using namespace std; double x0,x1,y0,y1; class mend_euler{ public: mend_euler(double h,int n); double f(double x,double y); private: double h; int n; }; mend_euler::mend_euler(double a,int b){ int i=1; h=a; n=b; while(i<=n) { x1=x0+h; y1=y0+h/2*(f(x0,y0)+f(x1,y0+h*f(x0,y0))); cout<<endl; cout<<"x1="<<x1<<" y1="<<y1<<" y="<<x0/(1+x0*x0)<<" e="<<y1-x0/(1+x0*x0)<<endl; i++; x0=x1; y0=y1; } } double mend_euler::f(double x,double y){ return 1/(1+x*x)-2*y*y; } int main(){ double h; int n; cout<<endl<<"input x0="; cin>>x0; cout<<endl<<"input y0="; cin>>y0; cout<<endl<<"intput h="; cin>>h; cout<<endl<<"intput n="; cin>>n; mend_euler euler(h,n); getch(); return 1; } 以上程序在 DEV C++中调试通过 注:其中 x0,y0表示初值 h为 步长, n为节点数目.

评论