Add surface based on two vectors in 3D plot

조회 수: 1 (최근 30일)
Snoopy
Snoopy 2018년 10월 6일
편집: jonas 2018년 10월 6일
In the code below, my aim is to mark the span spanned by the vectors x1 and x2. So I need to add a 2D surface in the 3D plot indicting this span which needs to visually contain the two vectors - so that I can give a sense of what a span is to the students. I tried this with the mesh function but apparently something is wrong. Is my approach wrong? How can I draw this surface?
t = [0 0 0]';
x1 = [1 2 1]';
x2 = [3 1 1]';
y = [3 3 4]';
X = [x1 x2];
B_hat = inv(X'*X)*X'*y;
plot3([t(1) x1(1)],[t(2) x1(2)],[t(3) x1(3)])
hold on
plot3([t(1) x2(1)],[t(2) x2(2)],[t(3) x2(3)])
axis([-1 5 -1 5 -1 5])
grid on
hold on
x1_fit = min(x1):1:max(x1);
x2_fit = min(x2):1:max(x2);
[x1_FIT,x2_FIT] = meshgrid(x1_fit,x2_fit);
y_FIT = B_hat(1)*x1_FIT + B_hat(2)*x2_FIT;
mesh(x1_FIT,x2_FIT,y_FIT)

채택된 답변

jonas
jonas 2018년 10월 6일
편집: jonas 2018년 10월 6일
It's not trivial to make a triangular surface from three points in space, at least not from the mesh function. I would probably just use a patch.
patch('xdata',[t(1) x1(1) x2(1)],'ydata',[t(2) x1(2) x2(2)],'zdata',[t(3) x1(3) x2(3)])

추가 답변 (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