Anonymous function surface plot

28 ビュー (過去 30 日間)
Tyrell Wellick
Tyrell Wellick 2021 年 2 月 21 日
編集済み: Tyrell Wellick 2021 年 2 月 21 日
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 Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by