Roots of a function: Difference between revisions

Line 1,235:
 
Note these roots are exact solutions with floating-point calculation.
 
=={{header|Oforth}}==
 
<lang Oforth>func: findRoots(f, a, b, st)
{
| x y lasty |
a f perform dup ->y ->lasty
 
a b st step: x [
x f perform -> y
y ==0 ifTrue: [ System.Out "Root found at " << x << cr ]
else: [ y lasty * sgn -1 == ifTrue: [ System.Out "Root near " << x << cr ] ]
y ->lasty
]
}</lang>
 
{{out}}
<pre>
findRoots(#[ dup 3 pow over sq 3 * - swap 2 * + ], -1, 3, 0.0001)
Root found at 0
Root found at 1
Root found at 2
 
findRoots(#[ dup 3 pow over sq 3 * - swap 2 * + ], -1.000001, 3, 0.0001)
Root near 9.90000000000713e-005
Root near 1.000099
Root near 2.000099
</pre>
 
=={{header|Octave}}==
1,015

edits