MultiStart Usinglsqcurvefit
或者lsqnonlin
此示例显示如何使用函数来使用lsqcurvefit
together withMultiStart
。The end of the example shows the same solution usinglsqnonlin
。
许多拟合问题有多个本地解决方案。金宝搏官方网站MultiStart
can help find the global solution, meaning the best fit. This example first useslsqcurvefit
because of its convenient syntax.
该模型是
where the input data is , 和the parameters , , , 和 are the unknown model coefficients.
Step 1. Create the objective function.
编写一个采用数据矩阵的匿名函数xdata
withN
行和两列,并返回响应矢量N
行。该功能也采用系数矩阵p
, corresponding to the coefficient vector
。
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 。在响应中包括随机噪声。
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
超过
in absolute value, because the sine function takes values in its full range over any interval of width
。假设系数
必须小于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
lsqcurvefit
finds 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
。
创建一个问题结构MultiStart
can 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 usingMultiStart
with 50 iterations. Plot the smallest error as the number ofMultiStart
iterations.
ms = multiStart('PlotFcns',@gsplotbestf); [xmulti,errormulti] = run(ms,problem,50)
MultiStart完成了所有起始点的运行。所有50个本地求解器运行融合,并使用正本地求解器退出标志。
xmulti =1×4-2.9852 -0.2472 -0.4968 -1.0438
errormulti = 1.6464
MultiStart
finds 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, uselsqnonlin
as 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.
,lsqnonlin
finds the same relatively poor solution aslsqcurvefit
。
RunMultiStart
usinglsqnonlin
作为当地的解决者。
问题2 = createOptimproblem('lsqnonlin','x0',p0,'objective',fitfcn2,。。。'磅',磅,'UB',ub'); [xmultinonlin,errormultinonlin] = run(ms,problem2,50)
MultiStart完成了所有起始点的运行。所有50个本地求解器运行融合,并使用正本地求解器退出标志。
xmultinonlin =1×4-2.9852 -0.2472 -0.4968 -1.0438
errormultinonlin = 1.6464
Again,MultiStart
finds a much better solution than the local solver alone.