Help with surf plot
古いコメントを表示
I'd like to plot the output of an objective function which uses a vector of variable length as input. For 2 dimensional vector inputs, I'm using surf to plot x, y and the response z. However, the output z of my objective function produces a vector, when it should be a matrix.
This is my (example) function file:
function z = objfun(inputvec)
z = inputvec(:,1) + inputvec(:,2);
end
This is the implementation for plotting:
[x, y] = meshgrid(4:6, 1:8);
z = objfun( [x, y] );
%
size_x = size(x)
size_y = size(y)
size_z = size(z) % Here is the issue: size_z should be equal size_x and size_y ?!
%
figure
surf(x,y,z)
If possible, I'd like to stick to using a vector as the input to my objective function.
I'm grateful for any advice.
回答 (1 件)
Star Strider
2015 年 5 月 14 日
Is this what you want to do?
[x, y] = meshgrid(4:6, 1:8);
z = x + y;
figure
surf(x,y,z)
6 件のコメント
bayleaf
2015 年 5 月 14 日
Star Strider
2015 年 5 月 14 日
Maybe I’m missing something, but that would seem to be relatively straightforward:
objfun = @(x) x{1} + x{2};
%
[x, y] = meshgrid(4:6, 1:8);
z = objfun( {x, y} );
%
size_x = size(x)
size_y = size(y)
size_z = size(z) % Here is the issue: size_z should be equal size_x and size_y ?!
%
figure
surf(x,y,z)
This approach uses a cell array of ‘x’ and ‘y’ as the single argument to ‘objfun’. That is likely the most efficient way to pass both matrices as a single argument. (I used an anonymous function here for convenience, but your function file can use the same approach.)
Star Strider
2015 年 5 月 18 日
It’s telling you that:
- x0 is not a cell array;
- M is not a cell array.
bayleaf
2015 年 5 月 18 日
Star Strider
2015 年 5 月 18 日
編集済み: Star Strider
2015 年 5 月 18 日
I’m continuing to guess, because I’m not certain what the problem is.
A) If you don’t want to use cells, you can concatenate the matrices instead.
For example:
objfun = @(x) x(:,:,1) + x(:,:,2).^2;
%
[x, y] = meshgrid(4:6, 1:8);
xy = cat(3,x,y); % Concatenation
z = objfun( xy );
%
size_x = size(x);
size_y = size(y);
size_z = size(z); % Here is the issue: size_z should be equal size_x and size_y ?!
%
figure
surf(x,y,z)
This concatenates them, creating (in this instance) an (8x3x2) array ‘xy’. The ‘objfun’ function then takes them apart to use them in its calculations. This might be easier and more intuitive than using cells, however ‘x’ and ‘y’ must have the same row and column sizes for this to work.
B) The fminsearch function requires a scalar or vector for its initial conditions, because it has to have one value for the initial estimate for each parameter you’re estimating, so if you’re estimating one parameter, a scalar will work, otherwise it wants a vector with an initial estimate for each parameter. I’ve never used a function with it that requires a matrix parameter, so I don’t know if that would work.
カテゴリ
ヘルプ センター および File Exchange で Creating, Deleting, and Querying Graphics Objects についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!