RTMIN: extrema and root finder
By Namir Shammas
RTMIN is a program that finds the roots or the optimum points of a function.
The program uses a straightforward version of Newton's algorithm to find the
root of a function or its first derivative. It uses the iteration...
x' = x - f(x) / f'(x)
...to find roots, and...
x' = x - f'(x) / f''(x)
...to find minima or maxima. The program uses the following approximation to
calculate the first and second derivatives:
f'(x) = (f(x+h) - f(x-h)) / 2h (to calculate the maxima/minima)
= (f(x+h) - f(x)) / h (to calculate the root)
f''(x) = (f(x+h) - 2f(x) + f(x-h) / h^2
h = 0.01 * (1 + abs(x))
Note: To use previous values for the guess, maximum number of iterations, and tolerance, simply
press the R/S key.
- To start the program execute XEQ "RTMIN"
- The program prompts you for the name of the function in Alpha mode.
- To find the root, execute XEQ B and respond to the prompts for the guess, maximum number
of iterations, and tolerance. The program will iterate and display the root, function value
at the root, and the number of iterations.
- To find the minimum or maximum, execute XEQ C and respond to the prompts for the guess,
maximum number of iterations, and tolerance. The program will iterate and display the min/max
value, function value, slope value, and second derivative at the located value, and the number
of iterations. When the second derivative is positive, the point is a minimum. By contrast, when
the second derivative is positive, the point is a maximum.
- To select a new function execute XEQ A and enter the new function name.
The label AA implements the following sample function:
f(x) = exp(x) - 3 * x^2
Given f(x) find the following:
Allow 55 iterations at most and use a tolerance of 1E-7.
- The root new x = 5.
- The minima/maxima near x = 4
The program found a root at x = 3.73308 and a min/max point at x = 2.83277. Since the second
derivative is positive, the program found a minimum at x = 2.83277
R00 = Function name
R01 = x
R02 = h
R03 = f(x)
R04 = f(x+h)
R05 = f(x-h)
R06 = Max Iters
R07 = Iters
R08 = Tolerance
Flag 00 is used and is cleared when the program ends.