Find the input value that makes a function reach a target output (the “goal”), similar to Excel’s Goal Seek.
This repo provides a small, dependency-light helper you can call like:
x = GoalSeek(fun, goal, x0)It solves the function fun(x) = goal and returns the x value (that returns the value of the function as same/close as/to goal).
- Excel-like call:
GoalSeek(fun, goal, x0)returns a single number. - Robust: automatic interval bracketing + bisection (no fragile vectorization).
- Positive solutions (optional): positive_only=True to restrict to x > 0.
- Custom interval (optional): bracket=(a, b) if you know a safe interval.
- Helpful errors: clear messages when no root is found or function is invalid.
-
Auto-Bracketing (interval expansion) Starting from your initial guess x0, the code expands an interval until the difference g(x) = fun(x) - goal changes sign. A sign change means there is a root inside that interval. You can also skip this step by supplying your own bracket=(a, b).
-
Bisection After a valid interval is found, the classic bisection method is used to converge to the root. Bisection is simple and reliable when a root is bracketed.
Note: older versions used a vectorized “line search” to guess the bracket. That was replaced with deterministic interval expansion for reliability. Thanks to ChatGPT. :)
This tool requires that NumPy (1.20+) be installed.
GoalSeek(fun, goal, x0, fTol=1e-6, MaxIter=200, positive_only=False, bracket=None) -> floatfun: your functionf(x)goal: target value to reach (solvef(x) = goal)x0: initial guess (used to build the interval if bracket is not given)fTol: tolerance on|f(x) - goal|MaxIter: maximum bisection iterationspositive_only: restrict tox > 0ifTruebracket: optional(a, b)that already contains the root
Returns a float (the root). Raises a GoalSeekError if it cannot find a valid interval or the function is not finite at sampled points.
The concrete Python for these is in
ExampleScript.pyinside the repo: ExampleScript.py
-
Square equals target Find
xsuch thatx^2 = 10. (Classic root finding.) -
Sales revenue You sell items at $5 with 10% commission. How many items to make $1000 net revenue? (Based on an Excel Goal Seek tutorial.)
-
Positive root only Solve
x^2 = 9but return only the positive solutionx = 3(the other solution is -3). -
Function with a singularity
f(x) = (10*x/(10 - x))^2has a pole atx = 10. Solvef(x) = 25within(0, 10). Here you pass a safe bracket like(1e-6, 9.999). -
Angles and units Solve
atan(x) = 30°. Since atan returns radians, convert the goal to radians first.
-
Removed deprecated
np.asscalarFixedNumPydeprecation that caused crashes in modern environments. -
Replaced vectorized line search with deterministic auto-bracketing Eliminates “
x_lbreferenced before assignment” and improves reliability. -
Added
positive_onlyoption Useful for domains where only positive solutions make sense (e.g., finance). -
Added optional
bracket=(a, b)Lets you guide the solver in tricky cases (singularities, constrained domains). -
Kept the original call pattern Still
GoalSeek(fun, goal, x0, ...)returning a scalar. -
Clearer error messages Better guidance when a root cannot be bracketed or function values are invalid.
You are free to use, modify and distribute the code as long as authorship is properly acknowledged. Please reference this repository in derivative works.
Above all, I give thanks to Allah, The Creator (C.C.), and honor His name Al-‘Alīm (The All-Knowing).
This repository is lovingly dedicated to my parents who have passed away, in remembrance of their guidance and support.
I would also like to thank ChatGPT (by OpenAI) for providing valuable support in updating and improving the Python implementation of Goal Seek.
Thanks to all users who provided feedback and reported issues, which directly contributed to making this tool more robust and user-friendly:
- NumPy deprecation (asscalar) issues
- “x_lb referenced before assignment” when bracketing failed
- The need to restrict solutions to positive values
Your feedback directly shaped these improvements!