# Vector Division

## Recommended Posts

akaitora    399
So recently I came across a problem where I needed to solve for "t" from the line equation

P = P0 + t(P1 - P0)

where P, P0, and P1 are vectors like [1,2,3] and t is a real number.

So I came up with the following equation for t

t = (P - P0) / (P1 - P0)

then it dawned on me that I don't know how to divide 2 vectors and get a scalar. In MatLab, if I divde 2 vectors I get a scalar
but I have no idea what it is doing behind the scenes. Could you please enlighten me?

##### Share on other sites
alvaro    21246
You can't divide vectors in general. If you happen to know that (P-P0) and (P1-P0) are proportional, you can compute their dot product and then divide by the square of the length of (P1-P0).
[code]P = P0 + t * (P1 - P0)
P - P0 = t * (P1 - P0)
dot_product(P - P0, P1 - P0) = dot_product(t * (P1 - P0), P1 - P0)
dot_product(P - P0, P1 - P0) = t * dot_product(P1 - P0, P1 - P0)
t = dot_product(P - P0, P1 - P0) / dot_product(P1 - P0, P1 - P0)
t = dot_product(P - P0, P1 - P0) / length_squared(P1 - P0)[/code]

##### Share on other sites
akaitora    399
Is that how you think MatLab does it?

##### Share on other sites
alvaro    21246
According to the help (it seems to be in the help page for mrdivide), MatLab is solving a least squares problem, which in this case can be done the way I described. So, yes.

##### Share on other sites
Nanoha    2682
From your first equation P = P0 + t(P1 - P0), those are all in one line so you could just use the magnitude of the vectors perhaps (imagine they are all just on the x-axis, then they're just plain old numbers). It should work for that particular equation.

##### Share on other sites
japro    887
[quote name='akaitora' timestamp='1312766167' post='4845990']
So recently I came across a problem where I needed to solve for "t" from the line equation

P = P0 + t(P1 - P0)
[/quote]

that is actually an equation system. You look at each component separately:
P.x = P0.x + t*(P1.x-P0.x)
P.y = P0.y + t*(P1.y-P0.y)
...

you can now solve all of these equations separately (using the formula you already derived, just applied to each component). In the general case this system doesn't have a solution since you will get different solutions for t. A solutions only exist in the case where P lies on the line defined by P0 and P1. In that case you get the same t from both equations.

Can you explain what underlying problem you are trying to solve?

Edit: Somehow I assumed the 2d case...

##### Share on other sites
alvaro    21246
[quote name='japro' timestamp='1312806127' post='4846140']
[quote name='akaitora' timestamp='1312766167' post='4845990']
So recently I came across a problem where I needed to solve for "t" from the line equation

P = P0 + t(P1 - P0)
[/quote]

that is actually an equation system. You look at each component separately:
P.x = P0.x + t*(P1.x-P0.x)
P.y = P0.y + t*(P1.y-P0.y)

you can now solve both of these equations separately (using the formula you already derived, just applied to each component). Now in the general case this system doesn't have a solution since you will get two different solutions for t. A solutions only exist in the case where P lies on the line defined by P0 and P1. In that case you get the same t from both equations.[/quote]

Let me finish that thought. When you have a system of equations with more equations than variables, you generally cannot solve it exactly, but you could still find a value of t that works well as a compromise for all the individual coordinates. You do this by minimizing the distance between P and P0+t*(P1-P0), which is a least squares problem. If you work out the problem (you can do this by orthogonal projection or by differentiating the square of the distance with respect to t), you'll arrive at the solution I posted earlier.