Article Preview
Buy Now
FEATURE
Evaluating Equations with XojoScript
A simple alternative to parsing a user's equation
Issue: 20.5 (September/October 2022)
Author: Harris Ulery
Author Bio: Harris Ulery is a long-time Xojo user and
Article Description: No description available.
Article Length (in bytes): 27,046
Starting Page Number: 28
Article Number: 20504
Resource File(s):
project-20504.zip Updated: 2022-08-31 23:28:44
Related Web Link(s):
me (
http://delaneyrm.com/Newton.html
Excerpt of article text...
Every now and then, I would like an application to be capable of evaluating an arbitrary function entered via the keyboard. Each time, the prospect of writing an equation parser was always more daunting than the need was pressing, so the notion was relegated to a lower priority. Recently, however, I was reviewing this magazine's articles on scripting with XojoScript. Marc Zeedar's 2007
xDev Magazine article (xdev#5612) illustrating the use of scripting to provide "plugin" filters for image processing, appeared to promise a simple mechanism to substitute for equation parsing.I had made a few feeble attempts at scripting before, but never gotten anywhere and couldn't figure out why. Still, I was anxious to give it another try—this time with more conviction. Let's roll up the sleeves and make a tool to find the roots of a user-supplied function,
f(x) = 0
.The Math
One common approach to this problem is the so-called Newtom-Raphson (N-R) method (see Wikipedia's article for a good discussion of this and related methods as well as their shortcomings). This scheme is an efficient iterative one that is easy to code. Starting with a guess (
x
), a refined estimate (0 x
) is obtained as:1
x
1 = x0 - f(x0 ) / f'(x0 ) (1a)
...End of Excerpt. Please purchase the magazine to read the full article.