HTBasic Help
×
Menu
Index
Froot
Find a root of an equation of the form f(x) = 0.
 
 
Loading        LOADSUB ALL FROM "FROOT.HTS"
or LOADSUB FROM "MATHLIB.HTS"
or LOADSUB FNFroot FROM "MATHLIB.HTS"
 
Usage                REAL A,B,Eps,X
INTEGER N
X=FNFroot(F$,A,B,N,Eps)
 
Description                
FNFroot attempts to find a value of x that satisfies the relation f(x) = 0, where f is the HTBasic function named in F$. A and b contain two distinct initial estimates for x as near to the root as possible. N and Eps contain stopping criteria. If the number of iterations within FNFroot exceeds n, FNFroot stops and returns MAXREAL (approximately 1.7×10308) to indicate failure. If f(x) Eps for a value of x, FNFroot considers the value to be a solution and returns that value of x.
 
F$ should contain the name of an HTBasic subroutine. The subroutine should take two REAL parameters. It should evaluate the function to be integrated at the second parameter and return its value in the first parameter. For example, if F$ = "Test", then the subroutine Test should begin with the definition line
 
SUB Test(REAL Y,X)
 
where X and Y may be replaced by the names of any REAL parameters. The subroutine Test would evaluate the desired function at the value X and return the value in Y.
 
FNFroot uses the secant method to find the root. If it finds a situation where f(a) and f(b) have opposite signs, it uses the bisection method to find the value of x between a and b that makes f(x) be 0. These methods are described in most texts on numerical mathematical methods.
 
Errors                
FNFroot causes HTBasic Errors if a = b, if N < 2, or if Eps < 0. It also causes an error if the subroutine named in F$ is undefined. The subroutine named in F$ may also cause HTBasic Errors                when it is evaluated.
 
Example
The following program finds the roots of the equation x - ¼e-x = 0.
 
10 LOADSUB ALL FROM "FROOT.HTS"
20 X=FNFroot("Func",0,1,100,1.0E-100)
30 CALL Func(Y,X)
40 PRINT "Root 1: (";X;",";Y;")"
50 X=FNFroot("Func",2,3,100,1.0E-100)
60 CALL Func(Y,X)
70 PRINT "Root 2: (";X;",";Y;")"
80 END
90 SUB Func(REAL Y,X)
100 Y=X-EXP(X)*.25
110 SUBEND
 
It produces the output
 
Root 1: ( .357401956181 , 0 )
Root 2: ( 2.15329236411 , 4.4408920985E-16 ).
 
The function x - ¼e-x is plotted below.
 
x - ¼e-x