简体   繁体   English

在MATLAB中绘制多变量函数图

[英]Graphing a multi variable function in MATLAB

I have never used MATLAB before, so I am very lost. 我以前从未使用过MATLAB,因此非常迷茫。 For my calculus class, we were tasked with finding a certain function and then using MATLAB to graph it. 对于我的微积分课,我们的任务是找到某个函数,然后使用MATLAB对其进行图形化。 Finding the function was no problem. 找到功能没问题。 However, trying to graph it has me pulling my hair out. 但是,尝试绘制它会使我拔出头发。 The function is z(x,y)= xy(x+y)(2x+y)(3x+y)(x-2y)(x-3y)(x-4y). 该函数为z(x,y)= xy(x + y)(2x + y)(3x + y)(x-2y)(x-3y)(x-4y)。 Any help or advice is GREATLY appreciated. 任何帮助或建议,我们将不胜感激。

You can define a anonymous function handle. 您可以定义一个匿名函数句柄。

% define function
% .* denotes element wise multiplication
f = @(x,y) x.*y.*(x+y).*(2*x+y).*(3*x+y).*(x-2*y).*(x-3*y).*(x-4*y);

% define range and resolution for x and y
x = -20:0.5:20;
y = -20:0.5:20;

% create meshgrid for 3d plotting
[X, Y] = meshgrid(x,y);

% calculate z values (for meshgrid)
z = f(X, Y);

% plot the function
figure()
surf(x,y,z)

To explain further, since you want to calculate the z value for x and y pairs, you should use a element wise multiplication .* . 为了进一步说明,由于要计算x和y对的z值,因此应使用元素明智的乘法.*

Then you have to create a meshgrid for the x and y values, to have all the possible x and y pairs in the two new matrices X and Y . 然后,您必须为x和y值创建一个meshgrid ,以在两个新矩阵XY具有所有可能的x和y对。 Providing these to your function will calculate the corresponding z value for all these pairs. 将这些提供给您的函数将为所有这些对计算对应的z值。 You can use these for plotting, eg surf. 您可以将它们用于绘图,例如冲浪。

在此处输入图片说明

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

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