使用矩阵内部函数的fminsearch

时间:2011-11-29 16:29:56

标签: matlab mathematical-optimization

我在最小化一个相当复杂的功能时遇到了麻烦:

% Current densities - psi is a 4x1 fourvector
j0 = @(psi) psi' * psi;

% Solutions
chi1 = @(n_,r_,kt_,theta_) ...
       1/sqrt(2) * ...
           [ exp(1i*n_*theta_) .* besselj(n_,kt_*r_); ...
             exp(1i*(n_+1)*theta_) .* besselj(n_+1,kt_*r_) ];
chi2 = @(n_,r_,kt_,theta_) ...
       1/sqrt(2) * ...
           [  exp(1i*n_*theta_) .* besselj(n_,kt_*r_); ...
             -exp(1i*(n_+1)*theta_) .* besselj(n_+1,kt_*r_) ];

uplus = @(n_,E_,m_,r_,kz_,kt_,theta_) ...
        sqrt((E_+m_)/(4*m_)) * ...
            [ chi1(n_,r_,kt_,theta_); ...
              (kz_-1i*kt_)/(E_+m_) * chi2(n_,r_,kt_,theta_) ];

这里:n,E,m,kz,theta都是用于任何目的的常数。我需要使j0适合一个阶梯函数(1代表r = 1 ... 10),模型函数是四矢量psi,包括对kt = {{0}上的uplus求和{1}},以便besselj(0,kt * 10)为零。问题是besselj(n,kt/10*r)不喜欢我复杂的设置:

fminsearch

uplus_reduced = @(r_,kt_) uplus(0,E,m,r_,kz,kt_,0); error_function = @(r_,coeffs_) error_j0_stepfunction(r_,coeffs_,... uplus_reduced); coeffs(1,:) = fminsearch( @(r) error_function(r',coeffs(:,1)), r); 就是这样:

error_j0_stepfunction

我希望我已经足够清楚了,同时简明扼要,这样才能得到回答。谢谢你的帮助!

编辑:我得到的错误就是这个(输出让我对它毫无意义):

function error = error_j0_stepfunction(r,coeffs,basisspinor)
% Zeros of BesselJ
Nzeros = 100;
lambda = [ besselzero(0,Nzeros,1)';
           besselzero(1,Nzeros,1)';
           besselzero(2,Nzeros,1)' ];

% calculate psi= sum over zeros
psi = zeros(4,length(r));
for k=1:length(coeffs)
    size_psi = size(psi(:,:) )
    size_coeffs = size(coeffs(k))
    size_basisspinor = size( basisspinor(r(:)', lambda(1,k)/10))
    psi(:,:) = psi(:,:) + coeffs(k) * ...
        basisspinor(r(:), lambda(1,k)/10);
end;
% calculate density (j0)
density = zeros(1,length(r));
for k=1:length(r)
    density(k) = j0(psi(:,k));
end;
% calculate square error
error = sum((1-density(:)).^2);
end

任何阅读上述错误和代码的人都应该有足够的理解。 coeffs是20x3矩阵,r是1x1000(或1000x1)

1 个答案:

答案 0 :(得分:1)

您对basisspinor语句中size的号召是r(:)'作为第一个参数,而第二次拨打basisspinor只有r(:)。我想第一个版本也打算用于第二次调用。