0
$\begingroup$

(As always, feel free to inform me on how I can better ask questions if my formatting isn't preferred)

Just to use an arbitrary case, say I have a function in Mathematica

cylinder[i_]:=RegionPlot3D[x^2+y^2 <= 1 && i-1 <= z <= i, {x,-1,1},{y,-1,1},{z,0,10}];

And say I don't know what i should be yet other than it will be an integer between 1 and 10. How would I use the Show function to display an unknown number of the cylinder functions?

My attempt at a solution came in the form

n=5(*User defines n*); Show[For[j=1,j<=n,j++,cylinder[j]]]

$\endgroup$
5
  • $\begingroup$ why not do Show[cylinder[#] & /@ Range[1, n]] ? For loops are not recommended in Mathematica :) !Mathematica graphics $\endgroup$
    – Nasser
    Mar 21, 2022 at 21:09
  • $\begingroup$ This works with my code! Although, can you explain the code in English? I am unsure what & /@ is supposed to represent specifically. $\endgroup$
    – Chloe
    Mar 21, 2022 at 21:49
  • $\begingroup$ it is a map command, same as writing Show[Map[cylinder[#] &, Range[1, n]]] you are mapping or applying your function to Range 1 to n. So instead of applying the function one by one in a loop as you were doing, you can use map and it is done automatically for you. $\endgroup$
    – Nasser
    Mar 21, 2022 at 21:59
  • $\begingroup$ I'll definitely need to re-read this a couple times, but I appreciate your time explaining!! $\endgroup$
    – Chloe
    Mar 21, 2022 at 22:37
  • $\begingroup$ The map command is one of the most used commands in Mathematica. If you have function f[x] and want to apply it on list of values {1,2,3,4} one by one, then you can use Map for this. Just look up help on Mathematica Map and you'll see many examples. $\endgroup$
    – Nasser
    Mar 21, 2022 at 22:45

2 Answers 2

2
$\begingroup$

For the specific example, multiple cylinders can be included in a single RegionPlot3D

Clear["Global`*"]

cylinder[i_] := 
 RegionPlot3D[
  x^2 + y^2 <= 1 && i - 1 <= z <= i, {x, -1, 1}, {y, -1, 1}, {z, 0, 10}]

Manipulate[
 RegionPlot3D[x^2 + y^2 <= 1 &&
   Or @@ ((# - 1 <= z <= #) & /@ cyl),
  {x, -1, 1}, {y, -1, 1}, {z, 0, 10},
  PlotPoints -> 75,
  MaxRecursion -> 4],
 {{cyl, {1,5,9}, "cylinders"},
  (# -> (# - 1 <= z <= #)) & /@ Range[10],
  ControlType -> CheckboxBar}]

enter image description here

$\endgroup$
1
  • $\begingroup$ May be a bit much for my use case, but definitely great study material to better use Mathematica in the future! Thank you for your time! $\endgroup$
    – Chloe
    Mar 21, 2022 at 22:40
1
$\begingroup$

You already have a function that takes a single integer, and you want a function that applies that function to a range of integers. This is a perfect fit for Array:

cylinders[n_] := Array[cylinder, n]

This, of course, gives a list. You can wrap it in Show:

Show[cylinders[3]]

to keep cylinders independent of display, or you could make Show part of the definition if you don't need that independence:

cylinders[n_] := Show[Array[cylinder, n]]
$\endgroup$
1
  • $\begingroup$ This worked with my code! Thank you so much! $\endgroup$
    – Chloe
    Mar 21, 2022 at 21:43

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.