-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspring_ode.cpp
216 lines (199 loc) · 4.05 KB
/
spring_ode.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
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <cmath>
# include <ctime>
using namespace std;
int main ( );
void timestamp ( );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for SPRING_ODE.
//
// Discussion:
//
// This is a simple example of how to plot when you don't have a plotter.
// This is a particular kind of "ASCII graphics", or "typewriter graphics"
// or "lineprinter graphics", and shows you how valuable an illustration
// can be, even when it's as crude as this example.
//
// Hooke's law for a spring observes that the restoring force is
// proportional to the displacement: F = - k x
//
// Newton's law relates the force to acceleration: F = m a
//
// Putting these together, we have
//
// m * d^2 x/dt^2 = - k * x
//
// We can add a damping force with coefficient c:
//
// m * d^2 x/dt^2 = - k * x - c * dx/dt
//
// If we write this as a pair of first order equations for (x,v), we have
//
// dx/dt = v
// m * dv/dt = - k * x - c * v
//
// and now we can approximate these values for small time steps.
//
// Note that the plotting assumes that the value of X will always be
// between -1 and +1. If the initial condition uses V = 0, and X starts
// between -1 and +1, then this will be OK.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 15 May 2012
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
float c;
float dt;
int i;
int j;
float k;
float m;
int n;
int p;
float t;
float t_final;
float v;
float v_old;
float x;
float x_old;
char z[21];
timestamp ( );
cout << "\n";
cout << "SPRING_ODE\n";
cout << " C++ version\n";
cout << " Approximate the solution of a spring equation.\n";
cout << " Display the solution with line printer graphics.\n";
cout << "\n";
//
// Data
//
m = 1.0;
k = 1.0;
c = 0.3;
t_final = 20.0;
n = 100;
dt = t_final / ( float ) ( n );
//
// Initial conditions.
//
x = 1.0;
v = 0.0;
//
// Compute the approximate solution at equally spaced times.
//
for ( i = 0; i <= n; i++ )
{
x_old = x;
v_old = v;
t = ( float ) ( i ) * t_final / ( float ) ( n );
x = x_old + dt * v_old;
v = v_old + ( dt / m ) * ( - k * x_old - c * v_old );
//
// Approximate the position of X in [-1,+1] to within 1/10.
//
p = ( int ) ( 10 * ( 1.0 + x ) );
if ( p < 0 )
{
p = 0;
}
else if ( 20 < p )
{
p = 20;
}
//
// Fill in the next line of the plot, placing 'x' in the p position.
//
for ( j = 0; j <= 20; j++ )
{
if ( ( i % 10 ) == 0 )
{
z[j] = '-';
}
else
{
z[j] = ' ';
}
}
z[0] = '|';
z[5] = '.';
z[10] = '+';
z[15] = '.';
z[20] = '|';
z[p] = 'x';
for ( j = 0; j <= 20; j++ )
{
cout << z[j];
}
cout << "\n";
}
//
// Terminate.
//
cout << "\n";
cout << "SPRING_ODE:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// 31 May 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
size_t len;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}