返回首页
当前位置: 主页 > 编程语言 > Matlab教程 >

四阶R-K公式计算

时间:2014-09-26 22:56来源:电脑教程学习网 www.etwiki.cn 编辑:admin

#include<iostream.h>
#include<iomanip.h>
#include<math.h>
double fun(double x,double y)
{
double r;
r=y-2*x/y;
return r;
}
void jd_R_K(double xn,double yn,double h,int N)//经典公式
{
double K1,K2,K3,K4;
int i;
cout<<"经典公式计算得:"<<endl;
for(i=1;i<=N;i++)
{
K1=fun(xn,yn);
K2=fun(xn+h/2,yn+h/2*K1);
K3=fun(xn+h/2,yn+h/2*K2);
K4=fun(xn+h,yn+h*K3);
xn+=h;
yn+=h/6*(K1+2*K2+2*K3+K4);
cout<<"x"<<i<<"="<<xn<<setw(10)<<"y"<<i<<"="<<yn<<endl;
}
cout<<endl;
}
void Gill_R_K(double xn,double yn,double h,int N)//吉尔(Gill)公式
{
double K1,K2,K3,K4;
int i;
cout<<"吉尔(Gill)公式计算得:"<<endl;
for(i=1;i<=N;i++)
{
K1=fun(xn,yn);
K2=fun(xn+h/2,yn+h/2*K1);
K3=fun(xn+h/2,yn+(sqrt(2)-1)/2*h*K1+(1-sqrt(2)/2)*h*K2);
K4=fun(xn+h,yn-sqrt(2)/2*h*K2+(1+sqrt(2)/2)*h*K3);
xn+=h;
yn+=h/6*(K1+(2-sqrt(2))*K2+(2+sqrt(2))*K3+K4);
cout<<"x"<<i<<"="<<xn<<setw(10)<<"y"<<i<<"="<<yn<<endl;
}
cout<<endl;
}
void main()
{
double a=0,b=1;//定义域;
double x0=0;
double y0=1;
double h=0.1;
int N=int((b-a)/h);
cout<<"四阶R-K公式计算:"<<endl;
cout<<"y'=y-2x/y, x<-["<<a<<","<<b<<"],"<<endl;
cout<<"y(0)=1."<<endl<<endl;
jd_R_K(x0,y0,h,N);
Gill_R_K(x0,y0,h,N);
}
 

------分隔线----------------------------
标签(Tag):Matlab matlab技巧 matlab实例教程 matlab源代码 matlab基础教程
------分隔线----------------------------
推荐内容
猜你感兴趣