Message Boards Message Boards

0
|
8214 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Wolfram Daily Study Group Exercise: squared function?

Posted 5 years ago

I'm a beginner hobbyist., Can someone please explain why this is a squared function?

IN: NestList[2 # &, 2, 5]
OUT: {2, 4, 8, 16, 32, 64}

Since it is a nestlist, the function is applied from 0 to 5, but what makes it a power function and why doesn't the result start with 1 (2^0) ?

Thanks very much, Andrew Skipor

POSTED BY: Andrew Skipor
3 Replies
Posted 5 years ago

NestList always returns the second argument as the result of iteration 0, The return values are powers of two because the previous value is multiplied by 2 in each nest.

{2, 2*2, 2*2*2, 2*2*2*2 ...}
POSTED BY: Rohit Namjoshi

Here is another way to see this. We'll use Trace (which is a very useful function to understand what's going inside during the expression evaluation - I highly recommend reading the help page and getting familiar with it), and // Column at the end just prints resulting list one element on a line. Trace[NestList[2 # &, 2, 5]] // Column enter image description here

The first line is just the expression we are evaluating. Each of the next 5 lines corresponds to one application of the pure function inside your NestList (since the third argument of NestList is 5), starting from 2 (the second argument to NestList), and applying the function to each consecutive result.

Finally, the last line is the output - as Rohit pointed out, 2, which is the starting number, is also added to the list.

POSTED BY: Victor Kryukov

Thanks very much, This is very helpful. Andrew Skipor

POSTED BY: Andrew Skipor
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