改进的欧拉公式.cpp
来自「用C++实现的多个算法」· C++ 代码 · 共 41 行
CPP
41 行
//以y'=y-2*x/y 为例其中0<=x<=1,y(0)=1
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
double f(double x);
double f(double x,double y)
{
double w;
w=y-2.0*x/y;
return w;
}
void main(void)
{
double x,y,y1,h,p,yp,yc;
int i,n;
cout<<"请输入x,y和h的值:"<<endl;
cout<<"x=";
cin>>x;
cout<<"y=";
cin>>y;
cout<<"h=";
cin>>h;
n=(y-x)/h;
cout<<"n="<<n<<endl;
for(i=1;i<=n;i++)
{
yp=y+h*f(x,y);
yc=y+h*f(x+h,yp);
p=(yp+yc)/2.0;
x=x+h;
y=p;
y1=sqrt(1+2.0*x);
cout<<"x="<<x<<setw(8)<<"y="<<setprecision(6)<<p<<setw(8)<<"y*="<<setprecision(6)<<y1<<endl;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?