Help with surf plot

조회 수: 9 (최근 30일)
bayleaf
bayleaf 2015년 5월 14일
편집: Star Strider 2015년 5월 18일
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
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
bayleaf 2015년 5월 18일
This is now worrying me a bit.
It seems the only way to do the plotting is using the cell notation (e.g. objfun = @(x) x{1} + x{2};)
whereas the rest of my code is using the objective function in "value" notation (e.g. objfun = @(x) x(1) + x(2);)
Questions:
A) Would there be a way to make your first suggestion (comment 1) work without using "cells"
B) How to approach the problem if fminsearch needs x0 (starting conditions) to be a scalar, vector, or matrix.
Hopefully there is a solution
Star Strider
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.

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

카테고리

Help CenterFile 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!

Translated by