我试图在Ackley Function
中绘制matlab
,但我收到以下错误
使用冲浪时出错(第xxx行)
X,Y,Z和C不能复杂。
这是我使用
的代码clear; clc; close all;
% Parameters
nx = 2; % No. of Input variables
f = @ackley;
limits = repmat([-40 40], nx, 1);
titl = 'Ackley';
% Plot
[X,Y] = meshgrid(linspace(limits(1,1),limits(1,2),100),...
linspace(limits(2,1),limits(2,2),100));
Z = reshape(f([X(:)'; Y(:)']), 100, 100);
surfc(X,Y,Z);
Ackley.m
function [y] = ackley(x)
d = length(x);
a = 20;
b = 0.2;
c = 2*pi;
term1 = -a * exp(-b*sqrt(1/d * sum(x)));
term2 = -exp(1/d * sum(cos(c * x)));
y = term1 + term2 + a + exp(1);
end
答案 0 :(得分:0)
您在term1 = -a * exp(-b*sqrt(1/d * sum(x)));
中获得了sqrt()计算的否定参数。调整您的绘图范围。例如limits = repmat([0 40], nx, 1);
答案 1 :(得分:0)
正如 hbaderts 所提到的,当您按如下方式更改Ackley函数时,您的代码可以正常工作:
function [y] = ackley(x)
d = length(x);
a = 20;
b = 0.2;
c = 2*pi;
term1 = -a * exp(-b*sqrt(1/d * sum(x.^2)));
term2 = -exp(1/d * sum(cos(c * x)));
y = term1 + term2 + a + exp(1);
end