You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Extend line plot to a Surface
9 views (last 30 days)
Show older comments
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
Accepted Answer
kjetil87
on 23 Jul 2013
Edited: kjetil87
on 23 Jul 2013
x=10:-1:1; % your 2d_line
x3d=repmat(x,[numel(x),1]);
surf(x3d);
22 Comments
kjetil87
on 23 Jul 2013
Edited: kjetil87
on 23 Jul 2013
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.
Mazhar
on 24 Jul 2013
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.
kjetil87
on 24 Jul 2013
Edited: kjetil87
on 24 Jul 2013
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
on 24 Jul 2013
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
on 25 Jul 2013
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
on 25 Jul 2013
Edited: kjetil87
on 25 Jul 2013
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
on 26 Jul 2013
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
on 26 Jul 2013
Edited: kjetil87
on 26 Jul 2013
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)
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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)