I have gps trackings that I know they fall into a certain pattern - a line with a known angle. How do I find the line that minimizes the distances of the points from it but is in the correct angle?

Unfortunately, I can't post an image for example.

  • 1
    $\begingroup$ Welcome to Mathematics Stack Exchange! I would suggest you to explain a little bit how you would try to solve it, at least your thoughts about it, so other people could help you better. Good luck! $\endgroup$ – iadvd Jul 29 '15 at 8:01

If I well understand, you have a set of $n$ points $(x_1,y_1),(x_2,y_2),...,(x_k,y_k),..., (x_n,y_n)$ and you want to fit a straight line $y=ax+b$ which coefficient $a$ is known.

This is a regression problem of the kind $Y(x)=b$ where $Y(x)=y(x)-ax$

Hense $$b\simeq \frac{1}{n}\sum_{k=1}^n (y_k-ax_k)$$

In this particular case ($a$ known), the adjusted value of $b$ is the same if we consider the orthogonal distances.

  • $\begingroup$ Why $\simeq$ and not $=$ ? Cheers $\endgroup$ – Claude Leibovici Jul 29 '15 at 9:06
  • $\begingroup$ OK for $=$ if you prefer. Cheers ! $\endgroup$ – JJacquelin Jul 29 '15 at 9:18
  • $\begingroup$ Hi JJacquelin, thank you for your answer, this is exactly what I was looking for. Do you know how can I also get the estimated points on the line? $\endgroup$ – user3316066 Jul 29 '15 at 11:59
  • $\begingroup$ What do you mean exactly as "estimated points on the line" ? $\endgroup$ – JJacquelin Jul 29 '15 at 12:17
  • $\begingroup$ Sorry, I mean, is there a way, after I have the equation, to match a point on the line for every point from the input? $\endgroup$ – user3316066 Jul 29 '15 at 15:35

Consider your data as points $(x_i,y_i)$ and you have $n$ data points, then you want to find $\beta_1 ,\beta_2$ such that $y\simeq\beta_1+\beta_2x$.
$$ X\beta= \begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ 1 & x_3 \\ \vdots & \vdots\\ 1 & x_n\\ \end{bmatrix} \begin{bmatrix} \beta_1\\ \beta_2\\ \end{bmatrix}=\begin{bmatrix} y_1\\ y_2\\ \vdots\\ y_n \end{bmatrix}=y $$ If you have received lots of data from gps your $X$ matrix is tall and a pseudoinverse can be found as:

$$ X^+=(X^TX)^{-1}X^T $$

And then you can find the solution:

$$ \beta=X^+y $$

It's MATLAB code is


See here for more information.

If $\beta_2$ is known and you want to find $\beta_1$ such that, $y-\beta_2x\simeq\beta_1$ then

$$ X\beta= \begin{bmatrix} 1 \\ 1 \\ 1 \\ \vdots\\ 1 \\ \end{bmatrix} \begin{bmatrix} \beta_1\\ \end{bmatrix}=\begin{bmatrix} y_1-\beta_2x_1\\ y_2-\beta_2x_2\\ \vdots\\ y_n-\beta_2x_n \end{bmatrix} $$

$$ X^+=(X^TX)^{-1}X^T=(\begin{bmatrix} 1 & 1 & 1 & \cdots& 1 \end{bmatrix} \begin{bmatrix} 1 \\ 1 \\ 1 \\ \vdots\\ 1 \\ \end{bmatrix})^{-1} \begin{bmatrix} 1 & 1 & 1 & \cdots& 1 \end{bmatrix} $$ And the answer will be:

$$ \beta_1=X^+y=\frac{1}{n}\begin{bmatrix} 1 & 1 & 1 & \cdots& 1 \end{bmatrix}\begin{bmatrix} y_1-\beta_2x_1\\ y_2-\beta_2x_2\\ \vdots\\ y_n-\beta_2x_n \end{bmatrix}$$ $$ \beta_1=\frac{1}{n}\sum_{k=1}^n (y_i-\beta_2x_i) $$

  • 1
    $\begingroup$ If I well understand the question of user3316066, the parameter $\beta_2$ is known and given as an initial input. So, the regression is of lower case (no need to optimise and compute another value for $\beta_2$). $\endgroup$ – JJacquelin Jul 29 '15 at 8:28
  • $\begingroup$ Edited to work with both assumptions $\endgroup$ – SMA.D Jul 29 '15 at 8:48
  • 1
    $\begingroup$ Thank you for this excellent explanation. It's the best that I found after searching the net for about 15 minutes. $\endgroup$ – Carlo Wood May 14 '18 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.