⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fminsearch.cpp

📁 该程序为MATLAB对CDMA系统个部分的仿真
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//
// MATLAB Compiler: 2.0
// Date: Tue Jun 25 14:41:58 2002
// Arguments: "-p" "fcpp" 
//
#include "fminsearch.hpp"

//
// The function "Mfminsearch" is the implementation version of the "fminsearch"
// M-function from file "D:\MATLABR11\toolbox\matlab\funfun\fminsearch.m"
// (lines 1-280). It contains the actual compiled code for that M-function. It
// is a static function and must only be called from one of the interface
// functions, appearing below.
//
//
// function [x,fval,exitflag,output] = fminsearch(funfcn,x,options,varargin)
//
static mwArray Mfminsearch(mwArray * fval,
                           mwArray * exitflag,
                           mwArray * output,
                           int nargout_,
                           mwArray funfcn,
                           mwArray x_,
                           mwArray options,
                           mwArray varargin) {
    mwArray x(mclGetUninitializedArray());
    mwAnsArray ans;
    mwArray chi(mclGetUninitializedArray());
    mwArray convmsg1(mclGetUninitializedArray());
    mwArray defaultopt(mclGetUninitializedArray());
    mwArray f(mclGetUninitializedArray());
    mwArray formatsave(mclGetUninitializedArray());
    mwArray func_evals(mclGetUninitializedArray());
    mwArray fv(mclGetUninitializedArray());
    mwArray fxc(mclGetUninitializedArray());
    mwArray fxcc(mclGetUninitializedArray());
    mwArray fxe(mclGetUninitializedArray());
    mwArray fxr(mclGetUninitializedArray());
    mwArray header(mclGetUninitializedArray());
    mwArray how(mclGetUninitializedArray());
    mwForLoopIterator iterator_0;
    mwArray itercount(mclGetUninitializedArray());
    mwArray j(mclGetUninitializedArray());
    mwArray maxfun(mclGetUninitializedArray());
    mwArray maxiter(mclGetUninitializedArray());
    mwArray msg(mclGetUninitializedArray());
    mwArray n(mclGetUninitializedArray());
    mwArray nargin_(mclGetUninitializedArray());
    mwArray nargout(nargout_);
    mwArray numberOfVariables(mclGetUninitializedArray());
    mwArray one2n(mclGetUninitializedArray());
    mwArray onesn(mclGetUninitializedArray());
    mwArray printtype(mclGetUninitializedArray());
    mwArray prnt(mclGetUninitializedArray());
    mwArray psi(mclGetUninitializedArray());
    mwArray rho(mclGetUninitializedArray());
    mwArray sigma(mclGetUninitializedArray());
    mwArray tolf(mclGetUninitializedArray());
    mwArray tolx(mclGetUninitializedArray());
    mwArray two2np1(mclGetUninitializedArray());
    mwArray usual_delta(mclGetUninitializedArray());
    mwArray v(mclGetUninitializedArray());
    mwArray xbar(mclGetUninitializedArray());
    mwArray xc(mclGetUninitializedArray());
    mwArray xcc(mclGetUninitializedArray());
    mwArray xe(mclGetUninitializedArray());
    mwArray xin(mclGetUninitializedArray());
    mwArray xr(mclGetUninitializedArray());
    mwArray y(mclGetUninitializedArray());
    mwArray zero_term_delta(mclGetUninitializedArray());
    nargin_ = nargin(1, mwVarargin(funfcn, x_, options, varargin));
    mwValidateInputs("fminsearch", 3, &funfcn, &x_, &options);
    x.CopyInputArg(x_);
    //
    // %FMINSEARCH Multidimensional unconstrained nonlinear minimization (Nelder-Mead).
    // %   X = FMINSEARCH(FUN,X0) returns a vector X that is a local
    // %   minimizer of the function that is described in FUN (usually an M-file: FUN.M)
    // %   near the starting vector X0.  FUN should return a scalar function value when 
    // %   called with feval: F=feval(FUN,X).  See below for more options for FUN.
    // %
    // %   X = FMINSEARCH(FUN,X0,OPTIONS)  minimizes with the default optimization
    // %   parameters replaced by values in the structure OPTIONS, created
    // %   with the OPTIMSET function.  See OPTIMSET for details.  FMINSEARCH uses
    // %   these options: Display, TolX, TolFun, MaxFunEvals, and MaxIter. 
    // %
    // %   X = FMINSEARCH(FUN,X0,OPTIONS,P1,P2,...) provides for additional
    // %   arguments which are passed to the objective function, F=feval(FUN,X,P1,P2,...).
    // %   Pass an empty matrix for OPTIONS to use the default values.
    // %   (Use OPTIONS = [] as a place holder if no options are set.)
    // %
    // %   [X,FVAL]= FMINSEARCH(...) returns the value of the objective function,
    // %   described in FUN, at X.
    // %
    // %   [X,FVAL,EXITFLAG] = FMINSEARCH(...) returns a string EXITFLAG that 
    // %   describes the exit condition of FMINSEARCH.  
    // %   If EXITFLAG is:
    // %     1 then FMINSEARCH converged with a solution X.
    // %     0 then the maximum number of iterations was reached.
    // %   
    // %   [X,FVAL,EXITFLAG,OUTPUT] = FMINSEARCH(...) returns a structure
    // %   OUTPUT with the number of iterations taken in OUTPUT.iterations.
    // %
    // %   The argument FUN can be an inline function:
    // %      f = inline('norm(x)');
    // %      x = fminsearch(f,[1;2;3]);
    // %
    // %   FMINSEARCH uses the Nelder-Mead simplex (direct search) method.
    // %
    // %   See also FMINBND, OPTIMSET, OPTIMGET. 
    // 
    // %   Reference: Jeffrey C. Lagarias, James A. Reeds, Margaret H. Wright,
    // %   Paul E. Wright, "Convergence Properties of the Nelder-Mead Simplex
    // %   Algorithm in Low Dimensions", May 1, 1997.  To appear in the SIAM 
    // %   Journal of Optimization.
    // %
    // %   Copyright (c) 1984-98 by The MathWorks, Inc.
    // %   $Revision: 1.8 $  $Date: 1998/10/23 20:52:22 $
    // 
    // defaultopt = optimset('display','final','maxiter','200*numberOfVariables',...
    //
    defaultopt
      = Noptimset(
          1,
          mwVarargin(
            "display",
            "final",
            "maxiter",
            "200*numberOfVariables",
            "maxfunevals",
            "200*numberOfVariables",
            "TolX",
            1e-4,
            "TolFun",
            1e-4));
    //
    // 'maxfunevals','200*numberOfVariables','TolX',1e-4,'TolFun',1e-4);
    // % If just 'defaults' passed in, return the default options in X
    // if nargin==1 & nargout <= 1 & isequal(funfcn,'defaults')
    //
    {
        mwArray a_(nargin_ == mwArray(1.0));
        if (tobool(a_)) {
            a_ = a_ & nargout <= mwArray(1.0);
        } else {
            a_ = 0;
        }
        if (tobool(a_)
            && tobool(a_ & isequal(funfcn, mwVarargin("defaults")))) {
            //
            // x = defaultopt;
            //
            x = defaultopt;
            //
            // return
            //
            goto return_;
        } else {
        }
    //
    // end
    //
    }
    //
    // 
    // if nargin<3, options = []; end
    //
    if (tobool(nargin_ < mwArray(3.0))) {
        options = empty();
    }
    //
    // n = prod(size(x));
    //
    n = prod(size(mwValueVarargout(), x));
    //
    // numberOfVariables = n;
    //
    numberOfVariables = n;
    //
    // 
    // options = optimset(defaultopt,options);
    //
    options = Noptimset(1, mwVarargin(defaultopt, options));
    //
    // printtype = optimget(options,'display');
    //
    printtype = optimget(options, "display");
    //
    // tolx = optimget(options,'tolx');
    //
    tolx = optimget(options, "tolx");
    //
    // tolf = optimget(options,'tolfun');
    //
    tolf = optimget(options, "tolfun");
    //
    // maxfun = optimget(options,'maxfuneval');
    //
    maxfun = optimget(options, "maxfuneval");
    //
    // maxiter = optimget(options,'maxiter');
    //
    maxiter = optimget(options, "maxiter");
    //
    // % In case the defaults were gathered from calling: optimset('fminsearch'):
    // if ischar(maxfun)
    //
    if (tobool(ischar(maxfun))) {
        //
        // maxfun = eval(maxfun);
        //
        maxfun
          = (error(
               "Run-time Error: File: fminsearch Line: 66 Column: 12 T"
               "he Compiler does not support EVAL or INPUT functions"),
             mwArray::DIN);
    //
    // end
    //
    }
    //
    // if ischar(maxiter)
    //
    if (tobool(ischar(maxiter))) {
        //
        // maxiter = eval(maxiter);
        //
        maxiter
          = (error(
               "Run-time Error: File: fminsearch Line: 69 Column: 13 T"
               "he Compiler does not support EVAL or INPUT functions"),
             mwArray::DIN);
    //
    // end
    //
    }
    //
    // 
    // switch printtype
    // case {'none','off'}
    //
    if (switchcompare(printtype, cellhcat(mwVarargin("none", "off")))) {
        //
        // prnt = 0;
        //
        prnt = 0.0;
    //
    // case 'iter'
    //
    } else if (switchcompare(printtype, "iter")) {
        //
        // prnt = 2;
        //
        prnt = 2.0;
    //
    // case 'final'
    //
    } else if (switchcompare(printtype, "final")) {
        //
        // prnt = 1;
        //
        prnt = 1.0;
    //
    // case 'simplex'
    //
    } else if (switchcompare(printtype, "simplex")) {
        //
        // prnt = 3;
        //
        prnt = 3.0;
    //
    // otherwise
    //
    } else {
        //
        // prnt = 1;
        //
        prnt = 1.0;
    //
    // end
    //
    }
    //
    // 
    // header = ' Iteration   Func-count     min f(x)         Procedure';
    //
    header = " Iteration   Func-count     min f(x)         Procedure";
    //
    // 
    // % Convert to inline function as needed.
    // funfcn = fcnchk(funfcn,length(varargin));
    //
    funfcn = fcnchk(funfcn, mwVarargin(length(varargin)));
    //
    // 
    // n = prod(size(x));
    //
    n = prod(size(mwValueVarargout(), x));
    //
    // 
    // % Initialize parameters
    // rho = 1; chi = 2; psi = 0.5; sigma = 0.5;
    //
    rho = 1.0;
    chi = 2.0;
    psi = 0.5;
    sigma = 0.5;
    //
    // onesn = ones(1,n);
    //
    onesn = ones(mwVarargin(1.0, n));
    //
    // two2np1 = 2:n+1;
    //
    two2np1 = colon(2.0, n + mwArray(1.0));
    //
    // one2n = 1:n;
    //
    one2n = colon(1.0, n);
    //
    // 
    // % Set up a simplex near the initial guess.
    // xin = x(:); % Force xin to be a column vector
    //
    xin = x(colon());
    //
    // v = zeros(n,n+1); fv = zeros(1,n+1);
    //
    v = zeros(mwVarargin(n, n + mwArray(1.0)));
    fv = zeros(mwVarargin(1.0, n + mwArray(1.0)));
    //
    // v = xin;    % Place input guess in the simplex! (credit L.Pfeffer at Stanford)
    //
    v = xin;
    //
    // x(:) = xin;    % Change x to the form expected by funfcn 
    //
    x(colon()) = xin;
    //
    // fv = feval(funfcn,x,varargin{:}); 
    //
    fv
      = feval(
          mwValueVarargout(),
          FevalLookup(funfcn, 0, NULL),
          mwVarargin(x, varargin.cell(colon())));
    //
    // 
    // % Following improvement suggested by L.Pfeffer at Stanford
    // usual_delta = 0.05;             % 5 percent deltas for non-zero terms
    //
    usual_delta = 0.05;
    //
    // zero_term_delta = 0.00025;      % Even smaller delta for zero elements of x
    //
    zero_term_delta = 0.00025;
    //
    // for j = 1:n
    //
    for (iterator_0.Start(1.0, n, mwArray::DIN); iterator_0.Next(&j); ) {
        //
        // y = xin;
        //
        y = xin;
        //
        // if y(j) ~= 0
        //
        if (tobool(y(j) != mwArray(0.0))) {
            //
            // y(j) = (1 + usual_delta)*y(j);
            //
            y(j) = (mwArray(1.0) + usual_delta) * y(j);
        //
        // else 
        //
        } else {
            //
            // y(j) = zero_term_delta;
            //
            y(j) = zero_term_delta;
        //
        // end  
        //
        }
        //
        // v(:,j+1) = y;
        //
        v(colon(), j + mwArray(1.0)) = y;
        //
        // x(:) = y; f = feval(funfcn,x,varargin{:});
        //
        x(colon()) = y;
        f
          = feval(
              mwValueVarargout(),
              FevalLookup(funfcn, 0, NULL),
              mwVarargin(x, varargin.cell(colon())));
        //
        // fv(1,j+1) = f;
        //
        fv(1.0, j + mwArray(1.0)) = f;
    //
    // end     
    //
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -