绘制 2D 函数:我需要将所有值传递给函数吗?

数据挖掘 绘图 八度
2022-02-24 09:00:08

我是 Octave 的新手,我需要绘制这个函数:

% Computes the value of the Powell Sum benchmark function.
% SCORES = POWELLSUMFCN(X) computes the value of the Powell Sum function at 
% point X. POWELLSUMFCN accepts a matrix of size M-by-N and returns a vetor 
% SCORES of size M-by-1 in which each row contains the 
% function value for the corresponding row of X. 
% 
% Author: Mazhar Ansari Ardeh
% Please forward any comments or bug reports to mazhar.ansari.ardeh at
% Google's e-mail service or feel free to kindly modify the repository.
function scores = powellsumfcn(x)
    n = size(x, 2);
    absx = abs(x);

    scores = 0;
    for i = 1:n
        scores = scores + (absx(:, i) .^ (i + 1));
    end
end

我从BenchmarkFcns复制。

我不知道如何绘制它。

我需要将所有可能的值传递给函数 plot 吗?

1个回答

如何定义一个绘图函数,如:

function meshfcn(fcn, x, y)
    [X, Y] = meshgrid(x, y);
    Z = arrayfun(@(xx, yy)(fcn([xx, yy])), X, Y);
    mesh(X, Y, Z);

然后使用它:

x = -2:0.1:2; % the range of 'x' values to plot. change to your needs
y = -2:0.1:2; % the range of 'y' values to plot. change to your needs
fcn = @powellsumfcn; % the function needs to be on the path
meshfcn(fcn, x, y)