Euler method: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Changed to modular solution)
(→‎{{header|REXX}}: added a 2nd REXX version.)
Line 2,338: Line 2,338:


=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
{{trans|PLI}}
{{trans|PLI}}
<lang rexx>/* REXX ***************************************************************
<lang rexx>/* REXX ***************************************************************
Line 2,460: Line 2,461:
100 20.0729505571 20.0004723920</pre>
100 20.0729505571 20.0004723920</pre>


===version 2===
This REXX version allows values to be specified via the command line (CL).
<br>It also shows the percentage difference (analytic vs. Euler's method) for each calculation.
<lang rexx>/*REXX pgm solves example of Newton's cooling law via Euler's method (diff. step sizes).*/
numeric digits length( e() - 1) /*use the number of decimal digits in E*/
parse arg Ti Tr cc tt ss /*obtain optional arguments from the CL*/
if Ti=='' | Ti=="," then Ti=100 /*given? Default: initial temp in ºC.*/
if Tr=='' | Tr=="," then Tr= 20 /* " " room " " " */
if cc=='' | cc=="," then cc= 0.07 /* " " cooling constant. */
if tt=='' | tt=="," then tt=100 /* " " total time seconds. */
if ss ='' | ss ="," then ss=2 5 10 /* " " the step sizes. */
@= '═' /*the character used in title separator*/
do sSize=1 for words(ss); say; say; say center('time in' , 11)
say center('seconds' , 11, @) center('Euler method', 16, @) ,
center('analytic', 18, @) center('difference' , 14, @)
$=Ti; inc=word(ss,Ssize) /*the 1st value; obtain the increment.*/
do t=0 to Ti by inc /*step through calculations by the inc.*/
a=format(Tr + (Ti-Tr)/exp(cc*t),6,9) /*calculate the analytic (exact) value.*/
say center(t,11) format($,6,3) 'ºC ' a "ºC" format(abs(a-$)/a*100,6,2) '%'
$=$ + inc * cc * (Tr-$) /*calc. next value via Euler's method. */
end /*t*/
end /*stepSize*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
e: return 2.718281828459045235360287471352662497757247093699959574966967627724076630353548
/*──────────────────────────────────────────────────────────────────────────────────────*/
exp: procedure; parse arg x; ix=x%1; if abs(x-ix)>.5 then ix=ix+sign(x); x=x-ix; z=1
_=1; w=1; do j=1; _=_*x/j; z=(z+_)/1; if z==w then leave; w=z; end /*j*/
if z\==0 then z=e()**ix * z; return z</lang>
'''out put''' &nbsp; when using the default inputs:
<pre>
time in
══seconds══ ══Euler method══ ═════analytic═════ ══difference══
0 100.000 ºC 100.000000000 ºC 0.00 %
2 88.800 ºC 89.548658880 ºC 0.84 %
4 79.168 ºC 80.462699290 ºC 1.61 %
6 70.884 ºC 72.563745610 ºC 2.31 %
8 63.761 ºC 65.696725120 ºC 2.95 %
10 57.634 ºC 59.726824310 ºC 3.50 %
12 52.365 ºC 54.536841890 ºC 3.98 %
14 47.834 ºC 50.024887910 ºC 4.38 %
16 43.937 ºC 46.102383590 ºC 4.70 %
18 40.586 ºC 42.692322120 ºC 4.93 %
20 37.704 ºC 39.727757110 ºC 5.09 %
22 35.226 ºC 37.150488110 ºC 5.18 %
24 33.094 ºC 34.909918080 ºC 5.20 %
26 31.261 ºC 32.962060070 ºC 5.16 %
28 29.684 ºC 31.268673670 ºC 5.07 %
30 28.328 ºC 29.796514260 ºC 4.93 %
32 27.163 ºC 28.516680350 ºC 4.75 %
34 26.160 ºC 27.404046200 ºC 4.54 %
36 25.297 ºC 26.436768540 ºC 4.31 %
38 24.556 ºC 25.595857740 ºC 4.06 %
40 23.918 ºC 24.864805010 ºC 3.81 %
42 23.369 ºC 24.229258300 ºC 3.55 %
44 22.898 ºC 23.676740540 ºC 3.29 %
46 22.492 ºC 23.196404660 ºC 3.04 %
48 22.143 ºC 22.778820720 ºC 2.79 %
50 21.843 ºC 22.415790680 ºC 2.55 %
52 21.585 ºC 22.100187520 ºC 2.33 %
54 21.363 ºC 21.825815310 ºC 2.12 %
56 21.172 ºC 21.587287580 ºC 1.92 %
58 21.008 ºC 21.379921530 ºC 1.74 %
60 20.867 ºC 21.199646150 ºC 1.57 %
62 20.746 ºC 21.042922260 ºC 1.41 %
64 20.641 ºC 20.906673050 ºC 1.27 %
66 20.551 ºC 20.788223680 ºC 1.14 %
68 20.474 ºC 20.685248750 ºC 1.02 %
70 20.408 ºC 20.595726650 ºC 0.91 %
72 20.351 ºC 20.517899870 ºC 0.81 %
74 20.302 ºC 20.450240510 ºC 0.73 %
76 20.259 ºC 20.391420300 ºC 0.65 %
78 20.223 ºC 20.340284460 ºC 0.58 %
80 20.192 ºC 20.295829100 ºC 0.51 %
82 20.165 ºC 20.257181460 ºC 0.45 %
84 20.142 ºC 20.223582820 ºC 0.40 %
86 20.122 ºC 20.194373570 ºC 0.36 %
88 20.105 ºC 20.168980260 ºC 0.32 %
90 20.090 ºC 20.146904380 ºC 0.28 %
92 20.078 ºC 20.127712530 ºC 0.25 %
94 20.067 ºC 20.111027940 ºC 0.22 %
96 20.057 ºC 20.096523060 ºC 0.19 %
98 20.049 ºC 20.083913110 ºC 0.17 %
100 20.042 ºC 20.072950560 ºC 0.15 %


time in
══seconds══ ══Euler method══ ═════analytic═════ ══difference══
0 100.000 ºC 100.000000000 ºC 0.00 %
5 72.000 ºC 76.375047200 ºC 5.73 %
10 53.800 ºC 59.726824310 ºC 9.92 %
15 41.970 ºC 47.995019940 ºC 12.55 %
20 34.281 ºC 39.727757110 ºC 13.71 %
25 29.282 ºC 33.901915480 ºC 13.63 %
30 26.034 ºC 29.796514260 ºC 12.63 %
35 23.922 ºC 26.903486920 ºC 11.08 %
40 22.549 ºC 24.864805010 ºC 9.31 %
45 21.657 ºC 23.428170150 ºC 7.56 %
50 21.077 ºC 22.415790680 ºC 5.97 %
55 20.700 ºC 21.702378920 ºC 4.62 %
60 20.455 ºC 21.199646150 ºC 3.51 %
65 20.296 ºC 20.845376350 ºC 2.64 %
70 20.192 ºC 20.595726650 ºC 1.96 %
75 20.125 ºC 20.419801470 ºC 1.44 %
80 20.081 ºC 20.295829100 ºC 1.06 %
85 20.053 ºC 20.208467240 ºC 0.77 %
90 20.034 ºC 20.146904380 ºC 0.56 %
95 20.022 ºC 20.103521770 ºC 0.40 %
100 20.014 ºC 20.072950560 ºC 0.29 %


time in
══seconds══ ══Euler method══ ═════analytic═════ ══difference══
0 100.000 ºC 100.000000000 ºC 0.00 %
10 44.000 ºC 59.726824310 ºC 26.33 %
20 27.200 ºC 39.727757110 ºC 31.53 %
30 22.160 ºC 29.796514260 ºC 25.63 %
40 20.648 ºC 24.864805010 ºC 16.96 %
50 20.194 ºC 22.415790680 ºC 9.91 %
60 20.058 ºC 21.199646150 ºC 5.38 %
70 20.017 ºC 20.595726650 ºC 2.81 %
80 20.005 ºC 20.295829100 ºC 1.43 %
90 20.002 ºC 20.146904380 ºC 0.72 %
100 20.000 ºC 20.072950560 ºC 0.36 %
</pre>


=={{header|Ring}}==
=={{header|Ring}}==