Search code examples
matlaboptimizationsolverdata-fitting

Fitting Data with Linear Combination of Vectors in MATLAB with Constraints


Working in MATLAB. I have the following equation:

S = aW + bX + cY + dZ

where S,W,X,Y, and Z are all known n x 1 vectors. I am trying to fit the data of S with a linear combination of the basis vectors W,X,Y, and Z with the constraint of the constants (a,b,c,d) being greater than 0. I have managed to do this in Excel's solver, and have attempted to figure it out on MATLAB, being directed towards functions like fmincon, but I am not overly familiar with MATLAB and feel I am misunderstanding the use of fmincon.

I am looking for help with understanding fmincon's use towards my problem, or redirection towards a more efficient method for solving.

Currently I have:

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);

I am receiving an error stating "A must have 4 column(s)". Where A is referring to my variable data which corresponds to my S in the above equation. I do not understand why it is expecting 4 columns. Also to note the variables that are in my above snippet that are not explicitly defined are defined as [], serving as space holders.


Solution

  • What you are trying to do is similar to the answer I gave at is there a python (or matlab) function that achieves the minimum MSE between given set of output vector and calculated set of vector?.

    The procedure is similar to what you are doing in EXCEL with solver. You create an objective function that takes (a, b, c, d) as the input parameters and output a measure of fit (mse) and then use fmincon or similar solver to get the best (a, b, c, d) that minimize this mse. See the code below (no MATLAB to test it but it should work).

    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