SY.
SY.

Reputation: 181

Finding parameters next to vectors to get a desired vector

What is the simplest algorithm I can use to find such values of m1, m2, m3, ..., mn, that the following equation is satisified (of course to a certain accuracy threshold):

m1*v1 + m2*v2 + ... + mn*vn = vd

where v1, v2, ..., vn and vd are given vectors of 3-10 dimensions? The parameters m1, ..., mn should be positive real numbers.

I need an algorithm that's reliable and quick to code. Problem sizes will be small, (no larger than n=100) so speed isn't a very important issue, especially that the accuracies will be rather liberal.

Upvotes: 4

Views: 67

Answers (1)

javidcf
javidcf

Reputation: 59701

What you are describing is a system of linear equations. You can write it as the following matrix equation:

A * x = b

Where, if k is the dimension of the vectors:

    / v1[1] v2[1] ... vn[1] \
    | v1[2] v2[2] ... vn[2] |
A = | ..................... |
    | ..................... |
    \ v1[k] v2[k] ... vn[k] /

     / m1 \
     | m2 |
 x = | .. |
     | .. |
     \ mn /

    / vd[1] \
    | vd[2] |
b = | ..... |
    | ..... |
    \ vd[k] /

There are several ways to solve these. If n is equal to k and the problem has a solution (which may or may not), then you can solve it by inverting the coefficient matrix A and computing inverse(A) * b, by using Cramer's rule or, most commonly, with Gaussian elmination. If n is not equal to k, several things can happen, you can learn about it googling a little bit.

By the way, you said that m1 ... mn must be positive numbers (non-zero?). In this case, you may want to approach your problem from linear programming, adding restrictions like m1 > 0, m2 > 0, etc. and using simplex algorithm to solve it.

Whatever you use, is really not advisable to program the algorithm by yourself. There are plenty of libraries for every language that deal with this kind of problems.

Upvotes: 2

Related Questions