MATLAB Answers


How to draw a 3D graph with constraints?

DL 님이 질문을 제출함. 27 Oct 2019
최근 활동 DL 님이 댓글을 추가함. 28 Oct 2019
I want to draw a 3D graph for a function with several contraints.
For example, f(x,y)=1-0.5*x-0.7*y such that 0<=x<=1, 0<=y<=1, 0<=x+y<=1.
Since I am a beginner of using MATLAB, so any help would be very appreciated.

  댓글 수: 0

로그인 to comment.

답변 수: 1

John D'Errico 님의 답변 27 Oct 2019
 채택된 답변

The simple answer is to just use meshgrid.
[x,y] = meshgrid(linspace(0,1,100));
keepind = (x + y) <= 1;
x(keepind) = NaN;
y(keepind) = NaN;
f = 1 - .5*x - .7*y;

  댓글 수: 2

DL 27 Oct 2019
Thank you so much, John!! It works great!
DL 28 Oct 2019
One more question.
When the graph, f(x,y) is drawn, I would like to have a different color gradation for the region f(x,y)>=0 vs. f(x,y)<0.
For example, for the region f(x,y)>=0 is a blue-color gradation, but a red one for the region where f(x,y)<0.
Can you kindly help me out?
Thank you.

로그인 to comment.

Translated by