I have N lines that are defined by a y-intercept and an angle, q. The constraint is that all N lines must intersect at one point. The equations I can come up with to eventually get the constraint are these:
Y = tan(q(1))X + y(1)
Y = tan(q(2))X + y(2)
...
I can, by hand, get the constraint if N = 3 or 4 but I am having trouble just getting one constraint if N is greater than 4. If N = 3 or 4, then when I solve the equations above for X, I get 2 equations and then can just set them equal to each other. If N > 4, I get more than 2 equations that equal X and I dont know how to condense them down into one constraint. If I cannot condense them down into one constraint and am able to solve the optimization problem with multiple constraints that are created dynamically (depending on the N that is passed in) that would be fine also.
To better understand what I am doing I will show how I get the constraints for N = 3. I start off with these three equations:
Y = tan(q(1))X + y(1)
Y = tan(q(2))X + y(2)
Y = tan(q(3))X + y(3)
I then set them equal to each other and get these equations:
tan(q(1))X + y(1) = tan(q(2))X + y(2)
tan(q(2))X + y(2) = tan(q(3))X + y(3)
I then solve for X and get this constraint:
(y(2) - y(1)) / (tan(q(1)) - tan(q(2))) = (y(3) - y(2)) / (tan(q(2)) - tan(q(3)))
Notice how I have 2 equations to solve for X. When N > 4 I end up with more than 2. This is OK if I am able to dynamically create the constraints and then call an optimization function in MATLAB that will handle multiple constraints but so far have not found one.
You say the optimization algorithm needs to adjust
qsuch that the “real” problem is minimized while the above equations also hold.Note that the fifth Euclid axoim ensures that all lines will always intersect with all other lines, unless two
qs are equal but the correspondingy0s are not. This last case is so rare (in a floating point context) that I’m going to skip it here, but for added robustness, you should eventually include it.Now, first, think in terms of matrices. Your constraints can be formulated by the matrix equation:
where
q,yandy0are[Nx1]matrices,xan unknown scalar. Note thaty = c*ones(N,1), e.g., a matrix containing only the same constant. This is actually a non-linear constraint — that is, it cannot be expressed aswith
Asome design matrix andbsome solution vector. So, you’ll have to write a function defining this non-linear constraint, which you can pass on to an optimizer likefmincon. From the documentation:Note that you were actually going in the right direction. You can solve for the
x-location of the intersection for any pair of linesq(n),y0(n)andq(m),y0(m)with the equation:Your
nonlconfunction should findxfor all possible pairsn,m, and check if they are all equal. You can do this conveniently something like so:Note that you’re not actually solving for
qexplicitly (or need the y-coordinate of the intersection at all) — that is allfmincon‘s job.You will need to do some experimenting, because sometimes it is sufficient to define
which will be faster (less derivatives to compute), but other problems really need
or similar tricks. It really depends on the rest of the problem how difficult the optimizer will find each case.