c++实现欧拉法,改进Euler法,龙格-库塔方法求其数值解并与精确解进行比较。

#include<iostream>
#include<math.h>
using namespace std;


double fax(double x,double y)
{
	return y-2*x/y;
}

double Euler(double h,double x,double y)
{
	return y+h*fax(x,y);
}

double RungKutta(double h,double x,double y)
{
	double k1,k2,k3,k4;
	k1=fax(x,y);
	k2=fax(x+h/2,y+k1*h/2);
	k3=fax(x+h/2,y+k2*h/2);
	k4=fax(x+h,y+h*k3);
	return y+(k1+2*k2+2*k3+k4)*h/6;
}
double progressEuler(double h,double x,double y)
{
	double y1;
	y1=y+h*fax(x,y);
	return y+(fax(x,y)+fax(x+h,y1))*h/2;
}

double accurate(double x)
{
	double y=pow(1+2*x,1.0/2);
	return y;
}

int main()
{
	double h,x,r,y;
	double n1=0,n2=0,n3=0; 
	cout<<"please input x0,y,h,r:"<<endl;
	cin>>x>>y>>h>>r;
	n1=n2=n3=y;
	cout<<"x\t y(4阶龙格)\t y(改进)\t y(Euler)\t y(精确)"<<endl;
	for(x;x<r-h;x=x+h)
	{
	n1=RungKutta(h,x,n1);
	n2=progressEuler(h,x,n2);
	n3=Euler(h,x,n3);
	cout<<x+h<<"\t"<<n1<<" \t"<<n2<<" \t"<<n3<<" \t"<<accurate(x)<<endl;
	
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值