# Numerical Iteration or "Goal Seek" replacement

**URL:** https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668
**Category:** Ask for Help
**Created:** [April 17, 2024, 5:19pm UTC](https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668 "2024-04-17T17:19:08Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![corbmore](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/corbmore/32/69365_2.png) [@corbmore](https://community.glideapps.com/u/corbmore)
#### Post date: [April 17, 2024, 5:19pm UTC](https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668/1 "2024-04-17T17:19:08Z")

</div>

**Does anyone know of a way to run a numerical iteration to solve for a number?**

Unfortunately the function I need to use, cannot be solved algebraically. Looking through some of the prior help topics, there may have been a plugin people were trying?, but that was 4 years ago. For those familiar with excel, this would be the same as “Goal Seek” in the Data/“What-If” section.

---

<div class="post-metadata">

### Author: ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)
#### Post date: [April 17, 2024, 11:34pm UTC](https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668/2 "2024-04-17T23:34:26Z")

</div>

Can you describe more about your use case? Have you tried using JavaScript for this?

---

<div class="post-metadata">

### Author: ![corbmore](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/corbmore/32/69365_2.png) [@corbmore](https://community.glideapps.com/u/corbmore)
#### Post date: [April 19, 2024, 5:28am UTC](https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668/3 "2024-04-19T05:28:26Z")

</div>

I haven’t tried to use JS. honestly, I’m not ever sure where to start with JS.

The issue is trying to solve high degree polynomials or with multiple types of functions. For example…  
y = (1-e^(-x))/x

For my app… _I have a value for y and am trying to solve for x_. The only way to solve this would be to iterate by guessing values for x and converge on a value for y. So essentially you’re guessing until you find an x that matches y. Without going into a bunch of detail, there’s several factors (like the number of iterations or the “exactness” of the answer) that go into Excel’s built in tool, goal seek.

> **[Goal Seek in Excel](https://www.excel-easy.com/examples/goal-seek.html)**
>
> If you know the result you want from a formula, use Goal Seek in Excel to find the input value that produces this formula result.

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [April 19, 2024, 5:37am UTC](https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668/4 "2024-04-19T05:37:01Z")

</div>

I think you could probably fork the below and then adapt it to work as a Glide external code column.

> **[GitHub - adam-hanna/goal-seek: goal-seek is a javascript library that can be...](https://github.com/adam-hanna/goal-seek)**
>
> goal-seek is a javascript library that can be used to solve for the value of an independent variable: "x"; of a function: "f(x)"; such that f(x) equals some defined goal. In oth...

See also:

> **[Experimental Code Column | Glide Docs](https://www.glideapps.com/docs/automation/computed-columns/experimental-code-column)**
>
> Create new computed columns with JavaScript.

---

<div class="post-metadata">

### Author: ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)
#### Post date: [April 19, 2024, 11:47am UTC](https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668/5 "2024-04-19T11:47:58Z")

</div>

> [@corbmore](#):
>
> y = (1-e^(-x))/x

Do you have an example of a number that you would use for ‘y’ and what the result would be for ‘x’?

---

<div class="post-metadata">

### Author: ![corbmore](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/corbmore/32/69365_2.png) [@corbmore](https://community.glideapps.com/u/corbmore)
#### Post date: [April 19, 2024, 2:44pm UTC](https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668/6 "2024-04-19T14:44:19Z")

</div>

Yeah so the x will be between 0 and 1. Technically it can’t be 0 as it makes an invalid mathematical expression. Y will tend to be in the thousands to millions. Note… for simplicity I stripped the constants out of the equation.

FWIW… I was able to find a _decent_ work around for most situations. It’s not as precise as the goal seek but if I can’t get the JS forking to run then it can probably work for the time being.

---

<div class="post-metadata">

### Author: ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)
#### Post date: [April 19, 2024, 2:55pm UTC](https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668/7 "2024-04-19T14:55:13Z")

</div>

I have some javascript built based on the formula you shared. Not ‘Goal Seek’ per se, but specific to the formula you shared, and I was just looking for some real world examples to verify if what I had worked or if it was spitting out garbage numbers.

Since you say y can be thousands or millions, I’m guessing my javascript isn’t working correctly.

---

<div class="post-metadata">

### Author: ![corbmore](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/corbmore/32/69365_2.png) [@corbmore](https://community.glideapps.com/u/corbmore)
#### Post date: [April 19, 2024, 3:06pm UTC](https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668/8 "2024-04-19T15:06:17Z")

</div>

You could be correct. The constants will have a large effect.

y = (1-e^(-x))/x is actually

y = C1(1-e(-C2\*x))/x

Constant 1 (C1) is calculated in the app but has a large range but will typically be between 1000 and 10,000.  
Constant 2 (C2) will usually be 50 but is a user input that _could_ be changed (to 30, 40, 60 or 70)

---

<div class="post-metadata">

### Author: ![Jeff\_Hager](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/jeff_hager/32/43_2.png) [@Jeff\_Hager](https://community.glideapps.com/u/Jeff_Hager)
#### Post date: [April 19, 2024, 3:19pm UTC](https://community.glideapps.com/t/numerical-iteration-or-goal-seek-replacement/72668/9 "2024-04-19T15:19:28Z")

</div>

Still would be nice to have some actual real world input and output example numbers to know if I’m on the right track, but this does do something…just don’t know if it’s giving the result you want.

```auto
function solveForX(yString, c1String, c2String, epsilon = 1e-6) {
    const y = parseFloat(yString);
    const c1 = parseFloat(c1String);
    const c2 = parseFloat(c2String);
    if (isNaN(y) || isNaN(c1) || isNaN(c2)) {
        return null;
    }

    let x = 1; // Initial guess
    let previousX;

    do {
        previousX = x;
        x = x - (functionToSolveForX(x, c1, c2) - y) / derivativeOfFunction(x, c1, c2);
    } while (Math.abs(x - previousX) > epsilon);

    return x;
}

function functionToSolveForX(x, c1, c2) {
    return c1 * ((1 - Math.exp(-c2 * x)) / x);
}

function derivativeOfFunction(x, c1, c2) {
    return (c1 * (Math.exp(-c2 * x) * (c2 * x - 1) + 1)) / (x * x);
}

const yString = p1; // string input for y
const c1String = p2; // string input for C1
const c2String = p3; // string input for C2
const x = solveForX(yString, c1String, c2String);
if (x !== null) {
    return ("x =", x);
}

```
