Clear Filters
Clear Filters

Difference between fsurf and ezsurf

7 views (last 30 days)
I have a problem with fsurf command:
When I use
fsurf(@(x,y) ackleyfcn([x,y]),[-32 32 -32 32])
I got this warning:
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.
And this picture (after a long time, about 20 sec):
When I use
ezsurf(@(x,y) ackleyfcn([x,y]),[-32 32 -32 32])
I got no warning and the correct (and fast) picture:
function z = ackleyfcn(xx)
% Ackley's function
% Search domain: [-32,32]
% Global minimum: f(x) = 0 | x = (0,...,0)
d = size(xx, 2);
xx = max(-32,min(32,xx));
z = -20*exp(-0.2*sqrt(1/d*sum(xx.^2,2))) - exp(1/d*sum(cos(2*pi*xx),2)) + 20 + exp(1);
I think this Ackley's function is correctly vectorized. Am I right?
What is the error with fsurf and why the image generated by it is strange and takes longer to be generated?
Thanks in advance!
Ps.: I am using R2017b version.

Accepted Answer

David Franco
David Franco on 5 Mar 2018
MathWorks' Support Response:
I have been able to reproduce the slow down that you were experiencing. The fsurf function tries to determine what density of points to use in order to give an accurate depiction of the function you pass it. Since the ackleyfcn has many small oscillations, fsurf decides to use a very dense mesh in order to display it. This feature is not available in ezsurf which is why the plots look so different.
The time fsurf takes is also much longer because if it displaying many more points. If you would like to use fsurf to produce the plot similar to ezplot, you can turn off the AdaptiveMeshDenstity feature by using the following line of code:
set(fsurf(@(x,y) ackleyfcn([x,y]),[-32 32 -32 32]),'AdaptiveMeshDensity',0,'MeshDensity',60)
Note that this will increase the speed of fsurf and will produce a plot similar to that of ezsurf. However, this new surface uses fewer points and so does not fully represent the ackleyfcn function.

More Answers (1)

Walter Roberson
Walter Roberson on 5 Mar 2018
"I think this Ackley's function is correctly vectorized. Am I right?"
"Specify a function of the form z = f(x,y). The function must accept two matrix input arguments and return a matrix output argument of the same size. "
With your use of @(x,y) ackleyfcn([x,y]) the two inputs are being slammed together into the same array. You then use the width of that joined array as part of the computations. The result is going to be different for any given point than if a single point pair had been evaluated, and the results would be different if fsurf had happened to pass in a row vector of values for x and y compared to if it had happened to pass in a column vector of the same values.
  1 Comment
David Franco
David Franco on 5 Mar 2018
Because of the above problems I'm using my own function with surfc to replace fsurf:
function z = plotfcn(fcn,range,grid,shad)
% PLOTFCN evaluate and plot a 3D function
% FCN - @myFunction (function handle)
% RANGE - [x1min x1 max x2min x2max] (default = [-10 10 -10 10])
% GRID - grid size for the function evaluation (default = 101)
% SHAD - set color shading properties (default = 0)
% 0 = faceted (continued colormap with black mesh lines)
% 1 = interp (interpolated colormap)
% 2 = flat (continued colormap)
% Z - function eval (GRID x GRID)
% z = plotfcn2(@ackleyfcn, [-32 32; -32 32], 200, 1);
switch nargin
case 4
case 3
shad = 0;
case 2
shad = 0;
grid = 101;
case 1
shad = 0;
grid = 101;
range = [-10 10 -10 10];
disp('Not enough input arguments. Function required.')
x1 = meshgrid(linspace(range(1,1), range(1,2), grid));
x2 = meshgrid(linspace(range(1,3), range(1,4), grid))';
xx = [x1(:),x2(:)];
f = fcn(xx);
f = reshape(f,size(x1));
if nargout == 1
z = f;
title([func2str(fcn), ' [x,y]'])
colormap jet
if shad == 1
shading interp
elseif shad == 2
shading flat

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!