longe.cpp

来自「计算方法常微分初值问题」· C++ 代码 · 共 31 行

CPP
31
字号
#include <iostream.h>
#define f(x,y) (x/y)
const int MAX=100;
void main()
{
	double a,b,h,x[MAX],y[MAX],k[4];
	int m,i;
	cout<<"Input the begin and end of x (a<=x<=b):";
	cin>>a>>b;
	cout<<"Input the y value at begin of x (y0):";
	cin>>y[0];
	cout<<"Input m value[divide[a,b]]:";
	cin>>m;
	h=(b-a)/m;
	for(i=0;i<=m;i++)
		x[i]=a+i*h;
	for(i=0;i<m;i++)
	{
		k[0]=f(x[i],y[i]);
		k[1]=f((x[i]+h/2),(y[i]+h*k[0]/2));
		k[2]=f((x[i]+h/2),(y[i]+h*k[1]/2));
		k[3]=f((x[i]+h),(y[i]+h*k[2]));
		y[i+1]=y[i]+h*(k[0]+2*k[1]+2*k[2]+k[3])/6;
	}
	for(i=1;i<=m;i++)
	{
		cout<<"x"<<i<<"="<<x[i]<<","
			<<"y"<<i<<"="<<y[i]<<endl;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?