Roots of a function: Difference between revisions

Content added Content deleted
(Added Elixir)
m (→‎as-is function: added/changed whitespace and comments.)
Line 1,934: Line 1,934:
=={{header|REXX}}==
=={{header|REXX}}==
===as-is function===
===as-is function===
<lang rexx>/*REXX program to find the roots of a specific function. */
<lang rexx>/*REXX program finds the roots of a specific function: x^3 - 3*x^2 + 2*x */
parse arg bot top inc . /*allow user to specify options. */
parse arg bot top inc . /*obtain optional arguments from the CL*/
if bot=='' | bot==',' then bot=-3 /*Not specified? Then use default*/
if bot=='' | bot==',' then bot=-3 /*Not specified? Then use the default.*/
if top=='' | top==',' then top=+3 /* " " " " " */
if top=='' | top==',' then top=+3 /* " " " " " " */
if inc=='' | inc==',' then inc=.0001 /* " " " " " */
if inc=='' | inc==',' then inc=.0001 /* " " " " " " */
z=f(bot); !=sign(z)
z=f(bot); !=sign(z)


do j=bot to top by inc /*traipse through all the values.*/
do j=bot to top by inc /*traipse through the values specified.*/
z=f(j); $=sign(z) /*compute new value and the sign.*/
z=f(j); $=sign(z) /*compute the new value and the sign.*/
if z=0 then say 'found a root at' j/1
if z=0 then say 'found a root at' j/1
else if !\==$ then if !\==0 then say 'passed a root at' j/1
else if !\==$ then if !\==0 then say 'passed a root at' j/1
!=$ /*use the new sign. */
!=$ /*use the new sign for the next time. */
end /*j*/
end /*j*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────F function──────────────────────────*/
/*──────────────────────────────────F function────────────────────────────────*/
f: procedure; parse arg x; return x**3 - 3 * x**2 + 2 * x</lang>
f: procedure; parse arg x; return x**3 - 3 * x**2 + 2 * x</lang>
'''output''' when using the defaults for input:
'''output''' &nbsp; when using the defaults for input:
<pre>
<pre>
found a root at 0
found a root at 0