Anonymous

# 請教教我這段matlab程式的意思

function ObjVal = objfun1(Chrom, P1, P2);

% Compute population parameters

[Nind, Nvar] = size(Chrom);

% Check size of Chrom and do the appropriate thing

% if Chrom is [], then reset to [NaN P1]

if isempty(Chrom),

if nargin < 2, P1 = []; end, if isempty(P1), P1 = 1; end

Chrom = [NaN, P1]; Nind = 1;

end

% if Chrom is [NaN xxx] define size of boundary-matrix and others

if all([Nind == 1, isnan(Chrom(1))]),

% If only NaN is provided

if length(Chrom) == 1, option = 1; else option = Chrom(2); end

% Default dimension of objective function

Dim = 20;

% return text of title for graphic output

if option == 2, ObjVal = ['DE JONGs function 1'];

% return value of global minimum

elseif option == 3, ObjVal = 0;

% define size of boundary-matrix and values

else

% lower and upper bound, identical for all n variables

ObjVal = repmat([-512; 512], [1 Dim]);

end

Rating
• 豬皮
Lv 5

不知道這個函數是哪類的函數，所以僅能就語法解釋

function ObjVal = objfun1(Chrom, P1, P2);

% Compute population parameters

================================================

%取出Chrom大小

================================================

[Nind, Nvar] = size(Chrom);

% Check size of Chrom and do the appropriate thing

% if Chrom is [], then reset to [NaN P1]

================================================

%如果Chrom是空矩陣的話，檢查函數的引數數量 nargin

%若引數少於兩個，則將P1設成空矩陣，若P1是空矩陣

%，則將P1設為1。將Chrom設為 [NaN P1]，並將Nind設為

%1，因為Chrom現在是個1x2的矩陣

% NaN => Not A Number

================================================

if isempty(Chrom),

if nargin < 2, P1 = []; end, if isempty(P1), P1 = 1; end

Chrom = [NaN, P1]; Nind = 1;

end

% if Chrom is [NaN xxx] define size of boundary-matrix and others

================================================

%如果Chrom是一列矩陣的話，而且Chrom的第一個元素

%是NaN的話是空矩陣的話，執行以下程式碼

================================================

if all([Nind == 1, isnan(Chrom(1))]),

% If only NaN is provided

================================================

%如果Chrom只有一個元素的話，將option設為1，不然

%option的值就是Chrom的第二個元素，預設的Dim為20

================================================

if length(Chrom) == 1, option = 1; else option = Chrom(2); end

% Default dimension of objective function

Dim = 20;

% return text of title for graphic output

================================================

%如果option是2的話，回傳'DE JONG's function1'字串

================================================

if option == 2, ObjVal = ['DE JONGs function 1'];

% return value of global minimum

================================================

%如果option是3的話，回傳0

================================================

elseif option == 3, ObjVal = 0;

% define size of boundary-matrix and values

else

% lower and upper bound, identical for all n variables

================================================

%其餘的狀況，回傳 repmat([-512 512], [1 Dim])

================================================

ObjVal = repmat([-512; 512], [1 Dim]);

end

end