Search code examples
matlableast-squares

solve multidimensional equation using least square method in matlab


How do I get the coefficient a and b from this equation using least square method? What is the best way to solve this?

enter image description here

Let's say θ(k1,k2) is a matrix of 60x60 (constant/values), that is theta=rand(60,60), but

enter image description here

How do I solve for a and b in matlab? Any easy function to do it?

Thanks in advance!

Reference paper: Here (section III)


Solution

  • You can use the regress function to do this. Here is an example:

    % Generate an example
    n = 60;
    theta = rand(n);
    
    % Create regressors
    [M,N] = meshgrid(1:n,1:n);
    X = [M(:), N(:)];
    
    % Regress
    B=regress(theta(:), X);
    
    % Compare the results
    theta_hat = reshape(X*B,n,n);
    plot3(M,N,theta,'o');
    hold on;
    surf(M,N,theta_hat);
    

    Notice that the regression is done on theta(:) which is a (3600,1) vector containing the values of theta(k1,k2) uses the corresponding coordinates in X which is (3600,2). The first column of X is k1, the second is k2.

    The result of calling regress gives you B=[a;b] the coefficients that best fit the data in theta.

    One final note is that the least squares could be solved directly using

    B=inv(X'*X)*X'*theta(:)
    

    which should give the same result, but regress is the preferred MATLAB method.