-
Notifications
You must be signed in to change notification settings - Fork 0
/
backward_euler_methods.py
385 lines (332 loc) · 7.24 KB
/
backward_euler_methods.py
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#
def backward_euler ( f, tspan, y0, n ):
#*****************************************************************************80
#
## backward_euler() uses backward Euler to solve an ODE.
#
# Discussion:
#
# Thanks to Mark Asch, 25 January 2022, for noticing that
# t[0] = 0.0
# should be
# t[0] = tspan[0]
#
# Licensing:
#
# This code is distributed under the MIT license.
#
# Modified:
#
# 25 January 2022
#
# Author:
#
# John Burkardt
#
# Input:
#
# function f: evaluates the right hand side of the ODE.
#
# real tspan[2]: the starting and ending times.
#
# real y0[m]: the initial conditions.
#
# integer n: the number of steps.
#
# Output:
#
# real t[n+1], y[n+1,m]: the solution estimates.
#
from scipy.optimize import fsolve
import numpy as np
if ( np.ndim ( y0 ) == 0 ):
m = 1
else:
m = len ( y0 )
t = np.zeros ( n + 1 )
y = np.zeros ( [ n + 1, m ] )
dt = ( tspan[1] - tspan[0] ) / float ( n )
t[0] = tspan[0];
y[0,:] = y0
for i in range ( 0, n ):
to = t[i]
yo = y[i,:]
tp = t[i] + dt
yp = yo + dt * f ( to, yo )
yp = fsolve ( backward_euler_residual, yp, args = ( f, to, yo, tp ) )
t[i+1] = tp
y[i+1,:] = yp[:]
return t, y
def backward_euler_residual ( yp, f, to, yo, tp ):
#*****************************************************************************80
#
## backward_euler_residual() evaluates the backward Euler residual.
#
# Discussion:
#
# We are seeking a value YP defined by the implicit equation:
#
# YP = YO + ( TP - TO ) * F ( TP, YP )
#
# Licensing:
#
# This code is distributed under the MIT license.
#
# Modified:
#
# 20 October 2020
#
# Author:
#
# John Burkardt
#
# Input:
#
# real yp: the estimated solution value at the new time.
#
# function f: evaluates the right hand side of the ODE.
#
# real to, yo: the old time and solution value.
#
# real tp: the new time.
#
# Output:
#
# real value: the residual.
#
value = yp - yo - ( tp - to ) * f ( tp, yp );
return value
def backward_euler_test ( ):
#*****************************************************************************80
#
## backward_euler_test() tests backward_euler().
#
# Licensing:
#
# This code is distributed under the MIT license.
#
# Modified:
#
# 20 October 2020
#
# Author:
#
# John Burkardt
#
import numpy as np
import platform
print ( '' )
print ( 'backward_euler_test():' )
print ( ' Python version: %s' % ( platform.python_version ( ) ) )
print ( ' Test backward_euler.' )
tspan = np.array ( [ 0.0, 2.0 ] )
y0 = 5.1765
n = 100
humps_backward_euler ( tspan, y0, n )
tspan = np.array ( [ 0.0, 5.0 ] )
y0 = np.array ( [ 5000, 100 ] )
n = 200
predator_prey_backward_euler ( tspan, y0, n )
#
# Terminate.
#
print ( '' )
print ( 'backward_euler_test:' )
print ( ' Normal end of execution.' )
return
def humps_backward_euler ( tspan, y0, n ):
#*****************************************************************************80
#
## humps_backward_euler(): humps ODE using backward_euler().
#
# Licensing:
#
# This code is distributed under the MIT license.
#
# Modified:
#
# 20 October 2020
#
# Author:
#
# John Burkardt
#
# Input:
#
# real tspan[2]: the time span
#
# real y0[2]: the initial condition.
#
# integer n: the number of steps to take.
#
import matplotlib.pyplot as plt
import numpy as np
print ( '' )
print ( 'humps_backward_euler():' )
print ( ' Solve the humps ODE system using backward_euler().' )
t, y = backward_euler ( humps_deriv, tspan, y0, n )
plt.clf ( )
plt.plot ( t, y, 'r-', linewidth = 2 )
a = tspan[0]
b = tspan[1]
if ( a <= 0.0 and 0.0 <= b ):
plt.plot ( [a,b], [0,0], 'k-', linewidth = 2 )
ymin = min ( y )
ymax = max ( y )
if ( ymin <= 0.0 and 0.0 <= ymax ):
plt.plot ( [0, 0], [ymin,ymax], 'k-', linewidth = 2 )
plt.grid ( True )
plt.xlabel ( '<--- T --->' )
plt.ylabel ( '<--- Y(T) --->' )
plt.title ( 'humps ODE solved by backward_euler()' )
filename = 'humps_backward_euler.png'
plt.savefig ( filename )
print ( ' Graphics saved as "%s"' % ( filename ) )
plt.show ( block = False )
plt.close ( )
return
def humps_deriv ( x, y ):
#*****************************************************************************80
#
## humps_deriv() evaluates the derivative of the humps function.
#
# Discussion:
#
# y = 1.0 / ( ( x - 0.3 )^2 + 0.01 ) \
# + 1.0 / ( ( x - 0.9 )^2 + 0.04 ) \
# - 6.0
#
# Licensing:
#
# This code is distributed under the MIT license.
#
# Modified:
#
# 22 April 2020
#
# Author:
#
# John Burkardt
#
# Input:
#
# real x[:], y[:]: the arguments.
#
# Output:
#
# real yp[:]: the value of the derivative at x.
#
yp = - 2.0 * ( x - 0.3 ) / ( ( x - 0.3 )**2 + 0.01 )**2 \
- 2.0 * ( x - 0.9 ) / ( ( x - 0.9 )**2 + 0.04 )**2
return yp
def predator_prey_backward_euler ( tspan, y0, n ):
#*****************************************************************************80
#
## predator_prey_backward_euler(): predator ODE using backward_euler().
#
# Licensing:
#
# This code is distributed under the MIT license.
#
# Modified:
#
# 20 October 2020
#
# Author:
#
# John Burkardt
#
# Input:
#
# real tspan[2]: the time span
#
# real y0[2]: the initial condition.
#
# integer n: the number of steps to take.
#
import matplotlib.pyplot as plt
import numpy as np
print ( '' )
print ( 'predator_prey_backward_euler():' )
print ( ' Solve the predator prey ODE system using backward_euler().' )
t, y = backward_euler ( predator_prey_deriv, tspan, y0, n )
plt.clf ( )
plt.plot ( y[:,0], y[:,1], 'r-', linewidth = 2 )
plt.grid ( True )
plt.xlabel ( '<--- Prey --->' )
plt.ylabel ( '<--- Predators --->' )
plt.title ( 'predator prey ODE solved by backward_euler()' )
filename = 'predator_prey_backward_euler.png'
plt.savefig ( filename )
print ( ' Graphics saved as "%s"' % ( filename ) )
plt.show ( block = False )
plt.close ( )
return
def predator_prey_deriv ( t, rf ):
#*****************************************************************************80
#
## predator_prey_deriv() evaluates the right hand side of the system.
#
# Licensing:
#
# This code is distributed under the MIT license.
#
# Modified:
#
# 22 April 2020
#
# Author:
#
# John Burkardt
#
# Reference:
#
# George Lindfield, John Penny,
# Numerical Methods Using MATLAB,
# Second Edition,
# Prentice Hall, 1999,
# ISBN: 0-13-012641-1,
# LC: QA297.P45.
#
# Input:
#
# real T, the current time.
#
# real RF[2], the current solution variables, rabbits and foxes.
#
# Output:
#
# real DRFDT[2], the right hand side of the 2 ODE's.
#
import numpy as np
r = rf[0]
f = rf[1]
drdt = 2.0 * r - 0.001 * r * f
dfdt = - 10.0 * f + 0.002 * r * f
drfdt = np.array ( [ drdt, dfdt ] )
return drfdt
def timestamp ( ):
#*****************************************************************************80
#
## timestamp() prints the date as a timestamp.
#
# Licensing:
#
# This code is distributed under the MIT license.
#
# Modified:
#
# 21 August 2019
#
# Author:
#
# John Burkardt
#
import time
t = time.time ( )
print ( time.ctime ( t ) )
return
if ( __name__ == '__main__' ):
timestamp ( )
backward_euler_test ( )
timestamp ( )