1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
#include <cmath>
#include <math.h>
double ddx(double x,double y,double z)
{
double dx = (-5*z-3*y)/2;
return dx;
}
double test2ndorder(double x0, double y0, double z0, double x, double h)
{
int n = (int)((x - x0) / h);
double k1, k2, k3, k4;
double l1, l2, l3, l4;
double y = y0;
double z = z0;
for (int i = 1; i <= n; i++)
{
k1 = h * z0;
l1 = h * ddx(x0, y0, z0);
k2 = h * (z0 + 0.5*l1);
l2 = h * ddx(x0 + 0.5*h, y0 + 0.5*k1, z0 + 0.5*l1);
k3 = h * (z0 + 0.5*l2);
l3 = h * ddx(x0 + 0.5*h, y0 + 0.5*k2, z0 + 0.5*l2);
k4 = h * (z0 + l3);
l4 = h * ddx(x0 + h, y0 + k3, z0 + l3);
y = y + (1.0 / 6.0)*(k1 + 2 * k2 + 2 * k3 + k4);
z = z + (1.0 / 6.0)*(l1 + 2 * l2 + 2 * l3 + l4);
x = x + h;
}
return z;
}
int main()
{
double x0, y0, z0, x, y, z,h;
x0 = 0;
x = 1;
y0 = 3;
z0 = -4;
h =0.0001;
z = test2ndorder(x0, y0, z0, x, h);
std::cout << z;
}
|