0
$\begingroup$

I try to Append the vector S by z+x whereby z and x are calculated before. I somehow fail to extract x and z in the right way from the maximization problem. For some reason the standard solutions I found in the internet didn't work ( I probably failed to apply them correctly...)

S = {}; For[n = 3, n <= 10, n += 1, y = NMaximize[{z, n* x*z <= 1, x > 1/2} , {x, z}] ; AppendTo[S, [z + x], n] ;] 

It would be great, if you could write the correct syntax into the code.

$\endgroup$

1 Answer 1

4
$\begingroup$
  1. You should favour Reap+Sow instead of AppendTo.
  2. You have a syntax error [z + x] with unnecessary brackets.
  3. The z and x are not assigned any values but you are using them. You need to extract the values for z and x from the NMaximize results first.
S = Reap[ For[n = 3, n <= 10, n += 1, {xresult, zresult} = Values[Last[NMaximize[{z, n*x*z <= 1, x > 1/2}, {x, z}]]]; Sow[xresult + zresult]; ]][[2, 1]] (** results: {1.16667, 1., 0.9, 0.833333, 0.785714, 0.75, 0.722222, 0.7} **) 

You could also do away with the For loop entirely:

S = Total[Values[Last[NMaximize[{z, #*x*z <= 1, x > 1/2}, {x, z}]]]] & /@ Range[3, 10] 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.