简体   繁体   中英

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.

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. 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. Then use mesh or surf to plot your matrix against the a,b independent variables. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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