Making a 3D plot in a vectorized function

I have a vectorized function which accepts an input vector such as:
y=myfun(x)
where
x=[x(1) x(2)]
and I want to assign vectors in x(1) and x(2) in order to make 3D plot. Do you know how can I implement this?

回答(1 个)

Here's one way, using a cell array to hold the two vectors:
>> c = cell(1,2);
>> x = 1:10;
>> y = 1:20;
>> c{1} = x;
>> c{2} = y;
>> myfun(c)
where the file myfun.m has
function myfun(x)
[xx,yy] = meshgrid(x{1},x{2});
zz = xx + yy.^2;
figure
mesh(xx,yy,zz)
end

7 个评论

Is this possible outside the function?
Sorry, I don't understand. I do everything outside the function except defining the plotting.
Maybe you could write a little more detail on what you mean, and what you are trying to do?
I meant defining the plotting outside the function. Actually I have a symbolic script that in the end generates a function with the command matlabFunction. This command gives the option to store the function created in a file or creating a function handle. Since I do not store the function I must use the function handle to create my plot. The function handle has the form that I have pointed out in my first post such as:
y=myfun(x, a, b, c,d);
a, b, c, d are scalars and x is a vector. It is of the form
x(:,1)
x(:,2)
So I have the problem of assigning the meshgrid that I build in the function.
I'm still struggling to understand both what specifically you are trying to do, and where specifically you are stuck. Would it be possible for you to post a simplified version of your code that shows what you have done and how you are stuck?
One thing that is particularly unclear to me is the size and object type that x is. You referred to it as a vector, and then as [x(1) x(2)] and then as
x(:,1)
x(:,2)
which makes it seem like a 2-dimensional array.
My advice would be to start a new question that includes your code, and is very specific about what you have done and what you can't do.
Sorry, I am sure this is partly a communication issue, and I am just not quite understanding your question.
I appreciate that you try to answer my question. Ok I will try to post a simplified version of my code here. I have a script file which is such as:
syms a b c
f=a^3+b^2+c;
x=[a b];
g=matlabFunction(f,'vars',{x,c});
clear a b c
So:
g =
@(in1,c)c+in1(:,1).^3+in1(:,2).^2
I would to plot g if:
in1(:,1)=1:1:10;
in1(:,2)=10:1:20;
And my question is how can I do that. I hope that it is more clear now. Thank you
Unfortunately, I do not have the symbolic toolbox. I suggest that you take this latest comment you just made, and create a brand-new question out of it. It seems much clearer now what you are trying to do. Probably you will find some help.
Thank you very much for the effort. I will do that.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by