Hello all,
I have the following code:
a = 0:.1:1;
b = 0:.01:.1;
for k = 1:length(a)
c = a(k) + b(k);
end
I would like to plot 'c' versus 'a' and 'b' using 'surfl'; Is there a way to do it? thanks.

답변 (3개)

Grzegorz Knor
Grzegorz Knor 2011년 11월 29일

0 개 추천

[a b] = meshgrid(0:.1:1,0:.01:.1);
c = a + b;
surfl(a,b,c)

댓글 수: 1

Walter Roberson
Walter Roberson 2011년 11월 29일
Bahareh's previous question was specifically asking about combining a(K) and b(K) instead of producing a grid, so it seems your code (fine in most circumstances) is not what is desired _here_

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2011년 11월 29일

0 개 추천

a = 0:.1:1;
b = 0:.01:.1;
c = a + b;
t = nan(length(a),length(a));
z = diag(c) + tril(t,-1) + triu(t,1); %defined on diag, else nan
surfl(a,b,z);
I would not expect the plot to have any useful content, though. surface plots do interpolation from vertices to determine what to draw, but you are only defining the result along a line.
I will not suggest that you want the entire grid of a(J) + b(K) values as that would not be in accordance with your previous question.
Albert J. G.
Albert J. G. 2019년 3월 17일

0 개 추천

This question is about how to plot a surface diagram.
The data are in an excel file, as you can find.
My question is how to plot a surface diagram using data from A2 to C72? (if you think data is no enough, you can use C491)
Maybe x=kp; y = ki; z = delay. What I need is like surf(x,y,z), and it will give a surface plot with NO ERROR (this is the most important, I think).
This is the question borthering me for a long time.
BTW, it not hard to plot a 3D scattor diagram, using:
[num, txt]= xlsread('rank.xlsx', 'A2:C72');
x = num(:,1);
y = num(:,2);
z = num(:,3);
% the point on 3d diagram:
plot3(x,y,z,'.','color','black','markersize',12)
xlabel('kp')
ylabel('ki')
zlabel('delay')
grid on
But I really donnot how to plot a surface since Z do not have a obvious relation with X and Y.

댓글 수: 11

Walter Roberson
Walter Roberson 2019년 3월 18일
See scatteredInterpolant() or griddata()
Albert J. G.
Albert J. G. 2019년 4월 5일
편집: Albert J. G. 2019년 4월 5일
Thanks for replying.
I tried this but it seems doesn't work.
The data I have is more like scatter so it's a soild one.
The question is how can i draw a perfect surface plot with MATLAB.
p.s. 1. the updated excel file is attached; 2. For the surface plot we have x-axis: kp, y-axis: ki, z-axis: delay; 3. If you can show the source code and the diagram, i will be grateful!
Walter Roberson
Walter Roberson 2019년 4월 5일
편집: Walter Roberson 2019년 4월 5일
num = xlsread('rank.xlsx', 'A2:C491');
x = num(:,1);
y = num(:,2);
z = num(:,3);
xv = linspace(min(x), max(x));
yv = linspace(min(y), max(y));
[X, Y] = meshgrid(xv, yv);
Z = griddata(x, y, z, X, Y);
surf(X, Y, Z, 'edgecolor', 'none');
Albert J. G.
Albert J. G. 2019년 4월 6일
Thanks for answering!
However, there is a big mistake in your answer: the program didn't show the graph when delay (z-axis) is bigger than 0.11 sec. (The graph should show a full data till 0.21 sec (z-axis).)
A guess is that the program average the data of z-axis.
Is there any way to solve it?
Walter Roberson
Walter Roberson 2019년 4월 6일
You have a lot of duplicated data points. You only have 20 different x, and only 10 different y. I do not know what a "surface plot with NO ERROR" means to you.
Albert J. G.
Albert J. G. 2019년 4월 6일
It should be like a three-dimensional trapezoid, as shown in the attached file.
I just cannot find a way get it using MATLAB.
Walter Roberson
Walter Roberson 2019년 4월 6일
Your data is not compatible with a plot that looks like that.
num = xlsread('rank.xlsx', 'A2:C491');
x = num(:,1);
y = num(:,2);
z = num(:,3);
xv = linspace(min(x), max(x));
yv = linspace(min(y), max(y));
[X, Y] = meshgrid(xv, yv);
[ux, ~, xidx] = unique(x);
[uy, ~, yidx] = unique(y);
zz = accumarray([xidx,yidx],z(:),[], @(v) max(v), nan);
mask = ~isnan(zz);
ind = find(~isnan(zz));
[xind,yind] = ind2sub(size(zz),ind);
xux = ux(xind); yuy = uy(yind); zuz = zz(mask);
Z = griddata(xux,yuy,zuz,X,Y);
surf(X,Y,Z,'edgecolor','none')
Albert J. G.
Albert J. G. 2019년 4월 6일
편집: Albert J. G. 2019년 4월 6일
I have to mention that I have updated my excel file (named rank.xlsx below).
num = xlsread('rank.xlsx', 'A2:C10745');
The graph is more and more perfect, but it seems there are a lot of sawtooths, which the attached file shows, around kp (x-axis) smaller than 50 when using the following codes you give:
num = xlsread('rank.xlsx', 'A2:C10745');
x = num(:,1);
y = num(:,2);
z = num(:,3);
xv = linspace(min(x), max(x));
yv = linspace(min(y), max(y));
[X, Y] = meshgrid(xv, yv);
[ux, ~, xidx] = unique(x);
[uy, ~, yidx] = unique(y);
zz = accumarray([xidx,yidx],z(:),[], @(v) max(v), nan);
mask = ~isnan(zz);
ind = find(~isnan(zz));
[xind,yind] = ind2sub(size(zz),ind);
xux = ux(xind); yuy = uy(yind); zuz = zz(mask);
Z = griddata(xux,yuy,zuz,X,Y);
surf(X,Y,Z,'edgecolor','none')
Walter Roberson
Walter Roberson 2019년 4월 6일
griddata has to do interpolation. Your data has some location with a single z of 0 and beside it locations with multiple z at higher values. So you end up with adjacent high and low locations when you do the maximum value projection. Interpolation for that turns out sawtooth.
Walter Roberson
Walter Roberson 2019년 4월 6일
Perhaps you would prefer a different approach:
num = xlsread('rank.xlsx', 'A2:C10745');
x = num(:,1);
y = num(:,2);
z = num(:,3);
K = boundary(x, y, z, 1);
trisurf(K, x, y, z);
Albert J. G.
Albert J. G. 2019년 4월 6일
Wow! That works!! Thx!!!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

태그

질문:

2011년 11월 29일

댓글:

2019년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by