RosettaCodeData/Task/Roots-of-a-function/Liberty-BASIC/roots-of-a-function.basic
2023-07-01 13:44:08 -04:00

46 lines
1.2 KiB
Text

' Finds and output the roots of a given function f(x),
' within a range of x values.
' [RC]Roots of an function
mainwin 80 12
xMin =-1
xMax = 3
y =f( xMin) ' Since Liberty BASIC has an 'eval(' function the fn
' and limits would be better entered via 'input'.
LastY =y
eps =1E-12 ' closeness acceptable
bigH=0.01
print
print " Checking for roots of x^3 -3 *x^2 +2 *x =0 over range -1 to +3"
print
x=xMin: dx = bigH
do
x=x+dx
y = f(x)
'print x, dx, y
if y*LastY <0 then 'there is a root, should drill deeper
if dx < eps then 'we are close enough
print " Just crossed axis, solution f( x) ="; y; " at x ="; using( "#.#####", x)
LastY = y
dx = bigH 'after closing on root, continue with big step
else
x=x-dx 'step back
dx = dx/10 'repeat with smaller step
end if
end if
loop while x<xMax
print
print " Finished checking in range specified."
end
function f( x)
f =x^3 -3 *x^2 +2 *x
end function