how to plot 3 different vectors without any relation, in a 3d plot
2 views (last 30 days)
Show older comments
Payam Morsali
on 8 Jul 2022
Commented: Star Strider
on 8 Jul 2022
Hi everyone,
I want to plot 3 vectors in a same 3D plot; (plot3 and meshgrid-surf are not working for my case).
let's say three vectors are as follows:
X=[1 2 3]
Y=[4 5 6]
Z=[11 22 33]
now Z(1) corresponds to X(1) and Y(1); and so on.
plot3 does not give my desired plot; and meshgrid is not applicable since there is no simple function between Z and X,Y.
for j=0:60
for i=0:60/j
R=[i, j, optmizie(fcn)]; %this one fetchs a lot of other functions so that's why I cannot use meshgrid
end
end
I want to have i and j as X and Y axises and R az the Z axis without specifying the relation among them.
Thanks in advance
0 Comments
Accepted Answer
Star Strider
on 8 Jul 2022
I am not certain what you want, so my best guess —
X=[1 2 3];
Y=[4 5 6];
Z=[11 22 33];
xv = linspace(min(X), max(X), 5);
yv = linspace(min(Y), max(Y), 5);
[Xm,Ym] = ndgrid(xv, yv);
Zm = griddata(X,Y,Z,Xm,Ym);
figure
surf(Xm, Ym, Zm)
grid on
This is not working with the provided data, however since this may be a ‘proxy problem’, it will likely work with your actual data.
.
2 Comments
Star Strider
on 8 Jul 2022
If you do not want to plot a surface, this is straightforward —
X=[1 2 3];
Y=[4 5 6];
Z=[11 22 33];
cm = colormap(turbo(numel(Z)));
[~,C] = sort(Z);
figure
stem3(X, Y, Z, 'Marker','none')
hold on
scatter3(X, Y, Z, 75, cm(C,:), 'filled')
plot3(X, Y, Z)
hold off
grid on
xlabel('Velocity')
ylabel('Acceleration')
zlabel('Fuel Cost Function')
I chose stem3 because it gives a bit more information than scatter3 (although they can be used together) and possibly with plot3 as well. I combined all of them here. My code will adapt to any number of points. Choose the appropriate colormap for your application. (The turbo colormap was introduced in R2020b.)
.
More Answers (0)
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!