I am trying to code functions that will put a variable or number through the sigmoid function and another one for the step function.

Yes

Can you please show your entire code and tell us what the problem is?

Usually we donâ€™t post solutions

I donâ€™t know how to code it, so I am just looking for a general idea to start.

Sigmoid is 1/(1 + e ^ x)

You need to use return lest you are saving the values directly. E should be exp() and power should be pow( input, power).

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate and use them.

This is a very short list:

## Resources < Click here to expand !

I encourage you to review the resources available here:

- The Processing
*website*has references, examples, tutorials, etc.

https://processing.org/ - The Processing PDE (IDE) has lots to offer!

An exploration of the tabs will reveal what is available; libraries, tools and local examples there. - The Coding Train

https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw

https://thecodingtrain.com/ - Happy Coding

https://happycoding.io/tutorials/processing/ - The source code can give insight if you are adventurous:

https://github.com/processing/processing

Many of the Processing functions are here:

https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java - The Processing Foundation

https://processingfoundation.org/

Check out the tabs.

In the education tab, there is a reference to the Coding Train. - And the Internet.
`:)`

`:)`

Thanks for your replies, I did search the internet, but I never really found a way to put variables in it, I probably didnâ€™t look hard enough. @paulgoux should I do this?:

```
float e = exp(1.0)
1/(1+pow(e, x);
```

I donâ€™t know what x should be, is it the input?

I want the sigmoid to go from 0 to 1, because it would find the probability, so I thought that the 1 in (1+e^x) would be 0, but I donâ€™t know if I am right.

X is the variable you are testing the sigmoid should return a value based on that calculation

Ok so x is the input. One more request @paulgoux, I would like the same function just the other way around, in your equation negative numbers get closer to 1, and positive numbers get closer to 0. Is there a way to make negative numbers closer to 0, and positive numbers closer to 1.

Just do 1 - your sigmoid function

Never mind, I got it, Thanks for your help everyone .

Hello,

can you please post your solution/ entire sketch?

Hi, sorry for the late reply, here it is.

```
1/(1+pow(exp(1), x);//Logistic Sig. X is the input.
((pow(exp(1), x))-(pow(exp(1),(x*-1))))-((pow(exp(1), x))+(pow(exp(1),(x*-1))))// Tanh X is the input.
```

There is probably a better way for the Tanh.

glad you figured it out.

might want to take a look at this example to compare your implementation. In this example sigmoid is precalculated at setup and stored in an array to be read later, saving compute time later. You donâ€™t need to read all of it, theres a link to his github for downloads, youâ€™ll want to download part4

Great! I also heard that pow is really slow

You can write a function where the multiplying is done in a for loop x times without pow

You can write a function for **integer exponents** and where execution time is important to speed things up.

Discussed and tested here:

In the example provided he did not require pow():

```
float x;
float y;
void setup()
{
size(500, 500);
strokeWeight(2);
translate(width/2, height/2 - 10);
//Using integer steps in formula used
for (int i = -10; i<10; i+= 1)
{
y = 200/(1+pow(exp(1), i));
point(10*i, 1-y);
}
translate(0, height/2);
// Simplified and using float steps of 0.5
for (float i = -10; i<10; i+= .5)
{
y = 200/(1+exp(i));
point(10*i, 1-y);
}
}
```

References:

https://processing.org/reference/exp_.html

https://processing.org/reference/pow_.html

Processing source code for exp() and pow()

`:)`

Thanks, this looks like a really interesting article, just what I was looking for!