## Difference between fsurf and ezsurf

### David Franco (view profile)

on 2 Mar 2018
Latest activity Commented on by David Franco

on 5 Mar 2018

### David Franco (view profile)

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: Where:
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);
end
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?
Ps.: I am using R2017b version.

### David Franco (view profile)

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:
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.

### Walter Roberson (view profile)

on 5 Mar 2018

"I think this Ackley's function is correctly vectorized. Am I right?"
No.
"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.

David Franco

### David Franco (view profile)

on 5 Mar 2018
Because of the above problems I'm using my own function with surfc to replace fsurf:
% PLOTFCN evaluate and plot a 3D function
% INPUT:
% FCN - @myFunction (function handle)
% RANGE - [x1min x1 max x2min x2max] (default = [-10 10 -10 10])
% GRID - grid size for the function evaluation (default = 101)
% 0 = faceted (continued colormap with black mesh lines)
% 1 = interp (interpolated colormap)
% 2 = flat (continued colormap)
% OUTPUT:
% Z - function eval (GRID x GRID)
% EXAMPLE:
% z = plotfcn2(@ackleyfcn, [-32 32; -32 32], 200, 1);
switch nargin
case 4
case 3
case 2
grid = 101;
case 1
grid = 101;
range = [-10 10 -10 10];
otherwise
disp('Not enough input arguments. Function required.')
return
end
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;
end
figure
surfc(x1,x2,f)
title([func2str(fcn), ' [x,y]'])
colormap jet