【问题标题】:Fitting Data with Linear Combination of Vectors in MATLAB with Constraints用约束条件在 MATLAB 中用向量的线性组合拟合数据
【发布时间】:2021-05-21 22:24:34
【问题描述】:

在 MATLAB 中工作。我有以下等式:

S = aW + bX + cY + dZ

其中 S、W、X、Y 和 Z 都是已知的 n x 1 向量。我试图用基向量 W、X、Y 和 Z 的线性组合来拟合 S 的数据,其中常数(a、b、c、d)的约束大于 0。我已经设法做到了这在 Excel 的求解器中,并试图在 MATLAB 上解决它,针对 fmincon 之类的函数,但我对 MATLAB 并不太熟悉,觉得我误解了 fmincon。

我正在寻求帮助,以了解 fmincon 对我的问题的使用,或重定向到更有效的解决方法。

目前我有:

initials = [0.2 0.2 0.2 0.2];
fun = @(x)x(1)*W + x(2)*X + x(3)*Y + x(4)*Z;
lb = [0,0,0,0];
soln = fmincon(fun,initials,data,b,Aeq,beq,lb,ub);

我收到一条错误消息,指出“A 必须有 4 列”。其中 A 指的是我的变量数据,它对应于上述等式中的我的 S。我不明白为什么它需要 4 列。还要注意我上面的sn-p中没有明确定义的变量被定义为[],作为空间持有者。

【问题讨论】:

标签: matlab optimization solver data-fitting


【解决方案1】:

在这种情况下,使用 fmincon 是一个巨大的矫枉过正。这就像使用大型重型显微镜来破解坚果......或火星漫游者拖着手推车或......无论如何:)如果您不必处理大量向量,可能没关系。如果您需要适应数十万个此类向量,则可能需要数小时。但是这个经典的解决方案会快几个数量级。

%first make a n x 4 matrix of your vectors;
P=[W,X,Y,Z];
% now your equation looks like this S = P*m where m is 4 x 1 vectro
% containing your coefficients ( m = [a,b,c,d] )
% so solution will be simply
m_1 = inv(P'*P)*P'*S;
% or you can use this form
m_2 = (P'*P)\P'*S;
% or even simpler
m_3 = (S'/P')';
% all three solutions should give exactly same resul
% third solution is the neatest but may not work in every version of matlab

% Your modeled vector will be
Sm = P*m_3; %you can use any of m_1, m_2 or m_3;
% And your residual 
R = S-Sm;

如果您需要处理许多向量,请不要用于循环。因为 Matlab 中的循环非常慢,如果可能,您应该使用矩阵。 S 也可以是 nk 矩阵,其中 k 是您要处理的数字向量。在这种情况下,m 将是 4k 个矩阵。

【讨论】:

    【解决方案2】:

    您尝试做的与我在is there a python (or matlab) function that achieves the minimum MSE between given set of output vector and calculated set of vector? 给出的答案相似。

    该过程类似于您在 EXCEL 中使用求解器执行的操作。您创建一个以 (a, b, c, d) 作为输入参数并输出拟合度量 (mse) 的目标函数,然后使用 fmincon 或类似求解器获得最佳 (a, b, c, d)最小化这个mse。请参阅下面的代码(没有 MATLAB 对其进行测试,但它应该可以工作)。

    function [a, b, c, d] = optimize_abcd(W, X, Y, Z)
    %=========================================================
    %Second argument is the starting point, second to the last argument is the lower bound 
    %to ensure the solutions are all positive
    res = fmincon(@MSE, [0,0,0,0], [], [], [], [], [0,0,0,0], []);
    a=res(1);
    b=res(2);
    c=res(3);
    d=res(4);
    
        function mse = MSE(x_)
            a_=x_(1);
            b_=x_(2);
            c_=x_(3);
            d_=x_(4);
            S_ = a_*W + b_*X + c_*Y + d_*Z
            mse = norm(S_-S);
        end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 2014-04-10
      • 1970-01-01
      • 2014-03-28
      相关资源
      最近更新 更多