Question: Hello! I am told I have an incorrect algorithm despite getting correct solution. I need to adjust code with limited guidance on what to change.
Hello! I am told I have an incorrect algorithm despite getting correct solution. I need to adjust code with limited guidance on what to change. The attached MATLAB code uses Periodic Boundary Conditions to create a map and table of nearest neighbors, that is listing the index of each point, the # of nearest neighbors NNs for that point, and then listing each of the NNs for that point. I am given a file that includes x matrix of D points where the first column of the file is the indices of each point, nd column are the xvalues of those points, rd and th columns are yvalues and zvalues, respectively. We assume that the NNs of a point are within a radial distance of units and the length of the cubic box is From my understanding, the method works as follows: First we upload the points and impinge a box containing these points. To account for PBCs we create a set of shifted boxes around every side of the original. Each box contains exact shifted replicas of the original points. There are augmentedshift boxes, for a total boxes with the original. I know I have done this part correctly. Then, we need to use these augmented points PBCs to find nearest neighbors. I need to address changes in this part of my algorithm, which are mentioned in code comments. These comments are very limited and only about WHERE I need to make changes, but little about what changes are. Can you please look at my MATLAB code, and fix mistakes and add the portion for nearest neighbor search using augmented points and shifted boxes. I am looking for code. Thank you very much in advance. I cannot use any builtin MATLAB function. I cannot attach files so I cannot provide dataset. I have the first rows pasted here so it can be tested on those. I cannot attach MATLAB file, so it is included as image and pasted. I include my results in the attached image to show what result should be I should get NNs for each value, as I am now. I include the NN map output. I am not sure what is wrong with code that would allow me to still get correct results. I was told my replication is correct but NN search is not. This person didnt look close at coderesult Please help me fix code. Thank you.
Data: xyz
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
MATLAB code with hardly helpful comments:
clear
clc
xyz loadxyztxt;
L ;
r ;
N sizexyz;
NNs cellN;
figure;
hold on;
for i :N
xi xyzi;
yi xyzi;
zi xyzi;
for dx L L
for dy L L
for dz L L
Generate shifted points in the current box
for j :N
xj xyzj dx;
yj xyzj dy;
zj xyzj dz;
add new loops here for NN search??
for i :N
for j :Nmaybe
something here??? for NN search
end
end
end
Now Calculate distance to point in the current box
check EVERY point to each other including original points
d sqrtxi xjyi yjzi zj;
if distance is within range
if d r && j ~ i
Add index j to nearest neighbors of point i
NNsiNNsi j;
plotxi xjyi yjzi zjk;
end
end
end remove any leftover ends
end
end
end
scatterxyz: xyz: xyz: 'filled',m;
hold off;
xlabelX;
ylabelY;
zlabelZ;
titleNearest Neighbor Map';
grid on;
view;
Create table for result
results cellN;
Populate table
for i :N
resultsi xyzi; Index
resultsi numelNNsi; Number of first nearest neighbors
if ~isemptyNNsi
resultsi NNsi; Nearest neighbor index
else
resultsi; No nearest neighbor found
end
end
T celltableresults 'VariableNames', Index# First Neighbors', 'First Neighbors';
dispT;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
