Euler method: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
m (→version 2: changed comments and whitespace.) |
||
Line 2,684: | Line 2,684: | ||
<br>It also shows the percentage difference (analytic vs. Euler's method) for each calculation. |
<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).*/ |
<lang rexx>/*REXX pgm solves example of Newton's cooling law via Euler's method (diff. step sizes).*/ |
||
numeric digits length( e() ) |
numeric digits length( e() ) - length(.) /*use the number of decimal digits in E*/ |
||
parse arg Ti Tr cc tt ss /*obtain optional arguments from the CL*/ |
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 Ti='' | Ti="," then Ti= 100 /*given? Default: initial temp in ºC.*/ |
||
Line 2,690: | Line 2,690: | ||
if cc='' | cc="," then cc= 0.07 /* " " cooling constant. */ |
if cc='' | cc="," then cc= 0.07 /* " " cooling constant. */ |
||
if tt='' | tt="," then tt= 100 /* " " total time seconds. */ |
if tt='' | tt="," then tt= 100 /* " " total time seconds. */ |
||
if ss='' | ss="," then ss= |
if ss='' | ss="," then ss= 2 5 10 /* " " the step sizes. */ |
||
@= '═' /*the character used in title separator*/ |
@= '═' /*the character used in title separator*/ |
||
do sSize=1 for words(ss); say; say; say center('time in' , 11) |
do sSize=1 for words(ss); say; say; say center('time in' , 11) |
||
say center('seconds' , 11, @) center('Euler method', 16, @) , |
say center('seconds' , 11, @) center('Euler method', 16, @) , |
||
center('analytic', 18, @) center('difference' , 14, @) |
center('analytic', 18, @) center('difference' , 14, @) |
||
$=Ti; inc= word(ss, |
$=Ti; inc= word(ss, sSize) /*the 1st value; obtain the increment.*/ |
||
do t=0 to Ti by inc /*step through calculations by the inc.*/ |
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.*/ |
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) '%' |
say center(t,11) format($,6,3) 'ºC ' a "ºC" format(abs(a-$)/a*100,6,2) '%' |
||
$=$ + inc * cc * (Tr-$) |
$= $ + inc * cc * (Tr-$) /*calc. next value via Euler's method. */ |
||
end /*t*/ |
end /*t*/ |
||
end /*sSize*/ |
end /*sSize*/ |
||
Line 2,707: | Line 2,707: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
exp: procedure; parse arg x; ix= x%1; if abs(x-ix)>.5 then ix= ix+sign(x); x=x-ix; z=1 |
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 |
_=1; w=1; do j=1; _= _*x/j; z= (z+_)/1; if z==w then leave; w=z |
||
if z\==0 then z= e()**ix * z; |
end /*j*/; if z\==0 then z= e()**ix * z; return z</lang> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |