Message Boards Message Boards

1
|
5901 Views
|
5 Replies
|
7 Total Likes
View groups...
Share
Share this post:

[?] Put the results of "Do" into a List?

Posted 5 years ago

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!

POSTED BY: Shaoyan Robert
5 Replies
Posted 5 years ago

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}]
POSTED BY: Shaoyan Robert
Posted 5 years ago

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}]]
POSTED BY: J. M.

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}]
POSTED BY: Claudio Chaib
Posted 5 years ago

Thank you, Chaib, you are so warmhearted!

POSTED BY: Shaoyan Robert
Posted 5 years ago

Thank you, J.M.

POSTED BY: Shaoyan Robert
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract