forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch07.txt
174 lines (136 loc) · 4.2 KB
/
ch07.txt
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
chapter: Real-Time Simulations
==================
// Global Variables
float T; // thrust
float C; // drag coefficient
float V; // velocity
float M; // mass
float S; // displacement
.
.
.
// This function progresses the simulation by dt seconds using
// Euler's basic method
void StepSimulation(float dt)
{
float F; // total force
float A; // acceleration
float Vnew; // new velocity at time t + dt
float Snew; // new position at time t + dt
// Calculate the total force
F = (T − (C * V));
// Calculate the acceleration
A = F / M;
// Calculate the new velocity at time t + dt
// where V is the velocity at time t
Vnew = V + A * dt;
// Calculate the new displacement at time t + dt
// where S is the displacement at time t
Snew = S + Vnew * dt;
// Update old velocity and displacement with the new ones
V = Vnew;
S = Snew;
}
====================================
// New global variable
float eto; // truncation error tolerance
// This function progresses the simulation by dt seconds using
// Euler's basic method with an adaptive step size
void StepSimulation(float dt)
{
float F; // total force
float A; // acceleration
float Vnew; // new velocity at time t + dt
float Snew; // new position at time t + dt
float V1, V2; // temporary velocity variables
float dtnew; // new time step
float et; // truncation error
// Take one step of size dt to estimate the new velocity
F = (T − (C * V));
A = F / M;
V1 = V + A * dt;
// Take two steps of size dt/2 to estimate the new velocity
F = (T − (C * V));
A = F / M;
V2 = V + A * (dt/2);
F = (T − (C * V2));
A = F / M;
V2 = V2 + A * (dt/2);
// Estimate the truncation error
et = absf(V1 − V2);
// Estimate a new step size
dtnew = dt * SQRT(eto/et);
if (dtnew < dt)
{ // take at step at the new smaller step size
F = (T − (C * V));
A = F / M;
Vnew = V + A * dtnew;
Snew = S + Vnew * dtnew;
} else
{ // original step size is okay
Vnew = V1;
Snew = S + Vnew * dt;
}
// Update old velocity and displacement with the new ones
V = Vnew;
S = Snew;
}
====================================
// This function progresses the simulation by dt seconds using
// the "improved" Euler method
void StepSimulation(float dt)
{
float F; // total force
float A; // acceleration
float Vnew; // new velocity at time t + dt
float Snew; // new position at time t + dt
float k1, k2;
F = (T - (C * V));
A = F/M;
k1 = dt * A;
F = (T - (C * (V + k1)));
A = F/M;
k2 = dt * A;
// Calculate the new velocity at time t + dt
// where V is the velocity at time t
Vnew = V + (k1 + k2) / 2;
// Calculate the new displacement at time t + dt
// where S is the displacement at time t
Snew = S + Vnew * dt;
// Update old velocity and displacement with the new ones
V = Vnew;
S = Snew;
}
====================================
// This function progresses the simulation by dt seconds using
// the Runge-Kutta method
void StepSimulation(float dt)
{
float F; // total force
float A; // acceleration
float Vnew; // new velocity at time t + dt
float Snew; // new position at time t + dt
float k1, k2, k3, k4;
F = (T - (C * V));
A = F/M;
k1 = dt * A;
F = (T - (C * (V + k1/2)));
A = F/M;
k2 = dt * A;
F = (T - (C * (V + k2/2)));
A = F/M;
k3 = dt * A;
F = (T - (C * (V + k3)));
A = F/M;
k4 = dt * A;
// Calculate the new velocity at time t + dt
// where V is the velocity at time t
Vnew = V + (k1 + 2*k2 + 2*k3 + k4) / 6;
// Calculate the new displacement at time t + dt
// where S is the displacement at time t
Snew = S + Vnew * dt;
// Update old velocity and displacement with the new ones
V = Vnew;
S = Snew;
}
==================