mesh or surf from x y z coordinates

13 次查看(过去 30 天)
I'm not experienced at all with meshes or 3d plotting, please be patient.
I have a firefly algorithm that optimizes pid paramters given a transfer function.
I would like to visualize how the swarm moves through the iterations, i tried with scatter3 and it works decently still i would prefer a better visualization so i'm trying to understand if surf and/or mesh are viable options.
The output data of the swarm is in the swarm.mat file, basically a nested cell array where the each cell represents a firefly has 2 nested cells, one with coordinates (the 3 pid parameters) and one with fitness evaluation:
{1×2 cell} -> {[35.3854 38.5853 45.3113]} {[0.0910]}
i would like to create a mesh out of the coordinates, first statically from the .mat i'm sharing, then i'll loop it on my own for each iteration creating a gif (already did it with the scatter version).
Is creating a mesh something doable with the data i have? if so, i would really appreciated some hints.
  2 个评论
Luca Ferro
Luca Ferro 2023-2-8
both @Voss and @Star Strider answer perfectly my question. I think that for this project i'll go for the mesh instead of the surf due to it's primitive chart properties so that's why i accepted the mesh answer and not the surf one.
Luca Ferro
Luca Ferro 2023-2-8
Fyi this is the gif i managed to get from it :)

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2023-2-7
I am not certain what you want.
One approach —
LD = load(websave('swarm','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1287560/swarm.mat'))
LD = struct with fields:
ans: {1×40 cell} bhTf: [1×1 tf] dcTf: [1×1 tf] f: {1×40 cell} ffBest: {[8.1415 9.1602 2.9251] [0.0191]} hvTf: [1×1 tf]
% ANS = LD.ans
xyz = cell2mat(cat(1,LD.ans{:})) % Create MAtrix From Cell Arrays
xyz = 40×4
91.0537 84.7324 86.2379 0.2784 175.0091 182.4063 174.8000 5.2681 133.3574 141.3248 149.6582 1.7180 105.0710 90.8758 98.6591 0.4056 156.7677 151.8562 141.8196 1.7975 25.9244 20.1691 29.4020 0.0295 194.0592 198.0614 192.5378 22.3368 153.8176 147.2895 146.1314 3.4201 39.0956 55.6750 51.6562 0.1623 209.8560 191.6183 193.3051 23.6999
xv = linspace(min(xyz(:,1)), max(xyz(:,1)), size(xyz,1)); % Linear Vector For Interpolation
yv = linspace(min(xyz(:,2)), max(xyz(:,2)), size(xyz,1)); % Linear Vector For Interpolation
[X,Y] = ndgrid(xv, yv); % Create Grid Matrices
F = scatteredInterpolant(xyz(:,1), xyz(:,2), xyz(:,3)); % Create Interpolation Function
Z = F(X,Y); % Interpolate
figure
surfc(X, Y, Z)
xlabel('X')
ylabel('Y')
zlabel('Z')
colormap(turbo)
colorbar
view(-60,25)
See the documentation on scatteredInterpolant for details on the interpolation.
.

更多回答(1 个)

Voss
Voss 2023-2-7
Maybe something like this:
load swarm
data = vertcat(f{:});
data = cell2mat(data(:,1))
data = 40×3
35.3854 38.5853 45.3113 52.8554 65.7013 68.9522 197.9251 200.1266 197.5666 145.8464 147.2955 142.6821 16.4811 18.2823 25.7062 141.9191 133.8020 143.5314 124.4413 114.8835 120.5723 74.6623 68.1428 74.7161 187.6630 198.0871 195.7163 13.2538 21.4821 21.1957
I = scatteredInterpolant(data(:,[1 2]),data(:,3));
[pid1,pid2] = meshgrid(unique(data(:,1)),unique(data(:,2)));
surf(pid1,pid2,I(pid1,pid2))

类别

Help CenterFile Exchange 中查找有关 Particle Swarm 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by