MATLAB: How to obtain integer solutions only with fsolve

fsolveMATLABnonlinear

I am trying to solve a system of nonlinear equations which consists of 3 equations with 4 unknowns by applying fsolve. In general, there are infinitely many solutions. However, one of the unknowns can take only integer values from 1 to 5. Thus, the number of solutions are restricted to at most 5. I do not how to impose that constraint: that one of the unknowns is an integer, and that it lies between [1,5].

Best Answer

Far too late to answer this, but worth explaining how to solve it.
Intlinprog, has been suggested is not a viable solution, because the problem is apparently nonlinear.
A problem with 3 equations in 4 unknowns, IF a solution locus exists, will typically have infinitely many solutions, lying along some curvilinear path through the 4 dimensional parameter space of your unknowns. It would generally be a 1-dimensional locus, although scattered points might exist as solutions too. But think a 1-d path, embedded in the 4-d parameter space. But if one of those parameters may take on only 5 possible values, this restricts that to a finite set of solutions in general.
That integer constraint actually makes the problem much simpler.
Just solve the problem 5 times. Substitute the 5 possible integer values, essentially eliminating that variable from the problem. Then try to solve the problem each time using any normal solver that you wish, with the one integer value fixed. It is only 5 calls, so not a serious problem. Take the solution set that makes you happy, assuming a solution is found.
Remember that fsolve need not always find a solution, and that the solution it does find is dependent up on the starting values provided.
The nice thing is once you fix that integer parameter, the problem now reduces to one with 3 equations in 3 unknowns. So in fact, there should be a FINITE number of solutions. You might even be able to use a tool like solve here, hoping to find a more complete set of solutions, since the problem is now a system of 3 equations in 3 unknowns.
If all 4 unknowns were fully continuous, thus with 3 equations in 4 unknowns, the approach I have outlined would still work to some extent. You would get what is a snapshot of solution(s) at the given value of the eliminated variable. But with the integer constraint, it is easily solved.
If you still need more explanation, perhaps an example is best. I'll make it a really simple one. For example, I'll pose ONE nonlinear equation, in two unknowns.
x^2 + 2*y^2 = 48
There it is, one equation, two unknowns. Yes, I know, that is the equation of an ellipse. (Its a secret. Don't tell anyone.) I'll add one confounding factor, that y may take on ONLY the integer values 1:5.
Yes, you could try to use some nonlinear mixed integer programming tool. But that would only yield ONE solution, at best, if it found one at all.
What matters here is you know that y takes on a VERY limited set of possible values. So we could simply write the problem as
x^2 = 48 - 2*y.^2
Of course, this problem is simple that we should now recognize that for each value of y, x takes on exactly two solutions. At most two real solutions. For some values of y, the only solutions will be complex valued. Thus, when y==5, it reduces to
x^2 = -2
y = (1:5)';
48 - 2*y.^2
ans =
46
40
30
16
-2
So the solution locus is very simple.
[sqrt(48 - 2*y.^2), -sqrt(48 - 2*y.^2)]
ans =
6.7823 + 0i -6.7823 + 0i
6.3246 + 0i -6.3246 + 0i
5.4772 + 0i -5.4772 + 0i
4 + 0i -4 + 0i
0 + 1.4142i 0 - 1.4142i
The problem case you have described is essentially the same, just slightly more complicated.