이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
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');
kjetil87
2013년 7월 25일
also i think you can leave either y3d or x3d in 2d, but it really doesnt matter.
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 (한국어)