How to calculate arctan in netlogo? - netlogo

NetLogo doesn't seem to define a function for arctan? I am correct or does there exist any function for this. Is there any library which has this function?

It has the more practical two-argument atan function. It's like the atan2 function you see in a lot of programming languages. Note that it uses NetLogo's angle orientation, so everything is flipped around the 45º line (up is 0º, right is 90º). If you want standard atan2, use atan y x instead of atan x y.

Related

ParaView Calculator atan2

I'm currently trying to convert given Cartesian coordinates (x,y,z) to spherical coordinates (r, theta, phi) using the ParaView Calculator-Filter, where theta is the polar angle and phi the azimuthal angle.
This I want to do over a domain of a quarter-sphere:
(r in [r_inn, r_out], theta in [0, pi], phi in [0, 2pi].
So far I defined the following result variables that give the expected results:
r = sqrt(coordsX^2 + coordsY^2 + coordsZ^2)
theta = acos(coordsZ/r)
For the azimuthal vector I'm aware that I would have to take care of the quadrant of (x,y) when using
phi = atan(y/x).
This usually is achieved using an extra function like atan2 in C. Such a function does not seem to be supplied by the Calculator Filter or the Python Calculator Filter.
Is there any easy way to implementing something like the atan2 using the graphical interface?
Any comments are much appreciated, thanks!
UPDATE:
After Neil Twist pointed out, that in the Python Calculator the inverse tangent function can be called as arctan2(y, x), I'm now facing the problem that I can't access the coordinates of a cell via the variables coordsX/Y/Z, that are available in the simple Calculator filter.
Now, the question is: How can I access the cell coordinates in the Python calculator?
You can use the numpy extensions of the Python Calculator in ParaView, but numpy have called the function arctan2 rather than atan2.
There are numpy docs for trigonometric functions, but annoyingly you can't use all the functions directly, for example you can do arctan2(x1, x2), but you can't do pi and have to use numpy.pi.
For context, there are PythonCalculator docs too.
To access the coordsX and coordsY is a bit tricky, but can be achieved using the points variable. This is actually an array of all the points, each of which is an array of x, y and z coords.
To use the co-ordinates you need to extract them like so:
[point[0] for point in points]
[point[1] for point in points]
[point[2] for point in points]
So to use the arctan function with Y and X coords, you could do the following:
arctan2([point[1] for point in points], [point[0] for point in points])
Update:
After a little more investigation, there may be a slightly nicer way to get coordsX/Y/Z:
points[:,0]
points[:,1]
points[:,2]
giving
arctan2(points[:,1], points[:,0])
Another helpful reference is the numpy_interface algorithms.

How long does it take for a curve too reach a specific x coordinate?

How do I calculate the time it takes for a curve to reach a specific x coordinate (in Matlab). Let's say we have:
dx/dt = x^2 + y^2 and dy/dt = 5.x.y and the curve starts at the point (a,b). With help from ode45 I was able to get the figure of the curve. I need too calculate the time it takes for the curve too reach x = c, (c>a). I've been told that this can be done by interpolation, but I have no idea how to write the code.
Depending on the behavior of your system around c, using data interpolation methods such as interp1 on the output may or may not work. The more rigorous way to solve this is either with events (see my answers here or here) or by using the single structure output argument form of ode45 in conjunction with deval and regular data interpolation methods. Both of these use polynomial interpolation methods designed to work with the underlying ODEs. Though more complicated, events are probably the best way to accuratly determine crossing times like your case.

Why does my MATLAB function not agree with my calculator?

I am trying to make a function for MATLAB and I just don't see how to set it up right. I set up the function and then I do it manually with a calculator and I seem to get a different answer. I am sure it is the way I set it up. If someone can point out the problem I would greatly appreciate it.
I am trying to make the following function:
x*e^(cos(x))
what I wrote in MATLAB:
function y = fun2(a)
y = a*exp(cos(a))
end
Your code is just fine. The most likely explanation is that your calculator is in deg mode and so treats the argument to cos as an angle measured in degrees. Computer code like MATLAB code always use radians rather than degrees. Switch your calculator to rad mode and the two evaluations will agree.

Differentiable approximation of the round() Matlab function

Could you possibly suggest a differentiable approximation for the round Matlab function?
The round function looks like this:
I would like a differentiable function which [closely] resembles the one in the figure.
You could write the derivative using the dirac delta - it's called 'dirac comb'.
Also you can write the round function as sum of the Heaviside step functions.
for both there are many approximations - just select a differentiable one.
Well, y=x is differentiable and, if you stand far enough back, looks exactly like your target function. It does differ from round in that it maps reals to reals rather than reals to integers.
I don't think it makes sense to talk about the differentiability of functions from the reals to the integers for anything other than trivial functions such as f(r) = 1 for all r in Reals.
You can probably get something closer to the step function by a judicious choice of trigonometric function. For example, this
is a plot of sin(x)+x. If I had more time I could probably tweak it to pass through (k,k) for all integers k, but I haven't so I'll leave that to you or other passers-by.
The derivative of your function is 1 almost everywhere. If possible merely use 1.
If you really need to make the function smooth, consider using a low-pass filter.

How do I do numerical integration of a vector in MATLAB?

I have a vector of 358 numbers. I'd like to make a numerical integration of this vector, but I don't know the function of this one.
I found that we can use trapz or quad, but i don't really understand how to integrate without the function.
If you know the horizontal spacing of your vector, you can use trapz in order to integrate it without the function. For example, to integrate y=sin(x) from 0 to pi with 358 sections,
x=0:pi/357:pi;
y=sin(x);
area=trapz(x,y);
If you just use trapz(y), you'll get a much larger number, since the default distance between points is assumed to be 1. This problem can be fixed by multiplying by the distance between x points:
area=pi/357*trapz(y);
You don't need to know the function in order to numerically integrate; that's the point of trapz and quad. Just pass trapz your vector. Here's a link to the documentation.
Think about integration as to find area under the curve, which is formed by your vector. Well it's not actually a curve, but polygonal chain. What TRAPZ function is doing, it finds sum of areas of each trapezoids formed by every two neighbor points in your vector and their projection on X axis. See the function documentation, if you have uneven distance between your points or if distance not equal one.
You can read more about this method, for example, on Wikipedia.