Distance between two moving particles

6 Ansichten (letzte 30 Tage)
Lee Quan
Lee Quan am 6 Jun. 2017
Bearbeitet: Lee Quan am 9 Jun. 2017
I'm new to programming and just started learning MATLAB last week. If anyone could help me out, I'd very much appreciate it.
Let's say I have two particles moving with constant speeds along straight line paths in 3-D space (figure enclosed). For each time step of 0.1 second, I need to calculate the distance between the moving particles. Is there any code available for this?

Akzeptierte Antwort

John D'Errico
John D'Errico am 6 Jun. 2017
Bearbeitet: John D'Errico am 6 Jun. 2017
Perhaps we need to look at this differently. You have two particles, I'll call them P and Q.
P and Q are moving along two straight line trajectories in 3 dimensions. They have their own velocities, but those velocities are fixed. So as a function of time, what is the distance between the two particles? Can we write this as a function?
Each line is defined by a starting point and a velocity vector. Each of those are vectors themselves, of length 3. So, I'll pick some random lines.
P0 = [1 2 3];
Q0 = [5 4 2];
Pvel = [0.1 0.2 0.1];
Qvel = [-0.25 -0.3 0.15];
The lines P and Q are defined simply enough as functions of time.
P = @(t) P0 + t(:)*Pvel;
Q = @(t) Q0 + t(:)*Qvel;
The functions work if you have the current release of MATLAB. On older releases, those last two lines will fail, so I would have had to write them as:
P = @(t) bsxfun(@plus,P0,t(:)*Pvel);
Q = @(t) bsxfun(@plus,Q0,t(:)*Qvel);
The latter form will work in any release.
How about the distance between the two points, as a function of time?
Dpq = @(t) sqrt(sum((P(t) - Q(t)).^2,2));
So now lets look at the lines.
t = 0:.1:20;
P_t = P(t);
Q_t = Q(t);
plot3(P_t(:,1),P_t(:,2),P_t(:,3),'r-');
hold on
plot3(Q_t(:,1),Q_t(:,2),Q_t(:,3),'b-');
box on
grid on
So, two lines. They are general, skew lines.
plot(t,Dpq(t))
The distance between them should be an arc of a hyperbola. Easy enough to show that is true:
syms T
simplify(((P0 + T*Pvel).^2 + (Q0 + T*Qvel).^2)*ones(3,1))
ans =
(47*T^2)/200 - (27*T)/10 + 59
This is the SQUARE of the distance. So effectively, we have this equation
D^2 = (47*T^2)/200 - (27*T)/10 + 59
relating distance (D) between the lines as a function of time (T). You should be able to convince yourself that this is the equation of a hyperbola. As T approaches infinity in either direction, the curve approaches a straight line as an asymptote.
Now, I'm not totally sure what you are looking for in all of this. But if your goal is merely to compute distance between the points at any time t, Dpq(t) does exactly that for you. It is vectorized, so if you have a vector of times in t, it will generate a vector of distances.
Perhaps this is what you are looking for?
  2 Kommentare
Lee Quan
Lee Quan am 9 Jun. 2017
Bearbeitet: Lee Quan am 9 Jun. 2017
Let's say moving particle A is our vehicle and moving particle B is an incoming obstacle (similar to the ones shown in my figure). Both move with constant velocities (constant speed and direction). This has to be shown as an animation.
My intention is to create a type of proximity sensor for moving particle A. Let's assume the sensor has a range of 10 m and the distance between the moving particle A and the moving obstacle B is d. The moment when d is less than or equal to 10 m, A is alerted of the presence of incoming obstacle B. This is pretty much it.
Lee Quan
Lee Quan am 9 Jun. 2017
Bearbeitet: Lee Quan am 9 Jun. 2017
I would also like to thank you, John D'Errico, for taking the time to answer my question. I never expected to receive such detailed, well thought out of answers.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

KSSV
KSSV am 6 Jun. 2017
Let A, B be your points with locations/ coordinates (x1,y1,z1) and (x2,y2,z2) respectively. You can calculate the distance using:
EuclidDistance = sqrt((x1-x2)^2 + (y1-y2)^2 + (z1-z2)^2);
Eg:
A = rand(1,3) ;
B = rand(1,3) ;
EuclidDistance = sqrt((A(1)-B(1))^2 + (A(2)-B(2))^2 + (A(3)-B(3))^2)
  4 Kommentare
Image Analyst
Image Analyst am 6 Jun. 2017
I read it as he wants the distance "distance between the moving particles" "For each time step" meaning he wants a bunch of distances. Granted, he made it confusing by saying "shortest distance" when in fact, for a particular time point there is only ONE distance between particles, not multiple distances from which we could extract a shortest.
Now if he wanted the shortest distance between paths (past trajectories), then there could be a shortest distance, and you could get that distance, along with the time point (element) at which it occurred using min:
[minDistance, indexOfMin] = min(EuclidDistance);
Lee Quan
Lee Quan am 6 Jun. 2017
Bearbeitet: Lee Quan am 6 Jun. 2017
Yup, right you are Image Analyst. Sorry for the confusion. For each time step, I need the distance between the 2 particles. So for 10 time steps, we would have 10 values for distance.

Melden Sie sich an, um zu kommentieren.


John D'Errico
John D'Errico am 6 Jun. 2017
Bearbeitet: John D'Errico am 6 Jun. 2017
"Is there any code available to do this?"
No. Why should there be? This is not a terribly difficult question of mathematics, or something that requires a toolbox because it would get a huge amount of use. You could look on the file exchange, but you never know the quality of what you find there. :)
Actually, I did look on the FEX. Not sure why you did not look. I did, and found this code. I've seen worse code, and although it has some limitations, it might do what you asked. Apparently there is a problem if one or both of the segments are of zero length. Truly high quality code would watch for problems like that. But it will probably work. It does apparently return the points of closest approach as an output.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by