Hello, I’m trying to find a homoclinic orbit. To do this, I calculate a very long trajectory, and then calculate the minimum distance between all points of the trajectory and a fixed point. How can I optimize this?
Fragment code for this:
UPD: Correct calculate distance

You gave insufficient information on the vector field that defines your ODE. Is it a planar vector field or one in dimension d>2.
What is the dimension of the unstable and the stable eigenspace associated to the linearization of the vector field at its equilibrium?

In 2d, if p is a saddle equilibrium point, and u is the unit vector in the unstable direction, then the initial state (condition) should be p+epsilon*u, for epsilon>0, very small. In a “long” interval of integration, theoretically your orbit should return to the equilibrium point along the homoclinic orbit. But due to errors it can diverge from it. You have to perform a careful management of round off errors.
Lately have been developed methods for computation of homoclinic orbits based on interval arithmetic. But DynamicalSystems.jl hasn’t implemented yet functions for computing these invariant sets.

You asked your question a bit too early… I released a new package for this HclinicBifurcationKit.jl but it needs an update (a tag) of BifurcationKit.
In any case, what you are asking for is not an easy matter.

I was advised to check the presence of a homoclinic by searching for the minimum distance between the equilibrium state and the trajectory points, and if it is quite small, then it is a homoclinic.

I was looking for such a distance for another system where there is a homoclinic and there the distance was quite small

I found your page and read about your work, I noticed that you work with models of neurons. I deal with them too, and that’s why I wanted to ask you for advice on how best to create activity mode maps. That is, how best to determine the burst and spike modes. I have already built such maps, but my algorithm is not universal

Meanwhile you can try to get the homoclini orbit as follows: Since your saddle focus is opposite to the case of Shilnikov type saddle focus, illustrated in the attached image, you shouldd take as initial condition, p+epsilon*vs, epsilon>0
(vs is the unit vector in the stable direction) and integrate backward in time. If after a long time of integration the orbit stays close to the equilibrium point, you can conclude that your computed orbit is an approximation of the homoclinic orbit. Since you don’t know in which region is included the homoclinic orbit with respect to the unstable plane, you should also try to compute the orbit starting at p-epsilon*vs, and integrate again backward in time.

Unfortunately most references on saddle-focus refer to Shilnikov phenomenon. I couldn’t find any case similar to your.

Thanks, I try in backward + eps and - eps for all directions, and the trajectory goes to infinity. For direct time, when I use + eps for example for y, the trajectory is attracted to another attractor. As a result, I have two attractors, chaotic and regular.

No, I have a stable fixed point, then a Hopf bifurcation, as a result of which a limit cycle is born, after which a Period-doubling cascade lead into a chaos. And I’m trying to find a homoclinic orbit in chaos

You said you calculated in all directions, but it’s important to specify which direction, the stable direction or one of the unstable directions ( the unstable space is 2d). As long as the real eigenvalue is simple and negative, it follows that the stable manifold is one-dimensional. If vs is a unit eigenvector in the space corresponding to \lambda\in\mathbb{R}, \lambda<0, the trajectory in the forward direction tends to the equilibrium point, while in the backward direction along the stable manifold which eventually continues as a homoclinic orbit.

Once you have the fixed point (equilibrium point), fixp, and \lambda<0 as the real eigenvalue of the Jacobian evaluated at fixp, calculate the eigenspace corresponding to this eigenvalue. Let v_s be a vector in this eigenspace (||v_s||=1).
The backward trajectory starting at fixp+\epsilon *v_s, computed carefully, is the stable manifold associated to your saddle focus.
It seems that in your code the stable direction, v_s, is not involved.

If \lambda is an eigenvalue of a n\times n matrix, J, (here J is your Jacobian at the equilibriun point), the corresponding eigenspace is S_\lambda=\{v\in \mathbb{R}^n\:|\: Jv=\lambda v\}, i.e. is the set of corresponding eigenvectors. Since in your case \lambda <0 is a simple eigenvalue (i.e. a simple root of the characteristic polynomial), the subspace (eigenspace) S_\lambda has dimension one.
In your data posted above, v_s has the coordinates in the first column of vectors. It defines the basis in the 1-dimensional subspace S_\lambda.

Previously, I parsed parameters and phase variables in function, but then I noticed that if this is not do the time during which the system integrates is reduced. I have not tried using named tuples, I will have to see how this affects the integration time.

The thing is that earlier I calculate a two-parameter map of the spectrum of Lyapunov exponents, map of modes and orbit diagram, and all this took a decent time. Therefore, had to optimize the function as much as possible to the detriment of code readability

UPD: I still don’t know how to write fast and good code in Julia, and the algorithms I writed and used to inherit initial conditions and determine modes are not very good from an optimization point of view.