Main Content

本页采用了机器翻译。点击此处可查看英文原文。

基于求解器的非负最小二乘法

此示例说明如何使用几种算法来求解线性最小二乘问题,并且约束边界是解为非负。线性最小二乘问题的形式为

minxCx-d2.

在这种情况下,将解限制为非负数,x0

首先,将数组 Cd 加载到您的工作区中。

load particle

查看每个数组的大小。

sizec = size(C)
sizec = 1×2

        2000         400

sized = size(d)
sized = 1×2

        2000           1

C 矩阵有 2000 行和 400 列。因此,为了具有正确的矩阵乘法大小,x 向量有 400 行。为了表示非负性约束,将所有变量的下界设置为零。

lb = zeros(size(C,2),1);

使用 lsqlin 求解问题。

[x,resnorm,residual,exitflag,output] = ...
    lsqlin(C,d,[],[],[],[],lb);
Minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.

要查看优化过程的详细信息,请检查输出结构。

disp(output)
            message: 'Minimum found that satisfies the constraints....'
          algorithm: 'interior-point'
      firstorderopt: 3.6717e-06
    constrviolation: 0
         iterations: 8
       linearsolver: 'sparse'
       cgiterations: []

输出结构显示,lsqlin 使用稀疏内部线性求解器进行内点算法,并经过 8 次迭代才达到约 3.7e-6 的一阶最优性测度。

更改算法

信赖区域反射算法处理边界约束问题。看看它在求解这个问题上表现如何。

options = optimoptions('lsqlin','Algorithm','trust-region-reflective');
[x2,resnorm2,residual2,exitflag2,output2] = ...
    lsqlin(C,d,[],[],[],[],lb,[],[],options);
Local minimum possible.

lsqlin stopped because the relative change in function value is less than the square root of the function tolerance and the rate of change in the function value is slow.
disp(output2)
          algorithm: 'trust-region-reflective'
         iterations: 10
      firstorderopt: 2.7870e-05
       cgiterations: 42
    constrviolation: []
       linearsolver: []
            message: 'Local minimum possible....'

这一次,求解器进行更多次迭代,并得出具有更高(更差)一阶最优性测度的解。

为了改进一阶最优性测度,请尝试将 SubproblemAlgorithm 选项设置为 'factorization'

options.SubproblemAlgorithm = 'factorization';
[x3,resnorm3,residual3,exitflag3,output3] = ...
    lsqlin(C,d,[],[],[],[],lb,[],[],options);
Optimal solution found.
disp(output3)
          algorithm: 'trust-region-reflective'
         iterations: 12
      firstorderopt: 5.5907e-15
       cgiterations: 0
    constrviolation: []
       linearsolver: []
            message: 'Optimal solution found.'

使用此选项可使一阶最优性测度接近于零,这是最好的结果。

变更求解器

尝试使用 lsqnonneg 求解器求解 t 问题,该求解器旨在处理非负线性最小二乘法。

[x4,resnorm4,residual4,exitflag4,output4] = lsqnonneg(C,d);
disp(output4)
    iterations: 184
     algorithm: 'active-set'
       message: 'Optimization terminated.'

lsqnonneg 没有报告一阶最优性测度。相反,调查剩余规范。要查看较低有效位数字,请从每个残差范数中减去 22.5794。

t = table(resnorm - 22.5794, resnorm2 - 22.5794, resnorm3 - 22.5794, resnorm4 - 22.5794,...
    'VariableNames',{'default','trust-region-reflective','factorization','lsqnonneg'})
t=1×4 table
     default      trust-region-reflective    factorization    lsqnonneg 
    __________    _______________________    _____________    __________

    7.5411e-05          4.9186e-05            4.9179e-05      4.9179e-05

默认的 lsqlin 算法比 trust-region-reflective 算法具有更高的残差范数。factorizationlsqnonneg 残差范数甚至更低,并且在此显示精度水平下是相同的。看看哪一个更低。

disp(resnorm3 - resnorm4)
   6.8212e-13

lsqnonneg 残差范数最低,可以忽略不计。然而,lsqnonneg 需要最多的迭代才能收敛。

另请参阅

|

相关主题