Roots of a function: Difference between revisions

m
→‎{{header|REXX}}: changed wording in the REXX section header, added whitespace, used a template for an output section.
m (→‎{{header|Julia}}: Updated for Julia 1.3)
m (→‎{{header|REXX}}: changed wording in the REXX section header, added whitespace, used a template for an output section.)
Line 2,590:
 
=={{header|REXX}}==
Both of these REXX versions use the   '''bisection method'''   is used.
===function is coded as a REXX function===
<lang rexx>/*REXX program finds the roots of a specific function: x^3 - 3*x^2 + 2*x via bisection*/
parse arg bot top inc . /*obtain optional arguments from the CL*/
Line 2,597:
if top=='' | top=="," then top= +5 /* " " " " " " */
if inc=='' | inc=="," then inc= .0001 /* " " " " " " */
z= f(bot - inc); !=sign(z) /*usecompute these1st valuesvalue forto initialstart comparecompares. */
!= sign(z) /*obtain the sign of the initial value.*/
 
do j=bot to top by inc /*traipse through the specified range. */
z= f(j); $= sign(z) /*compute new value; obtain the sign. */
if z=0 then say 'found an exact root at' j/1
else if !\==$ then if !\==0 then say 'passed a root at' j/1
!=$ != $ /*use the new sign for the next compare*/
end /*j*/ /*dividing by unity normalizes J [↑] */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
f: parse arg x; return x * (x * (x-3) +2) /*formula used ──► x^3 - 3x^2 + 2x */
/*with factoring ──► x{ x^2 -3x + 2 } */
/*more " ──► x{ x( x-3 ) + 2 } */</lang>
'''{{out|output''' |text=&nbsp; when using the defaults for input:}}
<pre>
found an exact root at 0
Line 2,617:
</pre>
 
===function is coded in-line===
This version is about 40% faster than the 1<sup>st</sup> REXX version.
<lang rexx>/*REXX program finds the roots of a specific function: x^3 - 3*x^2 + 2*x via bisection*/
Line 2,624:
if top=='' | top=="," then top= +5 /* " " " " " " */
if inc=='' | inc=="," then inc= .0001 /* " " " " " " */
x= bot -inc inc /*compute 1st value to start compares. */
z= x * (x * (x-3) + 2) /*formula used ──► x^3 - 3x^2 + 2x */
!= sign(z) /*obtain the sign of the initial value.*/
do x=bot to top by inc /*traipse through the specified range. */
z= x * (x * (x-3) + 2); $= sign(z) /*compute new value; obtain the sign. */
if z=0 then say 'found an exact root at' x/1
else if !\==$ then if !\==0 then say 'passed a root at' x/1
!= $ /*use the new sign for the next compare*/
end /*x*/ /*dividing by unity normalizes X [↑] */</lang>
{{out|output|text=&nbsp; is the same as the 1<sup>st</sup> REXX version.}} <br><br>