- . x, (7.12), (7.13) (7.14) h, p . , x, 2 h
, (7.15)
p = 2 (7.12), (7.13) p = 4 (7.14).
. (7.16)
ε, h ,
. (7.17)
7.3. - ε = 108 x = 1. .
Excel. .
Function fxy(ByVal x, ByVal y)
fxy = 2 * x * (1 + y ^ 2)
End Function
- :
Function Runge_Kutta(ByVal x0, ByVal y0, ByVal x, ByVal eps)
n = 2: erry = 1: y2h = Runge_Kutta4(x0, y0, x, n)
While Abs(erry) > eps
n2 = 2 * n: yh = Runge_Kutta4(x0, y0, x, n2)
erry = (yh - y2h) / 15: n = 2 * n: y2h = yh
Wend
Runge_Kutta = yh + erry
End Function
Runge_Kutta4(x0, y0, x, eps), 7.2. , 7.2, fxy(x, y) Runge_Kutta(x0, y0, x, eps). ,
fxy(x, y), Runge_Kutta4(x0, y0, x, n), Runge_Kutta(x0, y0, x, eps).
, ByVal , , .. .
7.6:
7.6
A | B | C | D | |
1 | xi | yi | y(x) | |
2 | 0 | 0 | 0 | 0 |
3 | 1 | 1,557407808 | 1,557407725 | 8,34673E-08 |
C 2, C 3 .
B 3 =Runge_Kutta4(A2;B2;A3;64). n = 64 , D 3 , 108.
B 3 , .. =Runge_Kutta(A2;B2;A3;0,00000001). 7.7:
7.7
A | B | C | D | |
1 | xi | yi | y(x) | |
2 | 0 | 0 | 0 | 0 |
3 | 1 | 1,557407725 | 1,557407725 | 7,14521E-10 |
, (7.15), (7.16).
|
|
C ++ -:
#include <iostream.h>
#include <math.h>
double f(double x,double y);
typedef double (*PF)(double,double);
double RK4(PF f,double x0, double y0,double x, const int n);
double RK4_eps(PF f,double x0, double y0,double x,double eps);
int main(){
double x0, x, y1, y2, eps, y0; PF pf; int n;
cout << "\n x0 = "; cin >> x0; cout << "\n y0 = "; cin >> y0;
cout << "\n n = "; cin >> n; cout << "\n x = "; cin >> x;
pf = f; y1 = RK4(pf,x0,y0,x,n);
cout << "\n n = "<<n<<" y("<<x<<") = " << y1;
cout << "\n eps = "; cin >> eps;
y2 = RK4_eps(pf,x0,y0,x,eps);
cout << "\n eps = "<<eps<<" y("<<x<<") = " << y2;
cout << "\n Press any key & Enter "; cin >> n; // pause
return 0;
}
double f(double x,double y){
double r; r = 2*x*(1+y*y);
return r;
}
double RK4(PF f,double x0, double y0,double x, const int n){
double h,k1,k2,k3,k4,y1,y2; int i;
h = (x-x0)/n; y1 = y0;
for(i = 1;i<=n;i++){ k1 = f(x0, y1); k2 = f(x0+h/2,y1+h*k1/2);
k3 = f(x0+h/2,y1+h*k2/2); k4 = f(x0+h,y1+h*k3);
y2 = y1+h*(k1+2*k2+2*k3+k4)/6; x0 = x0+h; y1 = y2; }
return y2;
}
double RK4_eps(PF f,double x0, double y0,double x,double eps){
typedef double (*PY)(double,double);
double erry,yh,y2h; int n; PY py;
n = 2; py = f; y2h = RK4(py,x0,y0,x,n);
do{ n = 2 * n; yh = RK4(py,x0,y0,x,n); erry = (yh-y2h)/15;
y2h = yh; }while(fabs(erry) > eps); yh = yh + erry;
return yh;
}
x = 1:
y1(1) = 1.55743 RK4(f, x0, y0, x, n)
n = 10, y2(1) = 1.55741 RK4_eps(f, x0, y0, x, eps) eps = 0.00000001.