-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eurler_RK4.cpp
138 lines (115 loc) · 2.81 KB
/
Eurler_RK4.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "stdafx.h"
#include <iostream>
using namespace std;
void Euler (float& position, float& velocity, float dt, float mass, float force)
{
velocity = dt * (mass/force) + velocity;
position = dt * velocity + position;
}
void RK4 (float& position, float& velocity, float dt, float mass, float T, float C)
{
float k1 = dt *( 1.0f/mass * ( T - C * velocity) );
float k2 = dt *( 1.0f/mass * ( T - C * (velocity + k1 /2.0f)) );
float k3 = dt *( 1.0f/mass * ( T - C * (velocity + k2 /2.0f)) );
float k4 = dt *( 1.0f/mass * ( T - C * (velocity + k3 )) );
velocity = velocity + (1.0/6.0f) * ( k1 + 2 * k2 + 2 * k3 + k4 );
position = dt * velocity + position;
}
int main()
{
{
cout << "EURLER STEP 1 second" << endl;
float velocity = 0.0f;
float pos = 0.0f;
float dt = 1.0f;
float mass = 10.0f;
float force = 4.0f;
cout << "STEP\tPOSITION\tVELOCITY"<< endl;
for ( int i= 0; i<10; i++ )
{
Euler(pos, velocity,dt, mass, force);
cout << i <<"\t"<< pos<< "\t" << velocity << endl;
}
}
{
cout << "EURLER STEP 0.5 second" << endl;
float velocity = 0.0f;
float pos = 0.0f;
float dt = 0.5f;
float mass = 10.0f;
float force = 4.0f;
cout << "STEP\tPOSITION\tVELOCITY"<< endl;
for ( int i= 0; i<20; i++ )
{
Euler(pos, velocity,dt, mass, force);
cout << i <<"\t"<< pos<< "\t" << velocity << endl;
}
}
{
cout << "EURLER STEP 0.25 second" << endl;
float velocity = 0.0f;
float pos = 0.0f;
float dt = 0.25f;
float mass = 10.0f;
float force = 4.0f;
cout << "STEP\tPOSITION\tVELOCITY"<< endl;
for ( int i= 0; i<40; i++ )
{
Euler(pos, velocity,dt, mass, force);
cout << i <<"\t"<< pos<< "\t" << velocity << endl;
}
}
{
cout << "RK4 STEP 1 SECOND" << endl;
//RK4
float pos = 0.0f;
float velocity = 0.0f;
float dt = 1.0f;
float m = 100.0f;
float T = 100.0f; //napêd
float C = 1.0f; //opór
//step
cout << "STEP\tPOSITION\tVELOCITY"<< endl;
for ( int i= 0; i<10; i++ )
{
RK4(pos, velocity,dt,m,T,C);
cout<< i << "\t" << pos << "\t" << velocity << endl;
}
}
{
cout << "RK4 STEP 0.5 SECOND" << endl;
//RK4
float pos = 0.0f;
float velocity = 0.0f;
float dt = 0.5f;
float m = 100.0f;
float T = 100.0f; //napêd
float C = 1.0f; //opór
//step
cout << "STEP\tPOSITION\tVELOCITY"<< endl;
for ( int i= 0; i<20; i++ )
{
RK4(pos, velocity,dt,m,T,C);
cout<< i << "\t" << pos << "\t" << velocity << endl;
}
}
{
cout << "RK4 STEP 0.25 SECOND" << endl;
//RK4
float pos = 0.0f;
float velocity = 0.0f;
float dt = 0.25f;
float m = 100.0f;
float T = 100.0f; //napêd
float C = 1.0f; //opór
//step
cout << "STEP\tPOSITION\tVELOCITY"<< endl;
for ( int i= 0; i<40; i++ )
{
RK4(pos, velocity,dt,m,T,C);
cout<< i << "\t" << pos << "\t" << velocity << endl;
}
}
system("Pause");
return 0;
}