Help with surf plot
8 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Star Strider
on 14 May 2015
Is this what you want to do?
[x, y] = meshgrid(4:6, 1:8);
z = x + y;
figure
surf(x,y,z)
6 Comments
Star Strider
on 18 May 2015
Edited: Star Strider
on 18 May 2015
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.
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!