How can I vectorize a function?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
The function I want to vectorize is Cross-in-Tray Function (2-D):
f(X,Y) = -0.0001*(abs(sin(X)*sin(Y)*exp(abs(100-sqrt(X^2+Y^2)/pi)))+1)^0.1;
I want to use the command (to plot the function):
fsurf(@(x,y) crossintrayfcn([x,y]))
I have this two function codes:
function z = crossintrayfcn(xx)
x = xx(:,1);
y = xx(:,2);
expcomponent = abs(100-(sqrt(x.^2 + y.^2)/pi));
z = -0.0001*((abs(sin(x).*sin(y).*exp(expcomponent))+1).^0.1);
end
And:
function [y] = crossintrayfcn(xx)
x1 = xx(1);
x2 = xx(2);
fact1 = sin(x1)*sin(x2);
fact2 = exp(abs(100 - sqrt(x1^2 + x2^2)/pi));
y = -0.0001 * (abs(fact1*fact2) + 1)^0.1;
end
But they plot nothing!
Thanks!
채택된 답변
Star Strider
2018년 3월 1일
Vectorising it simply requires using element-wise operations:
f = @(X,Y) -0.0001*(abs(sin(X).*sin(Y).*exp(abs(100-sqrt(X.^2+Y.^2)/pi)))+1).^0.1;
[x,y] = meshgrid(linspace(-10, 10, 49));
figure
surfc(x, y, f(x,y))
grid on
댓글 수: 9
Thanks for the reply Star Strider. But when I use your code inside a function:
fsurf(@(x,y) crossintrayfcn([x,y]))
Where:
function Z = crossintrayfcn(xx)
X = xx(:,1);
Y = xx(:,2);
Z = -0.0001*(abs(sin(X).*sin(Y).*exp(abs(100-sqrt(X.^2+Y.^2)/pi)))+1).^0.1;
end
I got the 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 the plot shows nothing, like the codes I posted before...
I want to plot without using pre-defined X and Y. Just like:
fsurf(@(x,y) sin(x)+cos(y))
Directly, without using meshgrid...
My pleasure.
I have no idea what you are doing.
For whatever reason, fsurf (that will throw the error you quoted, although it did not throw the error when I used it) will not plot your function, although fcontour and ezsurf have no problem with it:
f = @(X,Y) -0.0001*(abs(sin(X).*sin(Y).*exp(abs(100-sqrt(X.^2+Y.^2)/pi)))+1).^0.1;
figure(2)
fcontour(f, [-6 6 -6 6])
figure(3)
ezsurf(f, [-6 6 -6 6])
Use these instead. You might want to report the inability of fsurf (and fmesh) to plot your function, while fcontour and ezsurf will, as a bug. Use the Contact Us link in the upper right corner of this page to do it.
The meshgrid function (that the plotting functions likely use internally anyway) is the preferred approach, since it gives you more control over the mesh size.
Indeed 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?
I don't know what is the error with fsurf.
Ps.: I am using R2017b version.
Star Strider
2018년 3월 2일
It seems to be.
You’ve written it as a function of one variable, and to create the variable you’re horizontally concatenating two arrays to create the single variable. This may be confusing fsurf.
I’ve not examined the code for either fsurf or ezsurf, so I don’t know for sure how they work. Both apparently use meshgrid to create the matrix function arguments they send to the argument function.
I understand that ezsurf will be deprecated and replaced by fsurf, so it would probably be best for you to bring this behavior to the attention of MathWorks. It would seem that fsurf needs to be improved somewhat before ezsurf disappears. Your approach (that appears to be valid) may not be designed into fsurf.
Include the URL to this thread in your message to MathWorks, so you don’t have to repeat everything.
I got a 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.
Star Strider
2018년 3월 5일
Thank you for your efforts.
Interesting that in the FunctionSurface Properties (link) 'MeshDensity' is listed, though 'AdaptiveMeshDensity' is not.
I just finished reading your other post about this. Since you reproduced that text in its entirety here, I have not linked to it.
This is useful information. I am tagging your other post with ‘yair altman’ (who has the ‘Undocumented MATLAB’ site) so he will see it.
David Franco
2018년 3월 5일
Yes, this stuff is priceless and my suspicion has been confirmed:
That most beautiful figure produced by ezsurf is not as accurate as the figure produced by fsurf.
We were being deceived :)
Yair Altman
2018년 3월 24일
@StarStrider - thanks, duly noted
Star Strider
2018년 3월 24일
@Yair — My pleasure.
추가 답변 (1개)
David Franco
2018년 3월 5일
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
% 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)
% SHAD - set color shading properties (default = 0)
% 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
shad = 0;
case 2
shad = 0;
grid = 101;
case 1
shad = 0;
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
if shad == 1
shading interp
elseif shad == 2
shading flat
end
end
카테고리
도움말 센터 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
