Problem Summary
I am trying to estimate the (x,y)
coordinates of each node in a graph, where I know the distances between connected nodes. For example
Given this graph, I can define an equation for the length of each link simply using the Pythagorean theorem.
$$L_{AB}=\sqrt{(X_B-X_A)^2+(Y_B-Y_A)^2}$$ $$L_{AD}=\sqrt{(X_D-X_A)^2+(Y_D-Y_A)^2}$$ $$L_{BC}=\sqrt{(X_C-X_B)^2+(Y_C-Y_B)^2}$$ $$L_{BD}=\sqrt{(X_D-X_B)^2+(Y_D-Y_B)^2}$$ $$L_{CD}=\sqrt{(X_D-X_C)^2+(Y_D-Y_C)^2}$$
Solution Approach
In this problem I know all of the L
values, and I need to solve for all the X
and Y
values. I can arbitrarily pick a node, and call it the origin, so I can say A = (0,0)
, then I have 6 unknowns $X_{B-D}$ and $Y_{B-D}$.
I figure I can pose this question as minimization of the sum of the square of the errors, mathematically
$$f=(\sqrt{(X_B-X_A)^2+(Y_B-Y_A)^2}-L_{AB})^2\\+(\sqrt{(X_D-X_A)^2+(Y_D-Y_A)^2}-L_{AD})^2\\+(\sqrt{(X_C-X_B)^2+(Y_C-Y_B)^2}-L_{BC})^2\\+(\sqrt{(X_D-X_B)^2+(Y_D-Y_B)^2}-L_{BD})^2\\+(\sqrt{(X_D-X_C)^2+(Y_D-Y_C)^2}-L_{CD})^2)$$
Question
I'm not sure how to perform a minimization of a function in C++ (I'm not too familiar with this area, so I apologize if that is a broad question). If I were in MATLAB
, I could make some initial guesses
X = [0 1 2 3];
Y = [0 2 1 3];
Then call fminunc
fminunc(@f, [X,Y])
This would return an array that I could pull apart into the X
and Y
coordinates.
Basically in C++ I can define some function
double f(double* A)
{
// Let A be an array of all X and Y coordinates
// This function is the error function described above
}
I would like to write (or hopefully find!) a function that does the same thing as fminunc
but in C++, so I guess it would look like
double* fminunc(double(*f)(double*), A);
Where f
is the pointer to my function to minimize, and A
is my array of initial guesses, and it would return an array of the actual solution values.
Does such a function exist already? Otherwise, is there a name of a technique I can search for, again I'm not too familiar with this field, so something like "Jones' Method" could get me started in the right direction, I just need to know what I'm looking for.
Edit
As @TylerOlsen indicated, depending on the number of nodes and number of links, it is possible that this system may be under-constrained. Are there techniques that can provide a valid solution, even if there is no unique solution? The background to this problem is I'm working on a certain flavor of shortest-path algorithm, and as long as the coordinates respect the edge lengths, I think any valid solution should work.