Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

How to create a function whose output is either 1 or -1?

Please login with a confirmed email address before reporting spam

Hello everyone,

I would like to create a function that only gives the result of either 1 or -1.
But the probability for these two different results depends on a time-varying variable defined in COMSOL.
For example, at t0, the probability for 1 is 70%, and at t1, it might decrease to 54%, and so on.
Does anyone know how to do this in COMSOL?

2 Replies Last Post 25.05.2017, 22:18 GMT-4

Please login with a confirmed email address before reporting spam

Posted: 7 years ago 25.05.2017, 14:03 GMT-4
Updated: 7 years ago 25.05.2017, 14:08 GMT-4
Hi

One way is to express it in form of a conditional statement. In the variable section, you may define your function, F, as:

F= if ( probability > 0.7 , 1 , -1 )

It returns F=1 if probability > 0.7, and F=-1 otherwise.

Amin
Hi One way is to express it in form of a conditional statement. In the variable section, you may define your function, F, as: F= if ( probability > 0.7 , 1 , -1 ) It returns F=1 if probability > 0.7, and F=-1 otherwise. Amin

Please login with a confirmed email address before reporting spam

Posted: 7 years ago 25.05.2017, 22:18 GMT-4
Updated: 7 years ago 25.05.2017, 22:19 GMT-4
Hi, Mohammad Amin,

Thanks for your reply. But I'm afraid I didn't express myself clearly.
The function is actually used to randomly produce the result of 1 or -1.
But the probability for 1 or -1 are not the same.
For instance, if the probability for 1 is 70%, it means 70 out of 100 times when you run the function, you will get 1.

One way I think that might work is to combine "if" and "random" functions.
Let's say, if the random function is uniform distribution with mean and range of 0 and 1, respectively, which means the output ranges from -0.5 to 0.5.
Also, assume the probability for 1 is 70%, then the function should be "if(random(t)<=-0.2, -1, 1)".
I will run some tests to see if it works.
Hi, Mohammad Amin, Thanks for your reply. But I'm afraid I didn't express myself clearly. The function is actually used to randomly produce the result of 1 or -1. But the probability for 1 or -1 are not the same. For instance, if the probability for 1 is 70%, it means 70 out of 100 times when you run the function, you will get 1. One way I think that might work is to combine "if" and "random" functions. Let's say, if the random function is uniform distribution with mean and range of 0 and 1, respectively, which means the output ranges from -0.5 to 0.5. Also, assume the probability for 1 is 70%, then the function should be "if(random(t)

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.