简体   繁体   English

使用定义的函数的MATLAB 3D图

[英]MATLAB 3D plot using a defined function

I have defined a function in a separate file which takes seven parameters and outputs one value. 我在一个单独的文件中定义了一个函数,该函数需要七个参数并输出一个值。 I want to create a 3D plot which will change with two of the parameters and I'm really stuck at how to do this. 我想创建一个3D图,该图将随着两个参数的变化而变化,我真的对如何做到这一点感到困惑。

So say my function is called using Ruth(a, b, c, d, e, f), and I want cf to be fixed and plot the output value depending on values of a and b. 所以说我的函数是用Ruth(a,b,c,d,e,f)调用的,我希望cf是固定的,并根据a和b的值绘制输出值。 Can anyone guide me on this? 有人可以指导我吗? Would be much appreciated. 将不胜感激。

Thanks in advance! 提前致谢!

Use meshgrid to generate a grid of points over the range of a and b that you are interested in. Then evaluate your function at each of those (a,b) point pairs, forming a 2D matrix. 使用meshgrid生成您感兴趣的a和b范围内的点的网格。然后在每个(a,b)点对中评估函数,形成2D矩阵。 Then use mesh or surf to plot your matrix against the a,b independent variables. 然后使用meshsurf对a,b自变量绘制矩阵。 Here's an example, with a really simple function: 这是一个具有非常简单功能的示例:

function out = myfunc(a,b,c)
out = c + a.*b;

[a,b] = meshgrid(-50:50, -50:50);
c = 10;
z = myfunc(a, b, c);
mesh(a, b, z);

Note that in my case, I've written my function to be "vectorized", meaning it will accept vectors or arrays of inputs and operate on the entire array. 请注意,就我而言,我已将函数编写为“向量化”,这意味着它将接受向量或输入数组并在整个数组上进行操作。 If yours is not, you'll have to evaluate your function with a nested loop over a and b, something like this: 如果不是,则必须使用在a和b上的嵌套循环来评估函数,如下所示:

z = zeros(size(a));
for ii=1:size(a,1)
    for jj=1:size(a,2)
        z(ii,jj) = Ruth(a(ii,jj), b(ii,jj), c, d, e, f);
    end
end

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

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