简体   繁体   English

如何在 Octave 或 Matlab 中绘制具有两个变量的函数?

[英]How can I plot a function with two variables in Octave or Matlab?

I want to use Octave to plot fairly simple functions with two variables like: f(x,y) = x^2 + 3y .我想使用 Octave 绘制具有两个变量的相当简单的函数,例如: f(x,y) = x^2 + 3y 。 It is very easy to plot single variable functions, but I am having a lot of trouble finding out how to do two variable functions.绘制单变量函数非常容易,但是我在找出如何绘制两个变量函数时遇到了很多麻烦。 Does anyone know the best way of doing this?有谁知道这样做的最佳方法?

Plotting a function of two variables would normally mean a 3-dimensional plot - in MATLAB you would use the function plot3 for that.绘制两个变量的函数通常意味着一个 3 维图 - 在 MATLAB 中,您将为此使用函数 plot3。 To plot your function f(x,y) in the interval [-10,10] for both X and Y, you could use the following commands:要在区间 [-10,10] 中为 X 和 Y 绘制函数 f(x,y),您可以使用以下命令:

x = [-10:.1:10];
y = [-10:.1:10];
plot3(x, y, x.^2 + 3*y)
grid on

In case it may help someone out there... I ran in Octave the code in the accepted answer and I got this plot:如果它可以帮助那里的人......我在接受的答案中运行了八度代码,我得到了这个情节:

在此处输入图片说明

But I really wanted the function for every point in the Cartesian product of x and y, not just along the diagonal, so I used the function mesh to get this 3D plot with the projected contour lines in the x,y plane:但我真的想要 x 和 y 的笛卡尔积中的每个点的函数,而不仅仅是沿着对角线,所以我使用函数网格来获得这个 3D 图,其中投影轮廓线位于 x,y 平面:

x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy;
mesh(x, y, z)
meshc(xx,yy,z) 
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
grid on

在此处输入图片说明

To get rid of the mesh-wire texture of the plot, the function surf did the trick:为了摆脱情节的网状线纹理,功能surf做到了这一点:

x = [-10:.1:10];    
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy;
h = surf(xx,yy,z);
colormap hsv;
set(h,'linestyle','none');
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");

在此处输入图片说明

Another way to plot is as a heatmap with contour lines:另一种绘制方法是作为带有等高线的热图

x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + yy.*3;
contourf(xx,yy,z);
colormap hsv;
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
grid on

在此处输入图片说明

And for completeness, the levels can be labeled:为了完整起见,可以标记级别:

x = [-10:.1:10];
y = [-10:.1:10];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy;
[C,h] = contour(xx,yy,z);
clabel(C,h)
xlabel ("x");
ylabel ("y");
zlabel ("f(x,y)");
title ("f(x,y) = x^2 + 3y");
grid on

在此处输入图片说明

In addition to the excellent answers from @Toni and @esskov , for future plotters of functions with two variables, the contour and contourf functions are useful for some applications.除了来自@Toni@esskov的出色答案之外,对于具有两个变量的函数的未来绘图仪, contourcontourf函数对某些应用程序很有用。

等高线和填充等高线图

MATLAB Code (2018b): MATLAB 代码(2018b):

x = [-10:.1:10];    
y = [-20:.1:20];
[xx, yy] = meshgrid (x, y);
z = xx.^2 + 3*yy;           % Borrowed 4 lines from @Toni

figure
s(1) = subplot(1,2,1), hold on  % Left Plot
    [M,c] = contour(xx,yy,z);   % Contour Plot
    c.ShowText = 'on';          % Label Contours
    c.LineWidth = 1.2;          % Contour Line Width
    xlabel('X')
    ylabel('Y')
    box on
s(2) = subplot(1,2,2), hold on  % Right Plot
    [M2,c2] = contourf(xx,yy,z);
    colorbar                    % Add Colorbar
    xlabel('X')
    ylabel('Y')
    box on
title(s(1),'Contour Plot')
title(s(2),'Filled Contour Plot')

Update: Added example of surfc更新:添加了surfc例子

具有下方轮廓的 3D 表面

h = surfc(xx,yy,z)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM