Help with surf plot

8 visualizzazioni (ultimi 30 giorni)
bayleaf
bayleaf il 14 Mag 2015
Modificato: Star Strider il 18 Mag 2015
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.

Risposte (1)

Star Strider
Star Strider il 14 Mag 2015
Is this what you want to do?
[x, y] = meshgrid(4:6, 1:8);
z = x + y;
figure
surf(x,y,z)
  6 Commenti
bayleaf
bayleaf il 18 Mag 2015
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 il 18 Mag 2015
Modificato: Star Strider il 18 Mag 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.

Accedi per commentare.

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by