how to make this graph?
조회 수: 1 (최근 30일)
이전 댓글 표시
It is a simple doubt, but I am unable to resolve it. How can I plot these 3 functions together so that I can see them in full? do i really have to use meshgrid and contour?
[x1,x2]=meshgrid(-2:.1:4, -2:.1:4);
cost=x1.^2 + x2.^2 - 5*x1 - 6*x2 + 15;
contour(x1,x2,cost,1,'g','LineWidth',1)
figure(1)
y=2*x1.^2 + 2*x2.^2 - 3*x1 - 3*x2 - 2;
z=x1.^2 + x2.^2 - 4*x1 - 3*x2 + 8;
hold on
contour(x1,x2,y,1,'k','LineWidth',1)
contour(x1,x2,z,1,'k','LineWidth',1)
xlim([-2 4])
ylim([-2 4])
currently my result is this:

and I need it to be like this:

댓글 수: 0
채택된 답변
Steven Lord
2020년 9월 1일
When you pass a scalar positive integer value in as the fourth input to contour it is treated as the number of contour levels contour should choose and display, not as the contour levels themselves. To specify you want a single contour at a specific level, pass a two-element vector with the same value in each element as that input.
contour(x1,x2,cost,[1 1],'g','LineWidth',1)
But if I look at the minimum value of the z matrix you created, it is 1.75. That means there should not be a region in the contour plot for that set of data at level z = 1. So you're not going to be able to create the second figure you showed: there are only two circular regions, not three.
>> min(z, [], 'all')
ans =
1.75
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Contour Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!