ntf198602
(Shaoyan Robert)
1
Hi, guys
Could you please tell me how to put the results of “Do” into a List? For example:
Clear["`*"];
b = 2;
Do[Print[Maximize[{-x^2*a - 3*x*b + 1, -(6/8) <= x <= -(4/8)},
x]], {a, 1, 3}]
I hope the result would be
{{79/16,{x->-(3/4)}},{35/8,{x->-(3/4)}},{61/16,{x->-(3/4)}}}
Thank you very much!
ntf198602
(Shaoyan Robert)
2
I tried it just now. My be we can use Table instead of Do.
Clear["`*"];
b = 2;
Table[Maximize[{-x^2*a - 3*x*b + 1, -(6/8) <= x <= -(4/8)}, x], {a, 1,
3}]
Do[] is the wrong function to use if you want a list here, since Do[] does not yield output. Use Table[] instead:
With[{b = 2},
Table[Maximize[{-x^2*a - 3*x*b + 1, -(6/8) <= x <= -(4/8)}, x], {a, 1, 3}]]
Other way using function:
Clear["`*"];
b = 2;
k[a_] := Maximize[{-x^2*a - 3*x*b + 1, -(6/8) <= x <= -(4/8)},
x]; Map[k, {1, 2, 3}]
Function with Do:
Clear["`*"];
b = 2;
f[z_] := (Do[
k = Maximize[{-x^2*a - 3*x*b + 1, -(6/8) <= x <= -(4/8)},
x], {a, {z}}]; k);
Map[f, {1, 2, 3}]
ntf198602
(Shaoyan Robert)
5
Thank you, Chaib, you are so warmhearted!