0
$\begingroup$

I type the following code in Mathematica 11:

For[n = 1, n <= 3, n++,
 RegionPlot[x^2 + y^3 < n, {x, -2, 2}, {y, -2, 2}]
]

Then I press Shift+Enter, but Mathematica does not output any figures. Where is the problem? Thanks in advance.

$\endgroup$
3
  • 2
    $\begingroup$ This is a common mistake for users new to Mathematica, or coming from earlier versions. Plots return Graphics expressions rather than displaying them as side-effects. See (47123). You could add Print but generally a better method is to replace For with Table: Table[RegionPlot[x^2 + y^3 < n, {x, -2, 2}, {y, -2, 2}], {n, 1, 3}]. You can further format that output with Column or GraphicsColumn as desired, or animate it with ListAnimate. $\endgroup$
    – Mr.Wizard
    Commented Aug 16, 2016 at 8:24
  • $\begingroup$ Note: does there exist a canonical Q&A on this issue? If not I think a post should be made in (18393) regarding this, as it has come up quite often. $\endgroup$
    – Mr.Wizard
    Commented Aug 16, 2016 at 8:28
  • $\begingroup$ Mr.Wizard: Thank you for your comment. $\endgroup$ Commented Aug 16, 2016 at 8:31

2 Answers 2

5
$\begingroup$

It depends on what your aim. If it is just to 'print' 3 graphics:

For[n = 1, n <= 3, n++, 
 Print@RegionPlot[x^2 + y^3 < n, {x, -2, 2}, {y, -2, 2}]]

If you want an object you can use and refer to:

f[x_, y_] := x^2 + y^3
rps=RegionPlot[f[x, y] < #, {x, -2, 2}, {y, -2, 2}] & /@ Range[3]

This is also a useful link with respect to loops in Mathematica.

$\endgroup$
5
  • $\begingroup$ ubpdqn: Thank you for your answer. $\endgroup$ Commented Aug 16, 2016 at 8:32
  • $\begingroup$ @Wei-ChengLiu you're welcome. I had exactly the same question when I first started using Mma. Have fun:) $\endgroup$
    – ubpdqn
    Commented Aug 16, 2016 at 8:34
  • $\begingroup$ Surely in this case Table[]>For[] $\endgroup$
    – Feyre
    Commented Aug 16, 2016 at 9:53
  • 2
    $\begingroup$ @Feyre as you and Mr.Wizard have suggested Table may be natural alternative. I provided link to motivate looking at alternatives. $\endgroup$
    – ubpdqn
    Commented Aug 16, 2016 at 9:57
  • $\begingroup$ @Feyre & ubpdqn Thank you for your comments. $\endgroup$ Commented Aug 16, 2016 at 11:53
2
$\begingroup$

For fun

Example

Manipulate[
 RegionPlot[x^2 + y^3 < n, {x, -2, 2}, {y, -2, 2}],
 {n, 1, 3, 1}
 ]
$\endgroup$
2
  • $\begingroup$ e.doroskevic: Thank you for your answer. $\endgroup$ Commented Aug 16, 2016 at 8:32
  • $\begingroup$ @Wei-ChengLiu you are most welcome $\endgroup$ Commented Aug 16, 2016 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.