Extend line plot to a Surface

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

 Réponse acceptée

kjetil87
kjetil87 le 23 Juil 2013
Modifié(e) : kjetil87 le 23 Juil 2013

1 vote

x=10:-1:1; % your 2d_line
x3d=repmat(x,[numel(x),1]);
surf(x3d);

22 commentaires

Mazhar
Mazhar le 23 Juil 2013
Looks like this does the job :) Thanks. Could you please quickly explain how the 'repmat' and 'numel' commands work?
kjetil87
kjetil87 le 23 Juil 2013
Modifié(e) : kjetil87 le 23 Juil 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
Mazhar le 23 Juil 2013
Found out how they work! Thanks for the quick response.
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
Mazhar le 23 Juil 2013
Yep, the axis are in the correct place. Thanks again :)
Mazhar
Mazhar le 23 Juil 2013
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?
y3d=repmat(y,[numel(y),1]);
axis1=x;
axis2=1:numel(y);
surf(axis1,axis2,y3d);
kjetil87
kjetil87 le 23 Juil 2013
or maybe axis2=0:numel(y)-1; is better.
Mazhar
Mazhar le 24 Juil 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.
Narges M
Narges M le 24 Juil 2013
By "first part" you mean "extending the lines into the 3rd dimension?"
Mazhar
Mazhar le 24 Juil 2013
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
Mazhar le 24 Juil 2013
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
kjetil87 le 24 Juil 2013
Modifié(e) : kjetil87 le 24 Juil 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
Mazhar le 24 Juil 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
Mazhar le 25 Juil 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
kjetil87 le 25 Juil 2013
Modifié(e) : kjetil87 le 25 Juil 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');
kjetil87
kjetil87 le 25 Juil 2013
also i think you can leave either y3d or x3d in 2d, but it really doesnt matter.
Mazhar
Mazhar le 25 Juil 2013
Great! Now I can always have my surfaces cutting through!
Now to move forward to line of intersection.
Thanks again :D
Mazhar
Mazhar le 26 Juil 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
kjetil87 le 26 Juil 2013
Modifié(e) : kjetil87 le 26 Juil 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)
Mazhar
Mazhar le 26 Juil 2013
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
Mazhar le 2 Août 2013
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

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by