Extend line plot to a Surface
15 次查看(过去 30 天)
显示 更早的评论
Mazhar
2013-7-23
Hey, I am trying to plot a surface but am struggling to find the right commands. I have a 2D plot of a line going diagonally down. I want to extend this line to the 3rd dimension, so that it creates a surface. Is it possible to do this in MatLab? What commands can I use. Thanks
采纳的回答
kjetil87
2013-7-23
编辑:kjetil87
2013-7-23
x=10:-1:1; % your 2d_line
x3d=repmat(x,[numel(x),1]);
surf(x3d);
22 个评论
Mazhar
2013-7-23
Looks like this does the job :) Thanks. Could you please quickly explain how the 'repmat' and 'numel' commands work?
kjetil87
2013-7-23
编辑:kjetil87
2013-7-23
repmat basicly repeats your vector x times. i.e
x=[1,2];
repmat(x,[2,1])
ans =
1 2
1 2
repmat(x,[1,2])
ans =
1 2 1 2
The numel command just counts the number of elements in x. you dont really have to use the umel command you can specify a different numer if you want like i did above.
kjetil87
2013-7-23
also im not sure if i did the dimensions correcly to make it comply with what you needed, try
hold on;
xlabel('x')
ylabel('y')
zlabel('z')
To make sure it ended up as inteted. Otherwise try to
transpose
the matrix
Mazhar
2013-7-23
Sorry stuck again! I've just tried to make this work for my data and could not work it.
My data for plotting the line is; x=[0 1 2 3 4 5]; y=[5 3 2.5 2 1.5 0];if true
How would you do it for this set of data?
Mazhar
2013-7-24
This problem is killing my brain!
So I am trying to achieve this: I have 2 line graphs;
in (x,z) plane: x=[0 1 2 3 4 5]; z=[5 3 2.5 2 1.5 0];
in (x,y) plane: x=[0 2 3 4 4.5 5]; y=[5 3 2.5 2 1.5 0];
I want to extend both these line graphs in the third dimension and plot a line where they intersect.
For plotting the line of intersection I would need to use something like this:
% Take the difference between the two surface heights and find the contour
% where that surface is zero.
zdiff = z1 - z2;
C = contours(x, y, zdiff, [0 0]);
% Extract the x- and y-locations from the contour matrix C.
xL = C(1, 2:end);
yL = C(2, 2:end);
% Interpolate on the first surface to find z-locations for the intersection
% line.
zL = interp2(x, y, z1, xL, yL);
% Visualize the line.
line(xL, yL, zL, 'Color', 'k', 'LineWidth', 3);
But I am struggling with the first part.
Thanks again for all your help.
Mazhar
2013-7-24
Yeah!
But thinking about it now I will probably also struggle to do the second part as my surfaces aren't defined by equations so can't really solve for zdiff :(
Mazhar
2013-7-24
Also, I posted the question again with a bit more detail and an image to explain the problem.
Please have a look at it and see what you would suggest.
kjetil87
2013-7-24
编辑:kjetil87
2013-7-24
hm.. well, the plot i gave you earlier were`nt entirely correct.
x=[0 2 3 4 4.5 5];
y=[5 3 2.5 2 1.5 0];
z=0:5;
x3d=repmat(x,6,1);
y3d=repmat(y,6,1);
z3d=repmat(z,6,1).';
figure;surf(x3d,y3d,z3d); % now it is the correct plane.
%then the x-z line:
x=[0 1 2 3 4 5];
z=[5 3 2.5 2 1.5 0];
y=0:5;
x3d2=repmat(x,6,1);
y3d2=repmat(y,6,1).';
z3d2=repmat(z,6,1);
hold on;
surf(x3d2,y3d2,z3d2);
xlabel('x');ylabel('y');zlabel('z');
Atleast now you can visualize the surfaces. As for the interpolation and intersection i think maybe it should be possible if you interpolate each vector before using repmat (you will probably need to change the "6" in repmat also then). the intersection should then be where the values in all 3 matricies (x3d==x3d2 etc) are the same (within some limit )
Mazhar
2013-7-24
Thank you so much, you really came through kjetil87.
I see what you mean about the interpolation part. I'll have a go at that now. Please don't mind if I get stuck at that too and come crawling back to you :P
Thanks again.
Mazhar
2013-7-25
The code is working perfectly. I tried it for my larger set of data and it works there too.
Only one more thing! The length of z has to be the same as the length of the other two variables, in order for the dimensions to match for the surf command. Is there a way to not be limited in how far I can extend the plot? i.e. z=0:100;!
kjetil87
2013-7-25
编辑:kjetil87
2013-7-25
Cool , nice to hear it works!
Yes,in
surf(x,y,z)
the lengths must be equal to length(x)=n and length(y)=m , where [m,n]=size(z). Or atleast thats what the instruction says (help surf). But according to "surfchk" in "surf", size(x) must be equal to size(z) , and size(y) must be equal to size(z) .
so :
x=[0 2 3 4 4.5 5];
y=[5 3 2.5 2 1.5 0];
z=0:99;
zn=length(z);
x3d=repmat(x,zn,1);
y3d=repmat(y,zn,1);
z3d=repmat(z,6,1).';
figure;surf(x3d,y3d,z3d);
xlabel('x');ylabel('y');zlabel('z');
Mazhar
2013-7-25
Great! Now I can always have my surfaces cutting through!
Now to move forward to line of intersection.
Thanks again :D
Mazhar
2013-7-26
Hey, me again!
Having trouble trying to get the intersection line! I have been looking at interpolations I can't find anything good for 3D that I could use for my problem!
One thing I am thing might work, is if I check the x3d, y3d, z3d matrices for the two plots for equal values! but not sure if that would give me what I'm looking for!
You have any suggestions?
kjetil87
2013-7-26
编辑:kjetil87
2013-7-26
Im not a 100% sure if it will work, but e.g as of now there is two common points that are given in the vectors namely ,
(x3d==x3d2 & y3d==y3d2 & z3d==z3d2 )
what you need to do is to interpolate x, y and z before using repmat so your resolution is increased , then instead of x3d==x3d2 you can use
abs(x3d-x3d2)< tol
where tol is a very small number.
(so then you can use interp1 since you are not interpolating in 3d)
Mazhar
2013-7-26
Ok, I see what you mean! Trying it out just now and it seems to be working so far!
Hopefully can get this section done and over with now!
Mazhar
2013-8-2
kjetil87, Thank you very much for all your help!
that's all of my problem sorted now. I have a nice plot of the line intersecting the the two surfaces :D
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)