程序流程图如下:
//常微分方程的差分方法——改进的欧拉方法——求解初值问题
/************Analysis**************
*1.h:步长,N:步数,迭代次数,x0,y0为老值,x1,y1为新值
*f(x)=y-2x/y
*y(0)=1
**************************/
#include<iostream>
using namespace std;
double f(double x,double y)
{
return y-2*x/y;
}
int main()
{
double x0,y0;
double h;//step
unsigned N;
unsigned n=0;
cout<<"-------------Inproved Eular Method----------"<<endl;
cout<<"Please input initial value of x0 :"<<endl;
cin>>x0;
cout<<"Please input initial value of y0:"<<endl;
cin>>y0;
cout<<"Please input step value h:"<<endl;
cin>>h;
cout<<"Please input iterative times H:"<<endl;
cin>>N;
cout<<"------------------Output------------------"<<endl;
double x1,y1;
double yp;
double yc;
n=1;
while(true)
{
x1=x0+h;
yp=y0+h*f(x0,y0);
yc=y0+h*f(x1,yp);
y1=(yp+yc)/2;
cout<<"x1:"<<x1<<" "<<"y1:"<<y1<<endl;
if(n!=N)
{
n=n+1;
x0=x1;
y0=y1;
}
else
break;
}
cout<<"Hello,Boker..."<<endl;
system("pause");
return 0;
}
运行结果如下: