使用fminsearch时出错

时间:2012-11-17 23:27:40

标签: matlab optimization runtime-error fminsearch

我正在使用fminsearch来通过扰动某些参数来最小化粗尺度上的协方差与精细尺度上的协方差的平均值之间的误差。这是使用fminsearch的两行代码行,其中我用三个参数调用目标函数minimize_me,我打算扰乱它们:

opts = optimset('display', 'iter');
[x,fval,exitflag] = fminsearch( @(x) minimize_me(x(1), x(2), x(3)), [2, 5, 90], opts);

函数minimize_me如下所示,它在其体内使用了几个函数:

function diff = minimize_me(a_minor, a_major, theta)

%# Grid and model parameters
nModel=50;
nModel_want=1;
nI_grid1=5;
Nth=1;
nRow.Scale1=5;
nCol.Scale1=5;
nRow.Scale2=5^2;
nCol.Scale2=5^2;
nCell.Scale1=nRow.Scale1*nCol.Scale1;

%% Covariance computation, averaging and difference of coarse and fine scale averaged covariances

% Reading files by using the function 'general_gslib_file_to_mat.mat'
[Deff_matrix_NthModel,~,~]=general_gslib_file_to_mat(nModel,nCell.Scale1,nModel_want,nI_grid1,Nth,'effective_dispersivity_coarsegrid5x5_gslib_format');

%# Maximum value of covariance/variogram at coarse scale
sill = var(reshape(Deff_matrix_NthModel,nCell.Scale1,1)); % variance of the coarse data matrix of size (nRow.Scale1 X nCol.Scale1)

%% Compute the covariance at different lags using the function general_CovModel.m

for ihRow = 1:nRow.Scale1
    for ihCol = 1:nCol.Scale1
        [cov.Scale1(ihRow,ihCol),heff.Scale1(ihRow,ihCol)] = general_CovModel(theta, ihCol, ihRow, a_minor, a_major, sill, 'Exp');
    end
end

for ihRow = 1:nRow.Scale2
    for ihCol = 1:nCol.Scale2
        [cov.Scale2(ihRow,ihCol),heff.Scale2(ihRow,ihCol)] = general_CovModel(theta, ihCol/(nCol.Scale2/nCol.Scale1), ihRow/(nRow.Scale2/nRow.Scale1), a_minor, a_major,...
            sill/(nRow.Scale2*nCol.Scale2), 'Exp');
    end
end

%# Scale-up of fine scale values by averaging which is done using the function general_AverageProperty.m
[covAvg.Scale2,var_covAvg.Scale2,varNorm_covAvg.Scale2] = general_AverageProperty(nRow.Scale2/nRow.Scale1,nCol.Scale2/nCol.Scale1,1,nRow.Scale1,nCol.Scale1,1,cov.Scale2,1);

%# Difference between the coarse scaled covariance and average of fine scale covariance
diff = (covAvg.Scale2 - cov.Scale1)^2;
end

但是,在运行前面显示的前两行代码时,我收到此错误:

??? Subscripted assignment dimension mismatch.

Error in ==> fminsearch at 195
fv(:,1) = funfcn(x,varargin{:});

有人能指出什么是错的吗?谢谢!

1 个答案:

答案 0 :(得分:3)

问题是,您没有为任何人提供足够的测试代码。

所以你应该......

  • 学习使用调试器!在任何情况下都是一个好主意。这将帮助您发现您做错了什么,但也教您使用有价值的工具。

  • 使用起始值测试您的功能一次。它返回了什么?你试过这个吗?总是做这个测试。确认您的目标符合您的预期目标。

Fminsearch需要标量输出才能最小化。你的功能是否提供了这个功能?

哦,顺便说一下,定义一个名为diff的变量,或者作为MATLAB中一个有用的工具已经存在的任何东西,这是一个糟糕的主意。否则,您只是想在代码中创建难以发现的错误。

相关问题