往往在实际问题中都存在exp(x)、lnx、sinx等多种函数组合的非线性经验公式。对此我们就可以通过lsqnonlin函数进行求解,该函数的方法被称为非线性最小二乘,损失函数一样,只不过类似于优化算法,给定参数初始值,然后优化参数,非线性最小二乘模型如下,即目标函数。 1。lsqnonlin语法 lsqnonlin函数用于求解非线性最小二乘(非线性数据拟合)问题。语法xlsqnonlin(fun,x0)xlsqnonlin(fun,x0,lb,ub)xlsqnonlin(fun,x0,lb,ub,options)xlsqnonlin(problem)〔x,resnorm〕lsqnonlin()〔x,resnorm,residual,exitflag,output〕lsqnonlin()〔x,resnorm,residual,exitflag,output,lambda,jacobian〕lsqnonlin()lb、ub为解向量的下界和上界lbaub,若没有指定界,则lb〔〕,ub〔〕;options为指定的优化参数;fun为待拟合函数,计算x处拟合函数值,其定义为functionFmyfun(a,xdata)resnormsum((fun(a,xdata)ydata)。2),即在a处残差的平方和;residualfun(a,xdata)ydata,即在x处的残差;exitflag为终止迭代的条件;output为输出的优化信息;lambda为解x处的Lagrange乘子;jacobian为解x处拟合函数fun的jacobian矩阵。 2。拟合简单指数 对数据进行简单的指数衰减曲线拟合。 从添加了噪声的指数衰减模型生成数据。模型是: yexp(1。3t) 其中t的范围是从0到3,是均值为0、标准差为0。05的正态分布噪声。 问题表述为:给定数据(d、y),求出与数据拟合最佳的指数衰减率。 创建一个匿名函数,该函数接受指数衰减率r的值作为输入,并返回采用该衰减率的模型与数据之差组成的向量。 程序rngdefaultrng(default)将rand、randi和randn使用的随机数生成器的设置重置为其默认值。这样,会生成相同的随机数,就好像您重新启动了MATLAB。默认设置是种子为0的梅森旋转生成器。dlinspace(0,3);yexp(1。3d)0。05randn(size(d));fun(r)exp(dr)y;找到最佳衰减率的值。任意选择一个初始估计值x04。x04;xlsqnonlin(fun,x0)绘制数据和最佳拟合指数曲线。plot(d,y,ko,d,exp(xd),b)legend(原始数据,拟合数据)xlabel(t)ylabel(exp(tx)) 运行结果Localminimumpossible。lsqnonlinstoppedbecausethefinalchangeinthesumofsquaresrelativetoitsinitialvalueislessthanthevalueofthefunctiontolerance。stoppingcriteriadetailsx1。2645 3。拟合具有边界约束的问题 当某些拟合参数有边界时,找到最佳拟合模型。找到合适的中心化参数b和缩放参数a,以便拟合以下函数:aexp(t)exp(exp((tb)))。 程序最好地拟合以下标准正态密度分布:创建数据点组成的向量t,以及在这些点上的对应正态密度。tlinspace(4,4);y1sqrt(2pi)exp(t。22);创建一个缩放参数a为x(1)且中心化参数b为x(2)的函数,来计算中心化并缩放的函数与正态y之间的差。fun(x)x(1)exp(t)。exp(exp((tx(2))))y;从x0〔12,0〕开始寻找最佳拟合,缩放参数a的值介于12和32之间,中心化参数b的值介于1和3之间。lb〔12,1〕;ub〔32,3〕;x0〔12,0〕;xlsqnonlin(fun,x0,lb,ub)绘制这两个函数来查看拟合的质量。plot(t,y,r,t,fun(x)y,b)xlabel(t)legend(原始数据函数曲线,拟合函数曲线) 运行结果Localminimumpossible。lsqnonlinstoppedbecausethefinalchangeinthesumofsquaresrelativetoitsinitialvalueislessthanthevalueofthefunctiontolerance。stoppingcriteriadetailsx0。82310。2444 4。检查求解过程 在求解过程发生时和发生后都对其进行检查(通过将Display选项设置为iter在发生时进行检查,通过检查output结构体在发生后进行检查)。 假设您有观测时间数据xdata和观测响应数据ydata,并且要求得参数x(1)和x(2)以拟合以下形式的模型:ydatax(1)exp(x(2)xdata)。 程序输入观测时间和响应。xdata。。。〔0。91。513。819。824。128。235。260。374。681。3〕;ydata。。。〔455。2428。6124。167。343。228。113。10。41。31。5〕;创建简单的指数衰减模型。该模型计算预测值和观测值之间的差组成的向量。fun(x)x(1)exp(x(2)xdata)以x0〔100,1〕为起点拟合模型。通过将Display选项设置为iter来检查求解过程。获取output结构体以获取有关求解过程的详细信息。x0〔100,1〕;optionsoptimoptions(lsqnonlin,Display,iter,PlotFcn,optimplotx);〔x,resnorm,residual,exitflag,output〕lsqnonlin(fun,x0,〔〕,〔〕,options)Algorithm在trustregionreflective(默认值)和levenbergmarquardt之间进行选择。options。A〔x1,resnorm1,residual1,exitflag1,output1〕lsqnonlin(fun,x0,〔〕,〔〕,options) 运行结果NormofFirstorderIterationFunccountf(x)stepoptimality033596772。88e04ObjectivefunctionreturnedItryinganewpoint。。。1635967711。69762。88e04293213950。54。97e0431232139514。97e044152922530。257。06e045182922530。57。06e046212703500。1251。15e057242703500。251。15e058272527770。06251。63e059302527770。1251。63e0510332438770。031257。48e0411362436600。06258。7e0412392432760。06252e0413422431740。06251。14e0414452429990。1255。1e0315482426610。252。04e0316512419870。51。91e03175424064311。04e03185723797123。36e03196023268646。04e03206322235481。2e042166202592162。25e042269166443324。05e042372106320646。68e04247528704。71288。31e04257889。7947140。6742。22e0426819。573812。0259968427849。504890。06199262。2728879。504890。0004622620。0114Localminimumpossible。lsqnonlinstoppedbecausethefinalchangeinthesumofsquaresrelativetoitsinitialvalueislessthanthevalueofthefunctiontolerance。stoppingcriteriadetailsx498。83090。1013resnorm9。5049residual0。18170。06100。76280。11960。26590。59791。02611。51241。56151。6327exitflag3output包含以下字段的struct:firstorderopt:0。0114iterations:28funcCount:87cgiterations:0algorithm:trustregionreflectivestepsize:4。6226e04message:Localminimumpossible。lsqnonlinstoppedbecausethefinalchangeinthesumofsquaresrelativetoitsinitialvalueislessthanthevalueofthefunctiontolerance。stoppingcriteriadetailsOptimizationstoppedbecausetherelativesumofsquares(r)ischangingbylessthanoptions。FunctionTolerance1。000000e06。FirstOrderNormofIterationFunccountResidualoptimalityLambdastep033596772。88e040。01ObjectivefunctionreturnedItryinganewpoint。。。1133407613。91e041000000。2807772163046615。97e04100000。3731463212972926。55e041e060。05899334242882407。57e041000000。06454445282754071。01e051e060。07412666312499541。62e051000000。0945717362458961。35e051e070。01336068392438467。26e041e060。009443119422435685。66e041000000。0082162210452434241。61e04100000。0077793611482433228。8e0310000。067393312512424085。1e031000。67520913542336281。05e04106。5980414571690898。51e04154。6992156030814。71。54e050。1196。9391663147。4968e030。01129。79517669。515031170。0019。9606918699。504890。07140。00010。08048619729。504894。96e051e055。07028e05Localminimumpossible。lsqnonlinstoppedbecausetherelativesizeofthecurrentstepislessthanthevalueofthestepsizetolerance。stoppingcriteriadetailsx1498。83090。1013resnorm19。5049residual10。18170。06100。76280。11960。26590。59791。02611。51241。56151。6327exitflag14output1包含以下字段的struct:iterations:19funcCount:72stepsize:5。0703e05cgiterations:〔〕firstorderopt:4。9629e05algorithm:levenbergmarquardtmessage:Localminimumpossible。lsqnonlinstoppedbecausetherelativesizeofthecurrentstepislessthanthevalueofthestepsizetolerance。stoppingcriteriadetailsOptimizationstoppedbecausetherelativenormofthecurrentstep,1。016433e07,islessthanoptions。StepTolerance1。000000e06。 5。属性设置 优化选项,指定为optimoptions的输出或optimset返回的结构体。MaxIterations允许的迭代最大次数,为正整数。默认值为400。OptimalityTolerance一阶最优性的终止容差(正标量)。默认值为1e6。PlotFcn对算法执行过程中的各种进度测量值绘图,可以选择预定义的绘图,也可以自行编写绘图函数。传递名称、函数句柄或者由名称或函数句柄组成的元胞数组。对于自定义绘图函数,传递函数句柄。默认值是无(〔〕):optimplotx绘制当前点。optimplotfunccount绘制函数计数。optimplotfval绘制函数值。optimplotresnorm绘制残差范数。optimplotstepsize绘制步长大小。optimplotfirstorderopt绘制一阶最优性度量。 参考内容 〔1〕https:ww2。mathworks。cnhelpoptimuglsqnonlin。html 作者:郭志龙 编辑:郭志龙 校对:郭志龙