Message Boards Message Boards

0
|
2932 Views
|
14 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Plotting conditional Function?

Posted 1 year ago
Function[n, If[EvenQ[n], n/2, 3 n + 1]]

How can I plot this?

POSTED BY: Nelson Zink
14 Replies
Posted 1 year ago

Rohit,

Yep, popped right up.

POSTED BY: Nelson Zink
Posted 1 year ago

Eric,

ListPlot[{Log[NestList[Function[n, If[EvenQ[n], n/2, 3 n + 1]], 27, 111]]}, 
Joined -> True, PlotRange -> Automatic, ImageSize -> Large]

The plot created by this is what I was looking for. I don't know what to do with DiscretePlot to make it look like the plot from above.

Nelson

POSTED BY: Nelson Zink
Posted 1 year ago

You probably wouldn't bother. You asked how to plot a function. It's a function defined for integers. DiscretePlot is a nice way to plot such functions. Nowhere in your question did you indicate that you wanted to plot the nested application of your function, that the plot should be a connected line, or that you wanted to apply Log to it. I tried to answer the question as it was asked.

POSTED BY: Eric Rimbey

Another way

ResourceFunction["Collatz"][27] // Log // ListLinePlot
POSTED BY: Rohit Namjoshi
Posted 1 year ago

How can I get a nested list of, say, the first 20 sequences? Or for that matter, the second 20 sequences.

POSTED BY: Nelson Zink
Posted 1 year ago

Are these what you are looking for?

Table[ResourceFunction["Collatz"][i],{i,1,20}]

and

Table[ResourceFunction["Collatz"][i],{i,21,40}]
POSTED BY: Bill Nelson
Posted 1 year ago

Pretty much. I was hoping to end up with something shorter than this:

Sort[DeleteDuplicates[Flatten[Table[ResourceFunction["Collatz"][n], {n, 1, 20}]]]]
POSTED BY: Nelson Zink
Posted 1 year ago
Union[Flatten[ResourceFunction["Collatz"]/@Range[20]]]
POSTED BY: Bill Nelson
Posted 1 year ago

That'll do it! Thanks.

POSTED BY: Nelson Zink
Posted 1 year ago

Eric,

Couldn't get this to work, but with my level of expertise that doesn't mean much. Thanks.

POSTED BY: Nelson Zink
Posted 1 year ago
DiscretePlot[Function[n, If[EvenQ[n], n/2, 3 n + 1]][x], {x, -10, 10}]
POSTED BY: Eric Rimbey
Posted 1 year ago

Maybe try DiscretePlot.

POSTED BY: Eric Rimbey
Posted 1 year ago

Perhaps this

m=6721;
list=Reap[
  Sow[m];
  Do[
    m=Function[n, If[EvenQ[n], n/2, 3 n + 1]][m];
    Sow[m],
    {25}]
  ][[2,1]];
ListPlot[list,Joined->True]

Or simpler

ListPlot[NestList[Function[n, If[EvenQ[n], n/2, 3 n + 1]],6721,25],Joined->True]
POSTED BY: Bill Nelson
Posted 1 year ago

Bill,

Perfect, just what I wanted. Thanks.

POSTED BY: Nelson Zink
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