필터 지우기
필터 지우기

How to plot a function of two variables in MATLAB?

조회 수: 250 (최근 30일)
Indiase Straal
Indiase Straal 2019년 1월 9일
댓글: Ian Stebelton 2022년 2월 7일
Hi,
Consider a function . If I want to plot the level set, how can this be done in MATLAB?
Any help is greatly appreciated.
Kiran

채택된 답변

John D'Errico
John D'Errico 2019년 1월 9일
편집: John D'Errico 2019년 1월 9일
People seem not to recognize this. Not sure why, but it seems a common misperception. A level set is the set of all points where the function z(x1,x2) is constant, at some given value. In this case, that value is z(x1,x2)==1. So you want to do a contour plot!
The obvious solution is to try ezcontour. But if you did, you will be disapponted. Why? Because ezcontour does not allow you to specify the contour level of interest. Here, that is z(x1,x2) = 1. (Actually, it looks like ezcontour is now being deprecated, to be replaced eventually by fcontour. Sadly, they still have not obviously given us the ability to plot only ONE desired contour line with fcontour. IMHO, that would be a mistake. HAPPILY, they did give us that capability! It is just not documented as well as I would have liked.)
Instead, the classic solution in MATLAB is to use contour. Contour works on an array of values. So in the classical solution, you would first use meshgrid to generate a grid over x1 and x2. Then evaluate the function at each grid point in the arrays of x1 and x2, representing points in the (x1,x2) plane. Only then call contour, telling it to use a SPECIFIC contour level, here z==1.
So, lets instead try using a simpler solution in MATLAB, thus fcontour.
Create a function of two variables. Simplest is to learn about function handles. Don't forget to use the correct operators, that will allow vectorized operations between arrays of x1 and x2. Here that means you need to use the .^ and .* operators.
zfun = @(x1,x2) x1.^2 + x2.^2 - x1.*x2;
zhandle = fcontour(zfun)
zhandle =
FunctionContour with properties:
Function: @(x1,x2)x1.^2+x2.^2-x1.*x2
LineColor: 'flat'
LineStyle: '-'
LineWidth: 0.5000
Fill: 'off'
LevelList: [10 20 30 40 50 60 70]
Show all properties
So I did a contour plot. So what? Where is the contour that indicates where z(x1,x2)==1? Look carefully at the properties we see there. We find LevelList! Will that help?
zhandle.LevelList = 1;
xlabel x1
ylabel x2
title(func2str(zfun))
grid on
zhandle.YRange = [-2,2];
zhandle.XRange = [-2,2];
axis equal
That looks reasonable now. Anyway, not difficult. It took a few lines of code to make the picture as pretty as I might like, but then I tend to be a perfectionist.
It does get into some of the newer toys to be found in MATLAB, which is why I answered this question in some depth.
  댓글 수: 3
John D'Errico
John D'Errico 2019년 1월 9일
That is exactly how I would have suggested you solve the problem in the past. fcontour makes things go a little more simply, but you need to know how to use handles to control the result. Either approach works.
Ian Stebelton
Ian Stebelton 2022년 2월 7일
How can you label the z value of each level?

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

추가 답변 (1개)

madhan ravi
madhan ravi 2019년 1월 9일
help ezplot
help ezsurf

카테고리

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