Reputation: 1
I have multiple linear equations in the form of Zi=ai*Xi+bi*Yi
for i = 1..30
.
How can I calculate every pair of regression coefficient values, or those 30 values of a
and b
for each (Z,X,Y)
combination using MATLAB?
I've tried the following code:
A=Z; B=[Xs Ys];
C = B \ A;
A
are my Z
points while B
is a matrix of my X
and Y
points. However, I seem to only get one pair of regression coefficients for all of the points.
Thanks in advance!
Upvotes: 0
Views: 633
Reputation: 104484
What you have set up there is unfortunately not the right way to solve it if I understand your problem formulation. That assumption assumes that you are trying to fit all of the points on a single line. Each row of B
would thus serve as one point on only one line that you are trying to find the linear regression of. If you want to solve for multiple lines simultaneously, you are going to need to change your formulation.
That is actually very simple. I'm going to assume that you have 30 (x,y)
points where each point denotes one equation of a line. You have these set as Xs
and Ys
respectively. The outputs for each of these equations is also in Zs
. I'm also going to assume these are column vectors, and therefore, you have a system set up such that:
a_i
and b_i
are the coefficients for each line. You know the (x,y)
for each line and your goal is to solve for each corresponding a
and b
. As such, you would need to reformulate your system so that you're solve for a
and b
.
Rewriting that problem in matrix form, it can be done like so:
The right hand side vector of a_1, b_1, a_2, b_2, ...
is what you are ultimately solving for. You can see that we have a matrix equation of Y = M*X
where M
and Y
are known and X
is what we need to solve for by doing X = M\Y
. As such, all you need to rearrange your x
and y
values into a block matrix like the above. First we need to find the correct linear indices so that we can place our x
and y
values into this matrix, then solve the system by least squares with the ldivide
operator. The matrix is a N x 2N
matrix where N
is the total number of equations or constraints that we have (so in your case, 30):
N = numel(Xs);
M = zeros(N, 2*N);
xind = sub2ind(size(M), 1:N, 1:2:2*N);
yind = sub2ind(size(M), 1:N, 2:2:2*N);
M(xind) = Xs;
M(yind) = Ys;
sub2ind
allows you to place multiple values into a matrix with a single line of code. Specifically, sub2ind
determine the linear indices from a set of row and column coordinates to access into a matrix. If you don't already know, you can access values (and set values) in a matrix using a single number instead of a pair of row and columns. sub2ind
will allow you to set multiple values in a matrix at once by specifying a set of linear indices to access in the matrix with a corresponding vector.
In our case, we need two sets of linear indices - one for the x
values and one for the y
values. Note that the x
values start from the first column and skip every other column. The same behaviour can be said for the y
values but we start at the second column. Once we have those indices, we set the x
and y
values in this matrix and we now we simply solve for the coefficients:
coeff = M \ Z;
coeff
will now be 2N x 1
vector, so if you want, you can reshape this into a matrix:
coeff = reshape(coeff, 2, []);
Now, coeff
will be shaped such that each column will give you the pair of a,b
for each equation that you had. As such, the first column denotes a_1, b_1
, the second column denotes a_2, b_2
and so on. The first row of coeff
is all of the a
coefficients for each constraint while the second row is all of the b
coefficients for each constraint.
Upvotes: 0