How to draw a 3D plot of a function in form y=f(X) and X is an array [X(1),X(2)] ?

Hi every body i need to draw a function in form :
function F_cost= objfun(X)
F_cost=(cos(X(1)*X(2)));
I cant use fplot or ezsurf and i don't know why !!
for example ezsurf (objfun) or ezplot(objfun,[-3 3 -3 3]) doesn't work !!
Can any body help me plz !?
Bests Sadeg

 Akzeptierte Antwort

Mischa Kim
Mischa Kim am 10 Apr. 2014
Bearbeitet: Mischa Kim am 10 Apr. 2014
Sadeg, use something like
[X,Y] = meshgrid(0:0.1:1);
Z = sin(X).*cos(3*Y).^2;
surf(X,Y,Z)

4 Kommentare

I see. In the MATLAB command window use
x = linspace(0,10,20);
y = linspace(-5,5,20);
F_cost= objfun([x' y'])
to execute
function F_cost= objfun(X)
[X1, X2] = meshgrid(X(:,1),X(:,2))
F_cost = cos(X1).*X2;
surf(F_cost,X1,X2)
end
Thanks. It works very well but, is there any alternative method that lets us do the same but without any change in function definition code. i mean :
function F_cost= objfun(X)
F_cost=(cos(X(1)*X(2)));
There is no feasible alternative. The problem is
F_cost=(cos(X(1)*X(2)));
which in this form takes two scalars to compute F_cost, which is also a scalar. With the solution I proposed you create n-by-m matrices to compute F_cost for all data points which is readily available for plotting. You could wrap your function in for-loops to compute each point individually, but that's everything but efficient. Especially for large matrices.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Hi Mischa, thanks for your answer.Actually i cant change the function format at all. In fact its a part of a developing software so the format of functions are fixed by protocols. Consider there is 100s of two variable functions in form :
function F_cost= objfun(X)
F_cost=(cos(X(1)*X(2)));
And we want to 3Dplot them, do you have any idea ?

1 Kommentar

See answer above. Please post follow-up questions and comments as comments, not questions.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Vector Fields finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

ss
am 10 Apr. 2014

Kommentiert:

ss
am 10 Apr. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by