# Matrix using For Loop

I am trying to create matrix, where every element is generated from For Loop. Just for simplicity,let's consider the following mathematical problem: Find maximum value of the function $x^2+y^2$ over the domains: $[0,.01],[0,.02],..,[0,1]$. In other words it is needed to create the following matrix: \begin{bmatrix}max:x^2+y^2 over [0,0.01] & max:x^2+y^2 over [0,0.02] & .. & max:x^2+y^2 over [0,0.1] \\max:x^2+y^2 over [0,0.11] & max:x^2+y^2 over [0,0.12] & .. & max:x^2+y^2 over [0,0.2]\\ max:x^2+y^2 over [0,0.91] & max:x^2+y^2 over [0,0.92] & .. & max:x^2+y^2 over [0,1]\end{bmatrix} So to automatically generate the matrix consisting from the extreme values of the function over different domains I use the following code:

 m=Table[1^i*1^j*For[k=.01,k<=1,k+=.01,
For[z=.01,z<=1,z+=.01,
Print[Maximize[{x^2+y^2,0<=x<=k,0<=y<=z,{x,y}],{i,10},{j,10}]]]


It turns out that it doesn't work! How can I fix this problem. $$P.S.$$ Mathematically this problem is straightforward, I am interested to model this problem in Wolfram Mathematica.

• It's unclear what you want. The inner calculation doesn't depend on i or j, so even if you fix the other mistakes, your matrix elements will all be the same. I suggest you first attempt a function f[i_,j_]:=... that computes a single matrix element. Don't Print, that puts it on your screen and discards it. Then tabulate f to make your matrix. – John Doty Jul 20 '18 at 13:45
• @David please edit your question to clarify what you need and include new information, use the "comments" only for comments. – rhermans Jul 20 '18 at 14:08
• Your notation is misleading. Domain should be $[0,0.01]\times[0,0.01]$ – OkkesDulgerci Jul 20 '18 at 16:13

I am not guaranteeing that this is the fastest method, but its a fairly direct translation of what you did:

Array[
MaxValue[
x^2+y^2,
{x,y} ∈ Rectangle[{0,0},{0.01+0.1#1+0.01#2,0.01+0.1#1+0.01#2}]
]&,
{10,10},{{0,9}}
]

Table[k = (i - 1)/10 + j/100;  Maximize[x^2, 0 <= x <= k, {x}][[1]], {i, 10}, {j, 10}]

• Thank you for your answer, it works! I've slight modified the question. Please take a look. – David Jul 20 '18 at 14:33
• @David It is considered bad form to modify the question after answers have been provided, because this makes the answers less relevant and essentially "wastes" the effort of the answer writer. If you have a significant change in your question, you should ask another question altogether. – MarcoB Jul 20 '18 at 14:53

Here is my approach.

    val = Table[{{0, 0}, {i, i}}, {i, 1/100, 1, 1/100}];
Partition[ MaxValue[x^2 + y^2, {x, y} ∈ Rectangle[First@#, Last@#]] & /@ val, 10]

bounds = Partition[Range[1/100, 1, 1/100], 10];
objfunc[x_, y_] := x^2 + y^2;
obj = MaxValue[{objfunc[x, y], 0 <= x <= #, 0 <= y <= #}, {x, y}]&;
Map[obj, bounds, {-1}] // MatrixForm // TeXForm


$\left( \begin{array}{cccccccccc} \frac{1}{5000} & \frac{1}{1250} & \frac{9}{5000} & \frac{2}{625} & \frac{1}{200} & \frac{9}{1250} & \frac{49}{5000} & \frac{8}{625} & \frac{81}{5000} & \frac{1}{50} \\ \frac{121}{5000} & \frac{18}{625} & \frac{169}{5000} & \frac{49}{1250} & \frac{9}{200} & \frac{32}{625} & \frac{289}{5000} & \frac{81}{1250} & \frac{361}{5000} & \frac{2}{25} \\ \frac{441}{5000} & \frac{121}{1250} & \frac{529}{5000} & \frac{72}{625} & \frac{1}{8} & \frac{169}{1250} & \frac{729}{5000} & \frac{98}{625} & \frac{841}{5000} & \frac{9}{50} \\ \frac{961}{5000} & \frac{128}{625} & \frac{1089}{5000} & \frac{289}{1250} & \frac{49}{200} & \frac{162}{625} & \frac{1369}{5000} & \frac{361}{1250} & \frac{1521}{5000} & \frac{8}{25} \\ \frac{1681}{5000} & \frac{441}{1250} & \frac{1849}{5000} & \frac{242}{625} & \frac{81}{200} & \frac{529}{1250} & \frac{2209}{5000} & \frac{288}{625} & \frac{2401}{5000} & \frac{1}{2} \\ \frac{2601}{5000} & \frac{338}{625} & \frac{2809}{5000} & \frac{729}{1250} & \frac{121}{200} & \frac{392}{625} & \frac{3249}{5000} & \frac{841}{1250} & \frac{3481}{5000} & \frac{18}{25} \\ \frac{3721}{5000} & \frac{961}{1250} & \frac{3969}{5000} & \frac{512}{625} & \frac{169}{200} & \frac{1089}{1250} & \frac{4489}{5000} & \frac{578}{625} & \frac{4761}{5000} & \frac{49}{50} \\ \frac{5041}{5000} & \frac{648}{625} & \frac{5329}{5000} & \frac{1369}{1250} & \frac{9}{8} & \frac{722}{625} & \frac{5929}{5000} & \frac{1521}{1250} & \frac{6241}{5000} & \frac{32}{25} \\ \frac{6561}{5000} & \frac{1681}{1250} & \frac{6889}{5000} & \frac{882}{625} & \frac{289}{200} & \frac{1849}{1250} & \frac{7569}{5000} & \frac{968}{625} & \frac{7921}{5000} & \frac{81}{50} \\ \frac{8281}{5000} & \frac{1058}{625} & \frac{8649}{5000} & \frac{2209}{1250} & \frac{361}{200} & \frac{1152}{625} & \frac{9409}{5000} & \frac{2401}{1250} & \frac{9801}{5000} & 2 \\ \end{array} \right)$

For the specific case in OP, x^2 + y^2 is increasing in both arguments and, thus, its maximum value is reached at the upper right corner of the constraint set (which in OP's case is a square). So we can simply square the corner coordinates and double them to get the desired result:

2 bounds ^2
% // TeXForm


same picture as above

Note: This result is the same as the one produces by the Rationalized version of @chuy's answer:

2 bounds^2 == Array[MaxValue[x^2 + y^2, {x, y} \[Element]
Rectangle[{0, 0}, Rationalize @ {.01+ .1 #1 + .01 #2, .01 + .1 #1 + .01 #2}]] &,
{10, 10}, {{0, 9}}]


True

• This is just simplified example. Actually I consider much more complicated function, which extreme values are estimated numerically. – David Jul 20 '18 at 15:36
• @David, please see the update. – kglr Jul 20 '18 at 15:54
• Thank you for your answer, it works. But can you explain me, please, the following Array[***,{10,10},{{0,9}}]. Why you have selected {0,9}? – David Jul 23 '18 at 7:27
• @David, Array[...{10,10}, {{0,9}}] is from chuy's answer. – kglr Jul 23 '18 at 12:42