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
Star Strider 2015 年 5 月 14 日

0 投票

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 月 14 日
My objective function is for an optimisation problem and the code is much more complex. I'm using a separate function file for the objective function and I'm calling the surf command in a separate file. This example is just a simplification.
My objective function uses a vector input but it seems there is a problem when transferring the meshgrid matrix to my objective function to calculate my response z.
The result I'm looking for here, is the plot that you (Star Strider) produce with your code. However, I need a way to tell the objective function to use the first component of [x, y] for the first summand in my objective function and the second component (which is y) for the second summand in my objective function. Please note, the summation is just a simple example for the objective function. The objective function could also be something like the Rosenbrock function in multiple dimensions...
Star Strider
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.)
bayleaf
bayleaf 2015 年 5 月 18 日
編集済み: bayleaf 2015 年 5 月 18 日
Hi, I tried your approach: I changed my objective function in the function file which looks something like
y = (x{2} - x{1}.^2);
And I tried an optimisation using fminsearch in a seperate file:
[x, y] = fminsearch(@objfun, x0{indX0,:}, options);
later on plotting the output as
[x1, x2] = meshgrid (-6:6);
y1 = objfun({x1,x2});
figure
surf(x1,x2,y1)
This is the error massage I get using the cell array approach:
Cell contents reference from a non-cell array object.
Error in MYfminsearch (line 25)
[x, y] = fminsearch(@objfun, x0{indX0,:}, options);
Sorry to keep asking about this. What is the error telling me?
%
Something else I noticed applying the objective function to a matrics M of the size [2,3]:
For
y = objfun(M)
the error message is "Cell contents reference from a non-cell array objective".
For
y = objfun( {M} )
the error message is "Not enough input arguments".
This is confusing me as my objective function requires x{1} and x{2}, and that should be the case for my input M (size 3,2)?
Star Strider
Star Strider 2015 年 5 月 18 日
It’s telling you that:
  1. x0 is not a cell array;
  2. M is not a cell array.
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

質問済み:

2015 年 5 月 14 日

編集済み:

2015 年 5 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by