主要内容

MultiStart Usinglsqcurvefit或者lsqnonlin

此示例显示如何使用函数来使用lsqcurvefittogether withMultiStart。The end of the example shows the same solution usinglsqnonlin

许多拟合问题有多个本地解决方案。金宝搏官方网站MultiStartcan help find the global solution, meaning the best fit. This example first useslsqcurvefitbecause of its convenient syntax.

该模型是

y = a + b x 1 sin ( c x 2 + d ) ,

where the input data is x = ( x 1 , x 2 ) , 和the parameters a , b , c , 和 d are the unknown model coefficients.

Step 1. Create the objective function.

编写一个采用数据矩阵的匿名函数xdatawithN行和两列,并返回响应矢量N行。该功能也采用系数矩阵p, corresponding to the coefficient vector ( a , b , c , d )

fitfcn.= @(p,xdata)p(1) + p(2)*xdata(:,1).*sin(p(3)*xdata(:,2)+p(4));

Step 2. Create the training data.

Create 200 data points and responses. Use the values a = - 3 , b = 1 / 4 , c = 1 / 2 , d = 1 。在响应中包括随机噪声。

rng.default再现性的百分比n = 200;%数据点数preal = [-3,1/4,1/2,1];% Real coefficientsxdata = 5*rand(N,2);% Data pointsydata = fitfcn(preal,xdata)+ 0.1 * randn(n,1);% Response data with noise

Step 3. Set bounds and initial point.

Set bounds forlsqcurvefit。There is no reason for d 超过 π. in absolute value, because the sine function takes values in its full range over any interval of width 2 π. 。假设系数 c 必须小于20的绝对值,因为允许高频可能导致不稳定的响应或不准确的收敛。

lb = [-Inf,-Inf,-20,-pi]; ub = [Inf,Inf,20,pi];

Set the initial point arbitrarily to (5,5,5,0).

P0.= 5*ones(1,4);% Arbitrary initial pointP0.(4) = 0;%确保初始点满足界限

Step 4. Find the best local fit.

Fit the parameters to the data, starting atP0.

[Xfited,errorfited] = LSQCurveFit(FitFCN,P0,Xdata,Ydata,LB,UB)
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
xfitted =1×4-2.6149 -0.0238 6.0191 -1.6998
errorfitted = 28.2524

lsqcurvefitfinds a local solution that is not particularly close to the model parameter values (–3,1/4,1/2,1).

Step 5. Set up the problem forMultiStart

创建一个问题结构MultiStartcan solve the same problem.

问题=创造eOptimProblem('lsqcurvefit','x0',p0,'objective',fitfcn,。。。'磅',磅,'UB',UB,'xdata',xdata,'ydata',ydata);

Step 6. Find a global solution.

Solve the fitting problem usingMultiStartwith 50 iterations. Plot the smallest error as the number ofMultiStartiterations.

ms = multiStart('PlotFcns',@gsplotbestf); [xmulti,errormulti] = run(ms,problem,50)

Figure MultiStart contains an axes object. The axes object with title Best Function Value: 1.6464 contains 2 objects of type line, text.

MultiStart完成了所有起始点的运行。所有50个本地求解器运行融合,并使用正本地求解器退出标志。
xmulti =1×4-2.9852 -0.2472 -0.4968 -1.0438
errormulti = 1.6464

MultiStartfinds a global solution near the parameter values (–3,–1/4,–1/2,–1). (This is equivalent to a solution nearpreal=(-3,1 / 4,1 / 2,1),因为更改所有系数的符号,除了第一个给出相同的数字值fitfcn.。)残余误差的规范从约28到约1.6减小,降低超过10倍。

制定问题lsqnonlin

For an alternative approach, uselsqnonlinas the fitting function. In this case, use the difference between predicted values and actual data values as the objective function.

fitfcn2 = @(p)fitfcn(p,xdata)-ydata;[xlsqnonlin,errorlsqnonlin] = lsqnonlin(fitfcn2,p0,lb,Ub)
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
xlsqnonlin =1×4-2.6149 -0.0238 6.0191 -1.6998
errorlsqnonlin = 28.2524

Starting from the same initial pointP0.,lsqnonlinfinds the same relatively poor solution aslsqcurvefit

RunMultiStartusinglsqnonlin作为当地的解决者。

问题2 = createOptimproblem('lsqnonlin','x0',p0,'objective',fitfcn2,。。。'磅',磅,'UB',ub'); [xmultinonlin,errormultinonlin] = run(ms,problem2,50)

Figure MultiStart contains an axes object. The axes object with title Best Function Value: 1.6464 contains 2 objects of type line, text.

MultiStart完成了所有起始点的运行。所有50个本地求解器运行融合,并使用正本地求解器退出标志。
xmultinonlin =1×4-2.9852 -0.2472 -0.4968 -1.0438
errormultinonlin = 1.6464

Again,MultiStartfinds a much better solution than the local solver alone.

相关话题