How to increase number of contour lines in a plot?

조회 수: 142 (최근 30일)
Mahmoud Abbas
Mahmoud Abbas 2022년 4월 14일
댓글: Bjorn Gustavsson 2022년 4월 14일
Hello, could you help me make this plot have 20 controur lines and show their respective values on the plot
f = @(x1,x2) (x1-5)^2+(x2-4)^2;
fcontour(f,[0 10 0 10])
colorbar

답변 (2개)

Steven Lord
Steven Lord 2022년 4월 14일
f = @(x1,x2) (x1-5).^2+(x2-4).^2; % Vectorized the function
h = fcontour(f,[0 10 0 10]); % Let MATLAB choose the number and height of levels
fprintf("There is a contour at level: %g\n", h.LevelList)
There is a contour at level: 10 There is a contour at level: 20 There is a contour at level: 30 There is a contour at level: 40 There is a contour at level: 50 There is a contour at level: 60
figure
h = fcontour(f,[0 10 0 10], 'LevelList', 0:5:50); % I'll choose the levels
fprintf("There is a contour at level: %g\n", h.LevelList)
There is a contour at level: 0 There is a contour at level: 5 There is a contour at level: 10 There is a contour at level: 15 There is a contour at level: 20 There is a contour at level: 25 There is a contour at level: 30 There is a contour at level: 35 There is a contour at level: 40 There is a contour at level: 45 There is a contour at level: 50
  댓글 수: 1
Mahmoud Abbas
Mahmoud Abbas 2022년 4월 14일
Thank you, this works well however i need the number of contour lines to be exactly 20 and the associated values to be shown in the plot. is this possible?

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


Bjorn Gustavsson
Bjorn Gustavsson 2022년 4월 14일
If you check the documentation you will find this advice:
fcontour(f,'LevelList',[-1 0 1])
which adapted to your xy-region would be something like:
fcontour(f,[0 10 0 10],'LevelList',[-1 0 1])
HTH
  댓글 수: 2
Mahmoud Abbas
Mahmoud Abbas 2022년 4월 14일
I tried it but it didn't work and it showed me one contour line. I want the code to plot exactly 20 contours over the specified interval and show their values on the plot
Bjorn Gustavsson
Bjorn Gustavsson 2022년 4월 14일
That is a simple for you to achieve as modifying the levels in the "LevelList". I couldn't be bothered to figure out where to put them since you might want the somewhere else, but for 20 levels between two values you could do something like:
minVal = 0.5; % Still cannot be bothered to find out, so you have do decide.
maxVal = 13; % same as above.
LevelList = linspace(minVal,maxVal,20);
fcontour(f,[0 10 0 10],'LevelList',LevelList)

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

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by