0

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.

Wei-Cheng Liu
  • 439
  • 4
  • 10
  • 2
    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. – Mr.Wizard Aug 16 '16 at 08:24
  • 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. – Mr.Wizard Aug 16 '16 at 08:28
  • Mr.Wizard: Thank you for your comment. – Wei-Cheng Liu Aug 16 '16 at 08:31

2 Answers2

5

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.

ubpdqn
  • 60,617
  • 3
  • 59
  • 148
2

For fun

Example

Manipulate[
 RegionPlot[x^2 + y^3 < n, {x, -2, 2}, {y, -2, 2}],
 {n, 1, 3, 1}
 ]
e.doroskevic
  • 5,959
  • 1
  • 13
  • 32