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

 채택된 답변

Mischa Kim
Mischa Kim 2014년 4월 10일
편집: Mischa Kim 2014년 4월 10일
Sadeg, use something like
[X,Y] = meshgrid(0:0.1:1);
Z = sin(X).*cos(3*Y).^2;
surf(X,Y,Z)

댓글 수: 4

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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

ss
ss 2014년 4월 10일
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

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Vector Fields에 대해 자세히 알아보기

제품

질문:

ss
2014년 4월 10일

댓글:

ss
2014년 4월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by