Message Boards Message Boards

2
|
3776 Views
|
2 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Updating example from "Exploring Mathematics with Mathematica"?

I've just started reading an admittedly older book, Exploring Mathematics with Mathematica, by Theo Gray and Jerry Glynn, and I'm running into some issues with some sample code early in the text. I suspect this is just due to changes in syntax between versions of Mathematica. The book recommends a minimum of Version 2, and I'm on Version 10. I suspect some changes will be simple, and getting a few ideas from more advanced users who may be familiar with older versions might help me along.

Here's some sample code that I can't seem to run in Version 10:

test = Plot[Sin[x],{x,0,2 Pi},DisplayFunction->Identity];
Show[Graphics[{Thickness[0.001],Map[Line[{{#[[1]],0},#}]&,Nest[First,test,4]]}],Axes->Automatic]

I suspect the issue is with changes in the full form of the returned value of Plot?

Any hints or tips would be appreciated. I'd really like to understand the reason the original code no longer works, and as a bonus, some tips on recreating the effect intended (vertical lines extending from points in the plot to the X axis) using newer features and functions in Mathematica 10 would be great. Sample image Thank you!

Christopher Fox

POSTED BY: Christopher Fox
2 Replies

The example does not work any more; it has to do that the 'structure' of the graphics has changed (extra nested / a different tree). The Nest[...] Actually is the same as Part[test,1,1,1,1]. But this should not be performed on a graphics construct as it is bound to not work from version to version. One should use Cases[ PLOT, Line[x__]:>x,[Infinity]] or so to get all the line pieces. A better way in this case is as follows:

test=Reap[Plot[Sin[x], {x, 0, 2Pi}, EvaluationMonitor :> Sow[{x, Sin[x]}]]][[2,1]];
Show[Graphics[{Thickness[0.001],Map[Line[{{#[[1]],0},#}]&,test]}],Axes->Automatic]

or as follows:

test=Join@@Cases[Plot[Sin[x], {x, 0, 2Pi}],Line[x_]:>x,\[Infinity]];
Show[Graphics[{Thickness[0.001],Map[Line[{{#[[1]],0},#}]&,test]}],Axes->Automatic]

Both implementations will be more robust (especially the one with Sow and Reap) for future versions.

Alternatively you could also use ListLinePlot and then Filling option to get the lines:

data = Reap[Plot[ Sin[x], {x, 0, 2 Pi}, EvaluationMonitor :> Sow[{x, Sin[x]}]] ][[2, 1]];
ListPlot[data , Filling -> Axis]

Just a bad example I would say, not very well thought out...

POSTED BY: Sander Huisman

Thank you for the prompt and detailed reply, and thanks so much for the extra information on how to recreate the example using newer features!

Christopher

POSTED BY: Christopher Fox
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