6fe @verb #7214:"eqnsolve" this none this rxd #7214 @program #7214:"eqnsolve" this none this "FLOAT zero = :eqnsolve(STR expr, FLOAT x0, FLOAT x1, FLOAT eps)" "" "Returns a zero of the expression in the range [, ], to an error . The expression should be in a form \"return 2.0 - log(x);\" -- it must return a floating point number, and use x as its only argument." "Note that results are not guaranteed (a) if there is no zero in the range, (b) if eps is too big [it should be generally a small number like 1e-6], (c) you commit some evil mathematical mistake like dividing by zero or taking the square root of a negative number, or (d) your equation expression isn't in the correct form." {expr, x0, x1, eps} = args "Do argument checking." typeof(expr) != STR ? raise(E_ARGS, "expr is not a string.") | 0 typeof(x0) != FLOAT ? raise(E_ARGS, "x0 is not a float.") | 0 typeof(x1) != FLOAT ? raise(E_ARGS, "x1 is not a float.") | 0 typeof(eps) != FLOAT ? raise(E_ARGS, "eps is not a float.") | 0 x0 >= x1 ? raise(E_RANGE, "x0 must be less than x1.") | 0 "We're guaranteed termination as the search space halves with each execution." while (1) {compiled, f0} = $no_one:eval(tostr("x = ", x0, "; ", expr)) compiled ? 0 | raise(E_INVIND, "expr did not compile properly.") {compiled, f1} = $no_one:eval(tostr("x = ", x1, "; ", expr)) compiled ? 0 | raise(E_INVIND, "expr did not compile properly.") x = (x0 + x1) / 2.0 {compiled, f} = $no_one:eval(tostr("x = ", x, "; ", expr)) compiled ? 0 | raise(E_INVIND, "expr did not compile properly.") if (f == 0.0 || abs(x0 - x1) < eps) return x elseif (f * f0 < 0.0) x1 = x else x0 = x endif endwhile "Last modified by JS (#7214) on Fri Nov 2 23:03:30 2001 MST." . 0