|
Post by rblaser on Sept 19, 2021 16:19:45 GMT
Hi! I'm trying to calculate the distance (in pixels) between consecutive mouse clicks. I've stored the x,y coordinates of each click and have created a variable to express the distance up to the point of taking the square root of value - but I can't find a way to make this work. I've tried SQRT(xxx) and √(xxx), but both give me errors. Is there another way to express square root? Or perhaps some other shortcut to calculating the length of a line, or the distance between two points? Thanks!
|
|
|
Post by matia on Oct 12, 2021 20:27:17 GMT
Hello,
it has been some time, but better late than never. I've been having difficulties with the square root myself. There is a (bit complicated) solution. I followed the procedure from www.solving-math-problems.com/calculate-square-root.html#Newton where they had introduced me to the Newton's method that can be implemented into your Psytoolkit code.
The way it works is that it has a starting guess, and then calculates the average from your guess and the product of the division of your number you wish to sqrt and your guess. Then follows the second, third and so forth iteration. In my experience, fifth iteration usually does the trick if you know rough ball-park number that should be the square root of the mean of the variable you are trying to sqrt.
I am attaching my code. Now, $SomeNumber is a number from my experiment, that I want to get a square root from. It is important in this stage to have some idea about the size of this number... What I did was I had done an experiment couple of times and found a mean of $SomeNumber. Once you know the mean, you take its square root (using any calculator) because you will use this as an inital guess from which you will eventually come to your desired number.
set $AnyNumber $SomeNumber set $seed 90000 #This was the sqrt of the mean of the $SomeNumber variable from my pre-tests set $sqrt1 expression $AnyNumber/$seed set $firstAPP expression ($seed+$sqrt1)/2 #Now, this is my first approximation (which is not yet precise enough) set $sqrt2 expression $AnyNumber/$firstAPP #But, it is more precise than my initial guess set $secondAPP expression ($firstAPP+$sqrt2)/2 #Now, you just rinse and repeat. I got my sqrt after the fifth iteration set $sqrt3 expression $AnyNumber/$secondAPP #but, you can copy-paste the code to have as many as you like. set $thirdAPP expression ($secondAPP+$sqrt3)/2 #The fifth was usually off by one or two (we are talking about numbers set $sqrt4 expression $AnyNumber/$thirdAPP #bigger than 10000, so it didnt matter much was it 89478 or 89476... set $fourthAPP expression ($thirdAPP+$sqrt4)/2 set $sqrt5 expression $AnyNumber/$fourthAPP set $fifthAPP expression ($fourthAPP+$sqrt5)/2 set $sqrtSomeNumber $fifthAPP #I have used this code at the end of the task definition, and then I can save $fifthAPP as my sqrt It is complicated, but it is also quite cool, if you ask me Best of luck, Matia
|
|