关于用龙哥库塔法求微分方程的问题:
在程序里每次改变步长h,求得结果都会不一样比如取步长为0.2和0.5,计算结果就不一样,不知道是什么原因,请大神帮帮我,谢谢,我把那个微分方程以及代码附上:
微分方程:dy/dx=y*y;0<=x<=1; y(0)=1;
代码:
double f(double x, double y)定义函数f,用来求微分方程
{
return y*y;
}
///////////////////////////
void CQweDlg::OnButton1()
{
// TODO: Add your control notification handler code here
double xx[100][100],yy[100][100],k[100][100];
double h;步长
int i;
char ch1[10];
GetDlgItem(IDC_EDIT1)->GetWindowText(ch1,10);
h=atof(ch1);
long double T;
T=1/h;循环次数
xx[1][0]=0;
yy[1][0]=1;
for(i=0; i<=T;i++)
{
k[1][1]=f(xx[1][i],yy[1][i]);
k[1][2]=f(xx[1][i]+h/2,yy[1][i]+h*k[1][1]/2);
k[1][3]=f(xx[1][i]+h/2,yy[1][i]+h*k[1][2]/2);
k[1][4]=f(xx[1][i]+h,yy[1][i]+h*k[1][3]);
yy[1][i+1]=yy[1][i]+h*(k[1][1]+2*k[1][2]+2*k[1][3]+k[1][4])/6;
xx[1][i+1]=xx[1][i]+h;
CString str1,str2;
str1.Format("%.20f",yy[1][1000]);
str2.Format("%.2f",h);
GetDlgItem(IDC_EDIT2)->SetWindowText(str1);
}
}