使用fminsearch时出错 [英] Error while using fminsearch

查看:215
本文介绍了使用fminsearch时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用fminsearch通过扰动某些参数来最小化粗尺度上的协方差和细尺度上的协方差的平均值之间的误差.这是使用fminsearch的2行代码行,在其中我要使用三个要干扰的参数调用目标函数minimize_me:

I am using fminsearch to minimize the error between the covariance at coarse scale and the average of covariance at fine scale by perturbing certain parameters. These are 2 lines of code lines using fminsearch in which I am calling the objective function minimize_me with three arguments which I intend to perturb:

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

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

The function minimize_me is shown following and it uses couple of more functions inside its body:

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

但是,在运行前面显示的前两行代码时,出现此错误:

However, on running the first two lines of code shown earlier, I get this error:

??? Subscripted assignment dimension mismatch.

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

有人可以指出出什么问题吗?谢谢!

Can someone point what's wrong? Thanks!

推荐答案

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

The problem is, you don't provide enough for anyone to test your code.

所以你应该...

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

  • Learn to use the debugger! A good idea in any circumstance. This will help you discover what you have done wrong, but also teach you to use a valuable tool.

使用起始值测试一次功能.它返回什么?你有试过吗?始终执行此测试.验证您的目标是否达到了您的预期目标.

Test your function once with the starting values. What does it return? Have you tried this? Always do this test. Verify that your objective does what you expect it to do.

Fminsearch需要标量输出以最小化.您的功能可以做到这一点吗?

Fminsearch needs a scalar output to minimize. Does your function give that?

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

Oh, by the way, it is a terrible idea to define a variable named diff, or anything that already exists as a useful tool in MATLAB. Otherwise, you are just pleading to create difficult to find bugs in your code.

这篇关于使用fminsearch时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆