-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEulersMethod.cpp
More file actions
76 lines (66 loc) · 3.05 KB
/
EulersMethod.cpp
File metadata and controls
76 lines (66 loc) · 3.05 KB
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
//
// Created by root on 10/13/17.
//
#include <cstdlib>
#include "EulersMethod.h"
#include "Result.h"
using namespace std;
Result *EulersMethod::apply(InitialData *initialData, double h) {
int n = static_cast<int>(round((initialData->getTFinal() - initialData->getT0()) / h));
double tArray[n];
PointsTable *xTable = new PointsTable("x");
PointsTable *yTable = new PointsTable("y");
PointsTable *zTable = new PointsTable("z");
#if defined(dx)
xTable->addPoint(initialData->getT0(), initialData->getX0());
#endif
#if defined(dy)
yTable->addPoint(initialData->getT0(), initialData->getY0());
#endif
#if defined(dz)
zTable->addPoint(initialData->getT0(), initialData->getZ0());
#endif
tArray[0] = initialData->getT0();
double value;
for (int i = 1; i < n; i++) {
tArray[i] = tArray[i - 1] + h;
#if defined(dx)
#if defined(dy)
#if defined(dz)
value = xTable->getY(tArray[i - 1]) + h
* initialData->firstDerivativeX(xTable->getY(tArray[i - 1]), yTable->getY(tArray[i - 1]), zTable->getY(tArray[i - 1]),
tArray[i - 1]);
xTable->addPoint(tArray[i], value);
value = yTable->getY(tArray[i - 1]) + h
* initialData->firstDerivativeY(xTable->getY(tArray[i - 1]), yTable->getY(tArray[i - 1]), zTable->getY(tArray[i - 1]),
tArray[i - 1]);
yTable->addPoint(tArray[i], value);
value = zTable->getY(tArray[i - 1]) + h
* initialData->firstDerivativeZ(xTable->getY(tArray[i - 1]), yTable->getY(tArray[i - 1]), zTable->getY(tArray[i - 1]),
tArray[i - 1]);
zTable->addPoint(tArray[i], value);
#else
value = xTable->getY(tArray[i - 1]) + h * initialData->firstDerivativeX(xTable->getY(tArray[i - 1]), yTable->getY(tArray[i - 1]),
tArray[i - 1]);
xTable->addPoint(tArray[i], value);
value = yTable->getY(tArray[i - 1]) + h * initialData->firstDerivativeY(xTable->getY(tArray[i - 1]), yTable->getY(tArray[i - 1]),
tArray[i - 1]);
yTable->addPoint(tArray[i], value);
#endif
#else
value = xTable->getY(tArray[i - 1]) + h * initialData->firstDerivativeX(xTable->getY(tArray[i - 1]), tArray[i - 1]);
xTable->addPoint(tArray[i], value);
#endif
#endif
}
return new Result(xTable, yTable, zTable);
}
double EulersMethod::calculateNextX(double xPrev, double yPrev, double zPrev, double tPrev, double h) {
return xPrev + h * InitialData::derivativeX(xPrev, yPrev, zPrev, tPrev);
}
double EulersMethod::calculateNextY(double xPrev, double yPrev, double zPrev, double tPrev, double h) {
return yPrev + h * InitialData::derivativeY(xPrev, yPrev, zPrev, tPrev);
}
double EulersMethod::calculateNextZ(double xPrev, double yPrev, double zPrev, double tPrev, double h) {
return zPrev + h * InitialData::derivativeZ(xPrev, yPrev, zPrev, tPrev);
}