# How to plot a function of two variables in MATLAB?

742 ビュー (過去 30 日間)
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 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 件のコメント表示 2 件の古いコメント非表示 2 件の古いコメント
Ian Stebelton 2022 年 2 月 7 日
How can you label the z value of each level?

サインインしてコメントする。

### その他の回答 (1 件)

madhan ravi 2019 年 1 月 9 日
help ezplot
help ezsurf

サインインしてコメントする。

### カテゴリ

Find more on Contour Plots in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by