author:

  • 'Adrian E. Feiguin' title: 'Computational Physics' ...

Ordinary differential equations

Let’s consider a simple 1st order equation: $$\frac{dy}{dx}=f(x,y)$$

To solve this equation with a computer we need to discretize the differences: we have to convert the differential equation into a “finite differences” equation. The simplest solution is Euler’s method.

Euler’s method

Supouse that at a point $x_0$, the function $f$ has a value $y_0$. We want to find the approximate value of $y$ in a point $x_1$ close to $x_0$, $x_1=x_0+\Delta x$, with $\Delta x$ small. We assume that $f$, the rate of change of $y$, is constant in this interval $\Delta x$. Therefore we find: $$\begin{eqnarray} && dx \approx \Delta x &=&x_1-x_0, \\ && dy \approx \Delta y &=&y_1-y_0,\end{eqnarray}$$ with $y_1=y(x_1)=y(x_0+\Delta x)$. Then we re-write the differential equation in terms of discrete differences as: $$\frac{\Delta y}{\Delta x}=f(x,y)$$ or $$\Delta y = f(x,y)\Delta x$$ and approximate the value of $y_1$ as $$y_1=y_0+f(x_0,y_0)(x_1-x_0)$$ We can generalize this formula to find the value of $y$ at $x_2=x_1+\Delta x$ as $$y_{2}=y_1+f(x_1,y_1)\Delta x,$$ or in the general case: $$y_{n+1}=y_n+f(x_n,y_n)\Delta x$$

This is a good approximation as long as $\Delta x$ is “small”. What is small? Depends on the problem, but it is basically defined by the “rate of change”, or “smoothness” of $f$. $f(x)$ has to behave smoothly and without rapid variations in the interval $\Delta x$.

Notice that Euler’s method is equivalent to a 1st order Taylor expansion about the point $x_0$. The “local error” calculating $x_1$ is then $O(\Delta x^2)$. If we use the method $N$ times to calculate $N$ consecutive points, the propagated “global” error will be $NO(\Delta x^2)\approx O(\Delta x)$. This error decreases linearly with decreasing step, so we need to halve the step size to reduce the error in half. The numerical work for each step consists of a single evaluation of $f$.

Exercise 1.1: Newton’s law of cooling

If the temperature difference between an object and its surroundings is small, the rate of change of the temperature of the object is proportional to the temperature difference: $$\frac{dT}{dt}=-r(T-T_s),$$ where $T$ is the temperature of the body, $T_s$ is the temperature of the environment, and $r$ is a “cooling constant” that depends on the heat transfer mechanism, the contact area with the environment and the thermal properties of the body. The minus sign appears because if $T>T_s$, the temperature must decrease.

Write a program to calculate the temperature of a body at a time $t$, given the cooling constant $r$ and the temperature of the body at time $t=0$. Plot the results for $r=0.1\frac{1}{min}$; $T_0=83^{\circ} C$ using different intervals $\Delta t$ and compare with exact (analytical) results.


In [2]:
T0 = 10.   # initial temperature
Ts = 83.   # temp. of the environment
r = 0.1    # cooling rate
dt = 0.05  # time step
tmax = 60. # maximum time
nsteps = int(tmax/dt)  # number of steps

In [3]:
T = T0
for i in range(1,nsteps+1):
    new_T = T - r*(T-Ts)*dt
    T = new_T
    print i,i*dt, T
    # we can also do t = t - r*(t-ts)*dt


1 0.05 10.365
2 0.1 10.728175
3 0.15 11.089534125
4 0.2 11.4490864544
5 0.25 11.8068410221
6 0.3 12.162806817
7 0.35 12.5169927829
8 0.4 12.869407819
9 0.45 13.2200607799
10 0.5 13.568960476
11 0.55 13.9161156736
12 0.6 14.2615350953
13 0.65 14.6052274198
14 0.7 14.9472012827
15 0.75 15.2874652763
16 0.8 15.6260279499
17 0.85 15.9628978101
18 0.9 16.2980833211
19 0.95 16.6315929045
20 1.0 16.96343494
21 1.05 17.2936177653
22 1.1 17.6221496764
23 1.15 17.949038928
24 1.2 18.2742937334
25 1.25 18.5979222647
26 1.3 18.9199326534
27 1.35 19.2403329901
28 1.4 19.5591313252
29 1.45 19.8763356686
30 1.5 20.1919539902
31 1.55 20.5059942203
32 1.6 20.8184642492
33 1.65 21.1293719279
34 1.7 21.4387250683
35 1.75 21.7465314429
36 1.8 22.0527987857
37 1.85 22.3575347918
38 1.9 22.6607471178
39 1.95 22.9624433823
40 2.0 23.2626311653
41 2.05 23.5613180095
42 2.1 23.8585114195
43 2.15 24.1542188624
44 2.2 24.4484477681
45 2.25 24.7412055292
46 2.3 25.0324995016
47 2.35 25.3223370041
48 2.4 25.610725319
49 2.45 25.8976716925
50 2.5 26.183183334
51 2.55 26.4672674173
52 2.6 26.7499310802
53 2.65 27.0311814248
54 2.7 27.3110255177
55 2.75 27.5894703901
56 2.8 27.8665230382
57 2.85 28.142190423
58 2.9 28.4164794709
59 2.95 28.6893970735
60 3.0 28.9609500881
61 3.05 29.2311453377
62 3.1 29.499989611
63 3.15 29.767489663
64 3.2 30.0336522146
65 3.25 30.2984839536
66 3.3 30.5619915338
67 3.35 30.8241815761
68 3.4 31.0850606683
69 3.45 31.3446353649
70 3.5 31.6029121881
71 3.55 31.8598976271
72 3.6 32.115598139
73 3.65 32.3700201483
74 3.7 32.6231700476
75 3.75 32.8750541973
76 3.8 33.1256789263
77 3.85 33.3750505317
78 3.9 33.6231752791
79 3.95 33.8700594027
80 4.0 34.1157091056
81 4.05 34.3601305601
82 4.1 34.6033299073
83 4.15 34.8453132578
84 4.2 35.0860866915
85 4.25 35.325656258
86 4.3 35.5640279767
87 4.35 35.8012078369
88 4.4 36.0372017977
89 4.45 36.2720157887
90 4.5 36.5056557097
91 4.55 36.7381274312
92 4.6 36.969436794
93 4.65 37.1995896101
94 4.7 37.428591662
95 4.75 37.6564487037
96 4.8 37.8831664602
97 4.85 38.1087506279
98 4.9 38.3332068748
99 4.95 38.5565408404
100 5.0 38.7787581362
101 5.05 38.9998643455
102 5.1 39.2198650238
103 5.15 39.4387656986
104 5.2 39.6565718702
105 5.25 39.8732890108
106 5.3 40.0889225658
107 5.35 40.3034779529
108 5.4 40.5169605632
109 5.45 40.7293757603
110 5.5 40.9407288815
111 5.55 41.1510252371
112 5.6 41.3602701109
113 5.65 41.5684687604
114 5.7 41.7756264166
115 5.75 41.9817482845
116 5.8 42.1868395431
117 5.85 42.3909053454
118 5.9 42.5939508186
119 5.95 42.7959810645
120 6.0 42.9970011592
121 6.05 43.1970161534
122 6.1 43.3960310727
123 6.15 43.5940509173
124 6.2 43.7910806627
125 6.25 43.9871252594
126 6.3 44.1821896331
127 6.35 44.3762786849
128 6.4 44.5693972915
129 6.45 44.7615503051
130 6.5 44.9527425535
131 6.55 45.1429788408
132 6.6 45.3322639466
133 6.65 45.5206026268
134 6.7 45.7079996137
135 6.75 45.8944596156
136 6.8 46.0799873175
137 6.85 46.264587381
138 6.9 46.4482644441
139 6.95 46.6310231218
140 7.0 46.8128680062
141 7.05 46.9938036662
142 7.1 47.1738346479
143 7.15 47.3529654746
144 7.2 47.5312006472
145 7.25 47.708544644
146 7.3 47.8850019208
147 7.35 48.0605769112
148 7.4 48.2352740266
149 7.45 48.4090976565
150 7.5 48.5820521682
151 7.55 48.7541419074
152 7.6 48.9253711978
153 7.65 49.0957443418
154 7.7 49.2652656201
155 7.75 49.433939292
156 7.8 49.6017695956
157 7.85 49.7687607476
158 7.9 49.9349169439
159 7.95 50.1002423591
160 8.0 50.2647411473
161 8.05 50.4284174416
162 8.1 50.5912753544
163 8.15 50.7533189776
164 8.2 50.9145523827
165 8.25 51.0749796208
166 8.3 51.2346047227
167 8.35 51.3934316991
168 8.4 51.5514645406
169 8.45 51.7087072179
170 8.5 51.8651636818
171 8.55 52.0208378634
172 8.6 52.1757336741
173 8.65 52.3298550057
174 8.7 52.4832057307
175 8.75 52.635789702
176 8.8 52.7876107535
177 8.85 52.9386726998
178 8.9 53.0889793363
179 8.95 53.2385344396
180 9.0 53.3873417674
181 9.05 53.5354050586
182 9.1 53.6827280333
183 9.15 53.8293143931
184 9.2 53.9751678211
185 9.25 54.120291982
186 9.3 54.2646905221
187 9.35 54.4083670695
188 9.4 54.5513252342
189 9.45 54.693568608
190 9.5 54.8351007649
191 9.55 54.9759252611
192 9.6 55.1160456348
193 9.65 55.2554654066
194 9.7 55.3941880796
195 9.75 55.5322171392
196 9.8 55.6695560535
197 9.85 55.8062082732
198 9.9 55.9421772319
199 9.95 56.0774663457
200 10.0 56.212079014
201 10.05 56.3460186189
202 10.1 56.4792885258
203 10.15 56.6118920832
204 10.2 56.7438326228
205 10.25 56.8751134597
206 10.3 57.0057378924
207 10.35 57.1357092029
208 10.4 57.2650306569
209 10.45 57.3937055036
210 10.5 57.5217369761
211 10.55 57.6491282912
212 10.6 57.7758826498
213 10.65 57.9020032365
214 10.7 58.0274932203
215 10.75 58.1523557542
216 10.8 58.2765939754
217 10.85 58.4002110056
218 10.9 58.5232099505
219 10.95 58.6455939008
220 11.0 58.7673659313
221 11.05 58.8885291016
222 11.1 59.0090864561
223 11.15 59.1290410238
224 11.2 59.2483958187
225 11.25 59.3671538396
226 11.3 59.4853180704
227 11.35 59.6028914801
228 11.4 59.7198770227
229 11.45 59.8362776376
230 11.5 59.9520962494
231 11.55 60.0673357681
232 11.6 60.1819990893
233 11.65 60.2960890938
234 11.7 60.4096086484
235 11.75 60.5225606051
236 11.8 60.6349478021
237 11.85 60.7467730631
238 11.9 60.8580391978
239 11.95 60.9687490018
240 12.0 61.0789052568
241 12.05 61.1885107305
242 12.1 61.2975681768
243 12.15 61.406080336
244 12.2 61.5140499343
245 12.25 61.6214796846
246 12.3 61.7283722862
247 12.35 61.8347304248
248 12.4 61.9405567726
249 12.45 62.0458539888
250 12.5 62.1506247188
251 12.55 62.2548715952
252 12.6 62.3585972373
253 12.65 62.4618042511
254 12.7 62.5644952298
255 12.75 62.6666727537
256 12.8 62.7683393899
257 12.85 62.8694976929
258 12.9 62.9701502045
259 12.95 63.0702994535
260 13.0 63.1699479562
261 13.05 63.2690982164
262 13.1 63.3677527253
263 13.15 63.4659139617
264 13.2 63.5635843919
265 13.25 63.6607664699
266 13.3 63.7574626376
267 13.35 63.8536753244
268 13.4 63.9494069478
269 13.45 64.044659913
270 13.5 64.1394366135
271 13.55 64.2337394304
272 13.6 64.3275707333
273 13.65 64.4209328796
274 13.7 64.5138282152
275 13.75 64.6062590741
276 13.8 64.6982277787
277 13.85 64.7897366398
278 13.9 64.8807879566
279 13.95 64.9713840169
280 14.0 65.0615270968
281 14.05 65.1512194613
282 14.1 65.240463364
283 14.15 65.3292610472
284 14.2 65.4176147419
285 14.25 65.5055266682
286 14.3 65.5929990349
287 14.35 65.6800340397
288 14.4 65.7666338695
289 14.45 65.8528007002
290 14.5 65.9385366967
291 14.55 66.0238440132
292 14.6 66.1087247931
293 14.65 66.1931811691
294 14.7 66.2772152633
295 14.75 66.360829187
296 14.8 66.4440250411
297 14.85 66.5268049158
298 14.9 66.6091708913
299 14.95 66.6911250368
300 15.0 66.7726694116
301 15.05 66.8538060646
302 15.1 66.9345370342
303 15.15 67.0148643491
304 15.2 67.0947900273
305 15.25 67.1743160772
306 15.3 67.2534444968
307 15.35 67.3321772743
308 15.4 67.4105163879
309 15.45 67.488463806
310 15.5 67.566021487
311 15.55 67.6431913795
312 15.6 67.7199754226
313 15.65 67.7963755455
314 15.7 67.8723936678
315 15.75 67.9480316995
316 15.8 68.023291541
317 15.85 68.0981750833
318 15.9 68.1726842078
319 15.95 68.2468207868
320 16.0 68.3205866829
321 16.05 68.3939837495
322 16.1 68.4670138307
323 16.15 68.5396787616
324 16.2 68.6119803678
325 16.25 68.6839204659
326 16.3 68.7555008636
327 16.35 68.8267233593
328 16.4 68.8975897425
329 16.45 68.9681017938
330 16.5 69.0382612848
331 16.55 69.1080699784
332 16.6 69.1775296285
333 16.65 69.2466419803
334 16.7 69.3154087704
335 16.75 69.3838317266
336 16.8 69.4519125679
337 16.85 69.5196530051
338 16.9 69.5870547401
339 16.95 69.6541194664
340 17.0 69.720848869
341 17.05 69.7872446247
342 17.1 69.8533084016
343 17.15 69.9190418596
344 17.2 69.9844466503
345 17.25 70.049524417
346 17.3 70.1142767949
347 17.35 70.178705411
348 17.4 70.2428118839
349 17.45 70.3065978245
350 17.5 70.3700648354
351 17.55 70.4332145112
352 17.6 70.4960484386
353 17.65 70.5585681964
354 17.7 70.6207753555
355 17.75 70.6826714787
356 17.8 70.7442581213
357 17.85 70.8055368307
358 17.9 70.8665091465
359 17.95 70.9271766008
360 18.0 70.9875407178
361 18.05 71.0476030142
362 18.1 71.1073649991
363 18.15 71.1668281741
364 18.2 71.2259940333
365 18.25 71.2848640631
366 18.3 71.3434397428
367 18.35 71.4017225441
368 18.4 71.4597139313
369 18.45 71.5174153617
370 18.5 71.5748282849
371 18.55 71.6319541435
372 18.6 71.6887943727
373 18.65 71.7453504009
374 18.7 71.8016236489
375 18.75 71.8576155306
376 18.8 71.913327453
377 18.85 71.9687608157
378 18.9 72.0239170116
379 18.95 72.0787974266
380 19.0 72.1334034394
381 19.05 72.1877364222
382 19.1 72.2417977401
383 19.15 72.2955887514
384 19.2 72.3491108077
385 19.25 72.4023652536
386 19.3 72.4553534274
387 19.35 72.5080766602
388 19.4 72.5605362769
389 19.45 72.6127335955
390 19.5 72.6646699276
391 19.55 72.7163465779
392 19.6 72.767764845
393 19.65 72.8189260208
394 19.7 72.8698313907
395 19.75 72.9204822338
396 19.8 72.9708798226
397 19.85 73.0210254235
398 19.9 73.0709202964
399 19.95 73.1205656949
400 20.0 73.1699628664
401 20.05 73.2191130521
402 20.1 73.2680174868
403 20.15 73.3166773994
404 20.2 73.3650940124
405 20.25 73.4132685423
406 20.3 73.4612021996
407 20.35 73.5088961886
408 20.4 73.5563517077
409 20.45 73.6035699491
410 20.5 73.6505520994
411 20.55 73.6972993389
412 20.6 73.7438128422
413 20.65 73.790093778
414 20.7 73.8361433091
415 20.75 73.8819625925
416 20.8 73.9275527796
417 20.85 73.9729150157
418 20.9 74.0180504406
419 20.95 74.0629601884
420 21.0 74.1076453875
421 21.05 74.1521071605
422 21.1 74.1963466247
423 21.15 74.2403648916
424 21.2 74.2841630671
425 21.25 74.3277422518
426 21.3 74.3711035405
427 21.35 74.4142480228
428 21.4 74.4571767827
429 21.45 74.4998908988
430 21.5 74.5423914443
431 21.55 74.5846794871
432 21.6 74.6267560897
433 21.65 74.6686223092
434 21.7 74.7102791977
435 21.75 74.7517278017
436 21.8 74.7929691627
437 21.85 74.8340043169
438 21.9 74.8748342953
439 21.95 74.9154601238
440 22.0 74.9558828232
441 22.05 74.9961034091
442 22.1 75.036122892
443 22.15 75.0759422776
444 22.2 75.1155625662
445 22.25 75.1549847533
446 22.3 75.1942098296
447 22.35 75.2332387804
448 22.4 75.2720725865
449 22.45 75.3107122236
450 22.5 75.3491586625
451 22.55 75.3874128692
452 22.6 75.4254758048
453 22.65 75.4633484258
454 22.7 75.5010316837
455 22.75 75.5385265252
456 22.8 75.5758338926
457 22.85 75.6129547232
458 22.9 75.6498899495
459 22.95 75.6866404998
460 23.0 75.7232072973
461 23.05 75.7595912608
462 23.1 75.7957933045
463 23.15 75.831814338
464 23.2 75.8676552663
465 23.25 75.90331699
466 23.3 75.938800405
467 23.35 75.974106403
468 23.4 76.009235871
469 23.45 76.0441896916
470 23.5 76.0789687432
471 23.55 76.1135738994
472 23.6 76.1480060299
473 23.65 76.1822659998
474 23.7 76.2163546698
475 23.75 76.2502728964
476 23.8 76.284021532
477 23.85 76.3176014243
478 23.9 76.3510134172
479 23.95 76.3842583501
480 24.0 76.4173370583
481 24.05 76.450250373
482 24.1 76.4829991212
483 24.15 76.5155841256
484 24.2 76.5480062049
485 24.25 76.5802661739
486 24.3 76.6123648431
487 24.35 76.6443030188
488 24.4 76.6760815037
489 24.45 76.7077010962
490 24.5 76.7391625907
491 24.55 76.7704667778
492 24.6 76.8016144439
493 24.65 76.8326063717
494 24.7 76.8634433398
495 24.75 76.8941261231
496 24.8 76.9246554925
497 24.85 76.955032215
498 24.9 76.985257054
499 24.95 77.0153307687
500 25.0 77.0452541149
501 25.05 77.0750278443
502 25.1 77.1046527051
503 25.15 77.1341294415
504 25.2 77.1634587943
505 25.25 77.1926415004
506 25.3 77.2216782929
507 25.35 77.2505699014
508 25.4 77.2793170519
509 25.45 77.3079204666
510 25.5 77.3363808643
511 25.55 77.36469896
512 25.6 77.3928754652
513 25.65 77.4209110878
514 25.7 77.4488065324
515 25.75 77.4765624997
516 25.8 77.5041796872
517 25.85 77.5316587888
518 25.9 77.5590004949
519 25.95 77.5862054924
520 26.0 77.6132744649
521 26.05 77.6402080926
522 26.1 77.6670070521
523 26.15 77.6936720169
524 26.2 77.7202036568
525 26.25 77.7466026385
526 26.3 77.7728696253
527 26.35 77.7990052772
528 26.4 77.8250102508
529 26.45 77.8508851996
530 26.5 77.8766307736
531 26.55 77.9022476197
532 26.6 77.9277363816
533 26.65 77.9530976997
534 26.7 77.9783322112
535 26.75 78.0034405501
536 26.8 78.0284233474
537 26.85 78.0532812306
538 26.9 78.0780148245
539 26.95 78.1026247504
540 27.0 78.1271116266
541 27.05 78.1514760685
542 27.1 78.1757186881
543 27.15 78.1998400947
544 27.2 78.2238408942
545 27.25 78.2477216898
546 27.3 78.2714830813
547 27.35 78.2951256659
548 27.4 78.3186500376
549 27.45 78.3420567874
550 27.5 78.3653465034
551 27.55 78.3885197709
552 27.6 78.4115771721
553 27.65 78.4345192862
554 27.7 78.4573466898
555 27.75 78.4800599563
556 27.8 78.5026596565
557 27.85 78.5251463583
558 27.9 78.5475206265
559 27.95 78.5697830233
560 28.0 78.5919341082
561 28.05 78.6139744377
562 28.1 78.6359045655
563 28.15 78.6577250427
564 28.2 78.6794364175
565 28.25 78.7010392354
566 28.3 78.7225340392
567 28.35 78.743921369
568 28.4 78.7652017622
569 28.45 78.7863757533
570 28.5 78.8074438746
571 28.55 78.8284066552
572 28.6 78.8492646219
573 28.65 78.8700182988
574 28.7 78.8906682073
575 28.75 78.9112148663
576 28.8 78.931658792
577 28.85 78.952000498
578 28.9 78.9722404955
579 28.95 78.992379293
580 29.0 79.0124173966
581 29.05 79.0323553096
582 29.1 79.052193533
583 29.15 79.0719325654
584 29.2 79.0915729025
585 29.25 79.111115038
586 29.3 79.1305594628
587 29.35 79.1499066655
588 29.4 79.1691571322
589 29.45 79.1883113465
590 29.5 79.2073697898
591 29.55 79.2263329408
592 29.6 79.2452012761
593 29.65 79.2639752698
594 29.7 79.2826553934
595 29.75 79.3012421164
596 29.8 79.3197359059
597 29.85 79.3381372263
598 29.9 79.3564465402
599 29.95 79.3746643075
600 30.0 79.392790986
601 30.05 79.410827031
602 30.1 79.4287728959
603 30.15 79.4466290314
604 30.2 79.4643958862
605 30.25 79.4820739068
606 30.3 79.4996635373
607 30.35 79.5171652196
608 30.4 79.5345793935
609 30.45 79.5519064965
610 30.5 79.569146964
611 30.55 79.5863012292
612 30.6 79.6033697231
613 30.65 79.6203528745
614 30.7 79.6372511101
615 30.75 79.6540648545
616 30.8 79.6707945303
617 30.85 79.6874405576
618 30.9 79.7040033548
619 30.95 79.7204833381
620 31.0 79.7368809214
621 31.05 79.7531965168
622 31.1 79.7694305342
623 31.15 79.7855833815
624 31.2 79.8016554646
625 31.25 79.8176471873
626 31.3 79.8335589513
627 31.35 79.8493911566
628 31.4 79.8651442008
629 31.45 79.8808184798
630 31.5 79.8964143874
631 31.55 79.9119323155
632 31.6 79.9273726539
633 31.65 79.9427357906
634 31.7 79.9580221117
635 31.75 79.9732320011
636 31.8 79.9883658411
637 31.85 80.0034240119
638 31.9 80.0184068918
639 31.95 80.0333148574
640 32.0 80.0481482831
641 32.05 80.0629075417
642 32.1 80.077593004
643 32.15 80.0922050389
644 32.2 80.1067440137
645 32.25 80.1212102937
646 32.3 80.1356042422
647 32.35 80.149926221
648 32.4 80.1641765899
649 32.45 80.1783557069
650 32.5 80.1924639284
651 32.55 80.2065016088
652 32.6 80.2204691007
653 32.65 80.2343667552
654 32.7 80.2481949214
655 32.75 80.2619539468
656 32.8 80.2756441771
657 32.85 80.2892659562
658 32.9 80.3028196264
659 32.95 80.3163055283
660 33.0 80.3297240007
661 33.05 80.3430753807
662 33.1 80.3563600038
663 33.15 80.3695782037
664 33.2 80.3827303127
665 33.25 80.3958166612
666 33.3 80.4088375778
667 33.35 80.42179339
668 33.4 80.434684423
669 33.45 80.4475110009
670 33.5 80.4602734459
671 33.55 80.4729720787
672 33.6 80.4856072183
673 33.65 80.4981791822
674 33.7 80.5106882863
675 33.75 80.5231348448
676 33.8 80.5355191706
677 33.85 80.5478415748
678 33.9 80.5601023669
679 33.95 80.572301855
680 34.0 80.5844403458
681 34.05 80.596518144
682 34.1 80.6085355533
683 34.15 80.6204928756
684 34.2 80.6323904112
685 34.25 80.6442284591
686 34.3 80.6560073168
687 34.35 80.6677272802
688 34.4 80.6793886438
689 34.45 80.6909917006
690 34.5 80.7025367421
691 34.55 80.7140240584
692 34.6 80.7254539381
693 34.65 80.7368266684
694 34.7 80.7481425351
695 34.75 80.7594018224
696 34.8 80.7706048133
697 34.85 80.7817517892
698 34.9 80.7928430303
699 34.95 80.8038788151
700 35.0 80.8148594211
701 35.05 80.825785124
702 35.1 80.8366561983
703 35.15 80.8474729173
704 35.2 80.8582355528
705 35.25 80.868944375
706 35.3 80.8795996531
707 35.35 80.8902016548
708 35.4 80.9007506466
709 35.45 80.9112468933
710 35.5 80.9216906589
711 35.55 80.9320822056
712 35.6 80.9424217946
713 35.65 80.9527096856
714 35.7 80.9629461372
715 35.75 80.9731314065
716 35.8 80.9832657494
717 35.85 80.9933494207
718 35.9 81.0033826736
719 35.95 81.0133657602
720 36.0 81.0232989314
721 36.05 81.0331824368
722 36.1 81.0430165246
723 36.15 81.052801442
724 36.2 81.0625374347
725 36.25 81.0722247476
726 36.3 81.0818636238
727 36.35 81.0914543057
728 36.4 81.1009970342
729 36.45 81.110492049
730 36.5 81.1199395888
731 36.55 81.1293398908
732 36.6 81.1386931914
733 36.65 81.1479997254
734 36.7 81.1572597268
735 36.75 81.1664734281
736 36.8 81.175641061
737 36.85 81.1847628557
738 36.9 81.1938390414
739 36.95 81.2028698462
740 37.0 81.211855497
741 37.05 81.2207962195
742 37.1 81.2296922384
743 37.15 81.2385437772
744 37.2 81.2473510583
745 37.25 81.256114303
746 37.3 81.2648337315
747 37.35 81.2735095629
748 37.4 81.282142015
749 37.45 81.290731305
750 37.5 81.2992776484
751 37.55 81.3077812602
752 37.6 81.3162423539
753 37.65 81.3246611421
754 37.7 81.3330378364
755 37.75 81.3413726472
756 37.8 81.349665784
757 37.85 81.3579174551
758 37.9 81.3661278678
759 37.95 81.3742972285
760 38.0 81.3824257423
761 38.05 81.3905136136
762 38.1 81.3985610455
763 38.15 81.4065682403
764 38.2 81.4145353991
765 38.25 81.4224627221
766 38.3 81.4303504085
767 38.35 81.4381986565
768 38.4 81.4460076632
769 38.45 81.4537776249
770 38.5 81.4615087367
771 38.55 81.4692011931
772 38.6 81.4768551871
773 38.65 81.4844709112
774 38.7 81.4920485566
775 38.75 81.4995883138
776 38.8 81.5070903723
777 38.85 81.5145549204
778 38.9 81.5219821458
779 38.95 81.5293722351
780 39.0 81.5367253739
781 39.05 81.544041747
782 39.1 81.5513215383
783 39.15 81.5585649306
784 39.2 81.5657721059
785 39.25 81.5729432454
786 39.3 81.5800785292
787 39.35 81.5871781365
788 39.4 81.5942422459
789 39.45 81.6012710346
790 39.5 81.6082646795
791 39.55 81.6152233561
792 39.6 81.6221472393
793 39.65 81.6290365031
794 39.7 81.6358913206
795 39.75 81.642711864
796 39.8 81.6494983046
797 39.85 81.6562508131
798 39.9 81.6629695591
799 39.95 81.6696547113
800 40.0 81.6763064377
801 40.05 81.6829249055
802 40.1 81.689510281
803 40.15 81.6960627296
804 40.2 81.7025824159
805 40.25 81.7090695039
806 40.3 81.7155241563
807 40.35 81.7219465356
808 40.4 81.7283368029
809 40.45 81.7346951189
810 40.5 81.7410216433
811 40.55 81.747316535
812 40.6 81.7535799524
813 40.65 81.7598120526
814 40.7 81.7660129923
815 40.75 81.7721829274
816 40.8 81.7783220127
817 40.85 81.7844304027
818 40.9 81.7905082507
819 40.95 81.7965557094
820 41.0 81.8025729309
821 41.05 81.8085600662
822 41.1 81.8145172659
823 41.15 81.8204446796
824 41.2 81.8263424562
825 41.25 81.8322107439
826 41.3 81.8380496902
827 41.35 81.8438594417
828 41.4 81.8496401445
829 41.45 81.8553919438
830 41.5 81.8611149841
831 41.55 81.8668094091
832 41.6 81.8724753621
833 41.65 81.8781129853
834 41.7 81.8837224204
835 41.75 81.8893038083
836 41.8 81.8948572892
837 41.85 81.9003830028
838 41.9 81.9058810878
839 41.95 81.9113516823
840 42.0 81.9167949239
841 42.05 81.9222109493
842 42.1 81.9275998945
843 42.15 81.9329618951
844 42.2 81.9382970856
845 42.25 81.9436056002
846 42.3 81.9488875722
847 42.35 81.9541431343
848 42.4 81.9593724186
849 42.45 81.9645755565
850 42.5 81.9697526788
851 42.55 81.9749039154
852 42.6 81.9800293958
853 42.65 81.9851292488
854 42.7 81.9902036026
855 42.75 81.9952525845
856 42.8 82.0002763216
857 42.85 82.00527494
858 42.9 82.0102485653
859 42.95 82.0151973225
860 43.0 82.0201213359
861 43.05 82.0250207292
862 43.1 82.0298956255
863 43.15 82.0347461474
864 43.2 82.0395724167
865 43.25 82.0443745546
866 43.3 82.0491526818
867 43.35 82.0539069184
868 43.4 82.0586373838
869 43.45 82.0633441969
870 43.5 82.0680274759
871 43.55 82.0726873385
872 43.6 82.0773239019
873 43.65 82.0819372823
874 43.7 82.0865275959
875 43.75 82.091094958
876 43.8 82.0956394832
877 43.85 82.1001612857
878 43.9 82.1046604793
879 43.95 82.1091371769
880 44.0 82.113591491
881 44.05 82.1180235336
882 44.1 82.1224334159
883 44.15 82.1268212488
884 44.2 82.1311871426
885 44.25 82.1355312069
886 44.3 82.1398535508
887 44.35 82.1441542831
888 44.4 82.1484335117
889 44.45 82.1526913441
890 44.5 82.1569278874
891 44.55 82.161143248
892 44.6 82.1653375317
893 44.65 82.1695108441
894 44.7 82.1736632898
895 44.75 82.1777949734
896 44.8 82.1819059985
897 44.85 82.1859964685
898 44.9 82.1900664862
899 44.95 82.1941161538
900 45.0 82.198145573
901 45.05 82.2021548451
902 45.1 82.2061440709
903 45.15 82.2101133505
904 45.2 82.2140627838
905 45.25 82.2179924699
906 45.3 82.2219025075
907 45.35 82.225792995
908 45.4 82.22966403
909 45.45 82.2335157099
910 45.5 82.2373481313
911 45.55 82.2411613907
912 45.6 82.2449555837
913 45.65 82.2487308058
914 45.7 82.2524871518
915 45.75 82.256224716
916 45.8 82.2599435924
917 45.85 82.2636438744
918 45.9 82.2673256551
919 45.95 82.2709890268
920 46.0 82.2746340817
921 46.05 82.2782609113
922 46.1 82.2818696067
923 46.15 82.2854602587
924 46.2 82.2890329574
925 46.25 82.2925877926
926 46.3 82.2961248536
927 46.35 82.2996442294
928 46.4 82.3031460082
929 46.45 82.3066302782
930 46.5 82.3100971268
931 46.55 82.3135466411
932 46.6 82.3169789079
933 46.65 82.3203940134
934 46.7 82.3237920433
935 46.75 82.3271730831
936 46.8 82.3305372177
937 46.85 82.3338845316
938 46.9 82.337215109
939 46.95 82.3405290334
940 47.0 82.3438263882
941 47.05 82.3471072563
942 47.1 82.35037172
943 47.15 82.3536198614
944 47.2 82.3568517621
945 47.25 82.3600675033
946 47.3 82.3632671658
947 47.35 82.36645083
948 47.4 82.3696185758
949 47.45 82.3727704829
950 47.5 82.3759066305
951 47.55 82.3790270974
952 47.6 82.3821319619
953 47.65 82.3852213021
954 47.7 82.3882951956
955 47.75 82.3913537196
956 47.8 82.394396951
957 47.85 82.3974249662
958 47.9 82.4004378414
959 47.95 82.4034356522
960 48.0 82.4064184739
961 48.05 82.4093863816
962 48.1 82.4123394496
963 48.15 82.4152777524
964 48.2 82.4182013636
965 48.25 82.4211103568
966 48.3 82.424004805
967 48.35 82.426884781
968 48.4 82.4297503571
969 48.45 82.4326016053
970 48.5 82.4354385973
971 48.55 82.4382614043
972 48.6 82.4410700973
973 48.65 82.4438647468
974 48.7 82.4466454231
975 48.75 82.4494121959
976 48.8 82.452165135
977 48.85 82.4549043093
978 48.9 82.4576297877
979 48.95 82.4603416388
980 49.0 82.4630399306
981 49.05 82.465724731
982 49.1 82.4683961073
983 49.15 82.4710541268
984 49.2 82.4736988561
985 49.25 82.4763303619
986 49.3 82.47894871
987 49.35 82.4815539665
988 49.4 82.4841461967
989 49.45 82.4867254657
990 49.5 82.4892918384
991 49.55 82.4918453792
992 49.6 82.4943861523
993 49.65 82.4969142215
994 49.7 82.4994296504
995 49.75 82.5019325021
996 49.8 82.5044228396
997 49.85 82.5069007254
998 49.9 82.5093662218
999 49.95 82.5118193907
1000 50.0 82.5142602937
1001 50.05 82.5166889923
1002 50.1 82.5191055473
1003 50.15 82.5215100196
1004 50.2 82.5239024695
1005 50.25 82.5262829571
1006 50.3 82.5286515423
1007 50.35 82.5310082846
1008 50.4 82.5333532432
1009 50.45 82.535686477
1010 50.5 82.5380080446
1011 50.55 82.5403180044
1012 50.6 82.5426164144
1013 50.65 82.5449033323
1014 50.7 82.5471788156
1015 50.75 82.5494429216
1016 50.8 82.5516957069
1017 50.85 82.5539372284
1018 50.9 82.5561675423
1019 50.95 82.5583867046
1020 51.0 82.560594771
1021 51.05 82.5627917972
1022 51.1 82.5649778382
1023 51.15 82.567152949
1024 51.2 82.5693171843
1025 51.25 82.5714705983
1026 51.3 82.5736132453
1027 51.35 82.5757451791
1028 51.4 82.5778664532
1029 51.45 82.579977121
1030 51.5 82.5820772354
1031 51.55 82.5841668492
1032 51.6 82.5862460149
1033 51.65 82.5883147849
1034 51.7 82.5903732109
1035 51.75 82.5924213449
1036 51.8 82.5944592382
1037 51.85 82.596486942
1038 51.9 82.5985045073
1039 51.95 82.6005119847
1040 52.0 82.6025094248
1041 52.05 82.6044968777
1042 52.1 82.6064743933
1043 52.15 82.6084420213
1044 52.2 82.6103998112
1045 52.25 82.6123478122
1046 52.3 82.6142860731
1047 52.35 82.6162146427
1048 52.4 82.6181335695
1049 52.45 82.6200429017
1050 52.5 82.6219426872
1051 52.55 82.6238329737
1052 52.6 82.6257138089
1053 52.65 82.6275852398
1054 52.7 82.6294473136
1055 52.75 82.631300077
1056 52.8 82.6331435767
1057 52.85 82.6349778588
1058 52.9 82.6368029695
1059 52.95 82.6386189546
1060 53.0 82.6404258599
1061 53.05 82.6422237306
1062 53.1 82.6440126119
1063 53.15 82.6457925488
1064 53.2 82.6475635861
1065 53.25 82.6493257682
1066 53.3 82.6510791393
1067 53.35 82.6528237436
1068 53.4 82.6545596249
1069 53.45 82.6562868268
1070 53.5 82.6580053927
1071 53.55 82.6597153657
1072 53.6 82.6614167889
1073 53.65 82.6631097049
1074 53.7 82.6647941564
1075 53.75 82.6664701856
1076 53.8 82.6681378347
1077 53.85 82.6697971455
1078 53.9 82.6714481598
1079 53.95 82.673090919
1080 54.0 82.6747254644
1081 54.05 82.6763518371
1082 54.1 82.6779700779
1083 54.15 82.6795802275
1084 54.2 82.6811823264
1085 54.25 82.6827764147
1086 54.3 82.6843625327
1087 54.35 82.68594072
1088 54.4 82.6875110164
1089 54.45 82.6890734613
1090 54.5 82.690628094
1091 54.55 82.6921749535
1092 54.6 82.6937140788
1093 54.65 82.6952455084
1094 54.7 82.6967692808
1095 54.75 82.6982854344
1096 54.8 82.6997940072
1097 54.85 82.7012950372
1098 54.9 82.702788562
1099 54.95 82.7042746192
1100 55.0 82.7057532461
1101 55.05 82.7072244799
1102 55.1 82.7086883575
1103 55.15 82.7101449157
1104 55.2 82.7115941911
1105 55.25 82.7130362202
1106 55.3 82.7144710391
1107 55.35 82.7158986839
1108 55.4 82.7173191905
1109 55.45 82.7187325945
1110 55.5 82.7201389315
1111 55.55 82.7215382369
1112 55.6 82.7229305457
1113 55.65 82.724315893
1114 55.7 82.7256943135
1115 55.75 82.7270658419
1116 55.8 82.7284305127
1117 55.85 82.7297883602
1118 55.9 82.7311394184
1119 55.95 82.7324837213
1120 56.0 82.7338213027
1121 56.05 82.7351521961
1122 56.1 82.7364764352
1123 56.15 82.737794053
1124 56.2 82.7391050827
1125 56.25 82.7404095573
1126 56.3 82.7417075095
1127 56.35 82.742998972
1128 56.4 82.7442839771
1129 56.45 82.7455625572
1130 56.5 82.7468347444
1131 56.55 82.7481005707
1132 56.6 82.7493600679
1133 56.65 82.7506132675
1134 56.7 82.7518602012
1135 56.75 82.7531009002
1136 56.8 82.7543353957
1137 56.85 82.7555637187
1138 56.9 82.7567859001
1139 56.95 82.7580019706
1140 57.0 82.7592119608
1141 57.05 82.760415901
1142 57.1 82.7616138214
1143 57.15 82.7628057523
1144 57.2 82.7639917236
1145 57.25 82.765171765
1146 57.3 82.7663459061
1147 57.35 82.7675141766
1148 57.4 82.7686766057
1149 57.45 82.7698332227
1150 57.5 82.7709840566
1151 57.55 82.7721291363
1152 57.6 82.7732684906
1153 57.65 82.7744021482
1154 57.7 82.7755301374
1155 57.75 82.7766524867
1156 57.8 82.7777692243
1157 57.85 82.7788803782
1158 57.9 82.7799859763
1159 57.95 82.7810860464
1160 58.0 82.7821806162
1161 58.05 82.7832697131
1162 58.1 82.7843533645
1163 58.15 82.7854315977
1164 58.2 82.7865044397
1165 58.25 82.7875719175
1166 58.3 82.7886340579
1167 58.35 82.7896908876
1168 58.4 82.7907424332
1169 58.45 82.791788721
1170 58.5 82.7928297774
1171 58.55 82.7938656285
1172 58.6 82.7948963004
1173 58.65 82.7959218189
1174 58.7 82.7969422098
1175 58.75 82.7979574988
1176 58.8 82.7989677113
1177 58.85 82.7999728727
1178 58.9 82.8009730083
1179 58.95 82.8019681433
1180 59.0 82.8029583026
1181 59.05 82.8039435111
1182 59.1 82.8049237935
1183 59.15 82.8058991745
1184 59.2 82.8068696787
1185 59.25 82.8078353303
1186 59.3 82.8087961536
1187 59.35 82.8097521729
1188 59.4 82.810703412
1189 59.45 82.8116498949
1190 59.5 82.8125916455
1191 59.55 82.8135286872
1192 59.6 82.8144610438
1193 59.65 82.8153887386
1194 59.7 82.8163117949
1195 59.75 82.8172302359
1196 59.8 82.8181440847
1197 59.85 82.8190533643
1198 59.9 82.8199580975
1199 59.95 82.820858307
1200 60.0 82.8217540155

Let's try plotting the results. We first need to import the required libraries and methods


In [6]:
%matplotlib inline
import numpy as np
from matplotlib import pyplot

Next, we create numpy arrays to store the (x,y) values


In [7]:
my_time = np.zeros(nsteps)
my_temp = np.zeros(nsteps)

We have to re write the loop to store the values in the arrays. Remember that numpy arrays start from 0.


In [8]:
T = T0
my_temp[0] = T0
for i in range(1,nsteps):
    T = T - r*(T-Ts)*dt
    my_time[i] = i*dt
    my_temp[i] = T

In [79]:
pyplot.plot(my_time, my_temp, color='#003366', ls='-', lw=3)
pyplot.xlabel('time')
pyplot.ylabel('temperature');


We could have saved effort by defining


In [80]:
my_time = np.linspace(0.,tmax,nsteps)

In [81]:
pyplot.plot(my_time, my_temp, color='#003366', ls='-', lw=3)
pyplot.xlabel('time')
pyplot.ylabel('temperature');


Alternatively, and in order to re use code in future problems, we could have created a function.


In [9]:
def euler(y, f, dx):
    """Computes y_new = y + f*dx
    
    Parameters
    ----------
    y  : float
        old value of y_n at x_n
    f  : float
        first derivative f(x,y) evaluated at (x_n,y_n)
    dx : float
        x step
    """
    
    return y + f*dx

In [11]:
T = T0
for i in range(1,nsteps):
    T = euler(T, -r*(T-Ts), dt)
    my_temp[i] = T

Actually, for this particularly simple case, calling a function may introduce unecessary overhead, but it is a an example that we will find useful for future applications. For a simple function like this we could have used a "lambda" function (more about lambda functions here).


In [12]:
euler = lambda y, f, dx: y + f*dx

Now, let's study the effects of different time steps on the convergence:


In [21]:
dt = 1.
#my_color = ['#003366','#663300','#660033','#330066']
my_color = ['red', 'green', 'blue', 'black']
for j in range(0,4):
    nsteps = int(tmax/dt)    #the arrays will have different size for different time steps
    my_time = np.linspace(dt,tmax,nsteps) 
    my_temp = np.zeros(nsteps)
    T = T0
    for i in range(1,nsteps):
        T = euler(T, -r*(T-Ts), dt)
        my_temp[i] = T
        
    pyplot.plot(my_time, my_temp, color=my_color[j], ls='-', lw=3)
    dt = dt/2.

pyplot.xlabel('time');
pyplot.ylabel('temperature');
pyplot.xlim(8,10)
pyplot.ylim(48,58);


Challenge 1.1

To properly study convergence, one possibility it so look at the result at a given time, for different time steps. Modify the previous program to print the temperature at $t=10$ as a function of $\Delta t$.