필터 지우기
필터 지우기

plotting a surface between 2D curves

조회 수: 30 (최근 30일)
Chaman Srivastava
Chaman Srivastava 2021년 12월 15일
댓글: Star Strider 2021년 12월 15일
Hello Experts,
I need to fill/fill a curve between the 2D curves in 3D space. I've seen some comments with the surf command, but would the command help where the extremeties of the curve are defined? My script looks something like this for 2D curves spaced in 3D-
Hoping to get some advice on it.
clc
clear all
figure(2)
x = linspace(28,224);
y = repelem(1,100); y2 =repelem(2,100);
eq1 = 0.0645*log(x)+0.9221;
eq2 = 0.0225*log(x)+0.9441;
plot3(y,x,eq1)
hold on
plot3(y2,x,eq2)
view([35 25])
grid on
legend

채택된 답변

Star Strider
Star Strider 2021년 12월 15일
One option is a patch object for this and another is surf
figure(2)
x = linspace(28,224);
y1 = repelem(1,100); y2 =repelem(2,100);
eq1 = 0.0645*log(x)+0.9221;
eq2 = 0.0225*log(x)+0.9441;
plot3(y1, x, eq1, '-b', 'LineWidth',2)
hold on
plot3(y2, x, eq2, '-r', 'LineWidth',2)
% patch([y1 flip(y2)], [x flip(x)], [eq1 flip(eq2)], 'g', 'FaceAlpha',0.25, 'EdgeColor','none') % 'patch'
surf([y1; y2], [x; x], [eq1; eq2], 'MeshStyle','row') % 'surf'
view([35 25])
grid on
legend('eq1', 'eq2', 'Included Surface')
xlabel('X')
ylabel('Y')
I included working calls for both, so experiment to see which is best.
.
  댓글 수: 4
Chaman Srivastava
Chaman Srivastava 2021년 12월 15일
Thanks! Just out of curiosity, is it possible to create a mesh on the plot? it may look better :D
Star Strider
Star Strider 2021년 12월 15일
I considered that. It‘s slightly difficult, however not impossible.
It took a bit to figure it out. I used three different approaches before I finally got one that I liked.
The first one (figure(2)) simply draws the grid lines that surf provides in the original code.
The second (figure(3)) uses gridded interpolation techniques to construct the surface, however it also creates a connectig surface underneath the intended surface that I couldn’t get rid of, and only provide one set of grid lines as well.
The third (figure(4)) intrepolates all the matrices to create a surface, and finally does what I want! The ‘Ym’ and ‘Zm’ matrices had to be transposed from the way I originally created them in order to plot it correctly, however that code has the advantage of producing the result I want, with grid lines in both directions. I also plotted it as surfc in order to describe the surface in more detail.
x = linspace(28,224);
y1 = repelem(1,100); y2 =repelem(2,100);
eq1 = 0.0645*log(x)+0.9221;
eq2 = 0.0225*log(x)+0.9441;
xv = linspace(min(x), max(x), 25);
y1v = linspace(min(y1), max(y1), 25);
y2v = linspace(min(y2), max(y2), 25);
[Xm,Ym] = ndgrid([xv(:); xv(:)], [y1v(:); y2v(:)]);
Zm = griddata([x(:); x(:)], [y1(:); y2(:)], [eq1(:); eq2(:)], Xm, Ym);
figure(2)
plot3(y1, x, eq1, '-b', 'LineWidth',2)
hold on
plot3(y2, x, eq2, '-r', 'LineWidth',2)
% patch([y1 flip(y2)], [x flip(x)], [eq1 flip(eq2)], 'g', 'FaceAlpha',0.25, 'EdgeColor','none') % 'patch'
surf([y1; y2], [x; x], [eq1; eq2]) % 'surf'
% surf(Ym, Xm, Zm)
view([35 25])
grid on
legend('eq1', 'eq2', 'Included Surface')
xlabel('Y')
ylabel('X')
xv = linspace(min(x), max(x), 25);
y1v = linspace(min(y1), max(y1), 25);
y2v = linspace(min(y2), max(y2), 25);
[Xm,Ym] = ndgrid([xv(:); xv(:)], [y1v(:); y2v(:)]);
Zm = griddata([x(:); x(:)], [y1(:); y2(:)], [eq1(:); eq2(:)], Xm, Ym);
figure(3)
surf(Ym, Xm, Zm)
grid on
view([35 25])
xlabel('Y')
ylabel('X')
N = 25;
Xm = (ones(25,1) * linspace(28,224,N));
Ym = (ones(N,1) * linspace(1, 2, N));
k1 = linspace(0.0645, 0.0225, N);
k2 = linspace(0.9221, 0.9441, N);
Zm = k1(:).*log(Xm) + k2(:);
figure(4)
surfc(Ym, Xm', Zm')
grid on
view([35 25])
zlim([1 1.3])
xlabel('Y')
ylabel('X')
figure(5)
hsc = surfc(Ym, Xm', Zm');
hold on
hp3(1) = plot3(y1, x, eq1, '-b', 'LineWidth',2);
hp3(2) = plot3(y2, x, eq2, '-r', 'LineWidth',2);
hold off
grid on
view([35 25])
zlim([1 1.3])
xlabel('Y')
ylabel('X')
legend([hsc(1) hp3], 'Interpolated Surface', 'eq1', 'eq2', 'Location','best')
I created the matrices as square matrices, and changing‘N’ changes the number of grid lines, and accordingly the grid spacing and the surface resolution.
I’m happy with figure(4) ! Adding back the original lines creates figure(5).
.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by