Group Abstract Group Abstract

Message Boards Message Boards

Distance between 2 flat molecules

GROUPS:
Hi,

I have two sets of data that represent bunch of xyz coordinates of two molecules (OxF1 and TPAF1), and I need to fit these data into two planes, and then find the distance between them. I tried to do the following but I am not sure if it's right because the 3D plot I got looks a bit funny. Also, do you have any idea how to measure the distance between the two planes? I am a new user with Mathematica and your help will be greatly appreciated.

 dataTPAF1 = {{5.086735, -2.640504,
     4.650624}, {6.227440, -2.477485, 3.843544}, {6.160519, -1.731605,
     2.649288}, {4.941969, -1.164990, 2.267181}, {3.799611, -1.315889,
     3.093711}, {3.864651, -2.054553, 4.284675}, {4.609149, -0.320793,
     1.036262}, {3.147213, 0.034489, 1.303689}, {2.295200, 0.817536,
     0.531891}, {0.950277, 1.010999, 0.934789}, {0.504240, 0.379798,
     2.123730}, {1.354565, -0.416580, 2.898054}, {2.686409, -0.582108,
     2.489055}, {0.024258, 1.821306, 0.114273}, {-1.353392, 1.513264,
     0.069868}, {0.471982, 2.897945, -0.687157}, {-2.233955,
    2.208380, -0.751826}, {-0.408676,
    3.621409, -1.497147}, {-1.775026,
    3.272191, -1.550693}, {-2.675546, 3.957425, -2.411557}};


In[9]:= PlaneTPAF1 = Fit[dataTPAF1, {1, x, y}, {x, y}]
Out[9]= 2.18037 - 0.200355 x - 1.20793 y

dataOXF1 = {{0.989404, -2.326574, -0.675663}, {0.466929, \
-2.898174, 0.507492}, {-0.913869, -2.780568,
    0.804534}, {-1.736679, -2.093564, -0.078413}, {-1.209589, \
-1.521258, -1.260581}, {0.155306, -1.637212, -1.561214},
   {-3.234276, -1.809443,
    0.054719}, {-3.478451, -0.951209, -1.195212}, {-4.647861, \
-0.326679, -1.645893}, {-4.618728, 0.428750, -2.833379}, {-3.433297,
    0.549231, -3.579976}, {-2.261558, -0.087363, -3.148840}, \
{-2.289342, -0.827551, -1.957816}, {1.326341, -3.595313,
    1.453222}, {3.114413, -4.428375, 2.270776}, {2.668207, -3.758639 ,
     1.167271}, {1.006758, -4.128596,
    2.609730}, {2.180810, -4.675522, 3.140752}};


In[16]= PlaneOXF1 = Fit[dataOXF1, {1, x, y}, {x, y}]
Out[16]= -4.19445 - 0.401548 x - 1.7137 y

Show[ListPlot3D[dataTPAF1, PlotStyle -> Red],
ListPlot3D[dataOXF1, PlotStyle -> Green],
Plot3D[{PlaneTPAF1, PlaneOXF1}, {x, -7, 7}, {y, -7, 7}]]
POSTED BY: Sarah Ayoub
Answer
7 months ago
Hello, Sarah, welcome to the Wolfram Community! Please take a few minutes to read this tutorial about correct posting – especially of Mathematica code:

How to type up a post: editor tutorial & general tips

If you will not follow the tutorial, other members of community may not be able to test your code. To edit your post – click “Edit” in the lower right corner of your post.
POSTED BY: Moderation Team
Answer
7 months ago
Please pay attention to how code is posted on this site and what Moderation Team recommended to you.

I got your visualization working, but your planes are NOT parallel. So how (where) do you suggest to measure distance between them?
 pln1[x_, y_] = Fit[dataTPAF1, {1, x, y}, {x, y}];
 pln2[x_, y_] = Fit[dataOXF1, {1, x, y}, {x, y}];
 
 
 Show[
   ListPointPlot3D[{dataTPAF1, dataOXF1},
      PlotStyle -> {Directive[PointSize[Large], Red], Directive[PointSize[Large], Green]}],
   Plot3D[{pln1[x, y], pln2[x, y]}, {x, -7, 7}, {y, -7, 7},
      PlotStyle -> Opacity[.3], MeshStyle -> Opacity[.3]],
SphericalRegion -> True]

POSTED BY: Vitaliy Kaurov
Answer
7 months ago
Thanks Vitaliy Kaurov for your help! You're right the planes are not parallel! Maybe we can measure the distance between the planes by taking the closest and the farthest distances and divide by two? Do you have any idea how to do that using Mathematica?
POSTED BY: Sarah Ayoub
Answer
7 months ago
Your planes will intersect. Closest and furthest distances between them are 0 and infinity respectively. But this is not really what you want.

My guess is you want a measure of how far apart the fits are. This involves a measurement in your parameter space. One plausible way to proceed is as below.

Say the plane equations are a_j x + b_j y + c_j z + d_j == 0 for j=1,2. For a sensible measurement you could normalize each plane equation so that sum of squares of coefficients is 1. That is, divide (a_j,b_j,c_j,d_j) by sqrt(a^j^2+...+d_j^2). You now have two normalized vectors in parametrization space. You can take the distance between them.
POSTED BY: Daniel Lichtblau
Answer
7 months ago
Thanks Daniel,

I tried to normalize the plane equations using the normalize function in Mathematica and then get the distance between the planes, but it didn't give me anything (Im not sure where the problem was).   Can you give me an idea how to do this with Mathematica, or is this the wrong apporach?
POSTED BY: Sarah Ayoub
Answer
7 months ago
Normalize the coefficient vectors. If the results are two vectors c1 and c2, next take Norm[c1-c2].

I'm not sure if this answers your question. If not, it would be best if you posted the code you tried.
POSTED BY: Daniel Lichtblau
Answer
7 months ago
I am not sure if I understood you right because I still don't know how to get the distance, but here's what I meant when I tried to normalize each plane equation:
In[1]:= pln1 = Normalize[a*x1 + b*y1 + c*z1]
Out[1]= (a x1 + b y1 + c z1)/Norm[a x1 + b y1 + c z1]

In[2]:= pln2 = Normalize[a*x2 + b*y2 + c*z2]
Out[2]= (a x2 + b y2 + c z2)/Norm[a x2 + b y2 + c z2]

Also how can I include my data in the distance equetion ?
Sqrt[a (x1 - x2)^2 + b (y1 - y2)^2 + c (z1 - z2)^2]
POSTED BY: Sarah Ayoub
Answer
7 months ago
To gauge distances will need to work with coefficient vectors, not equations.

It would be very helpful if you gave background information on the full problem you are trying to solve. At this point I cannot say what would be a good way to assess distance since I'm not sure what we want distances of. Maybe you want to force the planes to be parallel (a simultaneous fit keeping x and y coefficients equal)? Hard to say without knowing something more about the actual problem at hand.
POSTED BY: Daniel Lichtblau
Answer
7 months ago
The main problem is finding the distance between two molecules. In my first post above I included their data that represent their xyz coordinates. After fitting them into two planes, we need to measure the distance between these two planes. I don't want to force the planes to be parallel...I am wondering now if I want to get the distance between their closest and furthest and divide by two, Can I choose that from the graph? Cause it's hard to find them from just looking at the data! If you want to give me any other advice ease go ahead please!
POSTED BY: Sarah Ayoub
Answer
7 months ago
Isn't it more natural to define the distance between geometric centers?
cent1 = Mean[dataTPAF1];
cent2 = Mean[dataOXF1];
dist = EuclideanDistance[cent1, cent2]
Out[]= 4.05188

If you know masses of atomes then perhaps even center of mass would be better. To visualize:
 pln1[x_, y_] = Fit[dataTPAF1, {1, x, y}, {x, y}];
 pln2[x_, y_] = Fit[dataOXF1, {1, x, y}, {x, y}];
 
 Show[
  ListPointPlot3D[{dataTPAF1, dataOXF1},
   PlotStyle -> {Directive[PointSize[Large], Red], Directive[PointSize[Large], Green]}],
 
  Plot3D[{pln1[x, y], pln2[x, y]}, {x, -7, 7}, {y, -7, 7}, PlotStyle -> Opacity[.3], Mesh -> False],
 
Graphics3D[{{PointSize[Large], Blue, Point[{cent1, cent2}]}, {Thick, Blue, Line[{cent1, cent2}]}}],

SphericalRegion -> True, BoxRatios -> 1, PlotRange -> 6, ImageSize -> 500,
PlotLabel -> Framed@Style["distance = " <> ToString[dist], 20]]

POSTED BY: Vitaliy Kaurov
Answer
7 months ago
Another, maybe very clean concept, is to measure distance between every point in 1st set and every point in 2nd set. Then average them:
Total[#/Times @@ Dimensions[#], 2]& @ Outer[EuclideanDistance, dataTPAF1, dataOXF1, 1]
Out[] = 6.240719517586845

You are then basically averaging lengths of all these segments:
Show[
ListPointPlot3D[{dataTPAF1, dataOXF1},
  PlotStyle -> {Directive[PointSize[Large], Red], Directive[PointSize[Large], Green]}],

Graphics3D[{Opacity[.2], Outer[Line[{#1, #2}] &, dataTPAF1, dataOXF1, 1]}],

SphericalRegion -> True, BoxRatios -> 1, Boxed -> False, Axes -> False]

POSTED BY: Vitaliy Kaurov
Answer
7 months ago
Sarah, do these molecules have a specific name or chemical formula? It also would be great to know are these experimental or simulation data and what type of physics or chemistry problem you are dealing with. Could you please add this information.
POSTED BY: Sam Carrettie
Answer
7 months ago