0

Note: i write this script in javascript. But any knowledge on how to produce this function is welcome!

In the following graph you see the s-curve or sigmoid curve. The blue line shows my value Y (y-axis) (y=75). The curve has a scale of [0,0] to [100,100]. So y is always from 0 to 100.

scurve illustrating the issue

I am trying to plot the red dot on the scurve where the blue line intersects. (so find the X value for a Y value.

My function to draw the scurve is as follows:

function sigmoid(x)
{
    return 1 / (1 + Math.pow(Math.exp(1), -x));
}

for (let z = -6; z <= 6; z += .05) {
    let sigmoidX = x1 + (x2 - x1) * ((z + 6) / 12);
    let sigmoidY = y1 + (y2 - y1) * sigmoid(z);
}

Now i need a function to get a X/Y value when Y is known and X is unknown

function getPointOnCurve(y) {
    ... code to calc x
    return x;
}

UPDATE:

    const points = [
        { x: 0, y: 0 },
        { x: 100, y: 100 }
    ];

function getSigmoid(points) {
    let x1 = points[0].x;
    let x2 = points[1].x;
    let y1 = points[0].y;
    let y2 = points[1].y;

    let PathData = [];

    for (let z = -6; z <= 6; z += .05) {
        let sigmoidX = x1 + (x2 - x1) * ((z + 6) / 12);
        let sigmoidY = y1 + (y2 - y1) * sigmoid(z);

        var vector = new Vector2(sigmoidX, sigmoidY);
        PathData.push(vector);
    }

    return PathData;
}
  • It's not very clear what you want. Is it true that you simply want to find the coordinates where $$ y = 75 $$ and $$ y = \frac{100}{1+e^{-x}} $$ intersect? This is what I interpret "I am trying to plot the red dot on the scurve where the blue line intersects" to mean. – Matti P. Aug 13 '19 at 06:06
  • I think you understand correctly, i want to plot the red dot where the blue line intersects the s-curve. And my only known value is Y (y-axis) (in this case 75).

    I am sorry but i don't understand how your function would give me the x (x-axis) value to get the coordinate. Can you explain in layman's terms please?

    edit: please forgive my little knowledge in mathematics

    – Robin Crama Aug 13 '19 at 06:17

1 Answers1

0

As I understand, you have a curve of the form $$\tag{1} y_1 = \frac{100}{1+e^{-x}} $$ and a line $$ y_2 = 75 $$ Now, you want to calculate their intersection point. This can be done easily by hand. First, equate the two to get $$ y_1 = y_2 \qquad \Rightarrow \qquad \frac{100}{1+e^{-x}} = 75 $$ We can solve the $x$-coordinate quite easily. Multiply both sides by $\frac{1+e^{-x}}{75}$ to get $$ \frac{100}{1+e^{-x}}\left( \frac{1+e^{-x}}{75} \right) = 75 \cdot \left( \frac{1+e^{-x}}{75} \right) $$ As you probably see, a lot of the terms cancel out and we get $$ \frac{100}{75} = 1 + e^{-x} $$ We are close to the solution. Subtract $1$ from both sides to get $$ \frac{100}{75} -1 = e^{-x} $$ or $\frac{1}{3}= e^{-x}$. This equation can be solved by taking the natural logarithm on both sides. The result is $$ \ln{\left( \frac{1}{3} \right)} = -x $$ Remembering the properties of logarithms, we get $\ln{\frac{1}{3}} = - \ln{3}$ and therefore $$ x = \ln{3} \approx 1.0986 $$ Therefore, the coordinates are

$$ (x,y) = (\ln{3}, 75) $$

You can test if this is the correct answer by inserting $x=\ln{3}$ into Equation (1)

Matti P.
  • 6,012
  • Thank you for your elaborate answer, i get the logic behind the steps but the X=3 is nowhere near the s curve? (see: https://ibb.co/KbCn4yw) The outcome should be something like 58 if i approx it. – Robin Crama Aug 13 '19 at 06:52
  • I suspect that since your code includes " let sigmoidX = x1 + (x2 - x1) * ((z + 6) / 12);", the $x$ coordinate is somehow scaled. Do you have the values of x1 and x2? How about y1, y2 ? – Matti P. Aug 13 '19 at 07:10
  • yes i do: x1: 0, x2: 0 / y1: 100, y2: 100. (my scales are from 0 to 100 in both directions) – Robin Crama Aug 13 '19 at 07:19
  • That's odd because in that case "x1 + (x2 - x1) * ((z + 6) / 12)" becomes identically zero ($x2-x1 = 0$) ... As I see it, your set of points sigmoidX is just a list of zeros, and sigmoidY is always equal to 100. I don't know if I'm interpreting the code wrong ... – Matti P. Aug 13 '19 at 07:25
  • Maybe you mean that $x2=100$ and $y1= 0$ ? – Matti P. Aug 13 '19 at 07:28
  • i updated my answer with my full function – Robin Crama Aug 13 '19 at 07:30
  • Okay now it's more clear! So there is some scaling factor for $x$ ... I'll need a minute to take a look at this. – Matti P. Aug 13 '19 at 07:31
  • Okay so plugging in the numbers, you get the parametric equation $$ \left{ \begin{array}{cr} x =& 100 \frac{z+6}{12} \ y =& 100 \cdot \text{sigmoid}(z) \end{array} \right. $$ and therefore, related to my derivation, what I actually solved was $z$ and not $x$. But $x$ can be calculated easily: $$ x = 100 \frac{z+6}{12} = 100 \frac{\ln{3} + 6}{12} \approx 59.155 $$ I think this should be a satisfying answer to you. – Matti P. Aug 13 '19 at 07:40
  • Matti P. Thank you! You have been a great help to me. Thanks for your devotion. – Robin Crama Aug 13 '19 at 07:48
  • You're welcome. I hope that you learned from this so that you know how to solve these problems in general. You can mark my answer as accepted. – Matti P. Aug 13 '19 at 07:53