# Convergence with Maximize - Iterations, Precision

I have problems with some applications for finding convergence - below is one example of failure for {x=0.35,y=0.35,z=0.3}, but it works fine for {x=0.3,y=0.3,z=0.4}.

How can I increase the iterations or change the precision in this structure?

Clear["Global*"]
SetDirectory[NotebookDirectory[]]
f[a_, b_, c_, x1_, x2_, x3_, y1_, y2_, y3_, z1_, z2_, z3_] := (a*x1)/(
a*x1 + b*y1 + c*z1) + (a*x2)/(a*x2 + b*y2 + c*z2) + (a*x3)/(
a*x3 + b*y3 + c*z3)
x = 0.35
y = 0.35
z = 0.3

g1[x1_, x2_, x3_] := x1 + x2 + x3 - 3*x
g2[y1_, y2_, y3_] := y1 + y2 + y3 - 3*y
g3[z1_, z2_, z3_] := z1 + z2 + z3 - 3*z
g4[x1_, y1_, z1_] := x1 + y1 + z1 - 1
g5[x2_, y2_, z2_] := x2 + y2 + z2 - 1
g6[x3_, y3_, z3_] := x3 + y3 + z3 - 1
inputs = {
{1, 5, 1},
{3, 5, 1},
{5, 5, 1},
{7, 5, 1},
{10, 5, 1},
{1, 5, 3},
{3, 5, 3},
{5, 5, 3},
{7, 5, 3},
{10, 5, 3},
{1, 5, 5},
{3, 5, 5},
{5, 5, 5},
{7, 5, 5},
{10, 5, 7},
{1, 5, 7},
{3, 5, 7},
{5, 5, 7},
{7, 5, 7},
{10, 5, 7},
{1, 5, 10},
{3, 5, 10},
{5, 5, 10},
{7, 5, 10},
{10, 5, 10}
} // Rationalize;

(table =
Prepend[Flatten[{#, {#[[1]], Values[#[[2]]]} &@
Maximize[{f[#[[1]], #[[2]], #[[3]], x1, x2, x3, y1, y2, y3, z1, z2, z3],
g1[x1, x2, x3] == 0, g2[y1, y2, y3] == 0, g3[z1, z2, z3] == 0,
g4[x1, y1, z1] == 0, g5[x2, y2, z2] == 0, g6[x3, y3, z3] == 0,
x1 >= 0, x2 >= 0, x3 >= 0, y1 >= 0, y2 >= 0, y3 >= 0,
z1 >= 0, z2 >= 0, z3 >= 0, x1 <= 1, x2 <= 1, x3 <= 1,
y1 <= 1, y2 <= 1, y3 <= 1, z1 <= 1, z2 <= 1, z3 <= 1},
{x1, y1, z1, x2, y2, z2, x3, y3, z3}, Reals]}]
& /@ inputs, {"a", "b", "c", "f", "x1", "y1", "z1",
"x2", "y2", "z2", "x3", "y3", "z3"}]) //
Grid[#, Frame -> All] &

Export["Output/table.xls", table /. r_Rational :> N[r, 2], "xls"]


Error message -- NMaximize: Failed to converge the requested accuracy or precision within 100 iterations.

Thank you!

• reference.wolfram.com/language/ref/NMaximize.html and click on the orange Details and Options and scroll down a bit to find MaxIterations. Then you can try inserting something like MaxIterations->10^3and see if that helps. You can also change your decimal constants (which are limited to MachinePrecision) to rationals (which have infinite precision) which will allow you to insert something like WorkingPrecision->64 and see if that helps. Bump these up or down a little at a time, not thinking a billion iterations should be better or a million digits of precision should be better
– Bill
Dec 19, 2022 at 19:52
• It works after changing from Maximize to NMaximize and 500 iterations - thank you!
– Tom
Dec 19, 2022 at 21:54

Only the input {a,b,c}={5,5,5} results in an error, and we can understand why: In that case the function is constant, given the constraints, and that can lead to numerical issues for NMaximize.

One solution is to simplify the function in the case a===b===c:

max[{x_,y_,z_}][{a_,b_,c_}]:=NMaximize[{
If[a===b===c,
3*x,
(a*x1)/(a*x1+b*y1+c*z1)+(a*x2)/(a*x2+b*y2+c*z2)+(a*x3)/(a*x3+b*y3+c*z3)],
{x1,y1,z1,x2,y2,z2,x3,y3,z3}];


Then

Map[max[{.35,.35,.3}],
{{1,5,1},{3,5,1},{5,5,1},{7,5,1},{10,5,1},
{1,5,3},{3,5,3},{5,5,3},{7,5,3},{10,5,3},
{1,5,5},{3,5,5},{5,5,5},{7,5,5},{10,5,7},
{1,5,7},{3,5,7},{5,5,7},{7,5,7},{10,5,7},
{1,5,10},{3,5,10},{5,5,10},{7,5,10},{10,5,10}}]
`

runs without error. OP should be aware of this sentence in the documentation:

If f is linear or concave and cons are linear or convex, the result given by NMaximize will be the global maximum, over both real and integer values; otherwise, the result may sometimes only be a local maximum.

• I deleted the {5,5,5} in inputs, and it works now for the parameter values above. But it doesn't converge for {0.25,.4,0.35}. So there must be something else causing it (too).
– Tom
Dec 19, 2022 at 21:29