Clear Filters
Clear Filters

how to plot 3 different vectors without any relation, in a 3d plot

4 views (last 30 days)
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
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

Accepted Answer

Star Strider
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);
Warning: The underlying triangulation is empty - the points may be collinear.
surf(Xm, Ym, Zm)
Error using surf
Data dimensions must agree.
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.
Payam Morsali
Payam Morsali on 8 Jul 2022
Thank you for your reply,
Let me explain a little more:
What I want is a series of dots on a 3D plane (X Y Z axises) like this:
Z should be 11 for X=1 and Y=1,
Z should be 22 for X=2 and Y=2,
Z should be 33 for X=3 and Y=3,
there should be 3 dots on the 3D plane, and, Z is not a function of X and Y;
For example I want to say for the a velocity of X=1 and acceleration of Y=1, the fuel cost function is Z=11.
All the values are known (I have the values of X, Y, and I have already calculated the values of Z for each pair of X and Y).
Star Strider
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);
stem3(X, Y, Z, 'Marker','none')
hold on
scatter3(X, Y, Z, 75, cm(C,:), 'filled')
plot3(X, Y, Z)
hold off
grid on
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.)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!