# Coding Different Trigonometric Functions in Processing

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

1 Like

Yes

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

Usually we don’t post solutions

1 Like

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

My first sketch here Mathematic function

2 Likes

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).

2 Likes

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:

I encourage you to review the resources available here:

`:)`

2 Likes

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

1 Like

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

2 Likes

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

3 Likes

Hello,

1 Like

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.

1 Like

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

2 Likes

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

1 Like

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:

Processing source code for exp() and pow()

`:)`

4 Likes

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

1 Like