I wrote a bit of code that grabs 3 points on a randomly oriented platelet to create two lines so that i can calculate the normal vector to the platelet using cross product. the normal vectors I am getting do not seem right and some of them turn out to be NaN i was wondering if someone can help me fix this. thank you!
matrix = [(1:natom)',mol_tag,x,y,z];mol_tag1 = matrix(:,2);unique_mol_tags = unique(mol_tag1);num_tags = length( unique_mol_tags );mol_normals = zeros(num_tags,3);for k = 1 : num_tags three_idx = find(mol_tag1 == unique_mol_tags(k),3,'first'); coords = matrix(three_idx,3:5); % gets the first 3 coordinates from each mol_tag grouping
p1 = coords(1,:); p2 = coords(2,:); p3 = coords(3,:); lin1 = [p2-p1]; %Creates 2 lines to form a plane
lin2 = [p3-p1]; u1 = cross(lin1,lin2); % calculate cross product to find normal vector of the plane
u = u1/norm(u1) % u(isnan(u))=0; % setting the NaN two zero doesn't seem to work either
mol_normals(k , :) = u; bqij = 0; for i = 1:3 for j = 1:3 singleij(i,j) = u(i)*u(j); end end bqij = bqij + singleij;endqij = (bqij-(1/3*(eye(3))))/(num_tags);
Best Answer