Anonymous function surface plot

28 visualizaciones (últimos 30 días)
Tyrell Wellick
Tyrell Wellick el 21 de Feb. de 2021
Editada: Tyrell Wellick el 21 de Feb. de 2021
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

Respuesta aceptada

Alan Stevens
Alan Stevens el 21 de Feb. de 2021
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 comentario
Tyrell Wellick
Tyrell Wellick el 21 de Feb. de 2021
Editada: Tyrell Wellick el 21 de Feb. de 2021
Yes. Thank you very much. I forgot that the Q is only a function. And the surface plot needs actual values from the grid.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by