-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrk4_prb.cpp
294 lines (266 loc) · 5.28 KB
/
rk4_prb.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# include <cmath>
# include <cstdlib>
# include <iomanip>
# include <iostream>
using namespace std;
# include "rk4.hpp"
int main ( );
void rk4_test ( );
double rk4_test_f ( double t, double u );
void rk4vec_test ( );
double *rk4vec_test_f ( double t, int n, double u[] );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for RK4_PRB.
//
// Discussion:
//
// RK4_PRB tests the RK4 library.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 July 2011
//
// Author:
//
// John Burkardt
//
{
timestamp ( );
cout << "\n";
cout << "RK4_PRB\n";
cout << " C++ version\n";
cout << " Test the RK4 library.\n";
rk4_test ( );
rk4vec_test ( );
//
// Terminate.
//
cout << "\n";
cout << "RK4_PRB\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
void rk4_test ( )
//****************************************************************************80
//
// Purpose:
//
// RK4_TEST demonstrates the use of RK4.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 09 October 2013
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Local, double DT, the time step.
//
// Local, double T0, the time at which the solution is known.
//
// Local, double TMAX, the maximum time at which a solution is desired.
//
// Local, double U0, the estimated solution at time T0.
//
{
double dt = 0.1;
double pi = 3.14159265;
double t0 = 0.0;
double t1;
double tmax = 12.0 * pi;
double u0 = 0.5;
double u1;
cout << "\n";
cout << "RK4_TEST\n";
cout << " RK4 takes one Runge Kutta step for a scalar ODE.\n";
cout << "\n";
cout << " T U[T]\n";
cout << "\n";
while ( true )
{
//
// Print (T0,U0).
//
cout << " " << t0 << " " << u0 << "\n";
//
// Stop if we've exceeded TMAX.
//
if ( tmax <= t0 )
{
break;
}
//
// Otherwise, advance to time T1, and have RK4 estimate
// the solution U1 there.
//
t1 = t0 + dt;
u1 = rk4 ( t0, u0, dt, rk4_test_f );
//
// Shift the data to prepare for another step.
//
t0 = t1;
u0 = u1;
}
return;
}
//****************************************************************************80
double rk4_test_f ( double t, double u )
//****************************************************************************80
//
// Purpose:
//
// RK4_TEST_F evaluates the right hand side of a particular ODE.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 July 2011
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, double T, the current time.
//
// Input, double U, the current solution value.
//
// Output, double RK4_TEST_F, the value of the derivative, dU/dT.
//
{
double dudt;
dudt = u * cos ( t );
return dudt;
}
//****************************************************************************80
void rk4vec_test ( )
//****************************************************************************80
//
// Purpose:
//
// RK4VEC_TEST tests the RK4VEC routine for a vector ODE.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 09 October 2013
//
// Author:
//
// John Burkardt
//
{
double dt = 0.2;
int i;
int n = 2;
double t0;
double t1;
double tmax = 12.0 * 3.141592653589793;
double *u0;
double *u1;
cout << "\n";
cout << "RK4VEC_TEST\n";
cout << " RK4VEC takes a Runge Kutta step for a vector ODE.\n";
cout << "\n";
cout << " T U[0] U[1]\n";
cout << "\n";
t0 = 0.0;
u0 = new double[n];
u0[0] = 0.0;
u0[1] = 1.0;
for ( ; ; )
{
//
// Print (T0,U0).
//
cout << " " << setw(14) << t0
<< " " << setw(14) << u0[0]
<< " " << setw(14) << u0[1] << "\n";
//
// Stop if we've exceeded TMAX.
//
if ( tmax <= t0 )
{
break;
}
//
// Otherwise, advance to time T1, and have RK4 estimate
// the solution U1 there.
//
t1 = t0 + dt;
u1 = rk4vec ( t0, n, u0, dt, rk4vec_test_f );
//
// Shift the data to prepare for another step.
//
t0 = t1;
for ( i = 0; i < n; i++ )
{
u0[i] = u1[i];
}
delete [] u1;
}
return;
}
//****************************************************************************80
double *rk4vec_test_f ( double t, int n, double u[] )
//****************************************************************************80
//
// Purpose:
//
// RK4VEC_TEST_F evaluates the right hand side of a vector ODE.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 09 October 2013
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, double T, the current time.
//
// Input, int N, the dimension of the system.
//
// Input, double U[N], the current solution value.
//
// Output, double RK4VEC_TEST_F[N], the value of the derivative, dU/dT.
//
{
double *uprime;
uprime = new double[n];
uprime[0] = u[1];
uprime[1] = - u[0];
return uprime;
}