Anonymous function surface plot

24 次查看(过去 30 天)
Im creating a quadratic form function. Its defined as anonymous function of n-dimensional vector. But when I try to plot it I get an error saying that Z (in surface plot) must be matrix and not a vector. I have n set to 2 but it wont let me plot it. Is there a way to choose have many inputs my anonymous function has?
n = 2;
x_opt = randi([-6,6],n,1);
A = randi(n,n);
T = A*A' + eye(n,n);
h = -T*x_opt;
Q = @(x) 1/2*x'*T*x + h'*x; % n dimensional vector as input
[X,Y] = meshgrid(linspace(-10,10,100));
surf(X,Y,Q,'Edge Color','None') % only 2 dimensional input

采纳的回答

Alan Stevens
Alan Stevens 2021-2-21
Are you looking for something like this?
n = 2;
x_opt = randi([-6,6],n,1);
A = randi(n,n);
T = A*A' + eye(n,n);
h = -T*x_opt;
Q = @(x) 1/2*x'*T*x + h'*x; % n dimensional vector as input
[X,Y] = meshgrid(linspace(-10,10,100));
Qvals = zeros(size(X));
for i=1:100
for j= 1:100
Qvals(i,j) = Q([X(i,j);Y(i,j)]);
end
end
surf(X,Y,Qvals,'EdgeColor','None') % only 2 dimensional input
  1 个评论
Tyrell Wellick
Tyrell Wellick 2021-2-21
编辑:Tyrell Wellick 2021-2-21
Yes. Thank you very much. I forgot that the Q is only a function. And the surface plot needs actual values from the grid.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by