TITLE: Optimization-like question
QUESTION [8 upvotes]: Let's say I have a formula like $ax + by + cz = N$. $a, b, c$, and $N$ are known and cannot be changed. $x, y$, and $z$ are known and can be changed.
The problem is that the equation is not true! My problem (for a program I'm writing) is: how can $x, y$, and $z$ be changed enough that they equal $N$ while differing from their previous values as little as possible?

REPLY [2 votes]: Define $A = [a,b,c]$ and $X=[x, y, z]^T$, so your equation is $A*X = N$. Assume your known point to be $ \bar X = [x_0, y_0,z_0]^T$. Define the residual  to be $R = N - A*\bar X$, since the equation is not true.
The question  becomes $$\min|| X- \bar X ||,s.t. A*X = N$$
The solution $X^*$ that minimize the distance is $X^* = \bar X + A^+R$ where $A^+$ is the Moore_Penrose pseudo inverse of $A$ and is this case since $A$ has linearly independent rows, $A^+ = A^T(AA^T)^{-1}$. The distance is $||X^* - \bar X || = || A^+R||$, you can pick whatever norm you like.
This is also true if $A$ is a matrix instead of a row vector suppose you have a linear equation  system.