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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <cmath>
double ddt[4];
void f(double t, double v[4])
{
ddt[0] = v[2] * t;
ddt[1] = v[3] * t - (0.5) * 9.81 * pow(t,2);
ddt[2] = 0 * t;
ddt[3] = -9.81 * t;
}
int main()
{
using namespace std;
void f(double t, double v[4]);
double h, t0, v1_0, v2_0, v3_0, v4_0;
int i, j;
const int nos = 10;
h = 0.1;
t0 = 0.0;
v1_0 = 0.0;
v2_0 = 0.0;
v3_0 = 20 * cos(30);
v4_0 = 20 * sin(30);
double t[nos + 1];
double v[4];
t[0] = t0;
v[0] = v1_0;
v[1] = v2_0;
v[2] = v3_0;
v[3] = v4_0;
double v_n[4];
cout << "x is: " << v[0] << endl;
cout << "y is: " << v[1] << endl << endl;
for(i = 0; i < nos; i++)
{
t[i + 1] = t[i] + h;
v_n[0] = v[0];
v_n[1] = v[1];
v_n[2] = v[2];
v_n[3] = v[3];
double added[4];
double k1[4], k2[4], k3[4], k4[4];
double v_npl[4];
f(t[i], v_n);
for(j = 0; j < 4; j++)
{
k1[j] = h * ddt[j];
added[j] = v_n[j] + (0.5 * k1[j]);
}
f(t[i] = 0.5 * h, added);
for(j = 0; j < 4; j++)
{
k2[j] = h * ddt[j];
added[j] = v_n[j] + (0.5 * k2[j]);
}
f(t[i] + 0.5 * h, added);
for(j = 0; j < 4; j++)
{
k3[j] = h * ddt[j];
added[j] = v_n[j] + k3[j];
}
f(t[i] + h, added);
for(j = 0; j < 4; j++)
{
k4[j] = h * ddt[j];
}
for(j = 0; j < 4; j++)
{
v_npl[j] = v_n[j] + ((k1[j] + 2 * k2[j] + 2 * k3[j] + k4[j]) / 6.0);
}
v[0] = v_npl[0];
v[1] = v_npl[1];
v[2] = v_npl[2];
v[3] = v_npl[3];
cout << "x is: " << v[0] << endl;
cout << "y is: " << v[1] << endl << endl;
}
return 0;
}
/*
doesn't seem to give "+" to y and y never returns to 0
*/
|