一个例子: clear all; x1=[0.4292 0.4269 0.381 0.4015 0.4117 0.3017] ; x2=[0.00014 0.00059 0.0126 0.0061 0.00425 0.0443] ; x=[x1 x2]; y=[0.517 0.509 0.44 0.466 0.479 0.309] ; f=@(p,x) 2.*p(1)*(1-1p(2))*(1-(1-x(:,1).^(1p(2.^p(2)).^2.*(x(:,1).^(-1p(2))-1).^(-p(2)).*x(:,1).^(-1p(2)-0.5).*x(:,2); p0=[8 0.5] ; opt=optimset( tolf n ,1e-3, tolx ,1e-3); [p r]=nlinfit(x,y,f,p0,opt); 一开始我以为x只能为一维数据,因为我的程序做出来的y预测值及系数预测均为复数的形式.后来看了这个例子发现不是x维数的原因,其原因是对数函数ln(x)的x值要大于0,我的数据算出来均为负数,所以才出现了这样的情况.所以我将x改为绝对值的形式,就避免了这种情况.并且关于optimset函数,是一个优化函数,也有了很好的启示作用.以下介绍一下optimset的使用方法: 创建或编辑一个最优化参数选项 句法规则 options = optimset( param1 ,val e1, param2 ,val e2..) %设置所有参数及其值,未设置的为默认值options = optimset %全部设置为默认 options = optimset(optimf n) %设置与最优化函数有关的参数为默认options = optimset(oldopts, param1 ,val e1..) %复制一个已存在的选项,修改特定项options = optimset(oldopts,newopts) %用另一个新选项合并目前选项 因素 parameter val e description display off | iter | final | notify off 表示不显示输出; iter 显示每次迭代的结果; final 只显示最终结果; notify 只在函数不收敛的时候显示结果. maxf nevals positive integer 函数允许估值的最大值. maxiter positive integer 迭代次数的最大值. tolf n positive scalar 函数迭代的终止误差. tolx positive scalar 结束迭代的x值. l - 只用于大规模数据拟合 m - 中等规模 b - 两者都可以 parameter name description l, m, b used by f nctions derivativecheck compare ser-s pplied analytic derivatives (gradients or jacobian) to finite differencing derivatives. m fgoalattain, fmincon, fminimax, fmin nc, fseminf, fsolve, lsqc rvefit, lsqnonlin diagnostics print diagnostic information abo t the f nction to be minimized or solved. b all b t fminbnd, fminsearch, fzero, and lsqnonneg diffmaxchange maxim m change in variables for finite difference derivatives. m fgoalattain, fmincon, fminimax, fmin nc, fseminf, fsolve, lsqc rvefit, lsqnonlin diffminchange minim m change in variables for finite difference derivatives. m fgoalattain, fmincon, fminimax, fmin nc, fseminf, fsolve, lsqc rvefit, lsqnonlin display level of display. off displays no o tp t; iter displays o tp t at each iteration; final displays j st the final o tp t; notify displays o tp t only if f nction does not converge. b all. see the individ al f nction reference pages for the val es that apply. goalsexactachieve n mber of goals to achieve exactly (do not over- or nderachieve). m fgoalattain gradconstr gradients for the nonlinear constraints defined by the ser. m fgoalattain, fmincon, fminimax gradobj gradient(s) for the objective f nction(s) defined by the ser. b fgoalattain, fmincon, fminimax, fmin nc, fseminf hessian if on , f nction ses ser-defined hessian, or hessian information (when sing hessm lt), for the objective f nction. if off , f nction approximates the hessian sing finite differences. l fmincon, fmin nc hessm lt hessian m ltiply f nction defined by the ser. l fmincon, fmin nc, q adprog hesspattern sparsity pattern of the hessian for finite differencing. the size of the matrix is n-by-n, where n is the n mber of elements in x0, the starting point. l fmincon, fmin nc hessupdate q asi-newton pdating scheme. m fmin nc jacobian if on , f nction ses ser-defined jacobian, or jacobian information (when sing jacobm lt), for the objective f nction. if off , f nction approximates the jacobian sing finite differences. b fsolve, lsqc rvefit, lsqnonlin jacobm lt jacobian m ltiply f nction defined by the ser. l fsolve, lsqc rvefit, lsqlin, lsqnonlin jacobpattern sparsity pattern of the jacobian for finite differencing. the size of the matrix is m-by-n, where m is the n mber of val es in the first arg ment ret rned by the ser-specified f nction f n, and n is the n mber of elements in x0, the starting point. l fsolve, lsqc rvefit, lsqnonlin largescale use large-scale algorithm if possible. b fmincon, fmin nc, fsolve, linprog, lsqc rvefit, lsqlin, lsqnonlin, q adprog levenbergmarq ardt chooses levenberg-marq ardt over ga ss-newton algorithm. m lsqc rvefit, lsqnonlin linesearchtype line search algorithm choice. m fmin nc, fsolve, lsqc rvefit, lsqnonlin maxf nevals maxim m n mber of f nction eval ations allowed. b fgoalattain, fminbnd, fmincon, fminimax, fminsearch, fmin nc, fseminf, fsolve, lsqc rvefit, lsqnonlin maxiter maxim m n mber of iterations allowed. b all b t fzero and lsqnonneg maxpcgiter maxim m n mber of pcg iterations allowed. l fmincon, fmin nc, fsolve, lsqc rvefit, lsqlin, lsqnonlin, q adprog meritf nction use goal attainmentminimax merit f nction (m ltiobjective) vs. fmincon (single objective). m fgoalattain, fminimax minabsmax n mber of f(x) to minimize the worst case absol te val es m fminimax nonleqnalgorithm choose levenberg-marq ardt or ga ss-newton over the tr st-region dogleg algorithm. m fsolve precondbandwidth upper bandwidth of preconditioner for pcg. l fmincon, fmin nc, fsolve, lsqc rvefit, lsqlin, lsqnonlin, q adprog tolcon termination tolerance on the constraint violation. b fgoalattain, fmincon, fminimax, fseminf tolf n termination tolerance on the f nction val e. b fgoalattain, fmincon, fminimax, fminsearch, fmin nc, fseminf, fsolve, linprog (large-scale only), lsqc rvefit, lsqlin (large-scale only), lsqnonlin, q adprog (large-scale only) tolpcg termination tolerance on the pcg iteration. l fmincon, fmin nc, fsolve, lsqc rvefit, lsqlin, lsqnonlin, q adprog tolx termination tolerance on x. b all f nctions except the medi m-scale algorithms for linprog, lsqlin, and q adprog typicalx typical x val es. the length of the vector is eq al to the n mber of elements in x0, the starting point. l fmincon, fmin nc, fsolve, lsqc rvefit, lsqlin, lsqnonlin, q adprog examples options = optimset( display , iter , tolf n ,1e-8) this statement makes a copy of the options str ct re called options, changing the val e of the tolx parameter and storing new val es in optnew. optnew = optimset(options, tolx ,1e-4);