Euler method: Difference between revisions

Content added Content deleted
m (→‎version 2: changed comments and whitespace.)
m (→‎version 2: simplified the REXX program, added more precision.)
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).*/
e=2.718281828459045235360287471352662497757247093699959574966967627724076630353547595
numeric digits length( e() ) - length(.) /*use the number of decimal digits in 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,704: Line 2,705:
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
exp: procedure expose e; arg x; ix= x%1; if abs(x-ix)>.5 then ix=ix+sign(x); x= x-ix; z=1
e: return 2.718281828459045235360287471352662497757247093699959574966967627724076630353548
_=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>
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|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>