定步长,可以在稍微修改之后变为变步长形式,代码如下:
void rkf78c( double h, double& T, vxd& X, double& err) { int N = X.size(); vxd X1(N); ; vxd Y0(N), Y1(N), Y2(N), Y3(N), Y4(N), Y5(N), Y6(N), Y7(N), Y8(N), Y9(N), Y10(N), Y11(N), Y12(N); ; i != N ; i++) { X1[i] = X[i]; } T1 = T; dxdt(T1, X1, Y0); ; i != N ; i++) { X1[i] = X[i] + h*2.0/27.0*Y0[i]; } T1 = T + h*2.0/27.0; dxdt(T1, X1, Y1); ; i != N ; i++) { X1[i] = X[i] + h*(Y0[i]+3.0*Y1[i])/36.0; } T1 = T + h*1.0/9.0; dxdt(T1, X1, Y2); ; i != N ; i++) { X1[i] = X[i] + h*(Y0[i]+3.0*Y2[i])/24.0; } T1 = T + h*1.0/6.0; dxdt(T1, X1, Y3); ; i != N ; i++) { X1[i] = X[i] + h*(Y0[i]*20.0+(-Y2[i]+Y3[i])*75.0)/48.0; } T1 = T + h*5.0/12.0; dxdt(T1, X1, Y4); ; i != N ; i++) { X1[i] = X[i] + h*(Y0[i]+Y3[i]*5.0+Y4[i]*4.0)/20.0; } T1 = T + h*1.0/2.0; dxdt(T1, X1, Y5); ; i != N ; i++) { X1[i] = X[i] + h*(-Y0[i]*25.0+Y3[i]*125.0-Y4[i]*260.0+Y5[i]*250.0)/108.0; } T1 = T + h*5.0/6.0; dxdt(T1, X1, Y6); ; i != N ; i++) { X1[i] = X[i] + h*(Y0[i]*93.0+Y4[i]*244.0-Y5[i]*200.0+Y6[i]*13.0)/900.0; } T1 = T + h*1.0/6.0; dxdt(T1, X1, Y7); ; i != N ; i++) { X1[i] = X[i] + h*(Y0[i]*180.0-Y3[i]*795.0+Y4[i]*1408.0-Y5[i]*1070.0+Y6[i]*67.0+Y7[i]*270.0)/90.0; } T1 = T + h*2.0/3.0; dxdt(T1, X1, Y8); ; i != N ; i++) { X1[i] = X[i] + h*(-Y0[i]*455.0+Y3[i]*115.0-Y4[i]*3904.0+Y5[i]*3110.0-Y6[i]*171.0+Y7[i]*1530.0-Y8[i]*45.0)/540.0; } T1 = T + h*1.0/3.0; dxdt(T1, X1, Y9); ; i != N ; i++) { X1[i] = X[i] + h*(Y0[i]*2383.0-Y3[i]*8525.0+Y4[i]*17984.0-Y5[i]*15050.0+Y6[i]*2133.0+Y7[i]*2250.0+Y8[i]*1125.0+Y9[i]*1800.0)/4100.0; } T1 = T + h; dxdt(T1, X1, Y10); ; i != N ; i++) { X1[i] = X[i] + h*(Y0[i]*60.0-Y5[i]*600.0-Y6[i]*60.0+(Y8[i] -Y7[i] +2.0*Y9[i])*300.0)/4100.0; } T1 = T; dxdt(T1, X1, Y11); ; i != N ; i++) { X1[i] = X[i] + h*(-Y0[i]*1777.0-Y3[i]*8525.0+Y4[i]*17984.0-Y5[i]*14450.0+Y6[i]*2193.0+Y7[i]*2550.0+Y8[i]*825.0+Y9[i]*1200.0+Y11[i]*4100.0)/4100.0; } T1 = T + h; dxdt(T1, X1, Y12); err = 0.0; ; i != X.size(); i++) { X[i] += h*(Y5[i]*272.0+(Y6[i]+Y7[i])*216.0+(Y8[i]+Y9[i])*27.0+(Y11[i]+Y12[i])*41.0)/840.0; err += fabs((Y0[i]+Y10[i]-Y11[i]-Y12[i])*h*41.0/840.0); } T += h; }